<?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.PointSignTaskMapper">
    
    <resultMap type="PointSignTask" id="PointSignTaskResult">
        <result property="id"    column="id"    />
        <result property="activityId"    column="activity_id"    />
        <result property="basePoints"    column="base_points"    />
        <result property="breakRule"    column="break_rule"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap id="PointSignTaskMap" type="com.ylx.point.domain.vo.PointSignTaskVo">
        <result property="signTaskId"    column="signTaskId"    />
        <result column="base_points" property="basePoints"/>
        <result column="break_rule" property="breakRule"/>
        <!-- 关联奖励列表 -->
        <collection property="signRewardList" ofType="com.ylx.point.domain.dto.PointSignRewardDTO">
            <result column="continue_days" property="continueDays"/>
            <result column="reward_points" property="rewardPoints"/>
        </collection>
    </resultMap>

    <sql id="selectPointSignTaskVo">
        select id, activity_id, base_points, break_rule, is_deleted, create_time, update_time from point_sign_task
    </sql>

    <update id="updatePointSignTask" parameterType="PointSignTask">
        update point_sign_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityId != null">activity_id = #{activityId},</if>
            <if test="basePoints != null">base_points = #{basePoints},</if>
            <if test="breakRule != null">break_rule = #{breakRule},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePointSignTaskById" parameterType="String">
        delete from point_sign_task where id = #{id}
    </delete>

    <select id="getTaskWithRewards" resultMap="PointSignTaskMap">
        SELECT
            t.id AS signTaskId,
            t.base_points,
            t.break_rule,
            r.reward_points,
            r.continue_days
        FROM point_sign_task t
        LEFT JOIN point_sign_reward r ON t.id = r.sign_task_id AND r.is_deleted = 0
        WHERE
            t.activity_id = #{activityId}
          AND t.is_deleted = 0
    </select>

    <insert id="insertPointSignTask" parameterType="PointSignTask" useGeneratedKeys="true" keyProperty="id">
        insert into point_sign_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,</if>
            <if test="basePoints != null">base_points,</if>
            <if test="breakRule != null">break_rule,</if>
            <if test="isDeleted != null">is_deleted,</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="basePoints != null">#{basePoints},</if>
            <if test="breakRule != null">#{breakRule},</if>
            <if test="isDeleted != null">#{isDeleted},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

</mapper>