package shop.alien.store.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import shop.alien.entity.store.*;
import shop.alien.entity.store.vo.*;
import shop.alien.mapper.*;
import shop.alien.store.config.WebSocketProcess;
import shop.alien.store.service.StoreCommentService;
import shop.alien.store.util.FileUploadUtil;
import shop.alien.util.common.DateUtils;
import shop.alien.util.common.netease.ImageCheckUtil;
import shop.alien.util.common.netease.TextCheckUtil;
import shop.alien.util.common.safe.TextModerationResultVO;
import shop.alien.util.common.safe.TextModerationUtil;
import shop.alien.util.common.safe.TextReviewServiceEnum;
import shop.alien.util.type.LifeNoticeUtil;
import shop.alien.store.util.StoreLikeRecordIdentityHelper;
import shop.alien.store.util.StoreLikeRecordIdentityHelper.StoreLikerScope;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 评论表 服务实现类
 *
 * @author ssk
 * @since 2025-01-02
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StoreCommentServiceImpl extends ServiceImpl<StoreCommentMapper, StoreComment> implements StoreCommentService {

    private final StoreCommentMapper storeCommentMapper;

    private final LifeLikeRecordMapper lifeLikeRecordMapper;

    private final StoreLikeRecordIdentityHelper lifeLikeIdentityHelper;

    private final FileUploadUtil fileUploadUtil;

    private final StoreImgMapper storeImgMapper;

    private final LifeUserViolationMapper lifeUserViolationMapper;

    private final StoreUserMapper storeUserMapper;

    private final LifeUserMapper lifeUserMapper;

    private final StoreCommentAppealMapper storeCommentAppealMapper;

    private final StoreInfoMapper storeInfoMapper;

    private final LifeNoticeMapper lifeNoticeMapper;

    private final LifeNoticeUtil lifeNoticeUtil;

    private final WebSocketProcess webSocketProcess;

    private final TagsSynonymMapper tagsSynonymMapper;

    private final LifeUserOrderMapper lifeUserOrderMapper;

    private final LawyerConsultationOrderMapper lawyerConsultationOrderMapper;

    private final OrderReviewMapper orderReviewMapper;

    @Autowired
    private TextModerationUtil textModerationUtil;
    @Autowired
    private CommonCommentMapper commonCommentMapper;

    /**
     * 评论列表
     *
     * @param pageNum      页数
     * @param pageSize     页容
     * @param businessId   业务id
     * @param businessType 业务类型(1:订单评论, 2:动态社区评论, 3:活动评论, 4:店铺打卡评论, 5:订单评价, 6:订单评论的评论)
     * @param storeId      门店id
     * @param replyStatus  回复状态(0:全部, 1:已回复, 2:未回复)
     * @param commentLevel 评论等级(0:全部, 1:好评, 2:中评, 3:差评)
     * @param days         查询时间, 多少天前
     * @param phoneId      消息标识
     * @param userType     评论的用户类型(0:商家, 其他:用户)
     * @param tagId        标签id
     * @param hasImage     是否有图片(0否/1是)
     * @return IPage<StoreComment>
     */
    @Override
    public IPage<StoreCommentVo> getList(Integer pageNum, Integer pageSize, Integer businessId, Integer businessType, Integer storeId, Integer replyStatus, Integer commentLevel, Integer days, String phoneId, Integer userType, Integer tagId, Boolean hasImage) {
        IPage<StoreCommentVo> storeCommentIPage = new Page<>(pageNum, pageSize);
        QueryWrapper<StoreCommentVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null != businessId, "a.business_id", businessId);
        queryWrapper.eq(null != businessType, "a.business_type", businessType);
        queryWrapper.eq(null != storeId, "a.store_id", storeId);
        //好评 a>=4
        queryWrapper.ge(null != commentLevel && 1 == commentLevel, "a.score", 4.5);
        //中评 a>=2&& a<4
        queryWrapper.ge(null != commentLevel && 2 == commentLevel, "a.score", 3).le(null != commentLevel && 2 == commentLevel, "a.score", 4);
        //差评 a<2
        queryWrapper.ge(null != commentLevel && 3 == commentLevel, "a.score", 0.5).le(null != commentLevel && 3 == commentLevel, "a.score", 2.5);
        if (null != days) {
            Date date = DateUtils.calcDays(new Date(), -days);
            queryWrapper.ge("a.created_time", DateUtils.formatDate(date, "yyyy-MM-dd"));
        }
        //通过当前登录人id 类型 查询举报业务id
        List<LifeUserViolation> lifeUserViolations = new ArrayList<>();
        if(businessType==2){
            String userId;
            if(phoneId.startsWith("store_")){
                userId = phoneId.substring("store_".length());
                StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getPhone,userId));
                if(storeUser!=null){
                    LambdaQueryWrapper<LifeUserViolation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserId,storeUser.getId());
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserType,1);
                    lambdaQueryWrapper.ne(LifeUserViolation::getProcessingStatus,2);
                    lifeUserViolations = lifeUserViolationMapper.selectList(lambdaQueryWrapper);
                }
            }else{
                userId = phoneId.substring("user_".length());
                LifeUser lifeUser = lifeUserMapper.selectOne(new LambdaQueryWrapper<LifeUser>().eq(LifeUser::getUserPhone,userId));
                if(lifeUser!=null){
                    LambdaQueryWrapper<LifeUserViolation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserId,lifeUser.getId());
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserType,2);
                    lambdaQueryWrapper.ne(LifeUserViolation::getProcessingStatus,2);
                    lifeUserViolations = lifeUserViolationMapper.selectList(lambdaQueryWrapper);
                }
            }
        }
        List<Integer> businessIds = lifeUserViolations.stream()
                .filter(Objects::nonNull)
                .map(LifeUserViolation::getBusinessId)
                .filter(id -> id != null && !id.toString().isEmpty())
                .collect(Collectors.toList());
        if(businessIds!=null && businessIds.size()>0){
            queryWrapper.notIn("a.id",businessIds);
        }

        //根据标签id查询表签字表拿到评论ids
        if(tagId !=null){
            List<TagsSynonym> tagsSynonymList = tagsSynonymMapper.selectList(new LambdaQueryWrapper<TagsSynonym>().eq(TagsSynonym::getMainTagId,tagId));
            if(CollectionUtils.isNotEmpty(tagsSynonymList)){
                List<Integer> commentIdList = tagsSynonymList.stream().filter(synonym -> synonym != null)
                        .map(TagsSynonym::getCommentId).filter(commentId -> commentId != null).collect(Collectors.toList());
                queryWrapper.in("a.id",commentIdList);
            }
        }

        // 回复状态筛选：在分页查询前加入SQL条件，保证「未回复」只显示未回复数据、总数和分页正确
        if (replyStatus != null && (replyStatus == 1 || replyStatus == 2)) {
            if (replyStatus == 1) {
                // 已回复：存在商家回复（子评论中 phone_id 含 store_）
                queryWrapper.apply("EXISTS (SELECT 1 FROM store_comment r WHERE r.reply_id = a.id AND r.phone_id LIKE '%store%' AND r.delete_flag = 0)");
            } else if (replyStatus == 2) {
                // 未回复：不存在商家回复
                queryWrapper.apply("NOT EXISTS (SELECT 1 FROM store_comment r WHERE r.reply_id = a.id AND r.phone_id LIKE '%store%' AND r.delete_flag = 0)");
            }
        }

        //先查询父级评论
        queryWrapper.isNull(businessType != 1, "a.reply_id").eq("a.delete_flag", 0).orderByDesc("a.created_time");
        IPage<StoreCommentVo> page = storeCommentMapper.getCommentPage(storeCommentIPage, queryWrapper);
        List<StoreCommentVo> records = page.getRecords();
        List<StoreCommentVo> storeCommentVoList = new ArrayList<>();

        //评价图片
        Set<Integer> imgIdList = new HashSet<>();
        for (int i = 0; i < records.size(); i++) {
            StoreCommentVo storeCommentVo = new StoreCommentVo();
            BeanUtils.copyProperties(records.get(i), storeCommentVo);
            //首评
            storeCommentVo.setFirstComment(i == 0);
            //有无图片
            if (records.get(i).getImgId() != null) {
                storeCommentVo.setHasImage(true);
                String[] split = records.get(i).getImgId().split(",");
                for (String s : split) {
                    imgIdList.add(Integer.parseInt(s));
                }
            } else {
                storeCommentVo.setHasImage(false);
            }
            //低分
            storeCommentVo.setLowScore(records.get(i).getScore() <= 1);

            QueryWrapper<StoreCommentAppealVo> storeCommentAppealVoQueryWrapper = new QueryWrapper<>();
            storeCommentAppealVoQueryWrapper.eq("a.comment_id", storeCommentVo.getId()).eq("a.delete_flag", 0).orderByDesc("a.created_time").last("limit 1");
            StoreCommentAppealVo commentDetail = storeCommentAppealMapper.getCommentDetail(storeCommentAppealVoQueryWrapper);
            if (ObjectUtils.isNotEmpty(commentDetail)) {
                storeCommentVo.setAppealFlag(1);
                storeCommentVo.setAppealStatusStr(commentDetail.getAppealStatusStr());
                storeCommentVo.setAppealStatus(commentDetail.getAppealStatus());
            }else {
                storeCommentVo.setAppealFlag(0);
            }

            //商家用户
            if (StringUtils.isNotEmpty(records.get(i).getPhoneId())) {
                if (records.get(i).getPhoneId().contains("store_")) {
                    storeCommentVo.setStoreUserFlag(0);
                    StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, records.get(i).getStoreId()));
                    storeCommentVo.setStoreUserName(storeUser.getName());
                    storeCommentVo.setStoreUserImg(storeUser.getHeadImg());
                } else {
                    storeCommentVo.setStoreUserFlag(1);
                }
            }
            StoreInfo storeInfo = storeInfoMapper.selectById(records.get(i).getStoreId());
            if (storeInfo != null) {
                storeCommentVo.setStoreName(storeInfo.getStoreName());
            }
            storeCommentVoList.add(storeCommentVo);
        }

        //塞图片
        if (!imgIdList.isEmpty()) {
            List<StoreImg> storeImgList = storeImgMapper.selectList(new QueryWrapper<StoreImg>().in("id", imgIdList));

            storeCommentVoList.forEach(storeCommentVo -> {
                if (null != storeCommentVo.getImgId()) {
                    storeCommentVo.setImgUrl(new ArrayList<>());
                    for (StoreImg storeImg : storeImgList) {
                        for (String s : storeCommentVo.getImgId().split(",")) {
                            if (storeImg.getId().equals(Integer.parseInt(s))) {
                                storeCommentVo.getImgUrl().add(storeImg.getImgUrl());
                            }
                        }
                    }
                }
            });
        }

        //父级评论下拼接子评论
        for (StoreCommentVo storeCommentVo : storeCommentVoList) {
            storeCommentVo.setCommitCount(0);
            //父级点赞状态
            if (StringUtils.isNotEmpty(phoneId)) {
                StoreLikerScope likerScope = lifeLikeIdentityHelper.resolveFromDianzanId(phoneId);
                LambdaQueryWrapper<LifeLikeRecord> likeRecordQueryWrapper = new LambdaQueryWrapper<>();
                likeRecordQueryWrapper.eq(LifeLikeRecord::getHuifuId, storeCommentVo.getId());
                lifeLikeIdentityHelper.applyLikerFilter(likeRecordQueryWrapper, likerScope);
                Integer i = lifeLikeRecordMapper.selectCount(likeRecordQueryWrapper);
                if (i > 0) {
                    storeCommentVo.setIsLike(1);
                } else {
                    storeCommentVo.setIsLike(0);
                }
            }
            QueryWrapper<StoreCommentVo> childQueryWrapper = new QueryWrapper<>();
            if(businessIds!=null && businessIds.size()>0){
                childQueryWrapper.notIn("a.id",businessIds);
            }
            childQueryWrapper.eq(null != businessId && 6 == businessId, "a.business_id", businessId).eq("a.delete_flag", 0).eq("a.reply_id", storeCommentVo.getId()).orderByAsc("a.created_time");
            List<StoreCommentVo> childCommentList = storeCommentMapper.getCommentList(childQueryWrapper);
            storeCommentVo.setCommitCount(storeCommentVo.getCommitCount() + childCommentList.size());
            //评价的评论的评论
            childCommentList.forEach(child -> {
                QueryWrapper<StoreCommentVo> childQueryCommentWrapper = new QueryWrapper<>();
                childQueryCommentWrapper
//                        .eq(null != businessId && 6 == businessId, "a.business_id", businessId)
                        .eq("a.delete_flag", 0).eq("a.reply_id", child.getId()).orderByDesc("a.created_time");
                List<StoreCommentVo> childComment2List = storeCommentMapper.getCommentList(childQueryCommentWrapper);

                //商家用户
                for (StoreCommentVo commentVo : childComment2List) {
                    if (StringUtils.isNotEmpty(commentVo.getPhoneId())) {
                        if (commentVo.getPhoneId().contains("store_")) {
                            commentVo.setStoreUserFlag(0);
                            StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, child.getStoreId()));
                            commentVo.setStoreUserName(storeUser.getName());
                            commentVo.setStoreUserImg(storeUser.getHeadImg());
                        } else {
                            commentVo.setStoreUserFlag(1);
                        }
                    }
                }

                child.setStoreComment(childComment2List);
                storeCommentVo.setCommitCount(storeCommentVo.getCommitCount() + childComment2List.size());

                //商家用户
                if (StringUtils.isNotEmpty(child.getPhoneId())) {
                    if (child.getPhoneId().contains("store_")) {
                        child.setStoreUserFlag(0);
                        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, child.getStoreId()));
                        child.setStoreUserName(storeUser.getName());
                        child.setStoreUserImg(storeUser.getHeadImg());
                    } else {
                        child.setStoreUserFlag(1);
                    }
                }

            });
            //筛选商家评论
            List<StoreCommentVo> storeCommentList = new ArrayList<>();
            if (null != userType && userType == 0) {
                List<StoreCommentVo> collect = childCommentList.stream().filter(childComment -> childComment.getPhoneId().contains("store_")).collect(Collectors.toList());
                if (!collect.isEmpty()) {
                    storeCommentList.add(collect.get(0));
                    storeCommentVo.setStoreComment(storeCommentList);
                }
            } else {
                storeCommentVo.setStoreComment(childCommentList);
            }
            //子级点赞状态
            if (StringUtils.isNotEmpty(phoneId)) {
                childCommentList.forEach(child -> {
                    StoreLikerScope childLikerScope = lifeLikeIdentityHelper.resolveFromDianzanId(phoneId);
                    LambdaQueryWrapper<LifeLikeRecord> childlikeRecordQueryWrapper = new LambdaQueryWrapper<>();
                    childlikeRecordQueryWrapper.eq(LifeLikeRecord::getHuifuId, child.getId());
                    lifeLikeIdentityHelper.applyLikerFilter(childlikeRecordQueryWrapper, childLikerScope);
                    if (lifeLikeRecordMapper.selectCount(childlikeRecordQueryWrapper) > 0) {
                        child.setIsLike(1);
                    } else {
                        child.setIsLike(0);
                    }
                });
            }
        }
        IPage<StoreCommentVo> resultPage = new Page<>();
        resultPage.setCurrent(page.getCurrent());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());
        resultPage.setPages(page.getPages());
        //筛选有无评论
        List<StoreCommentVo> collect = new ArrayList<>();
        if (replyStatus != null) {
            if (1 == replyStatus) {
                //只查询有回复的评论
                collect = storeCommentVoList.stream().filter(storeCommentVo -> ObjectUtils.isNotEmpty(storeCommentVo.getStoreComment())).collect(Collectors.toList());
            } else if (2 == replyStatus) {
                //只查询无回复的评论
                collect = storeCommentVoList.stream().filter(storeCommentVo -> ObjectUtils.isEmpty(storeCommentVo.getStoreComment())).collect(Collectors.toList());
            } else {
                collect = storeCommentVoList;
            }
        }
//        collect.forEach(item -> {
//            item.setCommitCount(null != item.getStoreComment() ? item.getStoreComment().size() : 0);
//        });
        resultPage.setRecords(collect);
        return resultPage;
    }

    /**
     * 获取最新一条评论/评价
     *
     * @param businessId   业务id
     * @param businessType 业务类型(1:订单评论, 2:动态社区评论, 3:活动评论,4:店铺打卡评论)
     * @param storeId      门店id
     * @return StoreCommentVo
     */
    @Override
    public StoreCommentVo getOne(Integer businessId, Integer businessType, Integer storeId) {
        AtomicReference<Integer> count = new AtomicReference<>(0);
        QueryWrapper<StoreCommentVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null != businessId, "a.business_id", businessId).eq(null != businessType, "a.business_type", businessType).eq(null != storeId, "a.store_id", storeId).eq("a.delete_flag", 0).orderByDesc("a.created_time");
        List<StoreCommentVo> commentList = storeCommentMapper.getCommentList(queryWrapper);
        count.updateAndGet(v -> v + commentList.size());
        //父级评论下拼接子评论
        for (StoreCommentVo storeCommentVo : commentList) {
            QueryWrapper<StoreCommentVo> childQueryWrapper = new QueryWrapper<>();
            childQueryWrapper.eq("a.delete_flag", 0).eq("a.reply_id", storeCommentVo.getId());
            List<StoreCommentVo> childCommentList = storeCommentMapper.getCommentList(childQueryWrapper);
            count.updateAndGet(v -> v + childCommentList.size());
            //评价的评论的评论
            childCommentList.forEach(child -> {
                QueryWrapper<StoreCommentVo> childQueryCommentWrapper = new QueryWrapper<>();
                childQueryCommentWrapper
//                        .eq(null != businessId && 6 == businessId, "a.business_id", businessId)
                        .eq("a.delete_flag", 0).eq("a.reply_id", child.getId()).orderByDesc("a.created_time");
                List<StoreCommentVo> childComment2List = storeCommentMapper.getCommentList(childQueryCommentWrapper);
                count.updateAndGet(v -> v + childComment2List.size());
            });
        }
        if (!commentList.isEmpty()) {
            StoreCommentVo storeCommentVo = commentList.get(0);
            storeCommentVo.setCommitCount(Integer.parseInt(String.valueOf(count)));
            return storeCommentVo;
        }
        return null;
    }

    /**
     * 评论/评价数量和评分
     *
     * @param businessId   业务id
     * @param businessType 业务类型(1:订单评论, 2:动态社区评论, 3:活动评论,4:店铺打卡评论)
     * @param storeId      门店id
     * @return Integer
     */
    @Override
    public Map<String, Object> getCommitCountAndScore(Integer businessId, Integer businessType, Integer storeId, String phoneId, Integer days) {
        Map<String, Object> map = new HashMap<>();
        AtomicReference<Integer> count = new AtomicReference<>(0);
        QueryWrapper<StoreCommentVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null != businessId, "a.business_id", businessId);
        queryWrapper.eq(null != businessType, "a.business_type", businessType);
        queryWrapper.eq(null != storeId, "a.store_id", storeId);

        if (null != days) {
            Date date = DateUtils.calcDays(new Date(), -days);
            queryWrapper.ge("a.created_time", DateUtils.formatDate(date, "yyyy-MM-dd"));
        }

        //通过当前登录人id 类型 查询举报业务id
        List<LifeUserViolation> lifeUserViolations = new ArrayList<>();
        if(businessType==2){
            String userId;
            if(phoneId!=null && !phoneId.equals("") && phoneId.startsWith("store_")){
                userId = phoneId.substring("store_".length());
                StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getPhone,userId));
                if(storeUser!=null){
                    LambdaQueryWrapper<LifeUserViolation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserId,storeUser.getId());
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserType,1);
                    lambdaQueryWrapper.ne(LifeUserViolation::getProcessingStatus,2);
                    lifeUserViolations = lifeUserViolationMapper.selectList(lambdaQueryWrapper);
                }
            }else if(phoneId!=null && !phoneId.equals("") && phoneId.startsWith("user_")){
                userId = phoneId.substring("user_".length());
                LifeUser lifeUser = lifeUserMapper.selectOne(new LambdaQueryWrapper<LifeUser>().eq(LifeUser::getUserPhone,userId));
                if(lifeUser!=null){
                    LambdaQueryWrapper<LifeUserViolation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserId,lifeUser.getId());
                    lambdaQueryWrapper.eq(LifeUserViolation::getReportingUserType,2);
                    lambdaQueryWrapper.ne(LifeUserViolation::getProcessingStatus,2);
                    lifeUserViolations = lifeUserViolationMapper.selectList(lambdaQueryWrapper);
                }
            }
        }

        List<Integer> businessIds = lifeUserViolations.stream()
                .filter(Objects::nonNull)
                .map(LifeUserViolation::getBusinessId)
                .filter(id -> id != null && !id.toString().isEmpty())
                .collect(Collectors.toList());
        if(businessIds!=null && businessIds.size()>0){
            queryWrapper.notIn("a.id",businessIds);
        }

        //先查询父级评论
        queryWrapper.isNull(businessType != 1, "a.reply_id").eq("a.delete_flag", 0).orderByDesc("a.created_time");
        List<StoreCommentVo> storeCommentVoList = storeCommentMapper.getCommentList(queryWrapper);
        count.updateAndGet(v -> v + storeCommentVoList.size());
        //评分合计
        Double sumScore = 0.0;
        //父级评论下拼接子评论
        for (StoreCommentVo storeCommentVo : storeCommentVoList) {
            sumScore += storeCommentVo.getScore();
            QueryWrapper<StoreCommentVo> childQueryWrapper = new QueryWrapper<>();
            childQueryWrapper.eq("a.delete_flag", 0).eq("a.reply_id", storeCommentVo.getId());
            List<StoreCommentVo> childCommentList = storeCommentMapper.getCommentList(childQueryWrapper);
            count.updateAndGet(v -> v + childCommentList.size());
            //评价的评论的评论
            childCommentList.forEach(child -> {
                QueryWrapper<StoreCommentVo> childQueryCommentWrapper = new QueryWrapper<>();
                childQueryCommentWrapper
//                        .eq(null != businessId && 6 == businessId, "a.business_id", businessId)
                        .eq("a.delete_flag", 0).eq("a.reply_id", child.getId()).orderByDesc("a.created_time");
                List<StoreCommentVo> childComment2List = storeCommentMapper.getCommentList(childQueryCommentWrapper);
                count.updateAndGet(v -> v + childComment2List.size());
            });
        }
        map.put("commitCount", count.toString());
        map.put("img", storeCommentVoList.stream().filter(i -> i.getScore() >= 4.5).map(StoreCommentVo::getUserImage).distinct().limit(6).collect(Collectors.toList()));
        if (sumScore == 0 || storeCommentVoList.isEmpty()) {
            map.put("score", "0");
        } else {
            BigDecimal score = new BigDecimal(sumScore).divide(new BigDecimal(storeCommentVoList.size()), 2, BigDecimal.ROUND_HALF_UP);
            map.put("score", score.toString());
        }

//        Map<String, Object> commentCountAndScore = storeCommentMapper.getCommentCountAndScore(businessId, businessType, storeId);
//        Object scoreObj = commentCountAndScore.get("score");
//        double scoreDouble = 0.0;
//        if (scoreObj != null) {
//            if (scoreObj instanceof Number) {
//                scoreDouble = ((Number) scoreObj).doubleValue();
//            } else if (scoreObj instanceof String) {
//                scoreDouble = Double.parseDouble((String) scoreObj);
//            }
//        }
//        if (scoreDouble == 0) {
//            map.put("score", "0");
//        } else {
//            BigDecimal scoreBigDecimal = new BigDecimal(String.valueOf(scoreDouble)).divide(new BigDecimal(commentCountAndScore.get("rootCount").toString()), 2, BigDecimal.ROUND_HALF_UP);
//            map.put("score", scoreBigDecimal.toString());
//        }
//        map.put("commitCount", commentCountAndScore.get("count").toString());
        return map;
    }

    /**
     * 评论
     *
     * @param multipartRequest 文件
     * @param businessId       业务id
     * @param businessType     业务类型(1:订单评论, 2:动态社区评论, 3:活动评论,4:店铺打卡评论)
     * @param storeId          门店id
     * @param userId           用户id
     * @param replyId          回复id
     * @param commentContent   评价内容
     * @return 0:成功, 1:失败, 2:文本内容异常, 3:图片内容异常
     */
    @Override
    public Integer userComment(MultipartRequest multipartRequest, Integer businessId, Integer businessType, Integer storeId, Integer userId, Integer replyId, String commentContent) {
        try {
            Map<String, String> checkText = TextCheckUtil.check(commentContent);
            if (null == checkText || checkText.get("result").equals("1")) {
                return 2;
            }
            StoreComment storeComment = new StoreComment();
            storeComment.setStoreId(storeId);
            storeComment.setUserId(userId);
            storeComment.setCommentContent(commentContent);
            storeComment.setReplyId(replyId);
            storeComment.setBusinessId(businessId);
            storeComment.setBusinessType(businessType);
            List<String> fileNameSet = new ArrayList<>(multipartRequest.getMultiFileMap().keySet());
            if (!fileNameSet.isEmpty()) {
                StringBuilder imgId = new StringBuilder();
                for (int i = 0; i < fileNameSet.size(); i++) {
                    MultipartFile multipartFile = multipartRequest.getFileMap().get(fileNameSet.get(i));
                    if (null != multipartFile) {
                        byte[] fileByte;
                        try {
                            fileByte = multipartFile.getBytes();
                        } catch (IOException e) {
                            return 1;
                        }
                        String base64 = Base64.getEncoder().encodeToString(fileByte);
                        Map<String, String> checkImage = ImageCheckUtil.check(base64, 2);
                        if (checkImage != null && checkImage.get("result").equals("1")) {
                            return 3;
                        }
                        StoreImg storeImg = new StoreImg();
                        storeImg.setStoreId(storeComment.getStoreId());
                        storeImg.setImgType(8);
                        storeImg.setImgSort(i + 1);
                        storeImg.setImgUrl(fileUploadUtil.uploadOneFile(multipartFile));
                        storeImgMapper.insert(storeImg);
                        imgId.append(storeImg.getId()).append(",");
                    }
                }
                if (!imgId.toString().isEmpty()) {
                    storeComment.setImgId(imgId.substring(0, imgId.length() - 1));
                }
            }
            storeComment.setCreatedUserId(storeComment.getUserId());
            return this.save(storeComment) ? 0 : 1;
        } catch (Exception e) {
            log.error("StoreCommentService.userComment ERROR Msg={}", e.getMessage());
            return 1;
        }

    }

    /**
     * 新增或修改评论/评价
     *
     * @param multipartRequest 文件
     * @param id               主键
     * @param businessId       业务id
     * @param businessType     业务类型(1:订单评论, 2:动态社区评论, 3:活动评论,4:店铺打卡评论)
     * @param storeId          门店id
     * @param userId           用户id
     * @param replyId          回复id
     * @param commentContent   评价内容
     * @param score            评分
     * @param otherScore       其他评分
     * @param isAnonymous      是否匿名(0:否(默认), 1:是)
     * @param evaluationTags   评价标签
     * @param phoneId          用户id
     * @return 0:成功, 1:失败, 2:文本内容异常, 3:图片内容异常
     */
    @Override
    public Integer addComment(MultipartRequest multipartRequest, Integer id, Integer businessId, Integer businessType, Integer storeId, Integer userId, Integer replyId, String commentContent, Double score, String otherScore, Integer isAnonymous, String evaluationTags, String phoneId) {
        try {
            List<String> servicesList = Lists.newArrayList();
            servicesList.add(TextReviewServiceEnum.COMMENT_DETECTION_PRO.getService());
            servicesList.add(TextReviewServiceEnum.LLM_QUERY_MODERATION.getService());
            TextModerationResultVO textCheckResult = textModerationUtil.invokeFunction(commentContent, servicesList);
            if ("high".equals(textCheckResult.getRiskLevel())) {
                return 2;
            }

            /*Map<String, String> checkText = TextCheckUtil.check(commentContent);
            if (null == checkText || checkText.get("result").equals("1")) {
                return 2;
            }*/
            StoreComment storeComment = new StoreComment();
            storeComment.setId(id);
            storeComment.setStoreId(storeId);
            storeComment.setUserId(userId);
            storeComment.setCommentContent(commentContent);
            storeComment.setReplyId(replyId);
            storeComment.setBusinessId(businessId);
            storeComment.setBusinessType(businessType);
            storeComment.setScore(score);

            if (StringUtils.isNotEmpty(otherScore)) {
                List<LifeCouponVo> lifeCouponVos = JSONArray.parseArray(otherScore, LifeCouponVo.class);
                lifeCouponVos.stream().filter(i -> "口味".equals(i.getName())).findFirst().ifPresent(item -> storeComment.setTasteScore(Double.valueOf(item.getRateScore())));
                lifeCouponVos.stream().filter(i -> "环境".equals(i.getName())).findFirst().ifPresent(item -> storeComment.setEnScore(Double.valueOf(item.getRateScore())));
                lifeCouponVos.stream().filter(i -> "服务".equals(i.getName())).findFirst().ifPresent(item -> storeComment.setServiceScore(Double.valueOf(item.getRateScore())));
            }
            storeComment.setOtherScore(otherScore);
            storeComment.setIsAnonymous(isAnonymous);
            storeComment.setEvaluationTags(evaluationTags);
            storeComment.setPhoneId(phoneId);
            List<String> fileNameSet = new ArrayList<>(multipartRequest.getMultiFileMap().keySet());
            if (!fileNameSet.isEmpty() && storeId != null) {
                StringBuilder imgId = new StringBuilder();
                for (int i = 0; i < fileNameSet.size(); i++) {
                    MultipartFile multipartFile = multipartRequest.getFileMap().get(fileNameSet.get(i));
                   /*//b
                    System.out.println(multipartFile.getSize());
                    //kb
                    System.out.println(multipartFile.getSize() / 1024);*/
                    if (null != multipartFile && multipartFile.getSize() / 1024 > 0) {
                        byte[] fileByte;
                        try {
                            fileByte = multipartFile.getBytes();
                        } catch (IOException e) {
                            return 1;
                        }
                        String base64 = Base64.getEncoder().encodeToString(fileByte);
                        Map<String, String> checkImage = ImageCheckUtil.check(base64, 2);
                        if (checkImage != null && checkImage.get("result").equals("1")) {
                            return 3;
                        }
                        StoreImg storeImg = new StoreImg();
                        storeImg.setStoreId(storeComment.getStoreId());
                        storeImg.setImgType(8);
                        storeImg.setImgSort(i + 1);
                        storeImg.setImgUrl(fileUploadUtil.uploadOneFile(multipartFile));
                        storeImgMapper.insert(storeImg);
                        imgId.append(storeImg.getId()).append(",");
                    }
                }
                if (!imgId.toString().isEmpty()) {
                    storeComment.setImgId(imgId.substring(0, imgId.length() - 1));
                }
            }
            storeComment.setCreatedUserId(storeComment.getUserId());
            int i = this.save(storeComment) ? 0 : 1;

            //判断类型如果为5是评价 更新订单表orderAppraise为1 订单已评价
            if(businessType == 5){
                LambdaUpdateWrapper<LifeUserOrder> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.eq(LifeUserOrder :: getId, businessId );
                lambdaUpdateWrapper.set(LifeUserOrder :: getOrderAppraise, 1);
                lifeUserOrderMapper.update(null,lambdaUpdateWrapper);
            }
            StoreInfoScoreVo storeInfoScoreVo = storeCommentMapper.getCommentCountAndScoreInfo(storeId);
            double total = storeInfoScoreVo.getTotal();
            double scoreAvg = (total == 0 ? 0 : storeInfoScoreVo.getScore() / total);
            double tasteScore = (total == 0 ? 0 : storeInfoScoreVo.getTasteScore() / total);
            double enScore = (total == 0 ? 0 : storeInfoScoreVo.getEnScore() / total);
            double serviceScore = (total == 0 ? 0 : storeInfoScoreVo.getServiceScore() / total);
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setId(storeId);
            storeInfo.setScoreAvg(new BigDecimal(scoreAvg).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfo.setTasteScore(new BigDecimal(tasteScore).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfo.setEnScore(new BigDecimal(enScore).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfo.setServiceScore(new BigDecimal(serviceScore).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfoMapper.updateById(storeInfo);
            StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeInfo.getId()).eq(StoreUser::getDeleteFlag, 0));

            // 如果差评，则发送差评提醒
            if(score != null && score >= 0.5 && score <= 2.5){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String commonDate = simpleDateFormat.format(new Date());
                LifeNotice lifeMessage = new LifeNotice();
                lifeMessage.setReceiverId("store_" + storeUser.getPhone());
                String text = "在"+commonDate+"，您的店铺有一条差评记录，您可查看评价内容是否属实，如不属实，可向平台进行申诉。";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("message", text);
                lifeMessage.setContext(jsonObject.toJSONString());
                lifeMessage.setTitle("差评通知");
                lifeMessage.setSenderId("system");
                lifeMessage.setIsRead(0);
                lifeMessage.setNoticeType(1);
                lifeNoticeUtil.fillUserTypeAndRefId(lifeMessage);
                lifeNoticeMapper.insert(lifeMessage);

                WebSocketVo websocketVo = new WebSocketVo();
                websocketVo.setSenderId("system");
                websocketVo.setReceiverId("store_" + storeUser.getPhone());
                websocketVo.setCategory("notice");
                websocketVo.setNoticeType("1");
                websocketVo.setIsRead(0);
                websocketVo.setText(JSONObject.from(lifeMessage).toJSONString());
                webSocketProcess.sendMessage("store_" + storeUser.getPhone(), JSONObject.from(websocketVo).toJSONString());
            }
            return i;
        } catch (Exception e) {
            log.error("StoreCommentService.userComment ERROR Msg={}", e.getMessage());
            return 1;
        }
        // return 0;
    }


    /**
     * 回复率, 评价比例
     *
     * @param storeId 门店id
     * @return StoreCommitPercentVo
     */
    @Override
    public StoreCommitPercentVo getCommitPercent(Integer storeId) {
        StoreCommitPercentVo storeCommit = storeCommentMapper.getCommentByStoreId(storeId);
        if (storeCommit != null) {
            if ("0".equals(storeCommit.getAllCommentCount())) {
                storeCommit.setHighPercent("0%");
                storeCommit.setMidPercent("0%");
                storeCommit.setLowPercent("0%");
            } else {
                double highPercentDouble = Double.parseDouble(storeCommit.getHighCommentCount()) / Double.parseDouble(storeCommit.getRootCommentCount());
                BigDecimal highPercent = BigDecimal.valueOf(highPercentDouble).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
                //好评
                storeCommit.setHighPercent(highPercent + "%");
                double midPercentDouble = Double.parseDouble(storeCommit.getMidCommentCount()) / Double.parseDouble(storeCommit.getRootCommentCount());
                BigDecimal midPercent = BigDecimal.valueOf(midPercentDouble).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
                //中评
                storeCommit.setMidPercent(midPercent + "%");
                double lowPercentDouble = Double.parseDouble(storeCommit.getLowCommentCount()) / Double.parseDouble(storeCommit.getRootCommentCount());
                BigDecimal lowPercent = BigDecimal.valueOf(lowPercentDouble).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
                //差评
                storeCommit.setLowPercent(lowPercent + "%");
            }
            if ("0".equals(storeCommit.getRootCommentCount())) {
                storeCommit.setReplyPercent("0%");
            } else {
                double replyPercentDouble = Double.parseDouble(storeCommit.getCommentCount()) / Double.parseDouble(storeCommit.getRootCommentCount());
                BigDecimal replyPercent = BigDecimal.valueOf(replyPercentDouble).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
                //回复率
                storeCommit.setReplyPercent(replyPercent + "%");
            }
            return storeCommit;
        }
        return null;
    }

    @Override
    public IPage<LifeUserOrderCommentVo> getCommentOrderPage(Integer pageNum, Integer pageSize, Integer type, String userId) {
        IPage<LifeUserOrderCommentVo> lifeUserOrderCommentVoIPage = new Page<>(pageNum, pageSize);
        IPage<LifeUserOrderCommentVo> commentOrderPage;
        //1.未评价 2.已评价
        if (type == 1) {
            commentOrderPage = storeCommentMapper.getCommentOrderWPJPage(lifeUserOrderCommentVoIPage, userId);
        } else {
            commentOrderPage = storeCommentMapper.getCommentOrderYPJPage(lifeUserOrderCommentVoIPage, userId);
        }
        List<String> collect = commentOrderPage.getRecords().stream().map(LifeUserOrderCommentVo::getGroupBuyImgId).collect(Collectors.toList());

        if (ObjectUtils.isNotEmpty(collect)) {
            List<StoreImg> storeImgList = storeImgMapper.selectList(new QueryWrapper<StoreImg>().inSql("id", String.join(",", collect)));
//            commentOrderPage.getRecords().forEach(i ->
//                    i.setGroupBuyImgUrl(storeImgList.stream().filter(j -> j.getId().toString().equals(i.getGroupBuyImgId()))
//                            .findFirst().map(StoreImg::getImgUrl).orElse(null)));
            // 2. 优化分组：id唯一，直接映射为 Map<Integer, StoreImg>（避免List处理）
            Map<Integer, StoreImg> imgIdToImgMap = storeImgList.stream()
                    .collect(Collectors.toMap(
                            StoreImg::getId,  // 键：图片id
                            Function.identity(),  // 值：图片对象本身
                            (existing, replacement) -> existing  // 若有重复id（理论上不会），保留第一个
                    ));

            // 3. 遍历订单评论记录，处理图片URL拼接
            for (LifeUserOrderCommentVo record : commentOrderPage.getRecords()) {
                String groupBuyImgId = record.getGroupBuyImgId();
                // 空指针防护：如果imgId为空，直接设为空字符串
                if (groupBuyImgId == null || groupBuyImgId.trim().isEmpty()) {
                    record.setGroupBuyImgUrl("");
                    continue;
                }

                // 分割imgId并一次性转换为Integer（避免重复解析）
                List<Integer> imgIds = Arrays.stream(groupBuyImgId.split(","))
                        .map(String::trim)  // 处理可能的空格
                        .filter(idStr -> !idStr.isEmpty())  // 过滤空字符串（如连续逗号导致的）
                        .map(idStr -> {
                            try {
                                return Integer.parseInt(idStr);
                            } catch (NumberFormatException e) {
                                // 处理非数字id的异常（如无效id）
                                return null;
                            }
                        })
                        .filter(Objects::nonNull)  // 过滤转换失败的null
                        .collect(Collectors.toList());

                // 拼接图片URL（使用StringBuilder高效拼接）
                StringBuilder imgUrlBuilder = new StringBuilder();
                for (Integer imgId : imgIds) {
                    StoreImg storeImg = imgIdToImgMap.get(imgId);
                    if (storeImg != null && storeImg.getImgUrl() != null) {
                        if (imgUrlBuilder.length() > 0) {
                            imgUrlBuilder.append(",");  // 非第一个元素前加逗号
                        }
                        imgUrlBuilder.append(storeImg.getImgUrl());
                    }
                }

                // 设置最终的图片URL字符串（去掉末尾可能的逗号，这里通过逻辑避免了）
                record.setGroupBuyImgUrl(imgUrlBuilder.toString());
            }
        }


        List<LifeUserOrderCommentVo> lifeUserOrderCommentVos =  commentOrderPage.getRecords();
        if(CollectionUtils.isEmpty(lifeUserOrderCommentVos)){
            return commentOrderPage;
        }
        List<String> orderIds = lifeUserOrderCommentVos.stream().map(LifeUserOrderCommentVo::getId).collect(Collectors.toList());
        List<StoreComment> storeCommentList = storeCommentMapper.selectList(new QueryWrapper<StoreComment>().inSql("business_id", String.join(",", orderIds)).eq("business_type", 5));
        if(CollectionUtils.isEmpty(storeCommentList)){
            return commentOrderPage;
        }
        Map<Integer, StoreComment> commentIdToMap = storeCommentList.stream()
                .collect(Collectors.toMap(
                        StoreComment::getBusinessId,
                        Function.identity(),
                        (existing, replacement) -> existing  // 若有重复id（理论上不会），保留第一个
                ));
        if(CollectionUtils.isEmpty(commentIdToMap)){
            return commentOrderPage;
        }
        for(LifeUserOrderCommentVo lifeUserOrderCommentVo : commentOrderPage.getRecords()){
            Integer orderId = Integer.parseInt(lifeUserOrderCommentVo.getId());

            if(commentIdToMap.containsKey(orderId)){
                StoreComment storeComment = commentIdToMap.get(orderId);
                lifeUserOrderCommentVo.setScore(storeComment.getScore().toString());
                lifeUserOrderCommentVo.setCommentDate(storeComment.getCreatedTime());
                String imgIds = storeComment.getImgId();

                if(StringUtils.isNotEmpty(imgIds)){
                    LambdaQueryWrapper<StoreImg> storeImgLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    List<String> imgIdList = Arrays.stream(imgIds.split(","))
                            .map(String::trim)
                            .collect(Collectors.toList());
                    storeImgLambdaQueryWrapper.in(StoreImg::getId, imgIdList);
                    List<StoreImg> storeImgList = storeImgMapper.selectList(storeImgLambdaQueryWrapper);
                    if(CollectionUtils.isNotEmpty(storeImgList)){
                        List<String> imgUrlList = storeImgList.stream()    // 转换为 Stream
                                .map(StoreImg::getImgUrl)                  // 映射，提取 name 字段
                                .collect(Collectors.toList());
                        lifeUserOrderCommentVo.setImgUrls(imgUrlList);
                    }
                }
            }
        }

        return commentOrderPage;
    }

    /**
     * 获取店铺评价计数统计
     *
     * @param storeId 门店id
     * @return StoreCommentCountVo
     */
    @Override
    public StoreCommentCountVo getAppraiseCount(Integer storeId) {
        StoreCommentCountVo countVo = new StoreCommentCountVo();

        // 查询条件：只统计当前门店、根评论（reply_id为null）、未删除的评论
        LambdaQueryWrapper<StoreComment> totalWrapper = buildBaseStoreCommentWrapper(storeId);
        countVo.setTotalCount(storeCommentMapper.selectCount(totalWrapper));

        // 2. 有图评论数（img_id不为空且不为空字符串）
        LambdaQueryWrapper<StoreComment> imageWrapper = buildBaseStoreCommentWrapper(storeId);
        imageWrapper.isNotNull(StoreComment::getImgId)
                .apply("img_id != ''");
        countVo.setImageCount(storeCommentMapper.selectCount(imageWrapper));

        // 3. 好评数（score >= 4.5）
        LambdaQueryWrapper<StoreComment> goodWrapper = buildBaseStoreCommentWrapper(storeId);
        goodWrapper.ge(StoreComment::getScore, 4.5);
        countVo.setGoodCount(storeCommentMapper.selectCount(goodWrapper));

        // 4. 中评数（score >= 3 && score <= 4）
        LambdaQueryWrapper<StoreComment> midWrapper = buildBaseStoreCommentWrapper(storeId);
        midWrapper.ge(StoreComment::getScore, 3.0)
                .le(StoreComment::getScore, 4.0);
        countVo.setMidCount(storeCommentMapper.selectCount(midWrapper));

        // 5. 差评数（score >= 0.5 && score <= 2.5）
        LambdaQueryWrapper<StoreComment> badWrapper = buildBaseStoreCommentWrapper(storeId);
        badWrapper.ge(StoreComment::getScore, 0.5)
                .le(StoreComment::getScore, 2.5);
        countVo.setBadCount(storeCommentMapper.selectCount(badWrapper));

        return countVo;
    }

    /**
     * 构建当前门店根评论的查询条件
     */
    private LambdaQueryWrapper<StoreComment> buildBaseStoreCommentWrapper(Integer storeId) {
        return new LambdaQueryWrapper<StoreComment>()
                .eq(StoreComment::getStoreId, storeId)
                .gt(StoreComment::getScore,0 )
                .eq(StoreComment::getBusinessType, 6)
                .eq(StoreComment::getDeleteFlag, 0);
    }

    @Override
    public IPage<UserOrderVo> getUserAllOrders(Integer pageNum, Integer pageSize, Integer userId, Integer type) {
        log.info("StoreCommentServiceImpl.getUserAllOrders?pageNum={}&pageSize={}&userId={}&type={}", pageNum, pageSize, userId, type);

        if (userId == null) {
            log.warn("用户ID不能为空");
            return new Page<>(pageNum, pageSize);
        }

        List<UserOrderVo> allOrders = new ArrayList<>();

        // 1. 查询商户订单（已完成状态：status = 7）
        Page<LifeUserOrderVo> storeOrderPage = new Page<>(pageNum, 10000); // 先查所有，后续合并分页
        QueryWrapper<LifeUserOrderVo> storeOrderWrapper = new QueryWrapper<>();
        storeOrderWrapper.eq("luo.user_id", String.valueOf(userId))
                .eq("luo.status", 7) // 已完成状态
                .eq("luo.delete_flag", 0);
        
        // 根据type筛选：1-未评价，2-已评价
        if (type != null) {
            if (type == 1) {
                // 未评价：orderAppraise = 0
                storeOrderWrapper.eq("luo.order_appraise", 0);
            } else if (type == 2) {
                // 已评价：orderAppraise = 1
                storeOrderWrapper.eq("luo.order_appraise", 1);
            }
        }
        
        storeOrderWrapper.orderByDesc("luo.created_time");
        IPage<LifeUserOrderVo> storeOrders = lifeUserOrderMapper.queryUserOrderList(storeOrderPage, storeOrderWrapper);

        // 转换商户订单为统一VO
        if (storeOrders != null && CollectionUtils.isNotEmpty(storeOrders.getRecords())) {
            // 如果type=2（已评价），需要查询评价信息
            Map<String, StoreComment> commentMap = new HashMap<>();
            if (type != null && type == 2) {
                List<String> orderIds = storeOrders.getRecords().stream()
                        .map(LifeUserOrderVo::getId)
                        .collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(orderIds)) {
                    LambdaQueryWrapper<StoreComment> commentWrapper = new LambdaQueryWrapper<>();
                    commentWrapper.in(StoreComment::getBusinessId, orderIds)
                            .eq(StoreComment::getBusinessType, 5) // 订单评价
                            .eq(StoreComment::getDeleteFlag, 0);
                    List<StoreComment> comments = storeCommentMapper.selectList(commentWrapper);
                    commentMap = comments.stream()
                            .collect(Collectors.toMap(
                                    c -> String.valueOf(c.getBusinessId()),
                                    Function.identity(),
                                    (existing, replacement) -> existing
                            ));
                }
            }
            
            for (LifeUserOrderVo order : storeOrders.getRecords()) {
                StoreComment comment = commentMap.get(order.getId());
                UserOrderVo vo = convertStoreOrderToVo(order, comment);
                allOrders.add(vo);
            }
        }

        // 2. 查询律师订单（已完成状态：orderStatus = 3）
        Page<LawyerConsultationOrderVO> lawyerOrderPage = new Page<>(pageNum, 10000);
        IPage<LawyerConsultationOrderVO> lawyerOrders = lawyerConsultationOrderMapper.getConsultationOrderListById(
                lawyerOrderPage, String.valueOf(userId), "3", null); // orderStatus = 3 表示已完成

        // 转换律师订单为统一VO
        if (lawyerOrders != null && CollectionUtils.isNotEmpty(lawyerOrders.getRecords())) {
            // 查询所有律师订单的评价信息（用于type筛选和评分填充）
            Map<Integer, OrderReview> reviewMap = new HashMap<>();
            List<Integer> orderIds = lawyerOrders.getRecords().stream()
                    .map(LawyerConsultationOrderVO::getId)
                    .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(orderIds)) {
                LambdaQueryWrapper<OrderReview> reviewWrapper = new LambdaQueryWrapper<>();
                reviewWrapper.in(OrderReview::getOrderId, orderIds)
                        .eq(OrderReview::getDeleteFlag, 0);
                List<OrderReview> reviews = orderReviewMapper.selectList(reviewWrapper);
                reviewMap = reviews.stream()
                        .collect(Collectors.toMap(
                                OrderReview::getOrderId,
                                Function.identity(),
                                (existing, replacement) -> existing
                        ));
            }
            
            for (LawyerConsultationOrderVO order : lawyerOrders.getRecords()) {
                OrderReview review = reviewMap.get(order.getId());
                
                // 根据type筛选
                if (type != null) {
                    if (type == 1 && review != null) {
                        // 未评价：跳过有评价的订单
                        continue;
                    } else if (type == 2 && review == null) {
                        // 已评价：跳过没有评价的订单
                        continue;
                    }
                }
                
                UserOrderVo vo = convertLawyerOrderToVo(order, review);
                allOrders.add(vo);
            }
        }

        // 3. 按创建时间倒序排序
        allOrders.sort((o1, o2) -> {
            Date date1 = o1.getCreatedTime();
            Date date2 = o2.getCreatedTime();
            if (date1 == null && date2 == null) return 0;
            if (date1 == null) return 1;
            if (date2 == null) return -1;
            return date2.compareTo(date1);
        });

        // 4. 手动分页
        int total = allOrders.size();
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, total);
        List<UserOrderVo> pageRecords = start < total ? allOrders.subList(start, end) : new ArrayList<>();

        // 5. 构建分页结果
        Page<UserOrderVo> resultPage = new Page<>(pageNum, pageSize);
        resultPage.setRecords(pageRecords);
        resultPage.setTotal(total);
        resultPage.setPages((total + pageSize - 1) / pageSize);

        return resultPage;
    }

     @Override
    public Integer saveCommentOnlyStore(String imageUrls, Integer id, Integer businessType, Integer storeId, Integer userId, Integer replyId, String commentContent, Double score, String otherScore, Integer isAnonymous) {
        try {
            // 1.文本审核，
            List<String> servicesList = Lists.newArrayList();
            servicesList.add(TextReviewServiceEnum.COMMENT_DETECTION_PRO.getService());
            servicesList.add(TextReviewServiceEnum.LLM_QUERY_MODERATION.getService());
            TextModerationResultVO textCheckResult = textModerationUtil.invokeFunction(commentContent, servicesList);
            if ("high".equals(textCheckResult.getRiskLevel())) {
                return 2;
            }
            // 2.构造评价实体类
            StoreComment storeComment = new StoreComment();
            storeComment.setId(id);
            storeComment.setStoreId(storeId);
            storeComment.setUserId(userId);
            storeComment.setCommentContent(commentContent);
            storeComment.setReplyId(replyId);
            storeComment.setBusinessType(businessType);
            storeComment.setScore(score);

            if (StringUtils.isNotEmpty(otherScore)) {
                JSONObject parse = JSONObject.parse(otherScore);
                storeComment.setScoreOne(parse.getDouble("scoreOne"));
                storeComment.setScoreTwo(parse.getDouble("scoreTwo"));
                storeComment.setScoreThree(parse.getDouble("scoreThree"));
            }
            storeComment.setOtherScore(otherScore);
            storeComment.setIsAnonymous(isAnonymous);
//            List<String> fileNameSet = new ArrayList<>(multipartRequest.getMultiFileMap().keySet());
            StringBuilder imgId = new StringBuilder();
            String[] split = imageUrls.split(",");

            for (int i = 0; i < split.length; i++) {
                StoreImg storeImg = new StoreImg();
                storeImg.setStoreId(storeComment.getStoreId());
                storeImg.setImgType(8);
                storeImg.setImgSort(i + 1);
                storeImg.setImgUrl(split[i]);
                storeImgMapper.insert(storeImg);
                imgId.append(storeImg.getId()).append(",");
            }
            if (!imgId.toString().isEmpty()) {
                storeComment.setImgId(imgId.substring(0, imgId.length() - 1));
            }
            storeComment.setCreatedUserId(storeComment.getUserId());

/*            List<MultipartFile> fileNameSet = multipartRequest.getMultiFileMap().get("file");
            if (!fileNameSet.isEmpty() && storeId != null) {
                StringBuilder imgId = new StringBuilder();
                for (int i = 0; i < fileNameSet.size(); i++) {
                    MultipartFile multipartFile = fileNameSet.get(i);
                    //b
                    System.out.println(multipartFile.getSize());
                    //kb
                    System.out.println(multipartFile.getSize() / 1024);
                    if (null != multipartFile && !multipartFile.isEmpty()) {
                        byte[] fileByte;
                        try {
                            fileByte = multipartFile.getBytes();
                        } catch (IOException e) {
                            return 1;
                        }
                        String base64 = Base64.getEncoder().encodeToString(fileByte);
                        Map<String, String> checkImage = ImageCheckUtil.check(base64, 2);
                        if (checkImage != null && checkImage.get("result").equals("1")) {
                            return 3;
                        }
                        StoreImg storeImg = new StoreImg();
                        storeImg.setStoreId(storeComment.getStoreId());
                        storeImg.setImgType(8);
                        storeImg.setImgSort(i + 1);
                        storeImg.setImgUrl(fileUploadUtil.uploadOneFile(multipartFile));
                        storeImgMapper.insert(storeImg);
                        imgId.append(storeImg.getId()).append(",");
                    }
                }
                if (!imgId.toString().isEmpty()) {
                    storeComment.setImgId(imgId.substring(0, imgId.length() - 1));
                }
            }*/
            storeComment.setCreatedUserId(storeComment.getUserId());
            int i = this.save(storeComment) ? 0 : 1;

            // 更新门店评价信息
            StoreInfoScoreVo storeInfoScoreVo = storeCommentMapper.getCommentCountAndScoreInfo(storeId);
            double total = storeInfoScoreVo.getTotal();
            double scoreAvg = (total == 0 ? 0 : storeInfoScoreVo.getScore() / total);
            double scoreOne = (total == 0 ? 0 : storeInfoScoreVo.getScoreOne() / total);
            double scoreTwo = (total == 0 ? 0 : storeInfoScoreVo.getScoreTwo() / total);
            double scoreThree = (total == 0 ? 0 : storeInfoScoreVo.getScoreThree() / total);
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setId(storeId);
            storeInfo.setScoreAvg(new BigDecimal(scoreAvg).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfo.setScoreOne(new BigDecimal(scoreOne).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfo.setScoreTwo(new BigDecimal(scoreTwo).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfo.setScoreThree(new BigDecimal(scoreThree).setScale(2, RoundingMode.HALF_UP).doubleValue());
            storeInfoMapper.updateById(storeInfo);
            StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeInfo.getId()).eq(StoreUser::getDeleteFlag, 0));

            // 如果差评，则发送差评提醒
            if(score != null && score >= 0.5 && score <= 2.5){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String commonDate = simpleDateFormat.format(new Date());
                LifeNotice lifeMessage = new LifeNotice();
                lifeMessage.setReceiverId("store_" + storeUser.getPhone());
                String text = "在"+commonDate+"，您的店铺有一条差评记录，您可查看评价内容是否属实，如不属实，可向平台进行申诉。";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("message", text);
                lifeMessage.setContext(jsonObject.toJSONString());
                lifeMessage.setTitle("差评通知");
                lifeMessage.setSenderId("system");
                lifeMessage.setIsRead(0);
                lifeMessage.setNoticeType(1);
                lifeNoticeUtil.fillUserTypeAndRefId(lifeMessage);
                lifeNoticeMapper.insert(lifeMessage);

                WebSocketVo websocketVo = new WebSocketVo();
                websocketVo.setSenderId("system");
                websocketVo.setReceiverId("store_" + storeUser.getPhone());
                websocketVo.setCategory("notice");
                websocketVo.setNoticeType("1");
                websocketVo.setIsRead(0);
                websocketVo.setText(JSONObject.from(lifeMessage).toJSONString());
                webSocketProcess.sendMessage("store_" + storeUser.getPhone(), JSONObject.from(websocketVo).toJSONString());
            }
            return i;
        } catch (Exception e) {
            log.error("StoreCommentService.saveCommentOnlyStore ERROR Msg={}", e.getMessage());
            return 1;
        }
    }

    /**
     * 转换商户订单为统一VO
     */
    private UserOrderVo convertStoreOrderToVo(LifeUserOrderVo order, StoreComment comment) {
        UserOrderVo vo = new UserOrderVo();
        vo.setOrderId(order.getId());
        vo.setOrderNumber(order.getOrderNo());
        vo.setOrderType(1); // 1-商户订单
        vo.setUserId(order.getUserId());
        vo.setBusinessId(order.getStoreId());
        vo.setBusinessName(order.getStoreName());
        vo.setOrderAmount(order.getPrice());
        vo.setFinalPrice(order.getFinalPrice());
        vo.setOrderStatus(order.getStatus());
        vo.setOrderStatusValue(order.getOrderStatusValue());
        vo.setOrderTime(order.getBuyTime());
        vo.setPayTime(order.getPayTime());
        vo.setCreatedTime(order.getCreatedTime());
        vo.setStoreId(order.getStoreId());
        vo.setCouponName(order.getCouponName());
        vo.setImgUrl(order.getImgUrl());
        vo.setOrderAppraise(order.getOrderAppraise());
        
        // 如果已评价，填充评分信息
        if (comment != null) {
            vo.setScore(comment.getScore());
            vo.setOtherScore(comment.getOtherScore());
        }
        
        return vo;
    }

    /**
     * 转换律师订单为统一VO
     */
    private UserOrderVo convertLawyerOrderToVo(LawyerConsultationOrderVO order, OrderReview review) {
        UserOrderVo vo = new UserOrderVo();
        vo.setOrderId(String.valueOf(order.getId()));
        vo.setOrderNumber(order.getOrderNumber());
        vo.setOrderType(2); // 2-律师订单
        vo.setUserId(String.valueOf(order.getClientUserId()));
        vo.setBusinessId(String.valueOf(order.getLawyerUserId()));
        vo.setBusinessName(order.getLawyerName());
        vo.setOrderAmount(String.valueOf(order.getOrderAmount()));
        vo.setOrderStatus(order.getOrderStatus());
        vo.setPaymentStatus(order.getPaymentStatus());
        vo.setOrderTime(order.getOrderTime());
        vo.setPayTime(order.getPaymentTime());
        vo.setCreatedTime(order.getCreatedTime());
        vo.setLawyerUserId(order.getLawyerUserId());
        vo.setProblemDescription(order.getProblemDescription());
        vo.setConsultationFee(order.getConsultationFee());
        vo.setStartTime(order.getStartTime());
        vo.setEndTime(order.getEndTime());
        vo.setLawyerHeadImg(order.getHeadImg());
        vo.setHasReview(review != null);
        
        // 如果已评价，填充评分信息
        if (review != null) {
            vo.setOverallRating(review.getOverallRating());
            vo.setServiceAttitudeRating(review.getServiceAttitudeRating());
            vo.setResponseTimeRating(review.getResponseTimeRating());
            vo.setProfessionalAbilityRating(review.getProfessionalAbilityRating());
        }
        
        return vo;
    }
}
