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

    <select id="getDynamicsList" resultType="shop.alien.entity.store.vo.LifeUserDynamicsVo">
        SELECT
        -- 1. 用ANY_VALUE()包裹非聚合列，符合only_full_group_by规则
        ANY_VALUE(dyna1.id) AS id,
        ANY_VALUE(dyna1.dynamicsType) AS dynamicsType,
        ANY_VALUE(dyna1.title) AS title,
        ANY_VALUE(dyna1.phoneId) AS phoneId,
        ANY_VALUE(dyna1.context) AS context,
        ANY_VALUE(dyna1.image_path) AS image_path,
        ANY_VALUE(dyna1.address) AS address,
        ANY_VALUE(dyna1.address_name) AS address_name,
        ANY_VALUE(dyna1.address_context) AS address_context,
        ANY_VALUE(dyna1.liulan_count) AS liulan_count,
        ANY_VALUE(dyna1.dianzan_count) AS dianzan_count,
        ANY_VALUE(dyna1.type) AS type,
        ANY_VALUE(dyna1.created_time) AS created_time,
        ANY_VALUE(dyna1.userType) AS userType,
        ANY_VALUE(dyna1.phone) AS phone,
        ANY_VALUE(dyna1.draft) AS draft,
        ANY_VALUE(dyna1.address_province) AS address_province,
        ANY_VALUE(dyna1.top_status) AS top_status,
        ANY_VALUE(dyna1.top_time) AS top_time,
        ANY_VALUE(dyna1.enable_status) AS enable_status,
        ANY_VALUE(dyna1.userName) AS userName,
        ANY_VALUE(dyna1.userImage) AS userImage,
        ANY_VALUE(dyna1.storeUserId) AS storeUserId,
        ANY_VALUE(dyna1.storeOrUserId) AS storeOrUserId,
        ANY_VALUE(dyna1.isExpert) AS isExpert,
        ANY_VALUE(dyna1.store_name) AS store_name,
        -- 2. 修正聚合计数逻辑：用COUNT(DISTINCT)避免重复计数，粉丝数统计lf1.id而非dyna1.id
        COUNT(DISTINCT lf1.id) AS fansCount,
        -- 保留内层的评论数、转发数（从dyna1中取聚合后的值）
        ANY_VALUE(dyna1.commentCount) AS commentCount,
        ANY_VALUE(dyna1.transferNum) AS transferNum
        FROM (
        SELECT
        -- 内层同样用ANY_VALUE()包裹非聚合列
        ANY_VALUE(dyna.id) AS id,
        ANY_VALUE(dyna.dynamicsType) AS dynamicsType,
        ANY_VALUE(dyna.title) AS title,
        ANY_VALUE(dyna.phoneId) AS phoneId,
        ANY_VALUE(dyna.context) AS context,
        ANY_VALUE(dyna.image_path) AS image_path,
        ANY_VALUE(dyna.address) AS address,
        ANY_VALUE(dyna.address_name) AS address_name,
        ANY_VALUE(dyna.address_context) AS address_context,
        ANY_VALUE(dyna.liulan_count) AS liulan_count,
        ANY_VALUE(dyna.dianzan_count) AS dianzan_count,
        ANY_VALUE(dyna.type) AS type,
        ANY_VALUE(dyna.created_time) AS created_time,
        ANY_VALUE(dyna.userType) AS userType,
        ANY_VALUE(dyna.phone) AS phone,
        ANY_VALUE(dyna.draft) AS draft,
        ANY_VALUE(dyna.address_province) AS address_province,
        ANY_VALUE(dyna.top_status) AS top_status,
        ANY_VALUE(dyna.top_time) AS top_time,
        ANY_VALUE(dyna.enable_status) AS enable_status,
        ANY_VALUE(dyna.userName) AS userName,
        ANY_VALUE(dyna.userImage) AS userImage,
        ANY_VALUE(dyna.storeUserId) AS storeUserId,
        ANY_VALUE(dyna.storeOrUserId) AS storeOrUserId,
        ANY_VALUE(dyna.isExpert) AS isExpert,
        ANY_VALUE(dyna.store_name) AS store_name,
        -- 3. 修正评论数/转发数：COUNT(DISTINCT)避免LEFT JOIN导致的重复计数
        COUNT(DISTINCT sc.id) AS commentCount,
        COUNT(DISTINCT lm.id) AS transferNum
        FROM (
        WITH dynamice AS (
        SELECT
        CASE
        WHEN image_path REGEXP '.mp4|.avi|.flv|.mkv|.rmvb|.wmv|.3gp|.mov' THEN 2
        WHEN image_path REGEXP '.jpg|.jpeg|.png|.bmp|.webp|.gif|.svg' THEN 1
        ELSE 0
        END AS dynamicsType,
        id, title, phone_id phoneId, context, image_path, address,address_name,address_context,
        liulan_count, dianzan_count, type, created_time,
        SUBSTRING_INDEX(phone_id, '_', 1) userType,
        SUBSTRING_INDEX(phone_id, '_', -1) phone,
        draft , address_province, top_status, top_time, enable_status
        FROM life_user_dynamics
        WHERE delete_flag = 0 AND draft = 0
        )
        SELECT
        dynamice.*,
        user.nick_name userName, user.head_img userImage,
        info.id storeUserId, user.id storeOrUserId,
        0 isExpert, info.store_name
        FROM dynamice
        LEFT JOIN store_user user ON dynamice.phone = user.phone AND user.delete_flag = 0
        LEFT JOIN store_info info ON info.id = user.store_id AND info.delete_flag = 0
        LEFT JOIN store_img img ON img.store_id = user.store_id AND img.img_type = '10' AND img.delete_flag = 0
        WHERE dynamice.userType = 'store'
        UNION
        SELECT
        dynamice.*,
        user.user_name userName, user.user_image userImage,
        user.id storeUserId, user.id storeOrUserId,
        IF(lue.expert_code IS NOT NULL , 1, 0) AS isExpert,
        '' store_name
        FROM dynamice
        JOIN life_user user ON dynamice.phone = user.user_phone AND user.delete_flag = 0
        LEFT JOIN life_user_expert  lue ON lue.user_id = user.id AND lue.delete_flag = 0
        WHERE dynamice.userType = 'user'
        ) dyna
        LEFT JOIN life_comment lc ON lc.dongtai_shequ_id = dyna.id
        LEFT JOIN store_comment sc ON sc.business_id = dyna.id AND sc.business_type = 2 AND sc.delete_flag = 0
        LEFT JOIN life_message lm ON lm.business_id = dyna.id
        -- 4. GROUP BY仅保留主键id（因dyna.id是唯一主键，ANY_VALUE()包裹的列在同一id下值唯一）
        GROUP BY dyna.id
        ) dyna1
        LEFT JOIN life_fans lf1 ON lf1.followed_id = dyna1.phoneId
        WHERE 1=1
        <if test="nickName != null and nickName != ''">
            AND dyna1.userName LIKE CONCAT('%', #{nickName}, '%')
        </if>
        <if test="userType != null and userType != ''">
            AND dyna1.userType = #{userType}
        </if>
        <if test="dynamicsType != null and dynamicsType != ''">
            AND dyna1.dynamicsType = #{dynamicsType}
        </if>
        <if test="releaseStartTime != null and releaseStartTime != ''">
            AND dyna1.created_time >= #{releaseStartTime}
        </if>
        <if test="storeName != null and storeName != ''">
            AND dyna1.store_name LIKE CONCAT('%', #{storeName}, '%')
        </if>
        <if test="releaseEndTime != null and releaseEndTime != ''">
            AND dyna1.created_time &lt;= #{releaseEndTime}
        </if>
        -- 5. 外层GROUP BY仅保留主键id，保证分组逻辑正确
        GROUP BY dyna1.id
        -- 6. 最终排序移到外层，子查询的ORDER BY无意义且影响性能
        ORDER BY dyna1.top_status DESC, dyna1.top_time DESC;
    </select>

    <select id="getDynamicsDetail" resultType="shop.alien.entity.store.vo.LifeUserDynamicsVo">
        select dyna1.*, COUNT(dyna1.id) AS fansCount from (
        select dyna.*,COUNT(sc.id) AS commentCount,  COUNT(lm.id) AS transferNum from (
        with dynamice as(
        select
        CASE
        WHEN image_path REGEXP '.mp4|.avi|.flv|.mkv|.rmvb|.wmv|.3gp|.mov' THEN 2
        WHEN image_path REGEXP '.jpg|.jpeg|.png|.bmp|.webp|.gif|.svg' THEN 1
        ELSE 0
        END AS dynamicsType, id, title, phone_id phoneId, context, image_path, address,address_name,address_context, liulan_count, dianzan_count, type, created_time, substring_index(phone_id, '_', 1) userType, substring_index(phone_id, '_', -1) phone, draft , address_province, top_status, top_time, enable_status
        from life_user_dynamics
        where id = #{id} and delete_flag = 0 and draft = 0 order by created_time desc
        )
        select dynamice.*, user.nick_name userName, user.head_img userImage, info.id storeUserId, user.id storeOrUserId, 0 isExpert
        from dynamice
        left join store_user user on dynamice.phone = user.phone and user.delete_flag = 0
        left join store_info info on info.id = user.store_id and info.delete_flag = 0
        left join store_img img on img.store_id = user.store_id and img.img_type = '10' and img.delete_flag = 0
        where dynamice.userType = 'store'
        union
        select dynamice.*, user.user_name userName, user.user_image userImage, user.id storeUserId, user.id storeOrUserId, IF(lue.expert_code IS NOT NULL , 1, 0) AS isExpert
        from dynamice
        join life_user user on dynamice.phone = user.user_phone and user.delete_flag = 0
        left join life_user_expert  lue on lue.user_id = user.id and lue.delete_flag = 0
        where dynamice.userType = 'user') dyna
        left join life_comment lc on lc.dongtai_shequ_id = dyna.id
        left join store_comment sc on sc.business_id = dyna.id and sc.business_type = 2 and sc.delete_flag = 0
        left join life_message lm on lm.business_id = dyna.id
        GROUP BY dyna.id order by  dyna.created_time desc) dyna1
        left join life_fans lf1 on lf1.followed_id =  dyna1.phoneId
        GROUP by dyna1.id order by dyna1.top_status desc, dyna1.top_time desc
    </select>

    <select id="getStoreDynamicslist" resultType="shop.alien.entity.store.vo.LifeUserDynamicsVo">
        select
       ANY_VALUE(dyna1.dynamicsType) AS dynamicsType,
        ANY_VALUE(dyna1.id) AS id,
        ANY_VALUE(dyna1.title) AS title,
        ANY_VALUE(dyna1.phoneId) AS phoneId,
        ANY_VALUE(dyna1.context) AS context,
        ANY_VALUE(dyna1.image_path) AS imagePath,
        ANY_VALUE(dyna1.address) AS address,
        ANY_VALUE(dyna1.address_name) AS addressName,
        ANY_VALUE(dyna1.address_context) AS addressContext,
        ANY_VALUE(dyna1.liulan_count) AS liulanCount,
        ANY_VALUE(dyna1.dianzan_count) AS dianzanCount,
        ANY_VALUE(dyna1.type) AS type,
        ANY_VALUE(dyna1.created_time) AS createdTime,
        ANY_VALUE(dyna1.userType) AS userType,
        ANY_VALUE(dyna1.phone) AS phone,
        ANY_VALUE(dyna1.draft) AS draft,
        ANY_VALUE(dyna1.address_province) AS addressProvince,
        ANY_VALUE(dyna1.top_status) AS topStatus,
        ANY_VALUE(dyna1.top_time) AS topTime,
        ANY_VALUE(dyna1.enable_status) AS enableStatus,
        ANY_VALUE(dyna1.business_id) AS businessId,
        ANY_VALUE(dyna1.userName) AS userName,
        ANY_VALUE(dyna1.userImage) AS userImage,
        ANY_VALUE(dyna1.storeUserId) AS storeUserId,
        ANY_VALUE(dyna1.storeName) AS storeName,
        ANY_VALUE(dyna1.storeOrUserId) AS storeOrUserId,
        ANY_VALUE(dyna1.isExpert) AS isExpert,
        ANY_VALUE(dyna1.isLike) AS isLike,
        ANY_VALUE(dyna1.commentCount) AS commentCount,
        ANY_VALUE(dyna1.transferNum) AS transferCount,
        COUNT(dyna1.id) AS fansCount
        from
        (
        select
        ANY_VALUE(dyna.dynamicsType) AS dynamicsType,
        dyna.id,
        ANY_VALUE(dyna.title) AS title,
        ANY_VALUE(dyna.phoneId) AS phoneId,
        ANY_VALUE(dyna.context) AS context,
        ANY_VALUE(dyna.image_path) AS image_path,
        ANY_VALUE(dyna.address) AS address,
        ANY_VALUE(dyna.address_name) AS address_name,
        ANY_VALUE(dyna.address_context) AS address_context,
        ANY_VALUE(dyna.liulan_count) AS liulan_count,
        ANY_VALUE(dyna.dianzan_count) AS dianzan_count,
        ANY_VALUE(dyna.type) AS type,
        ANY_VALUE(dyna.created_time) AS created_time,
        ANY_VALUE(dyna.userType) AS userType,
        ANY_VALUE(dyna.phone) AS phone,
        ANY_VALUE(dyna.draft) AS draft,
        ANY_VALUE(dyna.address_province) AS address_province,
        ANY_VALUE(dyna.top_status) AS top_status,
        ANY_VALUE(dyna.top_time) AS top_time,
        ANY_VALUE(dyna.enable_status) AS enable_status,
        ANY_VALUE(dyna.business_id) AS business_id,
        ANY_VALUE(dyna.userName) AS userName,
        ANY_VALUE(dyna.userImage) AS userImage,
        ANY_VALUE(dyna.storeUserId) AS storeUserId,
        ANY_VALUE(dyna.storeName) AS storeName,
        ANY_VALUE(dyna.storeOrUserId) AS storeOrUserId,
        ANY_VALUE(dyna.isExpert) AS isExpert,
        ANY_VALUE(dyna.isLike) AS isLike,
        COUNT(sc.id) AS commentCount,
        COUNT(lm.id) AS transferNum
        from
        (
        with dynamice as(
        select
        CASE
        WHEN image_path REGEXP '.mp4|.avi|.flv|.mkv|.rmvb|.wmv|.3gp|.mov' THEN 2
        WHEN image_path REGEXP '.jpg|.jpeg|.png|.bmp|.webp|.gif|.svg' THEN 1
        ELSE 0
        END AS dynamicsType, id, title, phone_id phoneId, context, image_path, address, address_name, address_context, liulan_count, dianzan_count, type, created_time, substring_index(phone_id, '_', 1) userType, substring_index(phone_id, '_', -1) phone, draft , address_province, top_status, top_time, enable_status,
        business_id
        from life_user_dynamics
        where phone_id = #{phoneId} and delete_flag = 0 and draft = 0 order by created_time desc
        )
        select
        dynamice.*,
        user.nick_name userName,
        user.head_img userImage,
        info.id storeUserId,
        info.store_name storeName,
        user.id storeOrUserId,
        0 isExpert,
        IF(llr.huifu_id IS NOT NULL or llr1.huifu_id IS NOT NULL, 1, 0) AS isLike
        from
        dynamice
        left join store_user user on
        dynamice.phone = user.phone
        and user.delete_flag = 0
        left join store_info info on
        info.id = user.store_id
        and info.delete_flag = 0
        left join store_img img on
        img.store_id = user.store_id
        and img.img_type = '10'
        and img.delete_flag = 0
        left join life_like_record llr on
        llr.huifu_id = dynamice.id
        and llr.delete_flag = 0
        and llr.dianzan_id = (
        select
        CONCAT('user_', lu1.user_phone)
        from
        life_user lu1
        where
        lu1.id = #{userId})
        left join life_like_record llr1 on
        llr1.huifu_id = dynamice.id
        and llr1.delete_flag = 0
        and llr1.dianzan_id = (
        select
        CONCAT('store_', lu2.phone)
        from
        store_user lu2
        where
        lu2.id = #{userId})
        where
        dynamice.userType = 'store') dyna
        left join life_comment lc on
        lc.dongtai_shequ_id = dyna.id
        left join store_comment sc on
        sc.business_id = dyna.id
        and sc.business_type = 2
        and sc.delete_flag = 0
        left join life_message lm on
        lm.business_id = dyna.id
        GROUP BY
        dyna.id
        order by
        dyna.created_time desc) dyna1
        left join life_fans lf1 on
        lf1.followed_id = dyna1.phoneId
        GROUP by
        dyna1.id
        order by
        dyna1.top_status desc,
        dyna1.top_time desc
    </select>
</mapper>
