<?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.card.mapper.SsCardMapper">

    <resultMap type="SsCardVO" id="SsCardResult" autoMapping="true" >
        <result property="availableWeek" column="available_week" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
        <result property="availableRooms" column="available_rooms" typeHandler="com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectSsCardVo">
        select
            c.coupon_id,
            c.name,
            c.type,
            c.discount_percent,
            c.store_id,
            s.name storeName,
            c.discount_amount,
            c.create_time,
            c.limit_num,
            c.descr,
            c.retail_price,
            c.stored_amount,
            c.hours,
            c.validity_value,
            c.validity_unit,
            c.status,
            c.is_hot,
            c.original_price,
            c.del_flag,
            c.available_rooms,
            c.available_week,
            c.available_time_end,
            c.available_time_start,
            c.merchant_id,
            c.sku_id
        from ss_card c
        left join ss_store s on c.store_id = s.store_id
    </sql>

    <sql id="searchCondition">
        <if test="query.name != null  and query.name != ''"> and c.name like concat('%', #{query.name}, '%')</if>
        <if test="query.type != null  and query.type != ''"> and c.type = #{query.type}</if>
        <if test="query.discountPercent != null "> and c.discount_percent = #{query.discountPercent}</if>
        <if test="query.storeId != null "> and c.store_id = #{query.storeId}</if>
        <if test="query.merchantId != null "> and c.merchant_id = #{query.merchantId}</if>
        <if test="query.discountAmount != null "> and c.discount_amount = #{query.discountAmount}</if>
        <if test="query.limitNum != null  and query.limitNum != ''"> and c.limit_num = #{query.limitNum}</if>
        <if test="query.descr != null  and query.descr != ''"> and c.descr = #{query.descr}</if>
        <if test="query.retailPrice != null "> and c.retail_price = #{query.retailPrice}</if>
        <if test="query.storedAmount != null "> and c.stored_amount = #{query.storedAmount}</if>
        <if test="query.hours != null "> and c.hours = #{query.hours}</if>
        <if test="query.validityValue != null "> and c.validity_value = #{query.validityValue}</if>
        <if test="query.validityUnit != null  and query.validityUnit != ''"> and c.validity_unit = #{query.validityUnit}</if>
        <if test="query.status != null  and query.status != ''"> and c.status = #{query.status}</if>
        <if test="query.isHot != null  and query.isHot != ''"> and c.is_hot = #{query.isHot}</if>
        <if test="query.originalPrice != null "> and c.original_price = #{query.originalPrice}</if>
        <if test="query.availableRooms != null  and query.availableRooms != ''"> and c.available_rooms = #{query.availableRooms}</if>
        <if test="query.availableWeek != null  and query.availableWeek != ''"> and c.available_week = #{query.availableWeek}</if>
        <if test="query.skuId != null "> and c.sku_id = #{query.skuId}</if>
    </sql>

    <select id="selectSsCardList" parameterType="SsCardQuery" resultMap="SsCardResult">
        <include refid="selectSsCardVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSsCardByCouponId" parameterType="Long" resultMap="SsCardResult">
        <include refid="selectSsCardVo"/>
        where c.coupon_id = #{couponId}
    </select>

    <select id="selectSsCardBySkuId" parameterType="string" resultMap="SsCardResult">
        <include refid="selectSsCardVo"/>
        where c.sku_id = #{skuId}
    </select>

    <insert id="insertSsCard" parameterType="SsCard" useGeneratedKeys="true" keyProperty="couponId">
        insert into ss_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="type != null">type,</if>
            <if test="discountPercent != null">discount_percent,</if>
            <if test="storeId != null">store_id,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="discountAmount != null">discount_amount,</if>
            <if test="createTime != null">create_time,</if>
            <if test="limitNum != null">limit_num,</if>
            <if test="descr != null">descr,</if>
            <if test="retailPrice != null">retail_price,</if>
            <if test="storedAmount != null">stored_amount,</if>
            <if test="hours != null">hours,</if>
            <if test="validityValue != null">validity_value,</if>
            <if test="validityUnit != null">validity_unit,</if>
            <if test="status != null">status,</if>
            <if test="isHot != null">is_hot,</if>
            <if test="originalPrice != null">original_price,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="availableRooms != null">available_rooms,</if>
            <if test="availableWeek != null">available_week,</if>
            <if test="availableTimeEnd != null">available_time_end,</if>
            <if test="availableTimeStart != null">available_time_start,</if>
            <if test="skuId != null">sku_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="discountPercent != null">#{discountPercent},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="discountAmount != null">#{discountAmount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="limitNum != null">#{limitNum},</if>
            <if test="descr != null">#{descr},</if>
            <if test="retailPrice != null">#{retailPrice},</if>
            <if test="storedAmount != null">#{storedAmount},</if>
            <if test="hours != null">#{hours},</if>
            <if test="validityValue != null">#{validityValue},</if>
            <if test="validityUnit != null">#{validityUnit},</if>
            <if test="status != null">#{status},</if>
            <if test="isHot != null">#{isHot},</if>
            <if test="originalPrice != null">#{originalPrice},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="availableRooms != null">#{availableRooms,typeHandler=com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler},</if>
            <if test="availableWeek != null">#{availableWeek,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
            <if test="availableTimeEnd != null">#{availableTimeEnd},</if>
            <if test="availableTimeStart != null">#{availableTimeStart},</if>
            <if test="skuId != null">#{skuId},</if>
         </trim>
    </insert>

    <update id="updateSsCard" parameterType="SsCard">
        update ss_card
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where coupon_id = #{data.couponId}
    </update>

    <sql id="updateColumns">
        <if test="data.name != null">name = #{data.name},</if>
        <if test="data.type != null">type = #{data.type},</if>
        <if test="data.discountPercent != null">discount_percent = #{data.discountPercent},</if>
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.merchantId != null">merchant_id = #{data.merchantId},</if>
        <if test="data.discountAmount != null">discount_amount = #{data.discountAmount},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.limitNum != null">limit_num = #{data.limitNum},</if>
        <if test="data.descr != null">descr = #{data.descr},</if>
        <if test="data.retailPrice != null">retail_price = #{data.retailPrice},</if>
        <if test="data.storedAmount != null">stored_amount = #{data.storedAmount},</if>
        <if test="data.hours != null">hours = #{data.hours},</if>
        <if test="data.validityValue != null">validity_value = #{data.validityValue},</if>
        <if test="data.validityUnit != null">validity_unit = #{data.validityUnit},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.isHot != null">is_hot = #{data.isHot},</if>
        <if test="data.originalPrice != null">original_price = #{data.originalPrice},</if>
        <if test="data.delFlag != null">del_flag = #{data.delFlag},</if>
        <if test="data.availableRooms != null">available_rooms = #{data.availableRooms,typeHandler=com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler},</if>
        <if test="data.availableWeek != null">available_week = #{data.availableWeek,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
        <if test="data.availableTimeEnd != null">available_time_end = #{data.availableTimeEnd},</if>
        <if test="data.availableTimeStart != null">available_time_start = #{data.availableTimeStart},</if>
        <if test="data.skuId != null">sku_id = #{data.skuId},</if>
    </sql>

    <delete id="deleteSsCardByCouponId" parameterType="Long">
        delete from ss_card where coupon_id = #{couponId}
    </delete>

    <delete id="deleteSsCardByCouponIds" parameterType="String">
        delete from ss_card where coupon_id in
        <foreach item="couponId" collection="array" open="(" separator="," close=")">
            #{couponId}
        </foreach>
    </delete>
</mapper>
