package cn.cecep.talroad.data.analyse.task.execute.report.specifications.audit;

import cn.cecep.talroad.domain.OdsPermissionSharereportattachmentA;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditRecord;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditResult;
import cn.cecep.talroad.mapper.OdsPermissionSharereportattachmentAMapper;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditRecordMapper;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditResultMapper;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author hehongyu
 * @description: 污染物实际排放计算过程
 * @date 2023/8/12 17:18
 */
@Slf4j
@Service
public class PollutantEmissionsCalculationProcessService {


    @Autowired
    private SRaActionReportsAuditResultMapper mapper;

    @Autowired
    private SRaActionReportsAuditRecordMapper recordMapper;

    @Autowired
    private OdsPermissionSharereportattachmentAMapper sharereportattachmentAMapper;

    public void execute(List<String> reportIds){
        LambdaQueryWrapper<SRaActionReportsAuditRecord> recordQueryWrapper = new QueryWrapper<SRaActionReportsAuditRecord>()
                .lambda()
                .select(SRaActionReportsAuditRecord::getId,SRaActionReportsAuditRecord::getReportId)
                .in(SRaActionReportsAuditRecord::getReportId, reportIds);
        List<SRaActionReportsAuditRecord> sRaActionReportsAuditRecords = recordMapper.selectList(recordQueryWrapper);
        if (CollectionUtil.isEmpty(sRaActionReportsAuditRecords)){
            log.warn("执行报告审核记录不存在！");
            return;
        }
        //报告id 和 报告记录id映射
        Map<String, String> reportIdMap = sRaActionReportsAuditRecords.stream().collect(Collectors.toMap(SRaActionReportsAuditRecord::getId, SRaActionReportsAuditRecord::getReportId));

        List<String> recordIds = sRaActionReportsAuditRecords.stream().map(SRaActionReportsAuditRecord::getId).collect(Collectors.toList());
        LambdaQueryWrapper<SRaActionReportsAuditResult> queryWrapper = new QueryWrapper<SRaActionReportsAuditResult>().lambda().in(SRaActionReportsAuditResult::getRecordId, recordIds);
        List<SRaActionReportsAuditResult> sRaActionReportsAuditResults = mapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(sRaActionReportsAuditResults)){
            log.warn("执行报告规范性审核结果记录不存在！");
            return;
        }
        //污染物实际排放计算过程 未上传  则为不规范
        LambdaQueryWrapper<OdsPermissionSharereportattachmentA> fileQueryWrapper = new QueryWrapper<OdsPermissionSharereportattachmentA>()
                .lambda().in(OdsPermissionSharereportattachmentA::getReportid, reportIds)
                .like(OdsPermissionSharereportattachmentA::getFilename,"实际排放计算过程");
        List<OdsPermissionSharereportattachmentA> odsPermissionSharereportattachmentAS = sharereportattachmentAMapper.selectList(fileQueryWrapper);
        //过滤没有附件的 报表id
        List<Long> notFileReportIds = odsPermissionSharereportattachmentAS.stream().filter(data -> StringUtils.isBlank(data.getFastdfsid()))
                .map(OdsPermissionSharereportattachmentA::getReportid)
                .collect(Collectors.toList());
        sRaActionReportsAuditResults
                .stream().filter(data -> {
            String reportId = reportIdMap.get(data.getRecordId());
            if (reportId == null){
                return false;
            }
            return notFileReportIds.contains(reportId);
        }).forEach(data -> data.setCalculationProcess("未上报自行监测布点图"));
        mapper.batchUpdate(sRaActionReportsAuditResults);
    }

}
