package com.fjhx.oa.service.daily.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.oa.entity.daily.dto.DailyReportCommentsDto;
import com.fjhx.oa.entity.daily.dto.DailyReportCommentsSelectDto;
import com.fjhx.oa.entity.daily.po.DailyReportComments;
import com.fjhx.oa.entity.daily.vo.DailyReportCommentsVo;
import com.fjhx.oa.mapper.daily.DailyReportCommentsMapper;
import com.fjhx.oa.service.daily.DailyReportCommentsService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 报表评论 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-04
 */
@DS(SourceConstant.OA)
@Service
public class DailyReportCommentsServiceImpl extends ServiceImpl<DailyReportCommentsMapper, DailyReportComments> implements DailyReportCommentsService {

    @Override
    public Page<DailyReportCommentsVo> getPage(DailyReportCommentsSelectDto dto) {
        IWrapper<DailyReportComments> wrapper = getWrapper();
        wrapper.orderByDesc("drc", DailyReportComments::getId);
        wrapper.eq(DailyReportComments::getDailyReportId, dto.getDailyReportId());
        //过滤掉回复评论
        wrapper.isNull("drc.reply_id");
        Page<DailyReportCommentsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<DailyReportCommentsVo> records = page.getRecords();
        //赋值用户名
        UserUtil.assignmentNickName(records, DailyReportComments::getCreateUser, DailyReportCommentsVo::setUserName);
        //赋值回复评论列表
        List<Long> dailyReportCommentsIds = records.stream().map(DailyReportComments::getId).collect(Collectors.toList());
        if (ObjectUtil.isEmpty(dailyReportCommentsIds)) {
            return page;
        }
        List<DailyReportComments> list = list(q -> q.in(DailyReportComments::getReplyId, dailyReportCommentsIds));
        List<DailyReportCommentsVo> dailyReportCommentsVos = BeanUtil.copyToList(list, DailyReportCommentsVo.class);
        UserUtil.assignmentNickName(dailyReportCommentsVos, DailyReportComments::getCreateUser, DailyReportCommentsVo::setUserName);
        Map<Long, List<DailyReportCommentsVo>> collect = dailyReportCommentsVos.stream().collect(Collectors.groupingBy(DailyReportComments::getReplyId));
        for (DailyReportCommentsVo dailyReportComments : records) {
            dailyReportComments.setReplyList(collect.get(dailyReportComments.getId()));
        }

        return page;
    }

    @Override
    public DailyReportCommentsVo detail(Long id) {
        DailyReportComments DailyReportComments = this.getById(id);
        DailyReportCommentsVo result = BeanUtil.toBean(DailyReportComments, DailyReportCommentsVo.class);
        return result;
    }

    @Override
    public void add(DailyReportCommentsDto dailyReportCommentsDto) {
        this.save(dailyReportCommentsDto);
    }

    @Override
    public void edit(DailyReportCommentsDto dailyReportCommentsDto) {
        this.updateById(dailyReportCommentsDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
