<?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.ylx.point.mapper.PointActivityTaskMapper">
    
    <resultMap type="PointActivityTask" id="PointActivityTaskResult">
        <result property="id"    column="id"    />
        <result property="activityId"    column="activity_id"    />
        <result property="taskName"    column="task_name"    />
        <result property="taskValue"    column="task_value"    />
        <result property="triggerName"    column="trigger_name"    />
        <result property="triggerValue"    column="trigger_value"    />
        <result property="rewardPoints"    column="reward_points"    />
        <result property="frequencyLimitName"    column="frequency_limit_name"    />
        <result property="frequencyLimitValue"    column="frequency_limit_value"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectPointActivityTaskVo">
        select id, activity_id, task_name, task_value, trigger_name, trigger_value, reward_points, frequency_limit_name, frequency_limit_value, sort_order, is_deleted, create_time, update_time, status from point_activity_task
    </sql>
    
    <select id="selectPointActivityTaskById" parameterType="String" resultMap="PointActivityTaskResult">
        <include refid="selectPointActivityTaskVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertPointActivityTask" parameterType="PointActivityTask" useGeneratedKeys="true" keyProperty="id">
        insert into point_activity_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="taskValue != null and taskValue != ''">task_value,</if>
            <if test="triggerName != null and triggerName != ''">trigger_name,</if>
            <if test="triggerValue != null and triggerValue != ''">trigger_value,</if>
            <if test="rewardPoints != null">reward_points,</if>
            <if test="frequencyLimitName != null and frequencyLimitName != ''">frequency_limit_name,</if>
            <if test="frequencyLimitValue != null and frequencyLimitValue != ''">frequency_limit_value,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="isDeleted != null">is_deleted,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null">#{activityId},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="taskValue != null and taskValue != ''">#{taskValue},</if>
            <if test="triggerName != null and triggerName != ''">#{triggerName},</if>
            <if test="triggerValue != null and triggerValue != ''">#{triggerValue},</if>
            <if test="rewardPoints != null">#{rewardPoints},</if>
            <if test="frequencyLimitName != null and frequencyLimitName != ''">#{frequencyLimitName},</if>
            <if test="frequencyLimitValue != null and frequencyLimitValue != ''">#{frequencyLimitValue},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="isDeleted != null">#{isDeleted},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>
    <insert id="batchInsertPointActivityTask">
        insert into point_activity_task (activity_id, task_name, task_value, trigger_name, trigger_value, reward_points, frequency_limit_name, frequency_limit_value, sort_order, is_deleted, create_time, update_time, status)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.activityId}, #{item.taskName}, #{item.taskValue},#{item.triggerName}, #{item.triggerValue}, #{item.rewardPoints}, #{item.frequencyLimitName}, #{item.frequencyLimitValue}, #{item.sortOrder}, #{item.isDeleted}, #{item.createTime}, #{item.updateTime}, #{item.status})
        </foreach>
    </insert>

    <delete id="deletePointActivityTaskById" parameterType="String">
        delete from point_activity_task where id = #{id}
    </delete>

    <delete id="deletePointActivityTaskByActivityId" parameterType="Long">
        delete from point_activity_task where activity_id = #{activityId}
    </delete>

    <select id="selectPointActivityTaskByActivityId" resultMap="PointActivityTaskResult">
        <include refid="selectPointActivityTaskVo"/>
        where activity_id = #{activityId} and is_deleted = 0
        order by sort_order
    </select>

    <select id="selectTasksByActivityType" resultType="com.ylx.point.domain.PointActivityTask">
        SELECT
            t.id,
            t.activity_id,
            t.task_name,
            t.task_value,
            t.trigger_name,
            t.trigger_value,
            t.reward_points,
            t.frequency_limit_name,
            t.frequency_limit_value,
            t.sort_order,
            t.status,
            t.is_deleted,
            t.create_time,
            t.update_time
        FROM
            point_activity_task t
        INNER JOIN point_activity a ON t.activity_id = a.id
        WHERE
            a.activity_type = #{activityType}
        AND t.is_deleted = 0
        AND t.status = 0
        AND a.status = 1
        AND (
            (a.is_permanent = '1')
            OR (a.start_time &lt;= NOW() AND a.end_time &gt;= NOW())
        )
        ORDER BY
            t.create_time ASC,
            t.sort_order ASC,
            t.id ASC
    </select>

</mapper>