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

    <!-- 通用结果映射 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.store.LifeFeedback">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="feedback_source" property="feedbackSource" />
        <result column="feedback_way" property="feedbackWay" />
        <result column="feedback_type" property="feedbackType" />
        <result column="content" property="content" />
        <result column="contact_way" property="contactWay" />
        <result column="feedback_time" property="feedbackTime" />
        <result column="staff_id" property="staffId" />
        <result column="handle_status" property="handleStatus" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, user_id, feedback_source, feedback_way, feedback_type, 
        content, contact_way, feedback_time, staff_id, handle_status,
        create_time, update_time
    </sql>

    <!-- 查询用户反馈列表(带工作人员名称) -->
    <!-- 只查询原始反馈，排除用户回复（用户回复的feedback_time晚于原始反馈） -->
    <select id="selectFeedbackListWithStaff" resultType="shop.alien.entity.store.vo.LifeFeedbackVo">
        SELECT 
            f.id,
            f.user_id AS userId,
            f.feedback_source AS feedbackSource,
            f.feedback_way AS feedbackWay,
            f.feedback_type AS feedbackType,
            f.content,
            f.contact_way AS contactWay,
            f.feedback_time AS feedbackTime,
            f.staff_id AS staffId,
            f.handle_status AS handleStatus,
            s.user_name AS staffName
        FROM life_feedback f
        LEFT JOIN life_sys s ON f.staff_id = s.id
        WHERE 1=1
        <if test="userId != null">
            AND f.user_id = #{userId}
        </if>
        <if test="feedbackSource != null">
            AND f.feedback_source = #{feedbackSource}
        </if>
        <if test="feedbackWay != null">
            AND f.feedback_way = #{feedbackWay}
        </if>
        <if test="handleStatus != null">
            AND f.handle_status = #{handleStatus}
        </if>
        <!-- 只查询原始反馈：feedback_way=0(用户反馈)或feedback_way=1(AI识别) -->
        <!-- 回复记录已存储在life_feedback_reply表中，life_feedback表中只包含原始反馈 -->
        AND (f.feedback_way = 0 OR f.feedback_way = 1)
        ORDER BY f.feedback_time DESC
    </select>

    <!-- 查询反馈详情(带工作人员名称) -->
    <select id="selectFeedbackDetail" resultType="shop.alien.entity.store.vo.LifeFeedbackVo">
        SELECT 
            f.id,
            f.user_id AS userId,
            f.feedback_source AS feedbackSource,
            f.feedback_way AS feedbackWay,
            f.feedback_type AS feedbackType,
            f.content,
            f.contact_way AS contactWay,
            f.feedback_time AS feedbackTime,
            f.staff_id AS staffId,
            f.handle_status AS handleStatus,
            s.user_name AS staffName
        FROM life_feedback f
        LEFT JOIN life_sys s ON f.staff_id = s.id
        WHERE f.id = #{feedbackId}
    </select>

    <!-- 统计处理中反馈数量 -->
    <select id="countPendingFeedback" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM life_feedback
        WHERE handle_status = 0
        <if test="feedbackSource != null">
            AND feedback_source = #{feedbackSource}
        </if>
    </select>

    <!-- 查询平台回复列表 -->
    <select id="selectPlatformReplies" resultType="shop.alien.entity.store.vo.LifeFeedbackVo">
        SELECT
            f.id,
            f.user_id AS userId,
            f.feedback_source AS feedbackSource,
            f.feedback_way AS feedbackWay,
            f.feedback_type AS feedbackType,
            f.content,
            f.feedback_time AS feedbackTime,
            f.staff_id AS staffId,
            f.handle_status AS handleStatus,
            s.user_name AS staffName
        FROM life_feedback f
        LEFT JOIN life_sys s ON f.staff_id = s.id
        WHERE f.user_id = #{userId}
        AND f.feedback_source = #{feedbackSource}
        AND f.feedback_way = 1
        AND f.feedback_time >= #{startTime}
        ORDER BY f.feedback_time ASC
    </select>

    <!-- ==================== Web中台接口 ==================== -->

    <!-- 中台-查询意见反馈列表 -->
    <select id="selectWebFeedbackList" resultType="shop.alien.entity.store.vo.LifeFeedbackListVo">
        SELECT
            f.id,
            u.nick_name AS nickName,
            u.phone AS phone,
            f.feedback_type AS feedbackType,
            CASE f.feedback_type
                WHEN 0 THEN 'bug反馈'
                WHEN 1 THEN '优化反馈'
                WHEN 2 THEN '新增功能反馈'
            END AS feedbackTypeName,
            f.feedback_way AS feedbackWay,
            CASE f.feedback_way
                WHEN 0 THEN '用户反馈'
                WHEN 1 THEN 'AI识别'
            END AS feedbackWayName,
            f.feedback_source AS feedbackSource,
            CASE f.feedback_source
                WHEN 0 THEN '用户端'
                WHEN 1 THEN '商家端'
            END AS feedbackSourceName,
            f.feedback_time AS feedbackTime,
            f.staff_id AS staffId,
            CONCAT(IFNULL(s.user_name, '')) AS staffInfo,
            f.handle_status AS handleStatus,
            CASE f.handle_status
                WHEN 0 THEN '处理中'
                WHEN 1 THEN '已解决'
            END AS handleStatusName
        FROM life_feedback f
        LEFT JOIN store_user u ON f.user_id = u.id
        LEFT JOIN life_sys s ON f.staff_id = s.id
        WHERE 1=1
        <if test="feedbackType != null">
            AND f.feedback_type = #{feedbackType}
        </if>
        <if test="handleStatus != null">
            AND f.handle_status = #{handleStatus}
        </if>
        <if test="feedbackSource != null">
            AND f.feedback_source = #{feedbackSource}
        </if>
        <if test="feedbackWay != null">
            AND f.feedback_way = #{feedbackWay}
        </if>
        ORDER BY f.feedback_time DESC
    </select>

    <!-- 中台-查询反馈详情 -->
    <select id="selectWebFeedbackDetail" resultType="shop.alien.entity.store.vo.LifeFeedbackDetailVo">
        SELECT
            f.id,
            u.nick_name AS nickName,
            u.phone AS phone,
            f.staff_id AS staffId,
            CONCAT(IFNULL(s.user_name, '')) AS staffInfo,
            f.feedback_source AS feedbackSource,
            CASE f.feedback_source
                WHEN 0 THEN '用户端'
                WHEN 1 THEN '商家端'
            END AS feedbackSourceName,
            f.feedback_way AS feedbackWay,
            CASE f.feedback_way
                WHEN 0 THEN '用户反馈'
                WHEN 1 THEN 'AI识别'
            END AS feedbackWayName,
            f.feedback_type AS feedbackType,
            CASE f.feedback_type
                WHEN 0 THEN 'bug反馈'
                WHEN 1 THEN '优化反馈'
                WHEN 2 THEN '新增功能反馈'
            END AS feedbackTypeName,
            f.feedback_time AS feedbackTime,
            f.content,
            f.contact_way AS contactWay,
            f.handle_status AS handleStatus,
            CASE f.handle_status
                WHEN 0 THEN '处理中'
                WHEN 1 THEN '已解决'
            END AS handleStatusName
        FROM life_feedback f
        LEFT JOIN store_user u ON f.user_id = u.id
        LEFT JOIN life_sys s ON f.staff_id = s.id
        WHERE f.id = #{feedbackId}
    </select>

</mapper>

