<?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.ylx.massage.mapper.TechnicianMomentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ylx.massage.domain.TechnicianMoment">
        <id column="id" property="id"/>
        <result column="technician_id" property="technicianId"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="media_type" property="mediaType"/>
        <result column="cover_url" property="coverUrl"/>
        <result column="view_count" property="viewCount"/>
        <result column="publish_time" property="publishTime"/>
        <result column="city_code" property="cityCode"/>
        <result column="latitude" property="latitude"/>
        <result column="longitude" property="longitude"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="reject_reason" property="rejectReason"/>
        <result column="is_recommend" property="isRecommend"/>
        <result column="is_list" property="isList"/>
        <result column="address" property="address"/>
    </resultMap>

    <!-- 查询推荐动态：只展示后台标记推荐的公开审核通过动态 -->
    <select id="selectRecommendedMoments" resultMap="BaseResultMap">
        SELECT
            tm.*
        FROM
            t_technician_moment tm
        WHERE
            tm.status = 1
            AND tm.audit_status = 2
            AND tm.visible_range = 1
            AND tm.is_recommend = 1
            AND tm.is_list = 1
        ORDER BY
            tm.view_count DESC,
            tm.publish_time DESC
    </select>

    <!-- 查询同城动态：按城市和发布时间倒序展示 -->
    <select id="selectSameCityMoments" resultMap="BaseResultMap">
        SELECT
            tm.*
        FROM
            t_technician_moment tm
        WHERE
            tm.status = 1
            AND tm.audit_status = 2
            AND tm.visible_range = 1
            AND tm.is_list = 1
            AND tm.city_code = #{cityCode}
        ORDER BY
            tm.publish_time DESC
    </select>

    <!-- 查询附近动态：10km内，按日期倒序，同一天按距离排序 -->
    <select id="selectNearbyMoments" resultMap="BaseResultMap">
        SELECT
            tm.*,
            ST_Distance_Sphere(
                POINT(tm.longitude, tm.latitude),
                POINT(#{longitude}, #{latitude})
            ) / 1000 AS distance
        FROM
            t_technician_moment tm
        WHERE
            tm.status = 1
            AND tm.audit_status = 2
            AND tm.visible_range = 1
            AND tm.is_list = 1
            AND tm.latitude IS NOT NULL
            AND tm.longitude IS NOT NULL
            AND ST_Distance_Sphere(
                POINT(tm.longitude, tm.latitude),
                POINT(#{longitude}, #{latitude})
            ) / 1000 &lt;= 10
        ORDER BY
            distance ASC,
            tm.publish_time DESC
    </select>

    <!-- 增加浏览量 -->
    <update id="incrementViewCount">
        UPDATE
            t_technician_moment
        SET
            view_count = view_count + 1
        WHERE
            id = #{momentId} and is_delete = 0 and audit_status = 2
    </update>

    <!-- 更新动态推荐状态 -->
    <update id="updateRecommendStatus">
        UPDATE
            t_technician_moment
        SET
            is_recommend = #{isRecommend},
            update_time = NOW()
        WHERE
            id = #{momentId}
            AND is_delete = 0
    </update>

    <!-- 更新动态上架状态 -->
    <update id="updateListStatus">
        UPDATE
            t_technician_moment
        SET
            is_list = #{isList},
            update_time = NOW()
        WHERE
            id = #{momentId}
            AND is_delete = 0
    </update>

    <!-- 查询后台动态管理审核状态统计 -->
    <select id="selectManageAuditStatusCount" resultType="com.ylx.massage.domain.vo.MomentAuditStatusCountVO">
        SELECT
            COALESCE(SUM(CASE WHEN audit_status = 1 THEN 1 ELSE 0 END), 0) AS pendingCount,
            COALESCE(SUM(CASE WHEN audit_status = 2 THEN 1 ELSE 0 END), 0) AS approvedCount,
            COALESCE(SUM(CASE WHEN audit_status = 3 THEN 1 ELSE 0 END), 0) AS rejectedCount
        FROM
            t_technician_moment
        WHERE
            is_delete = 0
            AND audit_status IN (1, 2, 3)
    </select>

    <!-- 根据动态ID查询动态简要详情（包含媒体URL列表） -->
    <select id="selectMomentSimpleDetail" resultType="java.util.HashMap">
        SELECT
            a.id,
            a.title,
            a.content,
            a.media_type,
            a.audit_status,
            a.reject_reason,
            GROUP_CONCAT(b.media_url ORDER BY b.sort_order SEPARATOR ',') as media_urls
        FROM
            t_technician_moment a
            LEFT JOIN t_moment_media b ON a.id = b.moment_id AND IFNULL(b.is_delete, 0) = 0
        WHERE
            a.id = #{momentId}
        GROUP BY
            a.id, a.title, a.content, a.media_type
    </select>

    <select id="getMerchantProject" resultType="com.ylx.merchant.domain.vo.MomentVO">
        SELECT
            id,
            title,
            content,
            media_type AS mediaType,
            cover_url AS coverUrl,
            view_count AS viewCount,
            publish_time AS publishTime
        FROM t_technician_moment
        WHERE technician_id = #{dto.id}
          AND status = 1           -- 仅查询正常状态的动态
          AND audit_status = 2     -- 仅查询审核通过的动态
          AND visible_range = 1    -- 仅查询可见范围的动态
          AND is_list = 1          -- 仅查询已上架的动态
        ORDER BY publish_time DESC
    </select>

</mapper>
