<?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.yiqi.activity.mapper.ActivityCouponItemMapper">

    <resultMap type="ActivityCouponItem" id="ActivityCouponItemResult">
        <result property="id" column="id"/>
        <result property="activityId" column="activity_id"/>
        <result property="couponTypeId" column="coupon_type_id"/>
        <result property="couponStock" column="coupon_stock"/>
        <result property="isCustomValidityPeriod" column="is_custom_validity_period"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="selectActivityCouponItemListVo">
        select a.*, ct.title as couponTypeTitle,ct.status as couponTypeStatus,
       IF(a.is_custom_validity_period = 'Y',a.start_date, ct.start_date) as startDate,
       IF(a.is_custom_validity_period = 'Y',a.end_date, ct.end_date)  as endDate,
        ct.deduct_amount as deductAmount,ct.min_amount as minAmount,ct.discount_way as discountWay
        from activity_coupon_item as a left join app_coupon_type as ct on a.coupon_type_id=ct.id
    </sql>

    <sql id="selectActivityCouponItemVo">
        select id, activity_id, coupon_type_id, coupon_stock, is_custom_validity_period, start_date, end_date, create_by_id, create_by, create_time, update_by_id, update_by, update_time, remark, del_flag
        from activity_coupon_item
    </sql>

    <select id="selectActivityCouponItemList" parameterType="ActivityCouponItem" resultType="com.yiqi.activity.domain.vo.ActivityCouponItemVO">
        <include refid="selectActivityCouponItemListVo"/>
        <where>
            a.del_flag='0' and ct.del_flag='0'
            <if test="activityId != null">
                and a.activity_id = #{activityId}
            </if>
        </where>
        order by a.id desc
    </select>

    <select id="selectActivityCouponItemById" parameterType="Long"
            resultMap="ActivityCouponItemResult">
        <include refid="selectActivityCouponItemVo"/>
        where id = #{id}
        and del_flag='0'
    </select>
    <select id="selectActivityCouponItemListByActivityId"
            resultType="com.yiqi.activity.domain.vo.ActivityCouponItemVO">
        <include refid="selectActivityCouponItemListVo"/>
        <where>
            a.activity_id = #{activityId}
            and a.del_flag='0' and ct.del_flag='0'
        </where>
    </select>

    <insert id="insertActivityCouponItem" parameterType="ActivityCouponItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity_coupon_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,
            </if>
            <if test="couponTypeId != null">coupon_type_id,
            </if>
            <if test="couponStock != null">coupon_stock,
            </if>
            <if test="isCustomValidityPeriod != null">is_custom_validity_period,
            </if>
            <if test="startDate != null">start_date,
            </if>
            <if test="endDate != null">end_date,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null">#{activityId},
            </if>
            <if test="couponTypeId != null">#{couponTypeId},
            </if>
            <if test="couponStock != null">#{couponStock},
            </if>
            <if test="isCustomValidityPeriod != null">#{isCustomValidityPeriod},
            </if>
            <if test="startDate != null">#{startDate},
            </if>
            <if test="endDate != null">#{endDate},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
        </trim>
    </insert>

    <update id="updateActivityCouponItem" parameterType="ActivityCouponItem">
        update activity_coupon_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityId != null">activity_id =
                #{activityId},
            </if>
            <if test="couponTypeId != null">coupon_type_id =
                #{couponTypeId},
            </if>
            <if test="couponStock != null">coupon_stock =
                #{couponStock},
            </if>
            <if test="isCustomValidityPeriod != null">is_custom_validity_period =
                #{isCustomValidityPeriod},
            </if>
            <if test="startDate != null">start_date =
                #{startDate},
            </if>
            <if test="endDate != null">end_date =
                #{endDate},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
        </trim>
        where id = #{id}
    </update>

    <update id="editStock" parameterType="com.yiqi.activity.domain.dto.ChangeActivityCouponItemStockDTO">
        update activity_coupon_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="isAdd == 'Y'.toString()">
                coupon_stock=coupon_stock + #{stockNum},
            </if>
            <if test="isAdd == 'N'.toString()">
                coupon_stock=coupon_stock - #{stockNum},
            </if>
            <if test="updateById != null">create_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">create_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">create_time =
                #{updateTime},
            </if>
        </trim>
        where id=#{activityCouponItemId}
    </update>
    <update id="minusActivityCouponStockByOrder">
        UPDATE activity_coupon_item
        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" `coupon_stock` = case " suffix="else coupon_stock end, ">
                <foreach collection="list" item="item">
                    WHEN coupon_type_id = #{item.couponTypeId} and activity_id = #{item.activityId}
                    THEN coupon_stock - #{item.couponCount}
                </foreach>
            </trim>
        </trim>
        where
        `coupon_type_id` in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item.couponTypeId}
        </foreach>
    </update>

    <update id="addActivityCouponStockByOrder">
        UPDATE activity_coupon_item
        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" `coupon_stock` = case " suffix="else coupon_stock end, ">
                <foreach collection="list" item="item">
                    WHEN coupon_type_id = #{item.couponTypeId} and activity_id = #{activityId}
                    THEN coupon_stock + #{item.couponStock}
                </foreach>
            </trim>
        </trim>
        where
        `coupon_type_id` in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item.coupon_type_id}
        </foreach>
    </update>

    <delete id="deleteActivityCouponItemById" parameterType="Long">
            update activity_coupon_item set del_flag='2' where id = #{id}
        </delete>

    <delete id="deleteActivityCouponItemByIds" parameterType="String">
        update activity_coupon_item set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>