<?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.business.borrow.mapper.BorrowMapper">

    <select id="findPageList" resultType="com.jeeplus.business.borrow.service.dto.BorrowDTO">
        select distinct
            a.id,
            a.create_by_id,
            a.create_time,
            a.no,
            a.reason,
            a.money_lower,
            a.money,
            (select (a.money_lower-(select sum(refund_money) from jy_refund_history where refund_id = re.id))) as finalMoney,
            a.proc_ins_id,
            a.process_definition_id,
            a.status,
            (select sum(refund_money) from jy_refund_history where refund_id = re.id) as refundLower,
            re.id as refundId,
            re.proc_ins_id as procInsIdRefund,
            re.process_definition_id as procDefIdRefund,
            re.status as refundStatus,
            (select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=a.status and dt.type='program_project_list_info_status') as borrowStatus,
            (select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=re.status and dt.type='program_project_list_info_status') as refStatus,
            so.name as officeName,
            su.name as createName,
            art1.ID_ as task_id,
            art2.ID_ as task_id_refund

        from jy_borrow_money a
        left join jy_refund re on a.id = re.borrow_id
        left join jy_refund_history rh on re.id = rh.refund_id
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        LEFT JOIN act_ru_task art1 ON a.proc_ins_id = art1.PROC_INST_ID_
        LEFT JOIN act_ru_task art2 ON re.proc_ins_id = art2.PROC_INST_ID_
        left join sys_user_manage_office sumo on sumo.office_id = su.office_id
        ${ew.customSqlSegment}
        ORDER BY a.create_time DESC
    </select>

    <select id="findById" resultType="com.jeeplus.business.borrow.service.dto.BorrowDTO">
        select
            a.id,
            a.create_by_id,
            a.create_time,
            a.no,
            a.reason,
            a.money_lower,
            a.money,
            a.final_money as boMoney,
            re.final_money,
            a.proc_ins_id,
            a.process_definition_id,
            a.status,
            re.borrow_id as borrowId,
            re.refund_lower,
            re.refund,
            re.id as refundId,
            re.proc_ins_id as procInsIdRefund,
            re.process_definition_id as procDefIdRefund,
            re.status as refundStatus,
            so.name as officeName,
            su.name as createName,
            art1.ID_ as task_id,
            art2.ID_ as task_id_refund

        from jy_borrow_money a
        left join jy_refund re on a.id = re.borrow_id
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        LEFT JOIN act_ru_task art1 ON a.proc_ins_id = art1.PROC_INST_ID_
        LEFT JOIN act_ru_task art2 ON re.proc_ins_id = art2.PROC_INST_ID_
        where a.id=#{id} or re.id = #{id}
    </select>

    <update id="updateStatusById">
        update jy_borrow_money set status = #{status} where id=#{id}
    </update>

    <update id="updateRefundById">
        update jy_refund set status = #{status} where id=#{id}
    </update>

    <update id="updateInfoById">
        update
            jy_borrow_money
        set
            money = #{borrowMoney.money},
            money_lower = #{borrowMoney.moneyLower},
            final_money = #{borrowMoney.finalMoney}
        where id=#{borrowMoney.id}
    </update>

    <update id="updateRefundInfoById">
        update
            jy_refund
        set
            refund=#{refundInfo.refund},
            refund_lower = #{refundInfo.refundLower}
        where id=#{refundInfo.id}
    </update>

    <select id="listByUserId" resultType="com.jeeplus.business.borrow.domain.BorrowMoney">
        select
            a.id,
            a.no,
            a.money_lower,
            a.final_money,
            a.reason,
            so.name as officeName,
            su.name as createName
        from jy_borrow_money a
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="findByBorId" resultType="com.jeeplus.business.borrow.domain.RefundHis">
        select
            a.id,
            a.create_by_id,
            a.create_time,
            a.refund_money,
            su.name as createName
        from jy_refund_history a
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        where  a.del_flag = '0' and a.refund_id = #{id} order by a.create_time DESC
    </select>

    <select id="findHisById" resultType="com.jeeplus.business.borrow.domain.RefundHis">
        select
            a.id,
            a.create_by_id,
            a.create_time,
            a.refund_money,
            a.final_money,
            su.name as createName
        from jy_refund_history a
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        where a.id = #{id}
    </select>

    <select id="getByRefundId" resultType="com.jeeplus.business.borrow.domain.RefundHis">
        select
            a.id,
            a.create_by_id,
            a.create_time,
            a.refund_money,
            a.final_money,
            su.name as createName
        from jy_refund_history a
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        where a.refund_id = #{id}
        order by a.create_time DESC limit 1
    </select>
    <select id="getByReId" resultType="com.jeeplus.business.borrow.service.dto.BorrowDTO">
    SELECT
	a.id,
	a.create_by_id,
	a.create_time,
	a.NO,
	a.reason,
	a.money_lower,
	a.money,
	(
	SELECT
		(
		a.money_lower - ( SELECT sum( refund_money ) FROM jy_refund_history WHERE refund_id = re.id ))) AS finalMoney,
	a.proc_ins_id,
	a.process_definition_id,
	a.STATUS,
	( SELECT sum( refund_money ) FROM jy_refund_history WHERE refund_id = re.id ) AS refundLower,
	re.id AS refundId,
	re.proc_ins_id AS procInsIdRefund,
	re.process_definition_id AS procDefIdRefund,
	re.STATUS AS refundStatus,
	(
	SELECT
		dv.label
	FROM
		sys_dict_value dv
		LEFT JOIN sys_dict_type dt ON dt.id = dv.dict_type_id
	WHERE
		dv.
	VALUE
		= a.STATUS
		AND dt.type = 'program_project_list_info_status'
	) AS borrowStatus,
	(
	SELECT
		dv.label
	FROM
		sys_dict_value dv
		LEFT JOIN sys_dict_type dt ON dt.id = dv.dict_type_id
	WHERE
		dv.
	VALUE
		= re.STATUS
		AND dt.type = 'program_project_list_info_status'
	) AS refStatus,
	so.NAME AS officeName,
	su.NAME AS createName
FROM
	jy_borrow_money a
	LEFT JOIN jy_refund re ON a.id = re.borrow_id
	AND re.tenant_id = '10006'
	LEFT JOIN sys_user su ON a.create_by_id = su.id
	AND su.del_flag = '0'
	AND su.tenant_id = '10006'
	LEFT JOIN sys_office so ON su.office_id = so.id
	AND so.del_flag = '0'
	AND so.tenant_id = '10006'
WHERE
	a.id = #{id}
	 and a.del_flag = '0'
    </select>



</mapper>