<?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.ActivityCouponInfoMapper">

    <resultMap type="ActivityCouponInfo" id="ActivityCouponInfoResult">
        <result property="id" column="id"/>
        <result property="activityId" column="activity_id"/>
        <result property="canMultiUse" column="can_multi_use"/>
        <result property="maxUseCount" column="max_use_count"/>
        <result property="isShowAppBuyPage" column="is_show_app_buy_page"/>
        <result property="isShowWxBuyPage" column="is_show_wx_buy_page"/>
        <result property="isShowWxBuyPageFromQrcode" column="is_show_wx_buy_page_from_qrcode"/>
        <result property="storeScope" column="store_scope"/>
        <result property="couponFrom" column="coupon_from"/>
        <result property="maxBuyNum" column="max_buy_num"/>
        <result property="userScope" column="user_scope"/>
        <result property="price" column="price"/>
        <result property="canOnlyNewUser" column="can_only_new_user"/>
        <result property="newUserRegistTime" column="new_user_regist_time"/>
        <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"/>
    </resultMap>

    <sql id="selectActivityCouponInfoVo">
        select a.id,a.title, a.description, a.img_url, a.content, a.start_date, a.end_date, ac.user_scope, a.activity_type,a.status, ac.can_multi_use,
               ac.max_use_count, ac.is_show_app_buy_page, ac.is_show_wx_buy_page, ac.is_show_wx_buy_page_from_qrcode,
               ac.store_scope, ac.coupon_from, ac.max_buy_num,ac.price,ac.can_only_new_user, ac.new_user_regist_time,
               a.create_by_id, a.create_by, a.create_time, a.update_by_id, a.update_by, a.update_time, a.remark
        from activity_coupon_info as ac
                 left join activity_info as a on ac.activity_id = a.id
    </sql>

    <select id="selectActivityCouponInfoList" parameterType="com.yiqi.activity.domain.dto.ActivityCouponInfoDTO" resultType="com.yiqi.activity.domain.vo.ActivityCouponInfoVO">
        <include refid="selectActivityCouponInfoVo"/>
        <where>
            a.del_flag='0'
            <if test="title != null  and title != ''">
                and a.title like concat('%', #{title}, '%')
            </if>
            <if test="startDate != null ">
                and a.start_date &gt;= #{startDate}
            </if>
            <if test="endDate != null ">
                and a.end_date &lt;= #{endDate}
            </if>
        </where>
    </select>

    <select id="selectActivityCouponInfoById" parameterType="Long"
            resultType="com.yiqi.activity.domain.vo.ActivityCouponInfoVO">
        <include refid="selectActivityCouponInfoVo"/>
        where a.id = #{id} and  a.del_flag='0'
    </select>
    <select id="selectActivityCouponInfoBySendCoupon"
            resultType="com.yiqi.activity.domain.vo.ActivityCouponInfoVO">
        select a.id,a.title, a.description, a.img_url, a.content, a.start_date, a.end_date, ac.user_scope, a.activity_type,a.status, ac.can_multi_use,
               ac.max_use_count, ac.is_show_app_buy_page, ac.is_show_wx_buy_page, ac.is_show_wx_buy_page_from_qrcode,
               ac.store_scope, ac.coupon_from, ac.max_buy_num,ac.price,ac.can_only_new_user, ac.new_user_regist_time,
               a.create_by_id, a.create_by, a.create_time, a.update_by_id, a.update_by, a.update_time, a.remark,
               aci.coupon_stock as couponStock, aci.is_custom_validity_period as isCustomValidityPeriod,
               aci.start_date as itemStartDate, aci.end_date as itemEndDate
        from activity_coupon_info as ac
                 left join activity_info as a on ac.activity_id = a.id
                 left join activity_coupon_item as aci on ac.activity_id = aci.activity_id
        where a.id = #{activityId} and aci.coupon_type_id = #{couponTypeId} and a.del_flag='0' and aci.del_flag='0'
    </select>
    <select id="selectActivityCouponItemList" resultType="com.yiqi.app.domain.AppCouponType">
        select at.*
        FROM app_coupon_type at
                 INNER JOIN app_coupon_type_apply_org org on at.id = org.coupon_type_id
        WHERE org.org_id = #{orgId}
          and at.del_flag = '0' and at.end_date >= now() and at.status = '0' and at.send_way = '1'
        <if test="activityAppCouponItemQueryDTO.title != null and activityAppCouponItemQueryDTO.title !=''">
            and at.title = #{ActivityAppCouponItemQueryDTO.title}
        </if>
        <if test="activityAppCouponItemQueryDTO.discountWay != null and activityAppCouponItemQueryDTO.discountWay !=''">
            and at.discount_way = #{ActivityAppCouponItemQueryDTO.discountWay}
        </if>
        order by at.create_time desc
    </select>
    <select id="countActivityCouponItemList" resultType="java.lang.Integer">
        select count(*)
        FROM activity_coupon_info aci
        INNER JOIN activity_info ai on aci.activity_id = ai.id
        INNER JOIN activity_coupon_item acit on aci.activity_id = acit.activity_id
        INNER JOIN app_coupon_type act on act.id = acit.coupon_type_id
        INNER JOIN activity_apply_org aao on aci.activity_id = aao.activity_id
        INNER JOIN sys_store ss on aao.org_id = ss.id
        WHERE aao.org_id = #{orgId} and aao.source_type = #{sourceType}
        and act.can_manual = 'Y' and act.del_flag = '0' and act.status = '0'
        and acit.del_flag = '0'
        and aci.del_flag = '0' and aci.coupon_from = '1'
        and ai.del_flag = '0' and ai.status = '0' and ai.activity_type = '1'
        <if test="activityAppCouponItemQueryDTO.discountWay != null and activityAppCouponItemQueryDTO.discountWay !=''">
            and act.discount_way = #{ActivityAppCouponItemQueryDTO.discountWay}
        </if>
    </select>

    <insert id="insertActivityCouponInfo" parameterType="ActivityCouponInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity_coupon_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null and activityId != ''">activity_id,
            </if>
            <if test="canMultiUse != null and canMultiUse != ''">can_multi_use,
            </if>
            <if test="maxUseCount != null">max_use_count,
            </if>
            <if test="isShowAppBuyPage != null and isShowAppBuyPage != ''">is_show_app_buy_page,
            </if>
            <if test="isShowWxBuyPage != null and isShowWxBuyPage != ''">is_show_wx_buy_page,
            </if>
            <if test="isShowWxBuyPageFromQrcode != null and isShowWxBuyPageFromQrcode != ''">is_show_wx_buy_page_from_qrcode,
            </if>
            <if test="storeScope != null and storeScope != ''">store_scope,
            </if>
            <if test="couponFrom != null and couponFrom != ''">coupon_from,
            </if>
            <if test="maxBuyNum != null">max_buy_num,
            </if>
            <if test="userScope != null and userScope != ''">user_scope,
            </if>
            <if test="price != null and price != ''">price,
            </if>
            <if test="canOnlyNewUser != null and canOnlyNewUser != ''">can_only_new_user,
            </if>
            <if test="newUserRegistTime != null">new_user_regist_time,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null and activityId != ''">#{activityId},
            </if>
            <if test="canMultiUse != null and canMultiUse != ''">#{canMultiUse},
            </if>
            <if test="maxUseCount != null">#{maxUseCount},
            </if>
            <if test="isShowAppBuyPage != null and isShowAppBuyPage != ''">#{isShowAppBuyPage},
            </if>
            <if test="isShowWxBuyPage != null and isShowWxBuyPage != ''">#{isShowWxBuyPage},
            </if>
            <if test="isShowWxBuyPageFromQrcode != null and isShowWxBuyPageFromQrcode != ''">#{isShowWxBuyPageFromQrcode},
            </if>
            <if test="storeScope != null and storeScope != ''">#{storeScope},
            </if>
            <if test="couponFrom != null and couponFrom != ''">#{couponFrom},
            </if>
            <if test="maxBuyNum != null">#{maxBuyNum},
            </if>
            <if test="userScope != null and userScope != ''">#{userScope},
            </if>
            <if test="price != null and price != ''">#{price},
            </if>
            <if test="canOnlyNewUser != null and canOnlyNewUser != ''">#{canOnlyNewUser},
            </if>
            <if test="newUserRegistTime != null">#{newUserRegistTime},
            </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>
        </trim>
    </insert>

    <update id="updateActivityCouponInfo" parameterType="ActivityCouponInfo">
        update activity_coupon_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="canMultiUse != null and canMultiUse != ''">can_multi_use =
                #{canMultiUse},
            </if>
            <if test="maxUseCount != null">max_use_count =
                #{maxUseCount},
            </if>
            <if test="isShowAppBuyPage != null and isShowAppBuyPage != ''">is_show_app_buy_page =
                #{isShowAppBuyPage},
            </if>
            <if test="isShowWxBuyPage != null and isShowWxBuyPage != ''">is_show_wx_buy_page =
                #{isShowWxBuyPage},
            </if>
            <if test="isShowWxBuyPageFromQrcode != null and isShowWxBuyPageFromQrcode != ''">is_show_wx_buy_page_from_qrcode =
                #{isShowWxBuyPageFromQrcode},
            </if>
            <if test="storeScope != null and storeScope != ''">store_scope =
                #{storeScope},
            </if>
            <if test="couponFrom != null and couponFrom != ''">coupon_from =
                #{couponFrom},
            </if>
            <if test="maxBuyNum != null">max_buy_num =
                #{maxBuyNum},
            </if>
            <if test="userScope != null and userScope != ''">user_scope =
                #{userScope},
            </if>
            <if test="price != null and price != ''">price =
                #{price},
            </if>
            <if test="canOnlyNewUser != null and canOnlyNewUser != ''">can_only_new_user =
                #{canOnlyNewUser},
            </if>
            <if test="newUserRegistTime != null">new_user_regist_time =
                #{newUserRegistTime},
            </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>
        </trim>
        where activity_id = #{activityId}
    </update>

    <delete id="deleteActivityCouponInfoById" parameterType="Long">
        update activity_coupon_info set del_flag='2' where activity_id = #{id}
    </delete>

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



</mapper>