package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.BaseCompanyMapper;
import com.usky.fire.mapper.DemReportInfoMapper;
import com.usky.fire.mapper.ReportResultMapper;
import com.usky.fire.service.ReportConfigService;
import com.usky.fire.service.ReportResultService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 报告结果表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-06-28
 */
@Service
public class ReportResultServiceImpl extends AbstractCrudService<ReportResultMapper, ReportResult> implements ReportResultService {
    @Autowired
    private ReportConfigService reportConfigService;
    @Autowired
    private BaseCompanyMapper baseCompanyMapper;
    @Autowired
    private DemReportInfoMapper demReportInfoMapper;

    @Override
    public CommonPage<ReportResult> reportResultList(Integer companyId, String sourceType, Integer pageNum, Integer pageSize) {
        Page<ReportResult> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<ReportResult> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportResult::getObjectId, companyId)
                .orderByDesc(ReportResult::getId);
        IPage<ReportResult> resultPage = this.page(page, queryWrapper);
        List<ReportResult> resultList = resultPage.getRecords();

        List<Integer> objectIdList = resultList.stream()
                .map(ReportResult::getObjectId)
                .distinct()
                .collect(Collectors.toList());

        if (objectIdList.isEmpty()) {
            return ToCommonPage(resultPage);
        }

        LambdaQueryWrapper<BaseCompany> baseCompanyQuery = Wrappers.lambdaQuery();
        baseCompanyQuery.select(BaseCompany::getId, BaseCompany::getCompanyId)
                .in(BaseCompany::getId, objectIdList);
        List<BaseCompany> baseCompanyList = baseCompanyMapper.selectList(baseCompanyQuery);

        List<String> companyIdList = baseCompanyList.stream()
                .map(BaseCompany::getCompanyId)
                .distinct()
                .collect(Collectors.toList());

        if (companyIdList.isEmpty()) {
            return ToCommonPage(resultPage);
        }

        LambdaQueryWrapper<DemReportInfo> demReportQuery = Wrappers.lambdaQuery();
        demReportQuery.select(DemReportInfo::getIsRead, DemReportInfo::getReadTime, DemReportInfo::getCompanyId, DemReportInfo::getReportStatus)
                .in(DemReportInfo::getCompanyId, companyIdList);
        List<DemReportInfo> demReportInfoList = demReportInfoMapper.selectList(demReportQuery);

        Map<String, DemReportInfo> demReportMap = demReportInfoList.stream()
                .collect(Collectors.toMap(DemReportInfo::getCompanyId, info -> info, (existing, replacement) -> existing));

        Map<Integer, String> baseCompanyMap = baseCompanyList.stream()
                .collect(Collectors.toMap(BaseCompany::getId, BaseCompany::getCompanyId, (existing, replacement) -> existing));

        for (ReportResult reportResult : resultList) {
            Integer objectId = reportResult.getObjectId();
            String relatedCompanyId = baseCompanyMap.get(objectId);

            if (relatedCompanyId != null) {
                DemReportInfo demReportInfo = demReportMap.get(relatedCompanyId);
                if (demReportInfo != null) {
                    reportResult.setReportStatus(demReportInfo.getReportStatus());
                    reportResult.setReadTime(demReportInfo.getReadTime());
                    reportResult.setIsRead(demReportInfo.getIsRead());
                }
            }
        }

        return ToCommonPage(resultPage);
    }

    @Override
    public CommonPage<Object> newReportResultList(String reportObject, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<ReportResult> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ReportResult> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(reportObject), ReportResult::getReportObject, reportObject)
                .orderByDesc(ReportResult::getId);
        page = this.page(page, queryWrapper);
        LambdaQueryWrapper<ReportConfig> queryWrapper1 = Wrappers.lambdaQuery();
        List<ReportConfig> list1 = reportConfigService.list(queryWrapper1);
        if (page.getTotal() > 0) {
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", page.getRecords().get(i).getId());
                map.put("reportObject", page.getRecords().get(i).getReportObject());
                map.put("overallScore", page.getRecords().get(i).getOverallScore());
                map.put("createDate", page.getRecords().get(i).getCreateDate());
                for (int j = 0; j < list1.size(); j++) {
                    if (page.getRecords().get(i).getConfigId().equals(list1.get(j).getId())) {
                        map.put("reportName", list1.get(j).getReportName());
                    }
                }
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }
}
