<?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.jeeplus.modules.workfullmanage.dao.WorkFullPayDao">
    
	<sql id="workFullPayColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.apply_unit AS "applyUnit",
		a.pay_content AS "payContent",
		a.construction_tender AS "constructionTender",
		a.this_apply_money AS "thisApplyMoney",
		a.arrange_money AS "arrangeMoney",
		a.arrange_pay_money AS "arrangePayMoney",
		a.confirm_pay_money AS "confirmPayMoney",
		a.total_pay_money AS "totalPayMoney",
		a.complete_engeer_value AS "completeEngeerValue",
		a.complete_per_pay AS "completePerPay",
		a.complete_process AS "completeProcess",
		a.process_instance_id AS "processInstanceId",
		a.full_pay_status AS "fullPayStatus",
		a.work_full_manage_id AS "workFullManageId",
		a.count_flag AS "countFlag",
		u.name AS "createBy.name"
	</sql>
	
	<sql id="workFullPayJoins">
		LEFT JOIN sys_user u on u.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkFullPay" >
		SELECT 
			<include refid="workFullPayColumns"/>
		FROM work_full_pay a
		<include refid="workFullPayJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkFullPay" >
		SELECT 
			<include refid="workFullPayColumns"/>
		FROM work_full_pay a
		<include refid="workFullPayJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="workFullManageId != null and workFullManageId != ''">
				AND a.work_full_manage_id = #{workFullManageId}
			</if>
			<if test="applyUnit != null and applyUnit != ''">
				AND a.apply_unit = #{applyUnit}
			</if>
			<if test="fullPayStatus != null and fullPayStatus != ''">
				AND a.full_pay_status = #{fullPayStatus}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkFullPay" >
		SELECT 
			<include refid="workFullPayColumns"/>
		FROM work_full_pay a
		<include refid="workFullPayJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_full_pay(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			apply_unit,
			pay_content,
			construction_tender,
			this_apply_money,
			arrange_money,
			arrange_pay_money,
			confirm_pay_money,
			total_pay_money,
			complete_engeer_value,
			complete_per_pay,
			complete_process,
			process_instance_id,
			full_pay_status,
			work_full_manage_id,
			count_flag
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{applyUnit},
			#{payContent},
			#{constructionTender},
			#{thisApplyMoney},
			#{arrangeMoney},
			#{arrangePayMoney},
			#{confirmPayMoney},
			#{totalPayMoney},
			#{completeEngeerValue},
			#{completePerPay},
			#{completeProcess},
			#{processInstanceId},
			#{fullPayStatus},
			#{workFullManageId},
			#{countFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE work_full_pay SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			apply_unit = #{applyUnit},
			pay_content = #{payContent},
			construction_tender = #{constructionTender},
			this_apply_money = #{thisApplyMoney},
			arrange_money = #{arrangeMoney},
			arrange_pay_money = #{arrangePayMoney},
			confirm_pay_money = #{confirmPayMoney},
			total_pay_money = #{totalPayMoney},
			complete_engeer_value = #{completeEngeerValue},
			complete_per_pay = #{completePerPay},
			complete_process = #{completeProcess},
			process_instance_id = #{processInstanceId},
			full_pay_status = #{fullPayStatus},
			work_full_manage_id = #{workFullManageId}
		WHERE id = #{id}
	</update>

	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_full_pay
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_full_pay SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkFullPay" statementType="STATEMENT">
		select * FROM work_full_pay  where ${propertyName} = '${value}'
	</select>

	<!-- 查询记录数 -->
	<select id="queryCount" resultType="java.lang.String">
		select count(*) from work_full_pay
		where work_full_manage_id = #{workFullManageId} AND full_pay_status = 4
	</select>

	<!-- 查询累计金额 只查询已通过审核的-->
	<select id="querySum" resultType="java.lang.String">
		select sum(confirm_pay_money) from work_full_pay where work_full_manage_id = #{workFullManageId} AND full_pay_status = 4
	</select>

	<update id="updateProcessInstanceId">
		UPDATE work_full_pay SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkFullPay">
		SELECT
		<include refid="workFullPayColumns"/>
		FROM work_full_pay a
		<include refid="workFullPayJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<!-- 依据项目编号获取第一条记录 -->
	<select id="getFirstRecord" resultType="WorkFullPay">
		SELECT
		<include refid="workFullPayColumns"/>
		FROM work_full_pay a
		<include refid="workFullPayJoins"/>
		WHERE a.work_full_manage_id = #{workFullManageId}
			AND a.count_flag = "1"
			AND full_pay_status = 4
	</select>

	<!-- 查询状态值不为4的数据是否存在 -->
	<select id="getStatusNotFour" resultType="java.lang.Integer">
		SELECT
			count(*)
		FROM work_full_pay a
		WHERE a.work_full_manage_id = #{workFullManageId}
		AND full_pay_status != 4 and full_pay_status != 5
	</select>
</mapper>