<?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.workreimbursementback.dao.WorkReimbursementBackDao">
    
	<sql id="workReimbursementBackColumns">
		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.achives_id as "achivesId",
		a.work_reimbursement_id AS "workReimbursementId",
		a.work_loan_id AS "workLoanId",
		a.money AS "money",
		DATE_FORMAT(w.loan_date, '%Y-%m-%d') as "workLoan.loanDate",
		w.id as "workLoan.id",
		CASE w.type
         WHEN '1' THEN '行政借款'
         WHEN '2' THEN '项目借款' end
		 as "workLoan.type",
		w.money as "workLoan.money",
		(w.money+0)-ifnull((w.repay+0),0)  as "workLoan.notRepay",
		w.num as "workLoan.num",
		p.project_name as "workLoan.projectRecords.projectName"
	</sql>
	
	<sql id="workReimbursementBackJoins">
		left join work_loan w on a.work_loan_id=w.id
		left join project_records p on p.id=w.project_record_id
	</sql>
	
    
	<select id="get" resultType="WorkReimbursementBack" >
		SELECT 
			<include refid="workReimbursementBackColumns"/>
		FROM work_reimbursement_back a
		<include refid="workReimbursementBackJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReimbursementBack" >
		SELECT 
			<include refid="workReimbursementBackColumns"/>
		FROM work_reimbursement_back a
		<include refid="workReimbursementBackJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="workReimbursementId != null and workReimbursementId != ''">
				AND a.work_reimbursement_id = #{workReimbursementId}
			</if>
			<if test="workLoanId != null and workLoanId != ''">
				AND a.work_loan_id = #{workLoanId}
			</if>
			<if test="money != null and money != ''">
				AND a.money = #{money}
			</if>
			<if test="achivesId != null and achivesId != ''">
				AND a.achives_id = #{achivesId}
			</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="WorkReimbursementBack" >
		SELECT 
			<include refid="workReimbursementBackColumns"/>
		FROM work_reimbursement_back a
		<include refid="workReimbursementBackJoins"/>
		<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_reimbursement_back(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			work_reimbursement_id,
			work_loan_id,
			money,
			achives_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{workReimbursementId},
			#{workLoan.id},
			#{money},
			#{achivesId}

		)
	</insert>
	
	<update id="update">
		UPDATE work_reimbursement_back SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			work_reimbursement_id = #{workReimbursementId},
			work_loan_id = #{workLoan.id},
			money = #{money},
			achives_id =#{achivesId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_reimbursement_back
		WHERE id = #{id}
	</update>
	<update id="deleteLoan">
		DELETE FROM work_reimbursement_back
		WHERE work_reimbursement_id = #{workReimbursementId} and achives_id !=#{achivesId}
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_reimbursement_back SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReimbursementBack" statementType="STATEMENT">
		select * FROM work_reimbursement_back  where ${propertyName} = '${value}'
	</select>
	<select id="findLoanList" resultType="WorkReimbursementBack" >
		SELECT
		w.id as "workLoan.id",
		DATE_FORMAT(w.loan_date, '%Y-%m-%d') as "workLoan.loanDate",
		CASE w.type
         WHEN '1' THEN '行政借款'
         WHEN '2' THEN '项目借款' end
		 as "workLoan.type",
		w.money as "workLoan.money",
		(w.money+0)-ifnull((w.repay+0),0)  as "workLoan.notRepay",
		w.num as "workLoan.num",
		p.project_name as "workLoan.projectRecords.projectName"
		FROM work_loan w
		left join project_records p on p.id=w.project_record_id
		WHERE w.staff_achives_id = #{achivesId} and w.status='5' and w.repay_state!='1'
	</select>
</mapper>