<?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.finance.contractRegistration.mapper.CwWorkContractBorrowMapper">

    <update id="updateStatusById">
        UPDATE cw_work_contract_borrow SET borrow_type = #{borrowType} WHERE del_flag = 0 AND id = #{id}
    </update>
    <update id="updateStatusByContractInfoId">
        UPDATE cw_work_contract_borrow SET borrow_type = #{borrowType} WHERE del_flag = 0 AND contract_info_id = #{id}
    </update>
    <update id="updateMessageByBorrowId">
        UPDATE cw_work_contract_borrow_message SET borrow_type = #{borrowType}
        <if test="type != null and type != ''">
            ,ret_data = NOW()
        </if>
        WHERE del_flag = 0 AND contract_borrow_id = #{id} order by create_time desc limit 1
    </update>
    <update id="deleteMessageById">
        UPDATE cw_work_contract_borrow_message SET del_flag = 1
		WHERE del_flag = 0 AND contract_borrow_id = #{id} order by create_time desc limit 1
    </update>
    <select id="selectFileByContractInfoId"
            resultType="com.jeeplus.finance.contractRegistration.domain.CwWorkContractBorrow">
        SELECT * FROM `cw_work_contract_borrow` WHERE del_flag = 0 AND contract_info_id = #{id}
    </select>
    <select id="findMessageList"
            resultType="com.jeeplus.finance.contractRegistration.domain.CwWorkContractBorrowMessage">
        SELECT
			id,
			create_by_id,
			create_time,
			update_by_id,
			update_time,
			del_flag,
			contract_borrow_id,
			contract_name,
			borrow_name,
			DATE_FORMAT(borrow_data,'%Y-%m-%d') as borrow_data,
			DATE_FORMAT(borrow_ret_data,'%Y-%m-%d') as borrow_ret_data,
			borrow_type,
			DATE_FORMAT( ret_data, '%Y-%m-%d' ) AS ret_data
		 FROM cw_work_contract_borrow_message WHERE del_flag = 0 AND contract_borrow_id = #{id}
		 order by create_time desc
    </select>
    <select id="selectByInfoId"
            resultType="com.jeeplus.finance.contractRegistration.domain.CwWorkContractBorrow">
        select * from cw_work_contract_borrow where contract_info_id = #{id}
    </select>


    <select id="getAdventMessageList" resultType="com.jeeplus.finance.contractRegistration.domain.CwWorkContractBorrowMessage">
        SELECT
			a.id,
			a.create_by_id,
			wci.create_by_id as "contractCreateById",
			wcb.contract_info_id as "contractId",
			wcb.proc_ins_id as "procInsId",
			wcb.process_definition_id as "processDefinitionId",
			a.create_time,
			a.update_by_id,
			a.update_time,
			a.del_flag,
			a.contract_borrow_id,
			a.contract_name,
			a.borrow_name,
			DATE_FORMAT(a.borrow_data,'%Y-%m-%d') as borrow_data,
			DATE_FORMAT(a.borrow_ret_data,'%Y-%m-%d') as borrow_ret_data,
			a.borrow_type,
			DATE_FORMAT( a.ret_data, '%Y-%m-%d' ) AS ret_data
		 FROM cw_work_contract_borrow_message a
		 left join cw_work_contract_borrow wcb on wcb.id = a.contract_borrow_id
		 left join cw_work_contract_info wci on wci.id = wcb.contract_info_id
		 WHERE a.del_flag = 0 and a.ret_data is null
			and now() > date_add(a.borrow_ret_data,interval #{adventDay} day)
		 order by a.create_time desc
    </select>
</mapper>
