<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.qunzhixinxi.hnqz.daemon.quartz.mapper.WmScorePackageSettleNoteMapper">

	<resultMap id="wmScorePackageSettleNoteMap"
			   type="com.qunzhixinxi.hnqz.daemon.quartz.entity.WmScorePackageSettleNote">
		<id property="id" column="id"/>
		<result property="packageId" column="package_id"/>
		<result property="packageStatusId" column="package_status_id"/>
		<result property="taskId" column="task_id"/>
		<result property="settleAmount" column="settle_amount"/>
		<result property="userId" column="user_id"/>
		<result property="invoiceType" column="invoice_type"/>
		<result property="categoryName" column="category_name"/>
		<result property="settleNo" column="settle_no"/>
		<result property="discount" column="discount"/>
		<result property="settleNoteStatus" column="settle_note_status"/>
		<result property="remark" column="remark"/>
		<result property="createTime" column="create_time"/>
		<result property="updateTime" column="update_time"/>
		<result property="subTime" column="sub_time"/>
		<result property="notifyTime" column="notify_time"/>
		<result property="idCardNumber" column="id_card_number"/>
		<result property="streamId" column="stream_id"/>
		<result property="subType" column="sub_type"/>
	</resultMap>

	<resultMap id="singleMemberSettleSameAmountInSeveralMonthsMap"
			   type="com.qunzhixinxi.hnqz.daemon.quartz.entity.SingleMemberSettleSameAmountInSeveralMonths">
		<id property="noteId" column="id"/>
		<result property="realname" column="realname"/>
		<result property="username" column="username"/>
		<result property="deptId" column="dept_id"/>
		<result property="deptName" column="dept_name"/>
		<result property="settleAmount" column="settle_amount"/>
		<result property="subToGigTime" column="sub_to_gig_time"/>
		<result property="settleState" column="settle_note_status"/>
		<result property="subTypeName" column="sub_type"/>
		<result property="packageName" column="score_package_name"/>
		<result property="settleNoteCreateTime" column="create_time"/>
	</resultMap>

	<!-- 查询指定部门对应的结算数据 -->
	<select id="listSettleAmountByDeptId" resultMap="wmScorePackageSettleNoteMap">
		SELECT n.settle_amount
		FROM wm_score_package_settle_note n
				 LEFT JOIN wm_score_package p ON p.id = n.package_id
		WHERE n.settle_note_status = 1
		  AND n.settle_amount IS NOT NULL
		  AND p.send_package_dept_id = #{deptId}
		  AND n.sub_type = #{subType}
		  AND n.notify_time BETWEEN #{startTime} AND #{endTime}
	</select>

	<select id="singleMemberSettleSameAmountInSeveralMonths" resultMap="singleMemberSettleSameAmountInSeveralMonthsMap">
		SELECT wspsn.id,
			   su.realname,
			   su.username,
			   sd.dept_id,
			   sd.name AS `dept_name`,
			   wspsn.settle_amount,
			   wspsn.sub_to_gig_time,
			   CASE wspsn.settle_note_status
				   WHEN 1 THEN '结算成功'
				   WHEN 2 THEN '结算失败'
				   WHEN 3 THEN '已提交'
				   WHEN 4 THEN '未结算'
				   WHEN 5 THEN '部分成功'
				   WHEN 8 THEN '结算成功（线下）'
				   WHEN 9 THEN '待结算'
				   END AS `settle_note_status`,
			   CASE wspsn.sub_type
				   WHEN 0 THEN '仁励家'
				   WHEN 1 THEN '税邦云'
				   WHEN 2 THEN '自由职家'
				   END AS `sub_type`,
			   sp.score_package_name,
			   wspsn.create_time
		FROM wm_score_package_settle_note wspsn
				 LEFT JOIN sys_user su ON wspsn.user_id = su.user_id
				 LEFT JOIN wm_score_package sp ON sp.id = wspsn.package_id
				 LEFT JOIN sys_dept sd ON sd.dept_id = sp.dept_id
		WHERE (wspsn.create_time BETWEEN #{startTime} AND #{endTime})
		  AND sd.name NOT IN ('上海问云生物科技有限公司', '北京遥领医疗科技有限公司')
		  AND wspsn.settle_note_status = 1
		ORDER BY wspsn.user_id ASC, wspsn.create_time DESC
	</select>

</mapper>
