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

import cn.cecep.talroad.domain.OdsPermissionSharereportledgerinfoLedgerinfoA;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditRecord;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditResult;
import cn.cecep.talroad.mapper.OdsPermissionSharereportledgerinfoLedgerinfoAMapper;
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 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:22
 */
@Slf4j
@Service
public class LedgerManagementStatusTableService {
    @Autowired
    private SRaActionReportsAuditResultMapper mapper;

    @Autowired
    private SRaActionReportsAuditRecordMapper recordMapper;

    @Autowired
    private OdsPermissionSharereportledgerinfoLedgerinfoAMapper 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<OdsPermissionSharereportledgerinfoLedgerinfoA> fileQueryWrapper = new QueryWrapper<OdsPermissionSharereportledgerinfoLedgerinfoA>()
                .lambda().in(OdsPermissionSharereportledgerinfoLedgerinfoA::getReportid, reportIds);
        List<OdsPermissionSharereportledgerinfoLedgerinfoA> odsPermissionSharereportattachmentAS = sharereportattachmentAMapper.selectList(fileQueryWrapper);
        //过滤未填“是否完整”则不完整 报表id
        List<String> notFileReportIds = odsPermissionSharereportattachmentAS.stream()
                .filter(data -> !"Y".equals(data.getWhethercomplete()))
                .map(OdsPermissionSharereportledgerinfoLedgerinfoA::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.setAttachment("台账管理情况表填报不完整"));
        mapper.batchUpdate(sRaActionReportsAuditResults);
    }
}
