<?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.checkoff.mapper.CheckoffRecordsMapper">

    <resultMap type="CheckoffRecordsVO" id="CheckoffRecordsResult" autoMapping="true" />

    <sql id="selectCheckoffRecordsVo">
        select
            cr.checkoff_id,
            cr.encrypted_code,
            cr.verify_token,
            cr.poi_id,
            cr.card_id,
            c.name as cardName,
            cr.checkoff_time,
            cr.user_id,
            u.user_name userName,
            cr.status,
            cr.result,
            cr.original_amount,
            cr.list_market_amount,
            cr.pay_amount,
            cr.sku_id
        from ss_checkoff_records cr
        left join ss_card c on cr.card_id = c.coupon_id
        left join ss_user u on cr.user_id = u.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.encryptedCode != null  and query.encryptedCode != ''"> and encrypted_code = #{query.encryptedCode}</if>
        <if test="query.verifyToken != null  and query.verifyToken != ''"> and verify_token = #{query.verifyToken}</if>
        <if test="query.poiId != null  and query.poiId != ''"> and poi_id = #{query.poiId}</if>
        <if test="query.cardId != null "> and card_id = #{query.cardId}</if>
        <if test="query.checkoffTime != null "> and checkoff_time = #{query.checkoffTime}</if>
        <if test="query.userId != null "> and user_id = #{query.userId}</if>
        <if test="query.status != null  and query.status != ''"> and status = #{query.status}</if>
    </sql>

    <select id="selectCheckoffRecordsList" parameterType="CheckoffRecordsQuery" resultMap="CheckoffRecordsResult">
        <include refid="selectCheckoffRecordsVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCheckoffRecordsByCheckoffId" parameterType="Long" resultMap="CheckoffRecordsResult">
        <include refid="selectCheckoffRecordsVo"/>
        where checkoff_id = #{checkoffId}
    </select>

    <select id="selectCheckoffRecordsByCardId" parameterType="Long" resultMap="CheckoffRecordsResult">
        <include refid="selectCheckoffRecordsVo"/>
        where card_id = #{cardId}
        order by checkoff_time desc limit 1
    </select>

    <insert id="insertCheckoffRecords" parameterType="CheckoffRecords">
        insert into ss_checkoff_records
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkoffId != null">checkoff_id,</if>
            <if test="encryptedCode != null">encrypted_code,</if>
            <if test="verifyToken != null">verify_token,</if>
            <if test="poiId != null">poi_id,</if>
            <if test="cardId != null">card_id,</if>
            <if test="checkoffTime != null">checkoff_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="status != null">status,</if>
            <if test="result != null">result,</if>
            <if test="originalAmount != null">original_amount,</if>
            <if test="listMarketAmount != null">list_market_amount,</if>
            <if test="payAmount != null">pay_amount,</if>
            <if test="skuId != null">sku_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="checkoffId != null">#{checkoffId},</if>
            <if test="encryptedCode != null">#{encryptedCode},</if>
            <if test="verifyToken != null">#{verifyToken},</if>
            <if test="poiId != null">#{poiId},</if>
            <if test="cardId != null">#{cardId},</if>
            <if test="checkoffTime != null">#{checkoffTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="status != null">#{status},</if>
            <if test="result != null">#{result},</if>
            <if test="originalAmount != null">#{originalAmount},</if>
            <if test="listMarketAmount != null">#{listMarketAmount},</if>
            <if test="payAmount != null">#{payAmount},</if>
            <if test="skuId != null">#{skuId},</if>
         </trim>
    </insert>

    <update id="updateCheckoffRecords" parameterType="CheckoffRecords">
        update ss_checkoff_records
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where checkoff_id = #{data.checkoffId}
    </update>

    <sql id="updateColumns">
        <if test="data.encryptedCode != null">encrypted_code = #{data.encryptedCode},</if>
        <if test="data.verifyToken != null">verify_token = #{data.verifyToken},</if>
        <if test="data.poiId != null">poi_id = #{data.poiId},</if>
        <if test="data.cardId != null">card_id = #{data.cardId},</if>
        <if test="data.checkoffTime != null">checkoff_time = #{data.checkoffTime},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.originalAmount != null">original_amount = #{data.originalAmount},</if>
        <if test="data.listMarketAmount != null">list_market_amount = #{data.listMarketAmount},</if>
        <if test="data.payAmount != null">pay_amount = #{data.payAmount},</if>
        <if test="data.skuId != null">sku_id = #{data.skuId},</if>
    </sql>

    <delete id="deleteCheckoffRecordsByCheckoffId" parameterType="Long">
        delete from ss_checkoff_records where checkoff_id = #{checkoffId}
    </delete>

    <delete id="deleteCheckoffRecordsByCheckoffIds" parameterType="String">
        delete from ss_checkoff_records where checkoff_id in
        <foreach item="checkoffId" collection="array" open="(" separator="," close=")">
            #{checkoffId}
        </foreach>
    </delete>
</mapper>
