package org.jeecg.modules.stm.audit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.audit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.jeecg.modules.stm.audit.entity.AuditAct;
import org.jeecg.modules.stm.audit.entity.AuditActRelOthers;
import org.jeecg.modules.stm.audit.entity.AuditReportParam;

import org.jeecg.modules.stm.base.lable.service.IStLableService;
import org.jeecg.modules.stm.utils.StmConstans;

import jakarta.servlet.http.HttpServletRequest;
import java.util.*;

  
@RestController
@RequestMapping("/audit/report")
@Slf4j
public class AuditReportController {

    @Autowired
    private IAuditActRelOthersService stActRelOthersService;
    @Autowired
    private IAuditLabelRelLibService stLabelRelLibService;
    @Autowired
    private IStLableService stLableService;
    @Autowired
    private IAuditComplianeItemService auditComplianeItemService;
    @Autowired
    private IAuditItemRelCheckService auditItemRelCheckService;
    @Autowired
    private IAuditFileContentCategoriesService auditFileContentCategoriesService;
    @Autowired
    private IAuditActService auditActService;

    @GetMapping(value = "/queryAllDataReport")
    public Result<Map<String,Object>> queryAllDataReport(AuditReportParam params, HttpServletRequest req) {

        List<Integer> actIdList = params.getActIds();
        List<Integer> checkIdList = params.getCheckIds();
        List<Integer> problemsIdList= params.getProblemIds();
        if(actIdList == null) actIdList = new ArrayList<>();

        if(checkIdList!=null&&checkIdList.size()>0) {
            List<Integer>  checkActIdList = getActIdByTids(StmConstans.Audit_TP_CHECK_METHOD,checkIdList);
            if(checkActIdList!=null && checkActIdList.size()>0) {
                actIdList.addAll(checkActIdList);
            }
        }
        if(problemsIdList!=null&&problemsIdList.size()>0) {
            List<Integer>  problemActIdList = getActIdByTids(StmConstans.Audit_TP_PROBLEM,problemsIdList);
            if(problemActIdList!=null && problemActIdList.size()>0) {
                actIdList.addAll(problemActIdList);
            }
        }

        Set<Integer> setIdList = new HashSet<>(actIdList);
        actIdList = new ArrayList<>(setIdList);
        List<AuditAct> actList =  auditActService.listByIds(actIdList);
        List<AuditActRelOthers> relOthers =  getActRelOthersByActIds(actIdList);
        Map<Integer,List<AuditActRelOthers>>  tmpTpMap = getTpIdsByListData(relOthers);

        Map<Integer,Map<Integer,List<Map<String,Object>>>> retMap  = new HashMap<>();
        tmpTpMap.forEach((key, value) -> {
            List<Integer> tpTidList =  new ArrayList<>();
            Map<Integer,List<Integer>> actRelTids = new HashMap<>();
            for(AuditActRelOthers rel:value) {
                tpTidList.add(rel.getTid());
                Integer actId = rel.getActId();
                List<Integer> tmpList = actRelTids.get(actId);
                if(tmpList == null) {
                    tmpList = new ArrayList<>();
                }
                tmpList.add(rel.getTid());
                actRelTids.put(actId,tmpList);
            }
            List<Map<String,Object>> datalist= getLibDatas(key, tpTidList);
           
            Map<Integer,Map<String,Object>> mapRetData = new HashMap<>();
            for(Map<String,Object> rel:datalist) {
                Integer id = Integer.valueOf(rel.get("id").toString());
                mapRetData.put(id,rel);
            }
            Map<Integer,List<Map<String,Object>>>  actDatasMap = new HashMap<>();
            actRelTids.forEach((actId, values) -> {
                List<Map<String,Object>> tmpList = new ArrayList<>();
                for(Integer tid:values) {
                    if(mapRetData.get(tid)!=null){
                        tmpList.add(mapRetData.get(tid));
                    }
                }
                if(tmpList!=null&&tmpList.size()>0) {
                    actDatasMap.put(actId,tmpList);
                }
            });

            if(key==StmConstans.Audit_TP_CHECK_METHOD){
                Map<Integer,List<Map<String,Object>>>  actDatasMap2 = new HashMap<>();
                actRelTids.forEach((actId, values) -> {
                    List<Map<String,Object>> tmpList = new ArrayList<>();
                    for(Integer tid:values) {
                        Map<String,Object> tt = getComlianceData(mapRetData.get(tid));
                        if(tt==null) continue;
                        if(tmpList.contains(tt))continue;
                        tmpList.add(tt);
                    }
                    if(tmpList!=null&&tmpList.size()>0) {
                        actDatasMap2.put(actId,tmpList);
                    }

                });
                if(actDatasMap2.size()>0) {
                    retMap.put(StmConstans.Audit_TP_COMPLIANE_ITEM, actDatasMap2);
                }
            }
            if(actDatasMap.size()>0) {
                retMap.put(key,actDatasMap);
            }

        });

        Map<String,Object> resDataMap = new HashMap<>();
        resDataMap.put("datas",retMap);
        resDataMap.put("acts",actList);
        return Result.OK(resDataMap);
    }

    /**
     *根据tidList 查找actId
     *
     */
    private List<Integer> getActIdByTids(Integer tp,List<Integer> tidList) {
        List<Integer> actIds = new ArrayList<>();
        QueryWrapper<AuditActRelOthers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tp",tp).in("tid",tidList);
        List<AuditActRelOthers> list = stActRelOthersService.list(queryWrapper);
        for(AuditActRelOthers rel:list){
            actIds.add(rel.getActId());
        }
        return actIds;
    }

    /**
     *根据actId 查找 tid
     * return tp 对应的 ids
     */
    private Map<Integer,List<AuditActRelOthers>> getTpIdsByListData(List<AuditActRelOthers> list ) {
        Map<Integer,List<AuditActRelOthers>> retMap = new HashMap<>();
        for (AuditActRelOthers rel : list) {
            List<AuditActRelOthers> tmpList = retMap.get(rel.getTp());
            if (tmpList == null) {
                tmpList = new ArrayList<>();
                tmpList.add(rel);
            } else {
                if(!tmpList.contains(rel)){
                    tmpList.add(rel);
                }
            }
            retMap.put(rel.getTp(), tmpList);
        }
        return retMap;
    }

    /**
     *根据actId 查找 tid
     * return actid 对应的 tids
     */
    private Map<Integer,List<AuditActRelOthers>> getActTidsByListData(List<AuditActRelOthers> list ) {
        Map<Integer,List<AuditActRelOthers>> retMap = new HashMap<>();
        for (AuditActRelOthers rel : list) {
            List<AuditActRelOthers> tmpList = retMap.get(rel.getActId());
            Integer tid = rel.getTid();
            if (tmpList == null) {
                tmpList = new ArrayList<>();
                tmpList.add(rel);
            } else {
                if (!tmpList.contains(rel)) {
                    tmpList.add(rel);
                }
            }
            retMap.put(rel.getActId(), tmpList);
        }
        return retMap;
    }


    private List<AuditActRelOthers> getActRelOthersByActIds(List<Integer> actIdList) {
        Map<Integer,List<Integer>> retMap = new HashMap<>();
        QueryWrapper<AuditActRelOthers> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("act_id",actIdList);

        List<AuditActRelOthers> list = stActRelOthersService.list(queryWrapper);
        return list;
    }

    private  List<Map<String,Object>> getLibDatas(Integer tp,List<Integer> idList) {
        List<Map<String,Object>> retList = new ArrayList<>();
        if(tp==1) {
            retList =  stActRelOthersService.queryActAndComplianeItems(idList);
        } else if(tp==2) {
            retList =  stActRelOthersService.queryActAndInnerStrategyItems(idList);
        } else if(tp==3) {
            retList =  stActRelOthersService.queryActAndInnerControlItems(idList);
        } else if(tp==4) {
            retList =  stActRelOthersService.queryActAndProblemItems(idList);
        } else if(tp==5) {
            retList =  stActRelOthersService.queryActAndCheckItems(idList);
        } else if(tp==6) {
            retList =  stActRelOthersService.queryActAndRiskItems(idList);
        } else if(tp==7) {
            retList =  stActRelOthersService.queryActAndPracticeItems(idList);
        } else if(tp==8) {
            retList =  stActRelOthersService.queryActAndRiskScenarioItems(idList);
        } else if(tp==9) {
            retList =  stActRelOthersService.queryAllAct();
        }
        return retList;
    }

    private Map<String,Object> getComlianceData(Map<String,Object> checkMap) {
        Map<String,Object> tmpMap  = new HashMap<>();
        if(checkMap==null) return null;
        tmpMap.put("id",checkMap.get("citemid")) ;
        tmpMap.put("item",checkMap.get("item")) ;
        tmpMap.put("content",checkMap.get("itemContent")) ;
        tmpMap.put("cgrp",checkMap.get("cgrp")) ;
        tmpMap.put("cgrpPath",checkMap.get("cgrpPath"));
        return tmpMap;
    }

}







