<?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.StoreOperationalActivitySignupMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.storePlatform.StoreOperationalActivitySignup">
        <id column="id" property="id" />
        <result column="activity_id" property="activityId" />
        <result column="store_id" property="storeId" />
        <result column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="phone" property="phone" />
        <result column="status" property="status" />
        <result column="signup_time" property="signupTime" />
        <result column="delete_flag" property="deleteFlag" />
        <result column="created_time" property="createdTime" />
        <result column="created_user_id" property="createdUserId" />
        <result column="updated_time" property="updatedTime" />
        <result column="updated_user_id" property="updatedUserId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, activity_id, store_id, user_id, user_name, phone, status, signup_time,
        delete_flag, created_time, created_user_id, updated_time, updated_user_id
    </sql>

    <select id="selectByActivityId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM store_operational_activity_signup
        WHERE activity_id = #{activityId}
          AND delete_flag = 0
        ORDER BY signup_time DESC
    </select>

    <select id="countSignupByActivityId" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM store_operational_activity_signup
        WHERE activity_id = #{activityId}
          AND delete_flag = 0
          AND status IN (0, 2)
    </select>

    <select id="countApprovedByActivityId" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM store_operational_activity_signup
        WHERE activity_id = #{activityId}
          AND delete_flag = 0
          AND status = 2
    </select>

    <select id="countApprovedByActivityAndUser" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM store_operational_activity_signup
        WHERE activity_id = #{activityId}
          AND user_id = #{userId}
          AND delete_flag = 0
          AND status in (0,2)
        LIMIT 1
    </select>

    <select id="countSignedUpByActivityAndUser" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM store_operational_activity_signup
        WHERE activity_id = #{activityId}
          AND user_id = #{userId}
          AND delete_flag = 0
          AND status IN (0, 2)
        LIMIT 1
    </select>

    <select id="selectLatestSignupStatus" resultType="java.lang.Integer">
        SELECT status
        FROM store_operational_activity_signup
        WHERE activity_id = #{activityId}
          AND user_id = #{userId}
          AND delete_flag = 0
        ORDER BY signup_time DESC, id DESC
        LIMIT 1
    </select>

    <select id="selectMySignups" resultType="shop.alien.entity.storePlatform.vo.StoreOperationalActivityMySignupVo">
        SELECT
            s.id AS signupId,
            s.activity_id AS activityId,
            a.store_id AS storeId,
            a.activity_name AS activityName,
            a.promotional_image AS promotionalImage,
            img.img_url AS activityTitleImgUrl,
            su.head_img AS storeUserHeadImg,
            su.nick_name AS storeUserNickName,
            a.start_time AS startTime,
            a.end_time AS endTime,
            a.status AS activityStatus,
            CASE
                WHEN a.signup_start_time IS NOT NULL AND NOW() &lt; a.signup_start_time THEN 0
                WHEN a.signup_end_time IS NOT NULL AND NOW() &gt; a.signup_end_time THEN 2
                ELSE 1
            END AS signupStatus,
            s.status AS signupAuditStatus,
            CASE WHEN a.status = 7 THEN 1 ELSE 0 END AS activityStartStatus,
            CASE WHEN ach.signup_id IS NULL THEN 0 ELSE 1 END AS hasAchievement,
            s.signup_time AS signupTime
        FROM store_operational_activity_signup s
        INNER JOIN store_operational_activity a ON a.id = s.activity_id
        LEFT JOIN store_img img ON img.store_id = a.store_id
            AND img.business_id = a.id
            AND img.img_type = 26
            AND img.delete_flag = 0
        LEFT JOIN store_user su ON su.store_id = a.store_id
            AND su.account_type = 1
            AND su.delete_flag = 0
        LEFT JOIN (
            SELECT DISTINCT signup_id
            FROM store_operational_activity_achievement
            WHERE delete_flag = 0
        ) ach ON ach.signup_id = s.id
        WHERE s.user_id = #{userId}
          AND s.delete_flag = 0
          AND a.delete_flag = 0
        ORDER BY s.signup_time DESC
    </select>
</mapper>
