<?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="shop.alien.mapper.storePlantform.StoreOperationalActivityAchievementMapper">

    <select id="selectCasePage" resultType="shop.alien.entity.storePlatform.vo.StoreOperationalActivityAchievementCaseVo">
        SELECT
            ach.id AS achievementId,
            ach.user_id AS userId,
            ach.activity_id AS activityId,
            act.activity_name AS activityName,
            act.start_time AS startTime,
            act.end_time AS endTime,
            CASE
                WHEN act.status = 7 THEN 7
                WHEN act.status IN (4, 5) AND act.end_time IS NOT NULL AND NOW() > act.end_time THEN 7
                WHEN act.status IN (4, 5) AND (act.start_time IS NULL OR (NOW() >= act.start_time AND act.end_time > NOW())) THEN 5
                ELSE act.status
            END AS activityStatus,
            u.user_name AS nickName,
            su.head_img AS storeUserHeadImg,
            su.nick_name AS storeUserNickName,
            SUBSTRING_INDEX(ach.media_urls, ',', 1) AS firstMediaUrl,
            COALESCE(ach.updated_time, ach.created_time) AS updatedTime
        FROM store_operational_activity_achievement ach
        INNER JOIN (
            SELECT t.activity_id, t.user_id, MAX(t.id) AS max_id
            FROM store_operational_activity_achievement t
            INNER JOIN (
                SELECT activity_id, user_id, MAX(COALESCE(updated_time, created_time)) AS max_time
                FROM store_operational_activity_achievement
                WHERE delete_flag = 0
                GROUP BY activity_id, user_id
            ) latest_time
                ON t.activity_id = latest_time.activity_id
                AND t.user_id = latest_time.user_id
                AND COALESCE(t.updated_time, t.created_time) = latest_time.max_time
            WHERE t.delete_flag = 0
            GROUP BY t.activity_id, t.user_id
        ) latest ON latest.max_id = ach.id
        INNER JOIN store_operational_activity act ON act.id = ach.activity_id
        LEFT JOIN life_user u ON u.id = ach.user_id
        LEFT JOIN store_user su ON su.store_id = act.store_id
            AND su.account_type = 1
            AND su.delete_flag = 0
        WHERE ach.delete_flag = 0
          AND act.delete_flag = 0
        <if test="activityStatus != null">
            <if test="activityStatus == 5">
                AND act.status IN (4, 5)
            </if>
            <if test="activityStatus != 5">
                AND act.status = #{activityStatus}
            </if>
        </if>
        ORDER BY COALESCE(ach.updated_time, ach.created_time) DESC, ach.id DESC
    </select>

    <select id="selectCaseHeader" resultType="shop.alien.entity.storePlatform.vo.StoreOperationalActivityAchievementCaseDetailVo">
        SELECT
            act.id AS activityId,
            act.activity_name AS activityName,
            act.start_time AS startTime,
            act.end_time AS endTime,
            u.id AS userId,
            u.user_name AS nickName,
            signup.user_name AS userName,
            signup.phone AS phone,
            signup.created_time AS createdTime,
            u.user_image AS userImage,
            u.user_phone AS userPhone
        FROM store_operational_activity act
        LEFT JOIN life_user u ON u.id = #{userId}
        LEFT JOIN store_operational_activity_signup signup ON signup.activity_id = #{activityId}
            AND signup.user_id = #{userId}
            AND signup.delete_flag = 0
        WHERE act.id = #{activityId}
          AND act.delete_flag = 0
          AND u.delete_flag = 0
        LIMIT 1
    </select>

    <select id="selectCaseItems" resultType="shop.alien.entity.storePlatform.vo.StoreOperationalActivityAchievementCaseItemVo">
        SELECT
            ach.id AS achievementId,
            ach.achievement_desc AS achievementDesc,
            ach.media_urls AS mediaUrls,
            ach.created_time AS createdTime,
            COALESCE(ach.updated_time, ach.created_time) AS updatedTime
        FROM store_operational_activity_achievement ach
        WHERE ach.activity_id = #{activityId}
          AND ach.user_id = #{userId}
          AND ach.delete_flag = 0
        ORDER BY COALESCE(ach.updated_time, ach.created_time) DESC, ach.id DESC
    </select>

    <select id="selectStoreCasePage" resultType="shop.alien.entity.storePlatform.vo.StoreOperationalActivityAchievementCaseVo">
        SELECT
            ach.id AS achievementId,
            ach.user_id AS userId,
            ach.activity_id AS activityId,
            act.activity_name AS activityName,
            CASE
                WHEN act.status = 7 THEN 7
                WHEN act.status IN (4, 5) AND act.end_time IS NOT NULL AND NOW() > act.end_time THEN 7
                WHEN act.status IN (4, 5) AND (act.start_time IS NULL OR NOW() >= act.start_time) THEN 5
                ELSE act.status
            END AS activityStatus,
            u.user_name AS nickName,
            signup.phone AS phone,
            u.user_name AS userName,
            u.user_image AS userImage,
            u.user_phone AS userPhone,
            CASE
                WHEN (act.result_media_url IS NOT NULL AND act.result_media_url != '')
                    OR (act.result_text IS NOT NULL AND act.result_text != '') THEN 1
                ELSE 0
            END AS hasResult,
            SUBSTRING_INDEX(ach.media_urls, ',', 1) AS firstMediaUrl,
            COALESCE(ach.updated_time, ach.created_time) AS updatedTime
        FROM store_operational_activity_achievement ach
        INNER JOIN (
            SELECT t.activity_id, t.user_id, MAX(t.id) AS max_id
            FROM store_operational_activity_achievement t
            INNER JOIN (
                SELECT activity_id, user_id, MAX(COALESCE(updated_time, created_time)) AS max_time
                FROM store_operational_activity_achievement
                WHERE delete_flag = 0
                GROUP BY activity_id, user_id
            ) latest_time
                ON t.activity_id = latest_time.activity_id
                AND t.user_id = latest_time.user_id
                AND COALESCE(t.updated_time, t.created_time) = latest_time.max_time
            WHERE t.delete_flag = 0
            GROUP BY t.activity_id, t.user_id
        ) latest ON latest.max_id = ach.id
        INNER JOIN store_operational_activity act ON act.id = ach.activity_id
        LEFT JOIN life_user u ON u.id = ach.user_id
        LEFT JOIN store_operational_activity_signup signup ON signup.activity_id = ach.activity_id
            AND signup.user_id = ach.user_id
            AND signup.delete_flag = 0
        WHERE ach.delete_flag = 0
          AND act.delete_flag = 0
          AND act.store_id = #{storeId}
        <if test="hasResult != null">
            <choose>
                <when test="hasResult == 0">
                    AND ((act.result_media_url IS NULL OR act.result_media_url = '')
                    AND (act.result_text IS NULL OR act.result_text = ''))
                </when>
                <when test="hasResult == 1">
                    AND ((act.result_media_url IS NOT NULL AND act.result_media_url != '')
                    OR (act.result_text IS NOT NULL AND act.result_text != ''))
                </when>
            </choose>
        </if>
        <if test="activityName != null and activityName != ''">
            AND act.activity_name LIKE CONCAT('%', #{activityName}, '%')
        </if>
        ORDER BY COALESCE(ach.updated_time, ach.created_time) DESC, ach.id DESC
    </select>
</mapper>
