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

    <select id="selectMaxWithdrawNoByPrefix" resultType="string">
        select max(withdraw_no) from biz_shop_withdraw where withdraw_no like concat(#{prefix}, '%')
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.modules.finance.domain.BizShopWithdraw" useGeneratedKeys="true" keyProperty="withdrawId">
        insert into biz_shop_withdraw (
            withdraw_no, shop_id, shop_name, account_id, account_type,
            account_real_name, account_no_mask, withdraw_amount, remark,
            withdraw_status, apply_time, create_by
        ) values (
            #{withdrawNo}, #{shopId}, #{shopName}, #{accountId}, #{accountType},
            #{accountRealName}, #{accountNoMask}, #{withdrawAmount}, #{remark},
            #{withdrawStatus}, #{applyTime}, #{createBy}
        )
    </insert>

    <select id="selectById" resultType="com.ruoyi.web.modules.finance.domain.BizShopWithdraw">
        select withdraw_id as withdrawId, withdraw_no as withdrawNo, shop_id as shopId, shop_name as shopName,
               account_id as accountId, account_type as accountType, account_real_name as accountRealName,
               account_no_mask as accountNoMask, withdraw_amount as withdrawAmount, remark as remark,
               withdraw_status as withdrawStatus, process_remark as processRemark,
               audit_by as auditBy, audit_time as auditTime, apply_time as applyTime, create_by as createBy
        from biz_shop_withdraw
        where withdraw_id = #{withdrawId}
    </select>

    <select id="selectSellerList" resultType="com.ruoyi.web.modules.finance.vo.SellerWithdrawListRowVO">
        select withdraw_no as withdrawNo,
               apply_time as applyTime,
               account_type as accountType,
               account_real_name as accountRealName,
               account_no_mask as accountNoMask,
               withdraw_amount as withdrawAmount,
               remark as remark,
               withdraw_status as withdrawStatus,
               process_remark as processRemark
        from biz_shop_withdraw
        where shop_id = #{shopId}
        <if test="query.withdrawStatus != null and query.withdrawStatus != ''">
            and withdraw_status = #{query.withdrawStatus}
        </if>
        <if test="query.beginApplyTime != null and query.beginApplyTime != ''">
            and apply_time &gt;= concat(#{query.beginApplyTime}, ' 00:00:00')
        </if>
        <if test="query.endApplyTime != null and query.endApplyTime != ''">
            and apply_time &lt;= concat(#{query.endApplyTime}, ' 23:59:59')
        </if>
        order by apply_time desc, withdraw_id desc
    </select>

    <select id="selectPlatformList" resultType="com.ruoyi.web.modules.finance.vo.PlatformWithdrawAuditListRowVO">
        select withdraw_id as withdrawId,
               withdraw_no as withdrawNo,
               apply_time as applyTime,
               shop_name as shopName,
               account_type as accountType,
               account_real_name as accountRealName,
               account_no_mask as accountNoMask,
               withdraw_amount as withdrawAmount,
               remark as remark,
               withdraw_status as withdrawStatus,
               process_remark as processRemark
        from biz_shop_withdraw
        where 1 = 1
        <if test="query.withdrawStatus != null and query.withdrawStatus != ''">
            and withdraw_status = #{query.withdrawStatus}
        </if>
        <if test="query.beginApplyTime != null and query.beginApplyTime != ''">
            and apply_time &gt;= concat(#{query.beginApplyTime}, ' 00:00:00')
        </if>
        <if test="query.endApplyTime != null and query.endApplyTime != ''">
            and apply_time &lt;= concat(#{query.endApplyTime}, ' 23:59:59')
        </if>
        order by apply_time desc, withdraw_id desc
    </select>

    <select id="selectPlatformSummaryList" resultType="com.ruoyi.web.modules.finance.vo.PlatformWithdrawSummaryRowVO">
        select s.shop_id as shopId,
               s.shop_name as shopName,
               s.shop_avatar as shopAvatar,
               s.shop_status as shopStatus,
               s.del_flag as delFlag,
               ifnull(wd.withdrawn_amount, 0) as withdrawnAmount,
               ifnull(wd.pending_audit_amount, 0) as pendingAuditAmount
        from biz_shop s
        left join (
            select shop_id,
                   sum(case when withdraw_status = '3' then withdraw_amount else 0 end) as withdrawn_amount,
                   sum(case when withdraw_status = '1' then withdraw_amount else 0 end) as pending_audit_amount
            from biz_shop_withdraw
            group by shop_id
        ) wd on wd.shop_id = s.shop_id
        where (
            s.del_flag = '0'
            or wd.shop_id is not null
        )
        <if test="query.shopName != null and query.shopName != ''">
            and s.shop_name like concat('%', #{query.shopName}, '%')
        </if>
        order by pendingAuditAmount desc, s.shop_id desc
    </select>

    <select id="selectSummaryDetailList" resultType="com.ruoyi.web.modules.finance.vo.PlatformWithdrawSummaryDetailRowVO">
        select apply_time as applyTime,
               shop_name as shopName,
               account_type as accountType,
               account_real_name as accountRealName,
               account_no_mask as accountNoMask,
               withdraw_amount as withdrawAmount,
               remark as remark,
               withdraw_status as withdrawStatus,
               process_remark as processRemark
        from biz_shop_withdraw
        where shop_id = #{shopId}
        <if test="query.withdrawStatus != null and query.withdrawStatus != ''">
            and withdraw_status = #{query.withdrawStatus}
        </if>
        <if test="query.beginApplyTime != null and query.beginApplyTime != ''">
            and apply_time &gt;= concat(#{query.beginApplyTime}, ' 00:00:00')
        </if>
        <if test="query.endApplyTime != null and query.endApplyTime != ''">
            and apply_time &lt;= concat(#{query.endApplyTime}, ' 23:59:59')
        </if>
        order by apply_time desc, withdraw_id desc
    </select>

    <select id="countByStatus" resultType="int">
        select count(1) from biz_shop_withdraw where withdraw_status = #{withdrawStatus}
    </select>

    <update id="updateForAudit" parameterType="com.ruoyi.web.modules.finance.domain.BizShopWithdraw">
        update biz_shop_withdraw
        set withdraw_status = #{withdrawStatus},
            process_remark = #{processRemark},
            audit_by = #{auditBy},
            audit_time = #{auditTime}
        where withdraw_id = #{withdrawId}
          and withdraw_status = '1'
    </update>

</mapper>
