<?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.PointActivityMapper">
    
    <resultMap type="PointActivity" id="PointActivityResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="description"    column="description"    />
        <result property="activityType"    column="activity_type"    />
        <result property="cityCode"    column="city_code"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="isPermanent"    column="is_permanent"    />
        <result property="expirePolicyId"    column="expire_policy_id"    />
        <result property="status"    column="status"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectPointActivityVo">
        select id, name, description, activity_type, city_code, start_time, end_time, is_permanent, expire_policy_id, status, sort_order, create_by, create_time, update_by, update_time from point_activity
    </sql>

    <select id="selectPointActivityList" parameterType="PointActivity" resultMap="PointActivityResult">
        <include refid="selectPointActivityVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="activityType != null "> and activity_type = #{activityType}</if>
            <if test="cityCode != null  and cityCode != ''"> and city_code = #{cityCode}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="isPermanent != null "> and is_permanent = #{isPermanent}</if>
            <if test="expirePolicyId != null "> and expire_policy_id = #{expirePolicyId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="sortOrder != null "> and sort_order = #{sortOrder}</if>
        </where>
    </select>
    
    <select id="selectPointActivityById" parameterType="Long" resultMap="PointActivityResult">
        <include refid="selectPointActivityVo"/>
        where id = #{id}
    </select>

    <select id="getTaskDetailListById" resultType="com.ylx.point.domain.vo.PointActivityTaskDetailVO">
        SELECT
            pat.task_name,
            COUNT(DISTINCT utc.open_id) AS completedCount,
            COALESCE(SUM(utc.completed_count), 0) AS completedTimes,
            COALESCE(SUM(ul.points), 0) AS points
        FROM `point_activity_task` pat
        LEFT JOIN `point_user_activity_task_completion` utc ON pat.activity_id = utc.activity_id AND pat.id = utc.task_id
        LEFT JOIN `point_user_log` ul ON utc.activity_id = ul.activity_id AND utc.task_id = ul.task_id AND ul.op_type = 1
        WHERE pat.activity_id = #{id}
        GROUP BY pat.id, pat.task_name
        ORDER BY pat.sort_order
    </select>

    <select id="selectTotalActiveTasks" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            point_activity pa
        INNER JOIN point_activity_task pat ON pa.id = pat.activity_id
        WHERE
            pa.status = 1
        AND pa.activity_type != 0
        AND ( pa.city_code = '0' OR pa.city_code = #{cityCode} )
        AND (pa.start_time IS NULL OR pa.start_time &lt;= NOW())
        AND ( pa.end_time IS NULL OR pa.end_time &gt;= NOW())
        AND pat.status = 0
        AND pat.is_deleted = 0
    </select>

    <select id="selectTaskWithProgress" resultType="com.ylx.point.domain.vo.UserPointActivityVo">
        SELECT
            a.id AS activityId,
            t.id taskId,
            t.task_name,
            t.reward_points,
            t.frequency_limit_value triggerValue,
            COALESCE(c.completed_count, 0) AS completed_count
        FROM point_activity a
        JOIN point_activity_task t ON a.id = t.activity_id
        LEFT JOIN point_user_activity_task_completion c ON t.id = c.task_id AND c.open_id = #{dto.openId}
        <where>
            AND t.is_deleted = 0 AND t.status = 0
            <if test="dto.type != null">
                 AND a.activity_type = #{dto.type}
            </if>
            <if test="dto.cityCode != null">
                AND a.city_code = #{dto.cityCode}
            </if>
            AND a.status = 1
        </where>
        ORDER BY t.sort_order ASC
    </select>

    <insert id="insertPointActivity" parameterType="PointActivity" useGeneratedKeys="true" keyProperty="id">
        insert into point_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="description != null">description,</if>
            <if test="activityType != null">activity_type,</if>
            <if test="cityCode != null">city_code,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isPermanent != null">is_permanent,</if>
            <if test="expirePolicyId != null">expire_policy_id,</if>
            <if test="status != null">status,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="description != null">#{description},</if>
            <if test="activityType != null">#{activityType},</if>
            <if test="cityCode != null">#{cityCode},</if>
            <if test="startTime != null">#{startTime,jdbcType=TIMESTAMP},</if>
            <if test="endTime != null">#{endTime,jdbcType=TIMESTAMP},</if>
            <if test="isPermanent != null">#{isPermanent},</if>
            <if test="expirePolicyId != null">#{expirePolicyId},</if>
            <if test="status != null">#{status},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP},</if>
         </trim>
    </insert>

    <update id="updatePointActivity" parameterType="PointActivity">
        update point_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="description != null">description = #{description},</if>
            <if test="activityType != null">activity_type = #{activityType},</if>
            <if test="cityCode != null">city_code = #{cityCode},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="isPermanent != null">is_permanent = #{isPermanent},</if>
            <if test="expirePolicyId != null">expire_policy_id = #{expirePolicyId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePointActivityById" parameterType="String">
        delete from point_activity where id = #{id}
    </delete>

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