package com.zcloud.service.eightwork.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.eightwork.*;
import com.zcloud.service.eightwork.EightWorkService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.ReturnMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zcloud.mapper.datasource.eightwork.EightWorkMapper;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author fangjiakai
 * @date 2024/01/19 14:35
 */
@Service
@Transactional //开启事物
public class EightWorkServiceImpl implements EightWorkService {
    @Autowired
    private EightWorkTaskFlowMapper eightWorkTaskFlowMapper;
    @Autowired
    private EightWorkTaskMapper eightWorkTaskMapper;
    @Autowired
    private EightWorkMapper eightworkMapper;


    @Override
    public List<PageData> getTaskSetps(PageData pd) {
        return eightWorkTaskFlowMapper.getTaskSetps(pd);
    }

    @Override
    public PageData checkWork(PageData pd) {
        return eightWorkTaskMapper.checkWorkCount(pd);
    }

    public ReturnMap getEightWorks()throws Exception{
        ReturnMap returnMap = new ReturnMap();

        // 循环获取最近6个月的年份和月份，并格式化为"YYYY-MM"格式
        TreeMap<String, List<PageData>> monthMap = new TreeMap<>();
        YearMonth currentYearMonth = YearMonth.now();
        for (int i = 5; i >= 0; i--) {
            YearMonth yearMonth = currentYearMonth.minusMonths(i);
            String formattedDate = yearMonth.format(DateTimeFormatter.ofPattern("yyyy-MM"));
            monthMap.computeIfAbsent(formattedDate, k -> new ArrayList<PageData>());
        }

        //统计数
        Map<String, List<PageData>> staMap = new HashMap<>();
        staMap.put("total", new ArrayList<>());
        staMap.put("flowing", new ArrayList<>());
        staMap.put("finished", new ArrayList<>());

        PageData corp = new PageData();
        corp.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        List<PageData> eightworks = eightworkMapper.getEightWorks(corp);

        if(eightworks != null && eightworks.size() > 0){
            eightworks.stream().forEach(e -> {
                staMap.get("total").add(e);
                if("99".equals(e.getString("NEXT_STEP_ID"))){
                    staMap.get("finished").add(e);
                    // 被打回 也统计 被删除与暂存 不计入 申请中
                    }else if(Integer.parseInt(e.getString("NEXT_STEP_ID")) > 1){
                    staMap.get("flowing").add(e);
                }

                if(monthMap.containsKey(e.getString("WORK_MONTH"))){
                    monthMap.get(e.getString("WORK_MONTH")).add(e);
                }

            });
        }

        TreeMap<String, Integer> monthMapCount = new TreeMap<>();
        monthMap.forEach((k, v) -> {
            monthMapCount.put(k, v.size());
        });

        Map<String, Integer> staMapCount = new HashMap<>();
        staMap.forEach((k, v) -> {
            staMapCount.put(k, v.size());
        });

        returnMap.put("eightworks", eightworks);
        returnMap.put("staMapCount", staMapCount);
        returnMap.put("monthMapCount", monthMapCount);
        return returnMap;
    }

    /**列表
     * @throws Exception
     */
    @Override
    public List<PageData> list(PageData pd)throws Exception{
        List<PageData> eightworks = eightworkMapper.getEightWorks(pd);
        List<PageData> returnList = eightworks.stream().filter(item -> {
            return !item.getString("NEXT_STEP_ID").equals("99") && item.getString("STATUS").equals("1");
        }).collect(Collectors.toList());
        return returnList;
    }
    public List<PageData> listAllForDiagnosis(PageData pd)throws Exception{
        return eightworkMapper.listAllForDiagnosis(pd);
    }

    @Override
    public List<PageData> getEightWorksByType(PageData pageData) {
        return eightworkMapper.getEightWorksByType(pageData);

    }

}
