<?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.bst.pay.mapper.PayMapper">

    <resultMap type="PayVO" id="PayResult" autoMapping="true"/>

    <sql id="selectPayVo">
        select
            bp.id,
            bp.no,
            bp.bst_type,
            bp.bst_id,
            bp.store_id,
            bp.amount,
            bp.channel_id,
            bp.status,
            bp.pay_time,
            bp.create_time,
            bp.description,
            bp.account,
            bp.channel_cost,
            bp.refunded,
            bp.refunding,
            bp.ip,
            bp.app_id,
            bp.channel_no,
            bp.expire_time,
            bp.app_name,
            bp.channel_name
        from bst_pay bp
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bp.id = #{query.id}</if>
        <if test="query.no != null  and query.no != ''"> and bp.no like concat('%', #{query.no}, '%')</if>
        <if test="query.bstType != null  and query.bstType != ''"> and bp.bst_type = #{query.bstType}</if>
        <if test="query.bstId != null "> and bp.bst_id = #{query.bstId}</if>
        <if test="query.amount != null "> and bp.amount = #{query.amount}</if>
        <if test="query.channelId != null "> and bp.channel_id = #{query.channelId}</if>
        <if test="query.status != null  and query.status != ''"> and bp.status = #{query.status}</if>
        <if test="query.description != null  and query.description != ''"> and bp.description like concat('%', #{query.description}, '%')</if>
        <if test="query.account != null  and query.account != ''"> and bp.account like concat('%', #{query.account}, '%')</if>
        <if test="query.ip != null  and query.ip != ''"> and bp.ip like concat('%', #{query.ip}, '%')</if>
        <if test="query.appId != null "> and bp.app_id = #{query.appId}</if>
        <if test="query.channelNo != null  and query.channelNo != ''"> and bp.channel_no like concat('%', #{query.channelNo}, '%')</if>
        <if test="query.eqNo != null  and query.eqNo != ''"> and bp.no = #{query.eqNo}</if>
        <if test="query.storeId != null  and query.storeId != ''"> and bp.store_id = #{query.storeId}</if>
        <if test="query.appName != null  and query.appName != ''"> and bp.app_name like concat('%', #{query.appName}, '%')</if>
        <if test="query.channelName != null  and query.channelName != ''"> and bp.channel_name like concat('%', #{query.channelName}, '%')</if>
        <if test="query.payMonth != null">and month(bp.pay_time) = #{query.payMonth}</if>
        <if test="query.payYear != null">and year(bp.pay_time) = #{query.payYear}</if>
        <if test="query.payDateStart != null"> and date(bp.pay_time) &gt;= #{query.payDateStart}</if>
        <if test="query.payDateEnd != null"> and date(bp.pay_time) &lt;= #{query.payDateEnd}</if>
        <if test="query.payDate != null"> and date(bp.pay_time) = #{query.payDate} </if>
        <if test="query.statusList != null and query.statusList.size() > 0">
            and bp.status in
            <foreach collection="query.statusList" item="status" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
        <if test="query.storeIds != null and query.storeIds.size() > 0">
            and bp.store_id in
            <foreach collection="query.storeIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ${query.params.dataScope}
    </sql>

    <select id="selectPayList" parameterType="PayQuery" resultMap="PayResult">
        <include refid="selectPayVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectPayById" parameterType="Long" resultMap="PayResult">
        <include refid="selectPayVo"/>
        where bp.id = #{id}
    </select>

    
    <insert id="insertPay" parameterType="Pay" useGeneratedKeys="true" keyProperty="id">
        insert into bst_pay
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="no != null and no != ''">no,</if>
            <if test="bstType != null and bstType != ''">bst_type,</if>
            <if test="bstId != null">bst_id,</if>
            <if test="amount != null">amount,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="description != null">description,</if>
            <if test="account != null and account != ''">account,</if>
            <if test="channelCost != null">channel_cost,</if>
            <if test="refunded != null">refunded,</if>
            <if test="refunding != null">refunding,</if>
            <if test="ip != null">ip,</if>
            <if test="appId != null">app_id,</if>
            <if test="channelNo != null and channelNo != ''">channel_no,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="appName != null and appName != ''">app_name,</if>
            <if test="channelName != null and channelName != ''">channel_name,</if>
            <if test="storeId != null and storeId != ''">store_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="no != null and no != ''">#{no},</if>
            <if test="bstType != null and bstType != ''">#{bstType},</if>
            <if test="bstId != null">#{bstId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="description != null">#{description},</if>
            <if test="account != null and account != ''">#{account},</if>
            <if test="channelCost != null">#{channelCost},</if>
            <if test="refunded != null">#{refunded},</if>
            <if test="refunding != null">#{refunding},</if>
            <if test="ip != null">#{ip},</if>
            <if test="appId != null">#{appId},</if>
            <if test="channelNo != null and channelNo != ''">#{channelNo},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="appName != null and appName != ''">#{appName},</if>
            <if test="channelName != null and channelName != ''">#{channelName},</if>
            <if test="storeId != null and storeId != ''">#{storeId},</if>
         </trim>
    </insert>


    <update id="updatePay" parameterType="Pay">
        update bst_pay
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <update id="updateByQuery">
        update bst_pay bp
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <sql id="updateColumns">
        <if test="data.no != null and data.no != ''">no = #{data.no},</if>
        <if test="data.bstType != null and data.bstType != ''">bst_type = #{data.bstType},</if>
        <if test="data.bstId != null">bst_id = #{data.bstId},</if>
        <if test="data.amount != null">amount = #{data.amount},</if>
        <if test="data.channelId != null">channel_id = #{data.channelId},</if>
        <if test="data.status != null and data.status != ''">status = #{data.status},</if>
        <if test="data.payTime != null">pay_time = #{data.payTime},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.description != null">description = #{data.description},</if>
        <if test="data.account != null and data.account != ''">account = #{data.account},</if>
        <if test="data.channelCost != null">channel_cost = #{data.channelCost},</if>
        <if test="data.refunded != null">refunded = #{data.refunded},</if>
        <if test="data.refunding != null">refunding = #{data.refunding},</if>
        <if test="data.ip != null">ip = #{data.ip},</if>
        <if test="data.appId != null">app_id = #{data.appId},</if>
        <if test="data.channelNo != null and data.channelNo != ''">channel_no = #{data.channelNo},</if>
        <if test="data.expireTime != null">expire_time = #{data.expireTime},</if>
        <if test="data.appName != null and data.appName != ''">app_name = #{data.appName},</if>
        <if test="data.channelName != null and data.channelName != ''">channel_name = #{data.channelName},</if>
        <if test="data.storeId != null and data.storeId != ''">store_id = #{data.storeId},</if>
    </sql>

    <delete id="deletePayById" parameterType="Long">
        delete from bst_pay where id = #{id}
    </delete>

    <delete id="deletePayByIds" parameterType="String">
        delete from bst_pay where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- addRefundingAmount -->
    <insert id="addRefundingAmount">
        update bst_pay
        set refunding = refunding + #{amount}
        where id = #{id} and amount >= refunded + refunding + #{amount}
    </insert>

    <!-- recordRefundAmount --> 

    <update id="recordRefundAmount">
        update bst_pay
        set refunding = refunding - #{amount},
            refunded = refunded + #{amount}
        where id = #{id} and refunding - #{amount} >= 0
    </update>

    <!-- selectNoList --> 

    <select id="selectNoList" parameterType="PayQuery" resultType="String">
        select bp.no
        from bst_pay bp
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectPayAmount" resultType="com.ruoyi.common.domain.vo.LocalDateDecimalVO">
        select
        date(pay_time) as `key`,
        sum(bp.amount) as `value`
        from bst_pay bp
        <where>
            <include refid="searchCondition"/>
        </where>
        group by `key`
    </select>

    <select id="selectSumOfAmountGroupByStoreId" resultType="com.ruoyi.common.domain.vo.LongDecimalVO">
        select
        bp.store_id as `key`,
        sum(bp.amount) as `value`
        from bst_pay bp
        <where>
            <include refid="searchCondition"/>
        </where>
        group by `key`
    </select>


</mapper>
