package com.ruoyi.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.busi.domain.BusiEvaluate;
import com.ruoyi.busi.domain.BusiNoticeSign;
import com.ruoyi.busi.mapper.BusiEvaluateMapper;
import com.ruoyi.busi.mapper.BusiNoticeSignMapper;
import com.ruoyi.busi.service.IBusiEvaluateService;
import com.ruoyi.busi.vo.BusiEvaluateVO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 评价Service业务层处理
 *
 * @author 朱春云
 * @date 2025-03-17
 */
@Service
public class BusiEvaluateServiceImpl extends ServiceImpl<BusiEvaluateMapper,BusiEvaluate>  implements IBusiEvaluateService
{
    @Autowired
    private BusiEvaluateMapper busiEvaluateMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private BusiNoticeSignMapper busiNoticeSignMapper;

    @Override
    public IPage<BusiEvaluate> queryListPage(BusiEvaluate pageReqVO, Page<BusiEvaluate> page) {
        return busiEvaluateMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 通过被评价人用户id查询评价内容
     *
     * @param userId 被评价人用户id
     * @return java.util.List<com.ruoyi.busi.domain.BusiEvaluate>
     * @author PQZ
     * @date 14:16 2025/3/21
     **/
    @Override
    public List<BusiEvaluateVO> listByToUserId(Long userId) {
        List<BusiEvaluateVO> busiEvaluateVOS = busiEvaluateMapper.listByToUserId(userId);

        // 第二步：拆分字符串并统计
        Map<String, Long> countMap = busiEvaluateVOS.stream()
                .filter(Objects::nonNull)
                .flatMap(s -> Arrays.stream(s.getEvaluate().split(",")))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        // 第三步：封装为 VO 列表
        return countMap.entrySet().stream()
                .map(entry -> {
                    BusiEvaluateVO vo = new BusiEvaluateVO();
                    vo.setEvaluate(entry.getKey());
                    vo.setNum(entry.getValue().intValue());
                    return vo;
                })
                .sorted(Comparator.comparingInt(BusiEvaluateVO::getNum).reversed())
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveVo(BusiEvaluate busiEvaluate) {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        busiEvaluate.setToUserId(sysUser.getUserId());
        busiEvaluate.setNickname(sysUser.getNickName());
        this.save(busiEvaluate);
        //处理报名是否评价的状态
        if (StringUtils.isNotEmpty(busiEvaluate.getSignId())){
            BusiNoticeSign busiNoticeSign = busiNoticeSignMapper.selectById(busiEvaluate.getSignId());
            busiNoticeSign.setIsEvaluate("1");
            busiNoticeSignMapper.updateById(busiNoticeSign);
        }
    }
}
