<?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.admin.mapper.WmScorePackageSettleNoteMapper">

	<resultMap id="wmScorePackageSettleNoteMap" type="com.qunzhixinxi.hnqz.admin.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>

	<select id="selectSettleNoteStatus" resultType="com.qunzhixinxi.hnqz.admin.entity.input.SettleNoteStatusOutput">
		SELECT (
				   SELECT IFNULL(SUM(1), 0)
				   FROM wm_score_package_settle_note
				   WHERE package_id = #{packageId}
					 AND settle_note_status = 1
			   ) success_count,
			   (
				   SELECT IFNULL(SUM(1), 0)
				   FROM wm_score_package_settle_note
				   WHERE package_id = #{packageId}
					 AND settle_note_status = 2
			   ) fail_count,
			   (
				   SELECT IFNULL(SUM(1), 0)
				   FROM wm_score_package_settle_note
				   WHERE package_id = #{packageId}
					 AND settle_note_status = 3
			   ) submit_count,
			   (
				   SELECT IFNULL(SUM(1), 0)
				   FROM wm_score_package_settle_note
				   WHERE package_id = #{packageId}
			   ) total_count
	</select>

	<select id="getWmTaskSettleInfoPage" resultType="com.qunzhixinxi.hnqz.admin.entity.output.WmTaskSettleNoteOutput">
		SELECT
		t.id,
		t.task_type_id,
		t.create_time,
		u.realname real_name,
		t.score_package_id,
		t.task_user_id,
		c.temp3 xmbm,
		c.temp4 visit_stage,
		e.id noteId,
		e.invoice_type,
		e.sub_type,
		e.category_name,
		e.discount,
		e.stream_id,
		e.settle_no,
		e.settle_amount,
		e.settle_note_status,
		e.sub_time,
		e.notify_time
		FROM wm_task t
		left JOIN wm_task_content c on t.task_content_id = c.id
		left JOIN wm_score_package_settle_note e on t.id = e.task_id
		LEFT JOIN sys_user u on t.task_user_id = u.user_id
		WHERE 1= 1
		<if test="query.userId!=null and query.userId!=''">
			AND t.task_user_id =#{query.userId}
		</if>
		<if test="query.packageId!=null and query.packageId!=''">
			AND t.score_package_id =#{query.packageId}
		</if>

	</select>

	<!-- 结算包结算列表（分页） -->
	<select id="getWmTaskForSettlePage" resultType="com.qunzhixinxi.hnqz.admin.entity.output.WmTaskSettleNoteOutput">
		SELECT
		t.id,
		t.task_type_id,
		t.create_time,
		t.score_package_id,
		t.task_user_id,
		u.realname real_name,
		u.username,
		c.temp3 xmbm,
		e.id noteId,
		e.invoice_type,
		e.sub_type,
		e.category_name,
		e.discount,
		e.stream_id,
		e.settle_no,
		e.settle_amount,
		e.settle_note_status,
		e.sub_time,
		e.notify_time
		FROM wm_task t
		LEFT JOIN wm_task_content c on t.task_content_id = c.id
		LEFT JOIN wm_score_package_settle_note e on t.id = e.task_id
		LEFT JOIN sys_user u on t.task_user_id = u.user_id
		WHERE 1= 1
		<if test="query.userId!=null and query.userId!=''">
			AND t.task_user_id =#{query.userId}
		</if>
		<if test="query.packageId!=null and query.packageId!=''">
			AND t.score_package_id =#{query.packageId}
		</if>
		<if test="query.startSubTime != null and query.endSubTime != null">
			AND e.sub_time BETWEEN #{query.startSubTime} AND #{query.endSubTime}
		</if>
		<if test="query.settleNoteStatus != null">
			AND e.settle_note_status = #{query.settleNoteStatus}
		</if>
		ORDER BY e.sub_time DESC, t.id
	</select>

	<!-- 结算包结算列表 -->
	<select id="listWmTaskForSettle" resultType="com.qunzhixinxi.hnqz.admin.entity.output.WmTaskSettleNoteOutput">
		SELECT
		t.id,
		t.task_type_id,
		t.create_time,
		t.score_package_id,
		t.task_user_id,
		tt.task_type_name,
		u.realname real_name,
		u.username,
		c.temp3 xmbm,
		e.id noteId,
		e.invoice_type,
		e.sub_type,
		e.category_name,
		e.discount,
		e.stream_id,
		e.settle_no,
		e.settle_amount,
		e.settle_note_status,
		e.sub_time,
		e.notify_time
		FROM wm_task t
		LEFT JOIN wm_task_content c on t.task_content_id = c.id
		LEFT JOIN wm_score_package_settle_note e on t.id = e.task_id
		LEFT JOIN sys_user u on t.task_user_id = u.user_id
		LEFT JOIN (
		SELECT id, task_type_name FROM wm_task_type WHERE del_flag=0 AND enable_flag=0 AND task_type_level=1
		) tt ON tt.id = t.task_type_id
		WHERE 1= 1
		<if test="query.userId!=null and query.userId!=''">
			AND t.task_user_id =#{query.userId}
		</if>
		<if test="query.packageId!=null and query.packageId!=''">
			AND t.score_package_id =#{query.packageId}
		</if>
		<if test="query.startSubTime != null and query.endSubTime != null">
			AND e.sub_time BETWEEN #{query.startSubTime} AND #{query.endSubTime}
		</if>
		<if test="query.settleNoteStatus != null">
			AND e.settle_note_status = #{query.settleNoteStatus}
		</if>
		ORDER BY e.sub_time DESC, t.id
	</select>

	<select id="listSettleNoteWarrantParams" resultType="com.qunzhixinxi.hnqz.admin.entity.SettleNoteWarrantParams">
		SELECT
		u.username,
		u.realname,
		u.bank_card_number AS bank_card,
		sd.dept_permissions,
		IF(sd.level = 5, wp.project_name, sp.score_package_name) AS `score_package_name`,
		n.settle_amount
		FROM wm_score_package_settle_note n
		LEFT JOIN wm_score_package sp ON sp.id = n.package_id
		LEFT JOIN sys_user u ON u.user_id = n.user_id
		LEFT JOIN sys_dept sd ON u.dept_id = sd.dept_id
		LEFT JOIN wm_project wp ON sp.project_num = wp.project_number
		WHERE n.id IN
		<foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</select>
</mapper>
