<?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.ruoyi.web.arap.mapper.ArapDeductMapper">

    <select id="selectDeductList" resultType="com.ruoyi.web.arap.domain.vo.ArapDeductItemVo">
        select
            r.id as docId,
            '收款单' as docType,
            r.recv_no as docNo,
            r.cust_no as custNo,
            r.cust_name as custName,
            r.bank_no as bankNo,
            r.recv_amt as arAmt,
            r.recv_date as docDate,
            ifnull(r.is_deduct, '0') as deducted
        from arap_recv r
        where r.del_flag = '0'
          and r.org_id = #{orgId}
          <if test="bizDate != null">
            and r.recv_date = #{bizDate}
          </if>
        union all
        select
            f.id as docId,
            '退款单' as docType,
            f.ref_no as docNo,
            f.cust_no as custNo,
            f.cust_name as custName,
            f.bank_no as bankNo,
            (0 - f.ref_amt) as arAmt,
            f.ref_date as docDate,
            ifnull(f.is_deduct, '0') as deducted
        from arap_refund f
        where f.del_flag = '0'
          and f.org_id = #{orgId}
          <if test="bizDate != null">
            and f.ref_date = #{bizDate}
          </if>
        order by docDate desc, docNo desc
    </select>

    <update id="updateRecvDeducted">
        update arap_recv
        set is_deduct = #{deducted},
            update_by = #{updateBy},
            update_time = now()
        where id = #{id}
          and org_id = #{orgId}
          and del_flag = '0'
    </update>

    <update id="updateRefundDeducted">
        update arap_refund
        set is_deduct = #{deducted},
            update_by = #{updateBy},
            update_time = now()
        where id = #{id}
          and org_id = #{orgId}
          and del_flag = '0'
    </update>
</mapper>

