<?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.microservices.zone.member.mapper.ZonePointsDocMapper">

    <resultMap type="ZonePointsDoc" id="ZonePointsDocResult">
        <result property="id" column="id"/>
        <result property="zoneId" column="zone_id"/>
        <result property="docId" column="doc_id"/>
        <result property="pointsStartVisits" column="points_start_visits"/>
        <result property="pointsTriggerCount" column="points_trigger_count"/>
        <result property="userId" column="user_id"/>
        <result property="createTime" column="create_time"/>
        <result property="expiryTime" column="expiry_time"/>
        <result property="isPointsTop" column="is_points_top"/>
    </resultMap>

    <sql id="selectZonePointsDocVo">
        select id, zone_id, doc_id, points_start_visits, points_trigger_count, user_id, create_time, expiry_time, is_points_top
        from zone_points_doc
    </sql>

    <select id="selectZonePointsDocById" parameterType="Long" resultMap="ZonePointsDocResult">
        <include refid="selectZonePointsDocVo"/>
        where id = #{id}
    </select>

    <select id="selectZonePointsDocByZoneIdAndDocId" resultMap="ZonePointsDocResult">
        <include refid="selectZonePointsDocVo"/>
        where zone_id = #{zoneId} and doc_id = #{docId}
    </select>

    <insert id="insertZonePointsDoc" parameterType="ZonePointsDoc" useGeneratedKeys="true" keyProperty="id">
        insert into zone_points_doc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="zoneId != null">zone_id,</if>
            <if test="docId != null">doc_id,</if>
            <if test="pointsStartVisits != null">points_start_visits,</if>
            <if test="pointsTriggerCount != null">points_trigger_count,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="expiryTime != null">expiry_time,</if>
            <if test="isPointsTop != null">is_points_top,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="zoneId != null">#{zoneId},</if>
            <if test="docId != null">#{docId},</if>
            <if test="pointsStartVisits != null">#{pointsStartVisits},</if>
            <if test="pointsTriggerCount != null">#{pointsTriggerCount},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="expiryTime != null">#{expiryTime},</if>
            <if test="isPointsTop != null">#{isPointsTop},</if>
        </trim>
    </insert>

    <update id="updateZonePointsDoc" parameterType="ZonePointsDoc">
        update zone_points_doc
        <trim prefix="SET" suffixOverrides=",">
            <if test="zoneId != null">zone_id = #{zoneId},</if>
            <if test="docId != null">doc_id = #{docId},</if>
            <if test="pointsStartVisits != null">points_start_visits = #{pointsStartVisits},</if>
            <if test="pointsTriggerCount != null">points_trigger_count = #{pointsTriggerCount},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="expiryTime != null">expiry_time = #{expiryTime},</if>
            <if test="isPointsTop != null">is_points_top = #{isPointsTop},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 增加触发次数（原子操作） -->
    <update id="incrementTriggerCount">
        update zone_points_doc
        set points_trigger_count = points_trigger_count + #{addCount}
        where id = #{id}
    </update>

    <!-- 检查用户在指定专区下是否已有置顶文章 -->
    <select id="hasPointsTopByZoneIdAndUserId" resultType="java.lang.Integer">
        select count(0)
        from zone_points_doc
        where zone_id = #{zoneId}
          and user_id = #{userId}
          and is_points_top = 1
    </select>

    <!-- 更新文章的积分置顶状态 -->
    <update id="updatePointsTopStatus">
        update zone_points_doc
        set is_points_top = #{isPointsTop}
        where id = #{id}
    </update>

    <!-- 查询用户在指定专区下的置顶文章 -->
    <select id="selectPointsTopByZoneIdAndUserId" resultMap="ZonePointsDocResult">
        <include refid="selectZonePointsDocVo"/>
        where zone_id = #{zoneId}
          and user_id = #{userId}
          and is_points_top = 1
        limit 1
    </select>

    <!-- 查询专区下置顶文章列表（左联 cms_doc 获取文章名称和概要，过期时间晚于当前时间） -->
    <resultMap type="com.microservices.zone.member.domain.vo.ZonePointsTopDocVo" id="ZonePointsTopDocVoResult">
        <result property="id" column="zpd_id"/>
        <result property="zoneId" column="zone_id"/>
        <result property="docId" column="doc_id"/>
        <result property="docName" column="doc_name"/>
        <result property="summary" column="summary"/>
        <result property="isPointsTop" column="is_points_top"/>
        <result property="expiryTime" column="expiry_time"/>
    </resultMap>

    <!-- 批量取消已过期的积分置顶 -->
    <update id="updateExpiredPointsTop">
        update zone_points_doc
        set is_points_top = 0,
            expiry_time   = null
        where is_points_top = 1
          and expiry_time &lt; NOW()
    </update>

    <select id="selectTopDocList" resultMap="ZonePointsTopDocVoResult">
        select zpd.id as zpd_id, zpd.zone_id, zpd.doc_id, zpd.is_points_top, zpd.expiry_time,
               cd.name as doc_name, cd.summary
        from zone_points_doc zpd
        left join cms_doc cd on zpd.doc_id = cd.id
        where zpd.zone_id = #{zoneId}
          and zpd.is_points_top = 1
          and zpd.expiry_time > NOW()
        order by zpd.create_time desc
    </select>

</mapper>