package com.ruoyi.system.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InfoPinglunMapper;
import com.ruoyi.system.domain.InfoPinglun;
import com.ruoyi.system.service.IInfoPinglunService;

/**
 * 信息评论Service业务层处理
 *
 * @author boman
 * @date 2023-11-28
 */
@Service
public class InfoPinglunServiceImpl implements IInfoPinglunService {
    @Autowired
    private InfoPinglunMapper infoPinglunMapper;

    /**
     * 查询信息评论
     *
     * @param infoPinglunId 信息评论主键
     * @return 信息评论
     */
    @Override
    public InfoPinglun selectInfoPinglunByInfoPinglunId(Long infoPinglunId) {
        return infoPinglunMapper.selectInfoPinglunByInfoPinglunId(infoPinglunId);
    }

    /**
     * 查询信息评论列表
     *
     * @param infoPinglun 信息评论
     * @return 信息评论
     */
    @Override
    public List<InfoPinglun> selectInfoPinglunList(InfoPinglun infoPinglun) {
        return infoPinglunMapper.selectInfoPinglunList(infoPinglun);
    }

    @Override
    public List<InfoPinglun> getPingLunInfo(Long annexId) {
        List<InfoPinglun> infoPingLunList = infoPinglunMapper.selectInfoPinglunByAnnexId(annexId);
        if (infoPingLunList != null && infoPingLunList.size() > 0) {
            //根据评论类型进行分组评论类型 1:评论 2:回复
            Map<String, List<InfoPinglun>> collect = infoPingLunList.stream().sorted(Comparator.comparing(InfoPinglun::getPingLunTime, Comparator.reverseOrder())).collect(Collectors.groupingBy(InfoPinglun::getInfoPingLunType));
            if (collect != null && collect.size() > 0){
                //所有评论类型的数据
                List<InfoPinglun> infoPingLunListFather = collect.get("1");
                //给他们找自己的回复类型的数据
                if (infoPingLunListFather != null && infoPingLunListFather.size() > 0){
                    for (InfoPinglun infoPinglun : infoPingLunListFather) {
                        List<InfoPinglun> infoPinglunList = collect.get("2");
                        //回复不是空
                        if(infoPinglunList != null && infoPinglunList.size() > 0){
                            Map<Long, List<InfoPinglun>> infoPingLunListParen = infoPinglunList.stream().collect(Collectors.groupingBy(InfoPinglun::getInfoPingLunParent));
                            List<InfoPinglun> infoPinglunListCh = infoPingLunListParen.get(infoPinglun.getInfoPinglunId());
                            if (infoPinglunListCh != null && infoPinglunListCh.size() > 0){
                                infoPinglun.setInfoPingLunList(infoPinglunListCh.stream().sorted(Comparator.comparing(InfoPinglun::getPingLunTime, Comparator.reverseOrder())).collect(Collectors.toList()));
                            }
                        }
                    }
                }
                return infoPingLunListFather;
            }
        }
        return null;
    }

    @Override
    public List<InfoPinglun> getPingLunInfoIndex(Long annexId) {
        List<InfoPinglun> infoPingLunList = infoPinglunMapper.selectInfoPinglunByAnnexId(annexId);
        if (infoPingLunList != null && infoPingLunList.size() > 0) {
            //根据评论类型进行分组评论类型 1:评论 2:回复
            Map<String, List<InfoPinglun>> collect = infoPingLunList.stream().sorted(Comparator.comparing(InfoPinglun::getPingLunTime, Comparator.reverseOrder())).collect(Collectors.groupingBy(InfoPinglun::getInfoPingLunType));
            if (collect != null && collect.size() > 0){
                //所有评论类型的数据
                List<InfoPinglun> infoPingLunListFather = collect.get("1");
                return infoPingLunListFather;
            }
        }
        return null;
    }

    /**
     * 新增信息评论
     *
     * @param infoPinglun 信息评论
     * @return 结果
     */
    @Override
    public int insertInfoPinglun(InfoPinglun infoPinglun) {
        infoPinglun.setCreateTime(DateUtils.getNowDate());
        infoPinglun.setInfoPingLunUserName("游客");
        infoPinglun.setInfoPingLunQuiltUserName("游客");
        infoPinglun.setPingLunTime(DateUtils.getNowDate());
        return infoPinglunMapper.insertInfoPinglun(infoPinglun);
    }

    /**
     * 修改信息评论
     *
     * @param infoPinglun 信息评论
     * @return 结果
     */
    @Override
    public int updateInfoPinglun(InfoPinglun infoPinglun) {
        infoPinglun.setUpdateTime(DateUtils.getNowDate());
        return infoPinglunMapper.updateInfoPinglun(infoPinglun);
    }

    /**
     * 批量删除信息评论
     *
     * @param infoPinglunIds 需要删除的信息评论主键
     * @return 结果
     */
    @Override
    public int deleteInfoPinglunByInfoPinglunIds(Long[] infoPinglunIds) {
        return infoPinglunMapper.deleteInfoPinglunByInfoPinglunIds(infoPinglunIds);
    }

    /**
     * 删除信息评论信息
     *
     * @param infoPinglunId 信息评论主键
     * @return 结果
     */
    @Override
    public int deleteInfoPinglunByInfoPinglunId(Long infoPinglunId) {
        return infoPinglunMapper.deleteInfoPinglunByInfoPinglunId(infoPinglunId);
    }
}
