<?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 = 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>
</mapper>
