<?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.modules.trading.mapper.BizSupplierSettlementMapper">

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizSupplierSettlement" id="BizSupplierSettlementResult">
        <id     property="id"                   column="id"/>
        <result property="settlementNo"         column="settlement_no"/>
        <result property="orderId"              column="order_id"/>
        <result property="orderNo"              column="order_no"/>
        <result property="supplierId"           column="supplier_id"/>
        <result property="supplierName"         column="supplier_name"/>
        <result property="totalHeads"           column="total_heads"/>
        <result property="totalAmount"          column="total_amount"/>
        <result property="feeMethod"            column="fee_method"/>
        <result property="feeStandard"          column="fee_standard"/>
        <result property="transactionFeeRate"   column="transaction_fee_rate"/>
        <result property="managementFeeRate"    column="management_fee_rate"/>
        <result property="serviceFeeAmount"     column="service_fee_amount"/>
        <result property="payableAmount"        column="payable_amount"/>
        <result property="settlementStatus"     column="settlement_status"/>
        <result property="settleVoucherUrl"     column="settle_voucher_url"/>
        <result property="settleVoucherPath"    column="settle_voucher_path"/>
        <result property="settleFinishTime"     column="settle_finish_time"/>
        <result property="delFlag"              column="del_flag"/>
        <result property="createBy"             column="create_by"/>
        <result property="createTime"           column="create_time"/>
        <result property="updateBy"             column="update_by"/>
        <result property="updateTime"           column="update_time"/>
        <result property="remark"               column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select s.id, s.settlement_no, s.order_id, s.order_no, s.supplier_id, sup.supplier_name,
               s.total_heads, s.total_amount, s.fee_method, s.fee_standard,
               s.transaction_fee_rate, s.management_fee_rate,
               s.service_fee_amount, s.payable_amount, s.settlement_status,
               s.settle_voucher_url, s.settle_voucher_path, s.settle_finish_time,
               s.del_flag, s.create_by, s.create_time, s.update_by, s.update_time, s.remark
        from biz_supplier_settlement s
        left join biz_supplier sup on sup.id = s.supplier_id and sup.del_flag = '0'
    </sql>

    <select id="selectBizSupplierSettlementById" parameterType="long" resultMap="BizSupplierSettlementResult">
        <include refid="selectVo"/>
        where s.id = #{id} and s.del_flag = '0'
    </select>

    <select id="selectBizSupplierSettlementList" parameterType="com.ruoyi.web.modules.trading.domain.BizSupplierSettlement"
            resultMap="BizSupplierSettlementResult">
        <include refid="selectVo"/>
        <where>
            s.del_flag = '0'
            <if test="settlementNo != null and settlementNo != ''">
                and s.settlement_no like concat('%', #{settlementNo}, '%')
            </if>
            <if test="orderNo != null and orderNo != ''">
                and s.order_no like concat('%', #{orderNo}, '%')
            </if>
        </where>
        order by s.create_time desc, s.id desc
    </select>

    <select id="selectByOrderId" parameterType="long" resultMap="BizSupplierSettlementResult">
        <include refid="selectVo"/>
        where s.order_id = #{orderId} and s.del_flag = '0'
    </select>

    <select id="selectMaxSettlementNoSuffixByDatePrefix" resultType="string">
        select max(right(settlement_no, 3))
        from biz_supplier_settlement
        where del_flag = '0'
          and settlement_no like concat('JSD', #{datePrefix}, '%')
          and length(settlement_no) = 14
    </select>

    <insert id="insertBizSupplierSettlement" parameterType="com.ruoyi.web.modules.trading.domain.BizSupplierSettlement"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_supplier_settlement (
            settlement_no, order_id, order_no, supplier_id, total_heads, total_amount,
            fee_method, fee_standard, transaction_fee_rate, management_fee_rate,
            service_fee_amount, payable_amount, settlement_status, del_flag,
            create_by, create_time, update_by, update_time, remark
        ) values (
            #{settlementNo}, #{orderId}, #{orderNo}, #{supplierId}, #{totalHeads}, #{totalAmount},
            #{feeMethod}, #{feeStandard}, #{transactionFeeRate}, #{managementFeeRate},
            #{serviceFeeAmount}, #{payableAmount}, #{settlementStatus}, #{delFlag},
            #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark}
        )
    </insert>

    <update id="updateCompleteSettle" parameterType="com.ruoyi.web.modules.trading.domain.BizSupplierSettlement">
        update biz_supplier_settlement
        set settlement_status = #{settlementStatus},
            settle_voucher_url = #{settleVoucherUrl},
            settle_voucher_path = #{settleVoucherPath},
            settle_finish_time = #{settleFinishTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and settlement_status = '0'
          and del_flag = '0'
    </update>

    <select id="countPendingBySupplierId" resultType="long">
        select count(*)
        from biz_supplier_settlement
        where supplier_id = #{supplierId}
          and del_flag = '0'
          and settlement_status = '0'
    </select>

</mapper>
