<?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.ss.refund.mapper.RefundMapper">

    <resultMap type="RefundVO" id="RefundResult" autoMapping="true"/>

    <sql id="selectRefundVo">
        select
            sr.refund_id,
            sr.refund_no,
            sr.bill_id,
            sr.amount,
            sr.create_time,
            sr.status,
            sr.reason,
            sr.mch_amount,
            sr.service_amount,
            sr.user_type,
            sr.user_id,
            sr.user_name,
            sr.bst_type,
            sr.channel_id,
            sr.app_id,
            spb.amount as pay_amount,
            spb.pay_no as pay_no
        from ss_refund sr
            left join ss_pay_bill spb on spb.pay_id = sr.bill_id
    </sql>

    <sql id="searchCondition">
        <if test="query.refundId != null "> and sr.refund_id = #{query.refundId}</if>
        <if test="query.refundNo != null  and query.refundNo != ''"> and sr.refund_no = #{query.refundNo}</if>
        <if test="query.billId != null "> and sr.bill_id = #{query.billId}</if>
        <if test="query.status != null  and query.status != ''"> and sr.status = #{query.status}</if>
        <if test="query.userType != null  and query.userType != ''"> and sr.user_type = #{query.userType}</if>
        <if test="query.userId != null "> and sr.user_id = #{query.userId}</if>
        <if test="query.userName != null  and query.userName != ''"> and sr.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.bstType != null  and query.bstType != ''"> and sr.bst_type = #{query.bstType}</if>
        <if test="query.channelId != null "> and sr.channel_id = #{query.channelId}</if>
        <if test="query.appId != null "> and sr.app_id = #{query.appId}</if>
        <if test="query.createDateStart != null">and date(sr.create_time) >= #{query.createDateStart}</if>
        <if test="query.createDateEnd != null">and date(sr.create_time) &lt;= #{query.createDateEnd}</if>
        <if test="query.statusList != null and query.statusList.size() > 0">
            and sr.status in
            <foreach item="item" collection="query.statusList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectRefundList" parameterType="Refund" resultMap="RefundResult">
        <include refid="selectRefundVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by sr.create_time desc
    </select>

    <select id="selectRefundByRefundId" parameterType="Long" resultMap="RefundResult">
        <include refid="selectRefundVo"/>
        where refund_id = #{refundId}
    </select>

    <select id="selectOne" resultMap="RefundResult">
        <include refid="selectRefundVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        limit 1
    </select>

    <resultMap id="LocalDateDecimalVO" type="LocalDateDecimalVO" autoMapping="true">
        <result column="value" property="value" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
    </resultMap>
    <select id="selectDailyAmount" resultMap="LocalDateDecimalVO">
        select
            sum(sr.amount) as `value`,
            date(sr.create_time) as `key`
        from ss_refund sr
        <where>
            <include refid="searchCondition"/>
        </where>
        group by `key`
    </select>

    <insert id="insertRefund" parameterType="Refund" useGeneratedKeys="true" keyProperty="refundId">
        insert into ss_refund
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="refundNo != null and refundNo != ''">refund_no,</if>
            <if test="billId != null">bill_id,</if>
            <if test="amount != null">amount,</if>
            <if test="createTime != null">create_time,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="reason != null and reason != ''">`reason`,</if>
            <if test="mchAmount != null">mch_amount,</if>
            <if test="serviceAmount != null">service_amount,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="bstType != null">bst_type,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="appId != null">app_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="refundNo != null and refundNo != ''">#{refundNo},</if>
            <if test="billId != null">#{billId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="reason != null and reason != ''">#{reason},</if>
            <if test="mchAmount != null">#{mchAmount},</if>
            <if test="serviceAmount != null">#{serviceAmount},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="bstType != null">#{bstType},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="appId != null">#{appId},</if>
         </trim>
    </insert>

    <update id="updateRefund" parameterType="Refund">
        update ss_refund
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where refund_id = #{refundId}
    </update>

    <sql id="updateColumns">
        <if test="data.refundNo != null and data.refundNo != ''">refund_no = #{data.refundNo},</if>
        <if test="data.billId != null">bill_id = #{data.billId},</if>
        <if test="data.amount != null">amount = #{data.amount},</if>
        <if test="data.mchAmount != null">mch_amount = #{data.mchAmount},</if>
        <if test="data.serviceAmount != null">service_amount = #{data.serviceAmount},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.reason != null and data.reason != ''">`reason` = #{data.reason},</if>
        <if test="data.userType != null">user_type = #{data.userType},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.userName != null">user_name = #{data.userName},</if>
        <if test="data.bstType != null">bst_type = #{data.bstType},</if>
        <if test="data.channelId != null">channel_id = #{data.channelId},</if>
        <if test="data.appId != null">app_id = #{data.appId},</if>
    </sql>

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

    <delete id="deleteRefundByRefundId" parameterType="Long">
        delete from ss_refund where refund_id = #{refundId}
    </delete>

    <delete id="deleteRefundByRefundIds" parameterType="String">
        delete from ss_refund where refund_id in
        <foreach item="refundId" collection="array" open="(" separator="," close=")">
            #{refundId}
        </foreach>
    </delete>
</mapper>
