<?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.workrepay.dao.WorkRepayDao">
    
	<sql id="workRepayColumns">
		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.reimbursement_id AS "workReimbursement.id",
		a.del_flag AS "delFlag",
		a.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.status AS "status",
		a.loan_id AS "workLoan.id",
		a.source AS "source",
		a.repay_date AS "repayDate",
		a.money AS "money",
		sa.name AS "workLoan.workStaffBasicInfo.name",
		wl.num AS "workLoan.num",
		oa.name AS "workLoan.workStaffBasicInfo.office.name",
		wl.loan_date AS "workLoan.loanDate",
		wl.money AS "workLoan.money",
		su.name AS "createBy.name",
		wr.number AS "workReimbursement.number",
		wl.repay_state AS "workLoan.repayState"
	</sql>
	
	<sql id="workRepayJoins">
		LEFT JOIN work_loan wl on wl.id = a.loan_id
		LEFT JOIN work_staff_achives sa on sa.id = wl.staff_achives_id
		LEFT JOIN sys_office oa ON oa.id = sa.office_id
		LEFT JOIN sys_user su on su.id = a.create_by
		LEFT JOIN work_reimbursement wr on wr.id = a.reimbursement_id
	</sql>
	
    
	<select id="get" resultType="WorkRepay" >
		SELECT 
			<include refid="workRepayColumns"/>
		FROM work_repay a
		<include refid="workRepayJoins"/>
		WHERE a.id = #{id}
	</select>

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

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkRepay">
		SELECT
		<include refid="workRepayColumns"/>
		FROM work_repay a
		<include refid="workRepayJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<select id="findList" resultType="WorkRepay" >
		SELECT 
			<include refid="workRepayColumns"/>
		FROM work_repay a
		<include refid="workRepayJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="workLoan != null and workLoan.id != null and workLoan.id != ''">
				AND a.loan_id = #{workLoan.id}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</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="WorkRepay" >
		SELECT 
			<include refid="workRepayColumns"/>
		FROM work_repay a
		<include refid="workRepayJoins"/>
		<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_repay(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			reimbursement_id,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			status,
			loan_id,
			repay_date,
			money,
			source
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{workReimbursement.id},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{status},
			#{workLoan.id},
			#{repayDate},
			#{money},
			#{source}
		)
	</insert>
	
	<update id="update">
		UPDATE work_repay SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			reimbursement_id = #{workReimbursement.id},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			status = #{status},
			loan_id = #{workLoan.id},
			repay_date = #{repayDate},
			money = #{money},
			source = #{source}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_repay
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_repay SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkRepay" statementType="STATEMENT">
		select * FROM work_repay  where ${propertyName} = '${value}'
	</select>

	<select id="findWorkRepayList" resultType="WorkRepay" parameterType="String">
		SELECT
		*
		FROM work_repay a
		<where>
			a.del_flag = '0'
			AND a.loan_id = #{loanId}
			AND a.status !='5'
		</where>
	</select>
	<select id="findWorkRepayView" resultType="WorkRepay" parameterType="String">
		SELECT
		*
		FROM work_repay a
		<where>
			a.del_flag = '0'
			AND a.loan_id = #{loanId}
			and a.reimbursement_id!=#{reimbursementId}
			AND a.status !='5'
		</where>
	</select>

	<update id="deleteRepay">
		DELETE FROM work_repay
		WHERE reimbursement_id = #{reimbursementId} and source='1'
	</update>
	<update id="updateReay">
		UPDATE work_repay SET
		  status ='5'
		WHERE loan_id = #{loanId} AND reimbursement_id=#{reimbursementId}
	</update>
	<select id="findSumRepayList" resultType="String" parameterType="String">
		SELECT
		ifnull(sum(a.money),0) as money
		FROM work_repay a
		<where>
			a.del_flag = '0'
			AND a.loan_id = #{loanId}
			AND a.status ='5'
		</where>
	</select>
</mapper>