package com.hotent.project.manager.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.enterpriseDeclare.manager.CostSurveyTemplateUploadDataManager;
import com.hotent.project.manager.ComprehensiveStatisticsManager;
import com.hotent.project.manager.CostAnnualPlanManager;
import com.hotent.project.manager.CostProjectApprovalManager;
import com.hotent.project.manager.CostProjectTaskManager;
import com.hotent.project.model.CostProjectApproval;
import com.hotent.project.model.CostProjectTask;
import com.hotent.project.req.ComprehensiveStatisticsReq;
import com.hotent.project.vo.ComprehensiveStatisticsVo;
import com.hotent.project.vo.CostSurveysVo;
import com.hotent.project.vo.SurveysVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

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

/**
 *@author: zhao yue yue
 *@create: 2025-12-08 14:07
 */
@Service
public class ComprehensiveStatisticsManagerImpl implements ComprehensiveStatisticsManager {

    @Autowired
    private CostSurveyTemplateUploadDataManager costSurveyTemplateUploadDataManager;
    @Autowired
    private CostProjectApprovalManager costProjectApprovalManager;
    @Autowired
    private CostProjectTaskManager costProjectTaskManager;

    @Override
    public CommonResult<ComprehensiveStatisticsVo> analyze(ComprehensiveStatisticsReq req) {
        List<String> taskIds=new ArrayList<>();
        ComprehensiveStatisticsVo comprehensiveStatisticsVo = new ComprehensiveStatisticsVo();
        QueryWrapper<CostProjectApproval> projectWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(req.getProjectId())) {
            projectWrapper.eq("project_id",req.getProjectId());
        }
        if (StringUtil.isNotEmpty(req.getProjectName())) {
            projectWrapper.like("project_name",req.getProjectName());
        }
        if (StringUtil.isNotEmpty(req.getStartYear())) {
            projectWrapper.ge("audit_period",req.getStartYear());
        }
        if (StringUtil.isNotEmpty(req.getEndYear())) {
            projectWrapper.le("audit_period",req.getEndYear());
        }
        List<CostProjectApproval> projectApprovals = costProjectApprovalManager.list(projectWrapper);
        QueryWrapper<CostProjectTask> taskWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(req.getAuditedUnitId())) {
            taskWrapper.eq("audited_unit_id",req.getAuditedUnitId());
        }
        if (StringUtil.isNotEmpty(req.getAuditedUnitName())) {
            taskWrapper.like("audited_unit_name",req.getAuditedUnitName());
        }
        if (projectApprovals!=null && !projectApprovals.isEmpty()) {
            List<String> projectids = projectApprovals.stream().map(CostProjectApproval::getProjectId).distinct().collect(Collectors.toList());
            taskWrapper.in("project_id",projectids);
        }
        List<CostProjectTask> projectTasks = costProjectTaskManager.list(taskWrapper);
        taskIds.addAll(projectTasks.stream().map(CostProjectTask::getId).distinct().collect(Collectors.toList()));
        if(!taskIds.isEmpty()){
            List<CostSurveysVo> costSurveysVos= costSurveyTemplateUploadDataManager.analyze(taskIds);
            List<String> ids = costSurveysVos.stream().map(CostSurveysVo::getTaskId).distinct().collect(Collectors.toList());

            List<CostSurveysVo> costSurveysVoList = new ArrayList<>();
            //根据任务进行重组数据
            List<CostSurveysVo> costSurveysVoArrayList = new ArrayList<>();
            ids.forEach(i->{
                List<CostSurveysVo> surveysVos = costSurveysVos.stream().filter(c -> c.getTaskId().equals(i)).collect(Collectors.toList());
                List<String> rowids = surveysVos.stream().map(CostSurveysVo::getRowid).distinct().collect(Collectors.toList());
                rowids.forEach(r->{
                    List<CostSurveysVo>  costSurveysVos1=surveysVos.stream().filter(v -> v.getRowid().equals(r)).collect(Collectors.toList());
                    if (!costSurveysVos1.isEmpty()) {
                        CostSurveysVo costSurveysVo1 = costSurveysVos1.get(0);
                        List<SurveysVo> collect = costSurveysVos1.stream().map(csv -> {
                            SurveysVo surveysVo = new SurveysVo();
                            String nianfen = (String) csv.getNianfen();
                            if (nianfen != null && nianfen.length() >= 4) {
                                String year = nianfen.substring(0, 4);
                                surveysVo.setName(year);// 2024, 2025, 2023
                            }
                            surveysVo.setValue(csv.getRvalue());
                            return surveysVo;
                        }).collect(Collectors.toList());
                        costSurveysVo1.setSurveysVos(collect);
                        costSurveysVo1.setNianfen(null);
                        costSurveysVoArrayList.add(costSurveysVo1);
                    }
                });
                List<CostSurveysVo> vos = costSurveysVoArrayList.stream().filter(c -> c.getParentId().equals("-1")).collect(Collectors.toList());
                vos.forEach(v->{
                    List<CostSurveysVo> voList = costSurveysVoArrayList.stream().filter(su -> su.getParentId().equals(v.getRowid())).sorted(Comparator.comparing(CostSurveysVo::getOrderNum)).collect(Collectors.toList());
                    v.setCostSurveysVos(voList);
                });
                costSurveysVoList.addAll(vos);

            });
            comprehensiveStatisticsVo.setCostSurveysList(costSurveysVoList);
        }
        return CommonResult.<ComprehensiveStatisticsVo>ok().value(comprehensiveStatisticsVo);
    }
}
