<?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.ycwl.basic.mapper.UserNotificationAuthorizationMapper">
    
    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="com.ycwl.basic.model.pc.notify.entity.UserNotificationAuthorizationEntity">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="member_id" property="memberId" jdbcType="BIGINT"/>
        <result column="template_id" property="templateId" jdbcType="VARCHAR"/>
        <result column="scenic_id" property="scenicId" jdbcType="BIGINT"/>
        <result column="authorization_count" property="authorizationCount" jdbcType="INTEGER"/>
        <result column="consumed_count" property="consumedCount" jdbcType="INTEGER"/>
        <result column="remaining_count" property="remainingCount" jdbcType="INTEGER"/>
        <result column="last_authorized_time" property="lastAuthorizedTime" jdbcType="TIMESTAMP"/>
        <result column="last_consumed_time" property="lastConsumedTime" jdbcType="TIMESTAMP"/>
        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, member_id, template_id, scenic_id, authorization_count, consumed_count, 
        remaining_count, last_authorized_time, last_consumed_time, status, create_time, update_time
    </sql>
    
    <!-- 根据用户ID、模板ID和景区ID查询授权记录 -->
    <select id="selectByMemberAndTemplateAndScenic" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM user_notification_authorization
        WHERE member_id = #{memberId} 
          AND template_id = #{templateId}
          AND scenic_id = #{scenicId}
          AND status = 1
        LIMIT 1
    </select>
    
    <!-- 根据用户ID查询授权记录列表 -->
    <select id="selectByMemberId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM user_notification_authorization
        WHERE member_id = #{memberId}
          AND status = 1
        ORDER BY create_time DESC
    </select>
    
    <!-- 根据用户ID和模板ID查询授权记录列表 -->
    <select id="selectByMemberIdAndTemplateId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM user_notification_authorization
        WHERE member_id = #{memberId}
          AND template_id = #{templateId}
          AND status = 1
        ORDER BY create_time DESC
    </select>
    
    <!-- 根据景区ID查询授权记录列表 -->
    <select id="selectByScenicId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM user_notification_authorization
        WHERE scenic_id = #{scenicId}
          AND status = 1
        ORDER BY create_time DESC
    </select>
    
    <!-- 增加授权次数 -->
    <update id="increaseAuthorizationCount">
        UPDATE user_notification_authorization
        SET authorization_count = authorization_count + #{count},
            last_authorized_time = NOW(),
            update_time = NOW()
        WHERE id = #{id}
          AND status = 1
    </update>
    
    <!-- 增加消费次数 -->
    <update id="increaseConsumedCount">
        UPDATE user_notification_authorization
        SET consumed_count = consumed_count + #{count},
            last_consumed_time = NOW(),
            update_time = NOW()
        WHERE id = #{id}
          AND status = 1
          AND consumed_count + #{count} &lt;= authorization_count
    </update>
    
    <!-- 检查用户是否还有剩余授权次数 -->
    <select id="selectRemainingCount" resultType="java.lang.Integer">
        SELECT COALESCE(remaining_count, 0)
        FROM user_notification_authorization
        WHERE member_id = #{memberId} 
          AND template_id = #{templateId}
          AND scenic_id = #{scenicId}
          AND status = 1
          AND remaining_count > 0
        LIMIT 1
    </select>
    
</mapper>