<?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.ruixuan.bytedance.mapper.BytedanceColonelActivityMapper">

    <resultMap type="com.ruixuan.bytedance.entity.BytedanceColonelActivity" id="BytedanceColonelActivityResult">
        <result property="id" column="id"/>
        <result property="activityId" column="activity_id"/>
        <result property="colonelBuyinId" column="colonel_buyin_id"/>
        <result property="activityName" column="activity_name"/>
        <result property="activityStartTime" column="activity_start_time"/>
        <result property="activityEndTime" column="activity_end_time"/>
        <result property="status" column="status"/>
        <result property="applicationStartTime" column="application_start_time"/>
        <result property="applicationEndTime" column="application_end_time"/>
        <result property="categoriesLimit" column="categories_limit"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectBytedanceColonelActivityVo">
        select id,
               activity_id,
               colonel_buyin_id,
               activity_name,
               activity_start_time,
               activity_end_time,
               status,
               application_start_time,
               application_end_time,
               categories_limit,
               create_time,
               update_time
        from bytedance_colonel_activity
    </sql>

    <select id="selectBytedanceColonelActivityList"
            parameterType="com.ruixuan.bytedance.entity.BytedanceColonelActivity"
            resultMap="BytedanceColonelActivityResult">
        <include refid="selectBytedanceColonelActivityVo"/>
        <where>
            <if test="activityId != null ">and activity_id = #{activityId}</if>
            <if test="colonelBuyinId != null ">and colonel_buyin_id = #{colonelBuyinId}</if>
            <if test="activityName != null  and activityName != ''">and activity_name like concat('%', #{activityName},
                '%')
            </if>
            <if test="activityStartTime != null ">and activity_start_time = #{activityStartTime}</if>
            <if test="activityEndTime != null ">and activity_end_time = #{activityEndTime}</if>
            <if test="status != null ">and status = #{status}</if>
            <if test="applicationStartTime != null  and applicationStartTime != ''">and application_start_time =
                #{applicationStartTime}
            </if>
            <if test="applicationEndTime != null ">and application_end_time = #{applicationEndTime}</if>
            <if test="categoriesLimit != null  and categoriesLimit != ''">and categories_limit = #{categoriesLimit}</if>
        </where>
    </select>

    <select id="selectBytedanceColonelActivityById" parameterType="Long" resultMap="BytedanceColonelActivityResult">
        <include refid="selectBytedanceColonelActivityVo"/>
        where id = #{id}
    </select>

    <insert id="insertBytedanceColonelActivity" parameterType="com.ruixuan.bytedance.entity.BytedanceColonelActivity"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into bytedance_colonel_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,</if>
            <if test="colonelBuyinId != null">colonel_buyin_id,</if>
            <if test="activityName != null">activity_name,</if>
            <if test="activityStartTime != null">activity_start_time,</if>
            <if test="activityEndTime != null">activity_end_time,</if>
            <if test="status != null">status,</if>
            <if test="applicationStartTime != null">application_start_time,</if>
            <if test="applicationEndTime != null">application_end_time,</if>
            <if test="categoriesLimit != null">categories_limit,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null">#{activityId},</if>
            <if test="colonelBuyinId != null">#{colonelBuyinId},</if>
            <if test="activityName != null">#{activityName},</if>
            <if test="activityStartTime != null">#{activityStartTime},</if>
            <if test="activityEndTime != null">#{activityEndTime},</if>
            <if test="status != null">#{status},</if>
            <if test="applicationStartTime != null">#{applicationStartTime},</if>
            <if test="applicationEndTime != null">#{applicationEndTime},</if>
            <if test="categoriesLimit != null">#{categoriesLimit},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateBytedanceColonelActivity" parameterType="com.ruixuan.bytedance.entity.BytedanceColonelActivity">
        update bytedance_colonel_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityId != null">activity_id = #{activityId},</if>
            <if test="colonelBuyinId != null">colonel_buyin_id = #{colonelBuyinId},</if>
            <if test="activityName != null">activity_name = #{activityName},</if>
            <if test="activityStartTime != null">activity_start_time = #{activityStartTime},</if>
            <if test="activityEndTime != null">activity_end_time = #{activityEndTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="applicationStartTime != null">application_start_time = #{applicationStartTime},</if>
            <if test="applicationEndTime != null">application_end_time = #{applicationEndTime},</if>
            <if test="categoriesLimit != null">categories_limit = #{categoriesLimit},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBytedanceColonelActivityById" parameterType="Long">
        delete
        from bytedance_colonel_activity
        where id = #{id}
    </delete>

    <delete id="deleteBytedanceColonelActivityByIds" parameterType="String">
        delete from bytedance_colonel_activity where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <insert id="replaceBatch">
        replace into bytedance_colonel_activity(
        institution_id,
        activity_id,
        colonel_buyin_id,
        activity_name,
        activity_start_time,
        activity_end_time,
        status,
        application_start_time,
        application_end_time,
        categories_limit
        )
        values
        <foreach collection="activityList" item="cell" separator=",">
            (
            #{cell.institutionId},
            #{cell.activityId},
            #{cell.colonelBuyinId},
            #{cell.activityName},
            #{cell.activityStartTime},
            #{cell.activityEndTime},
            #{cell.status},
            #{cell.applicationStartTime},
            #{cell.applicationEndTime},
            #{cell.categoriesLimit}
            )
        </foreach>
    </insert>


    <insert id="insertDetail"
            parameterType="com.ruixuan.bytedance.entity.BytedanceColonelActivityDetail" useGeneratedKeys="true"
            keyProperty="id">
        insert into bytedance_colonel_activity_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="institutionId != null">institution_id,</if>
            <if test="activityId != null">activity_id,</if>
            <if test="activityName != null">activity_name,</if>
            <if test="isNewShop != null">is_new_shop,</if>
            <if test="shopType != null">shop_type,</if>
            <if test="activityDesc != null">activity_desc,</if>
            <if test="applyStartTime != null">apply_start_time,</if>
            <if test="applyEndTime != null">apply_end_time,</if>
            <if test="commissionRate != null">commission_rate,</if>
            <if test="wechatId != null">wechat_id,</if>
            <if test="phoneNum != null">phone_num,</if>
            <if test="estimatedSingleSale != null">estimated_single_sale,</if>
            <if test="activityType != null">activity_type,</if>
            <if test="specifiedShopIds != null">specified_shop_ids,</if>
            <if test="categories != null">categories,</if>
            <if test="shopScore != null">shop_score,</if>
            <if test="minPromotionDays != null">min_promotion_days,</if>
            <if test="thresholdCrossBorder != null">threshold_cross_border,</if>
            <if test="hasThreshold != null">has_threshold,</if>
            <if test="colonelBuyinId != null">colonel_buyin_id,</if>
            <if test="monthsOfProtection != null">months_of_protection,</if>
            <if test="colonelName != null">colonel_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="institutionId != null">#{institutionId},</if>
            <if test="activityId != null">#{activityId},</if>
            <if test="activityName != null">#{activityName},</if>
            <if test="isNewShop != null">#{isNewShop},</if>
            <if test="shopType != null">#{shopType},</if>
            <if test="activityDesc != null">#{activityDesc},</if>
            <if test="applyStartTime != null">#{applyStartTime},</if>
            <if test="applyEndTime != null">#{applyEndTime},</if>
            <if test="commissionRate != null">#{commissionRate},</if>
            <if test="wechatId != null">#{wechatId},</if>
            <if test="phoneNum != null">#{phoneNum},</if>
            <if test="estimatedSingleSale != null">#{estimatedSingleSale},</if>
            <if test="activityType != null">#{activityType},</if>
            <if test="specifiedShopIds != null">#{specifiedShopIds},</if>
            <if test="categories != null">#{categories},</if>
            <if test="shopScore != null">#{shopScore},</if>
            <if test="minPromotionDays != null">#{minPromotionDays},</if>
            <if test="thresholdCrossBorder != null">#{thresholdCrossBorder},</if>
            <if test="hasThreshold != null">#{hasThreshold},</if>
            <if test="colonelBuyinId != null">#{colonelBuyinId},</if>
            <if test="monthsOfProtection != null">#{monthsOfProtection},</if>
            <if test="colonelName != null">#{colonelName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>


    <select id="getDetailByActivityId" resultType="com.ruixuan.bytedance.entity.BytedanceColonelActivityDetail">
        select id,
               institution_id,
               activity_id,
               activity_name,
               is_new_shop,
               shop_type,
               activity_desc,
               apply_start_time,
               apply_end_time,
               commission_rate,
               wechat_id,
               phone_num,
               estimated_single_sale,
               activity_type,
               specified_shop_ids,
               categories,
               shop_score,
               min_promotion_days,
               threshold_cross_border,
               has_threshold,
               colonel_buyin_id,
               months_of_protection,
               colonel_name
        from bytedance_colonel_activity_detail
        where activity_id = #{activityId}
    </select>

</mapper>