package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.api.base.IMesAccidentDateService;
import cn.estsh.i3plus.ext.mes.api.base.IMesNumberOfSafeOperationDaysService;
import cn.estsh.i3plus.ext.mes.api.base.bu.IBusiConfigService;
import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxCenterWorkingBoardService;
import cn.estsh.i3plus.ext.mes.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pojo.model.MesAccidentDateModel;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxCenterWorkingBoardBusiModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesOutPutStatisticsRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPlantInfoRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Slf4j
public class JxCenterWorkingBoardMonitorOneService extends JxCenterWorkingBoardCommonService implements IJxCenterWorkingBoardService {

    private IJxCenterWorkingBoardService proxyService0;

    private IJxCenterWorkingBoardService proxyService1;

    private IJxCenterWorkingBoardService proxyService;

    private MesPlantInfoRepository plantInfoRepository;

    private IMesNumberOfSafeOperationDaysService numberOfSafeOperationDaysService;

    private IMesAccidentDateService accidentDateService;

    private IBusiConfigService busiConfigService;

    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    public JxCenterWorkingBoardMonitorOneService(IJxCenterWorkingBoardService proxyService0, IJxCenterWorkingBoardService proxyService1, IJxCenterWorkingBoardService proxyService) {
        this.proxyService0 = proxyService0;
        this.proxyService1 = proxyService1;
        this.proxyService = proxyService;
        this.plantInfoRepository = (MesPlantInfoRepository) SpringContextsUtil.getBean("mesPlantInfoRepository");
        this.numberOfSafeOperationDaysService = (IMesNumberOfSafeOperationDaysService) SpringContextsUtil.getBean("mesNumberOfSafeOperationDaysService");
        this.accidentDateService = (IMesAccidentDateService) SpringContextsUtil.getBean("mesAccidentDateService");
        this.busiConfigService = (IBusiConfigService) SpringContextsUtil.getBean("busiConfigService");
        this.outPutStatisticsRepository = (MesOutPutStatisticsRepository) SpringContextsUtil.getBean("mesOutPutStatisticsRepository");
    }

    @Override
    public Map<String, Object> queryCenterWorkingBoardData(Map<String, Object> dataMap) {

        dataMap = proxyService0.queryCenterWorkingBoardData(dataMap);

        List<MesWorkCenterExt> workCenterExtDbList = (List<MesWorkCenterExt>) dataMap.get(MesWorkCenterExt.class.getSimpleName());

        packProxyCondition(dataMap, workCenterExtDbList);

        dataMap = proxyService.queryCenterWorkingBoardData(dataMap);

        String organizeCode = (String) dataMap.get(MesExtConstWords.ORGANIZE_CODE);

        dataMap.put("BOARD_TITLE", "PKATJX智能工场监控中心");

        dataMap.put("BOARD_BOTTOM", "PKATJX·松下厨电科技（嘉兴）有限公司");

        dataMap.put("工厂信息总览", getOrganizeInfo(organizeCode));

        dataMap.put("累计安全运行天数", getAmountSafeRunningDays(organizeCode));

        dataMap.put("当年累计总事故数", getCurYearAmountAccident(organizeCode));

        dataMap.put("生产进度", getProductionSchedule(organizeCode, workCenterExtDbList, dataMap));

        dataMap.put("本体产线状态", getOntologyLineWorkingStatus(organizeCode, workCenterExtDbList, dataMap));

        dataMap.put("本体能率", "");

        return clearUselessData(dataMap);
    }

    @Override
    public Map<String, Object> clearUselessData(Map<String, Object> dataMap) {
        dataMap.remove(MesWorkCenterExt.class.getSimpleName());
        dataMap.remove(MesWorkOrder.class.getSimpleName());
        dataMap.remove(MesOutPutStatistics.class.getSimpleName());
        dataMap.remove(MesExtConstWords.START_TIME);
        dataMap.remove(MesExtConstWords.END_TIME);
        dataMap.remove(MesExtConstWords.WORK_TIME);
        dataMap.remove(MesExtConstWords.SHIFT_CODE);
        dataMap.remove(MesExtConstWords.WORK_CENTER_CODE);
        return dataMap;
    }

    private void packProxyCondition(Map<String, Object> dataMap, List<MesWorkCenterExt> workCenterExtDbList) {
        List<String> workTimeList = workCenterExtDbList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getShiftCode()))).map(MesWorkCenterExt::getWorkTime).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(workTimeList)) return;
        Collections.sort(workTimeList);
        dataMap.put(MesExtConstWords.START_TIME, workTimeList.get(0));
        dataMap.put(MesExtConstWords.END_TIME, workTimeList.get(workTimeList.size() - 1));
    }

    private MesPlantInfo getOrganizeInfo(String organizeCode) {
        return plantInfoRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
    }

    private MesNumberOfSafeOperationDays getAmountSafeRunningDays(String organizeCode) {
        return numberOfSafeOperationDaysService.queryNumberOfSafeOperationDays(organizeCode);
    }

    private MesAccidentDateModel getCurYearAmountAccident(String organizeCode) {
        return accidentDateService.queryAccidentDaysBoard(organizeCode);
    }

    private Map<String, List<MesWorkOrder>> getCenterMap4Order(List<MesWorkOrder> workOrderList) {
        return CollectionUtils.isEmpty(workOrderList) ? null : workOrderList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesWorkOrder::getWorkCenterCode));
    }

    private Map<String, List<MesOutPutStatistics>> getCenterMap4OutPut(List<MesOutPutStatistics> outPutStatisticsList) {
        return CollectionUtils.isEmpty(outPutStatisticsList) ? null : outPutStatisticsList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesOutPutStatistics::getWorkCenterCode));
    }

    private JxCenterWorkingBoardBusiModel getProductionSchedule(String organizeCode, List<MesWorkCenterExt> workCenterExtDbList, Map<String, Object> dataMap) {

        JxCenterWorkingBoardBusiModel psData = new JxCenterWorkingBoardBusiModel().title("生产进度");

        List<MesWorkOrder> workOrderList = (List<MesWorkOrder>) dataMap.get(MesWorkOrder.class.getSimpleName());
        List<MesOutPutStatistics> outPutStatisticsList = (List<MesOutPutStatistics>) dataMap.get(MesOutPutStatistics.class.getSimpleName());
        if (CollectionUtils.isEmpty(workOrderList) && CollectionUtils.isEmpty(outPutStatisticsList)) return psData;

        MesBusiConfig catagoryCodeCfg = busiConfigService.getBusiConfigByCfgCode(organizeCode, MesExtConstWords.SX_CATEGORY_CFG);
        if (null == catagoryCodeCfg || StringUtils.isEmpty(catagoryCodeCfg.getCfgValue()) || StringUtils.isEmpty(catagoryCodeCfg.getCfgValueSeq())) return psData;

        String[] categoryCodeArr = catagoryCodeCfg.getCfgValueSeq().split(MesExtConstWords.COMMA);
        String[] categoryNameArr = catagoryCodeCfg.getCfgValue().split(MesExtConstWords.COMMA);
        if (categoryCodeArr.length != categoryNameArr.length) return psData;

        Map<String, List<MesWorkOrder>> centerMap4OrderFilter = new HashMap<>();
        Map<String, List<MesOutPutStatistics>> centerMap4OutPutFilter = new HashMap<>();

        Map<String, List<MesWorkOrder>> centerMap4Order = getCenterMap4Order(workOrderList);
        Map<String, List<MesOutPutStatistics>> centerMap4OutPut = getCenterMap4OutPut(outPutStatisticsList);

        Map<String, List<MesWorkOrder>> categoryCodeMap4Order = new HashMap<>();
        Map<String, List<MesOutPutStatistics>> categoryCodeMap4OutPut = new HashMap<>();

        for (MesWorkCenterExt workCenterExtDb : workCenterExtDbList) {
            if (null == workCenterExtDb) continue;
            if (StringUtils.isEmpty(workCenterExtDb.getCategoryCode()) || StringUtils.isEmpty(workCenterExtDb.getWorkTime()) || StringUtils.isEmpty(workCenterExtDb.getShiftCode())) continue;

            List<MesWorkOrder> workOrderList4Center = CollectionUtils.isEmpty(centerMap4Order) ? null : getWorkOrderListByWorkTime(centerMap4Order.get(workCenterExtDb.getWorkCenterCode()), workCenterExtDb.getWorkTime());
            List<MesOutPutStatistics> outPutStatisticsList4Center = CollectionUtils.isEmpty(centerMap4OutPut) ? null : getOutPutStatisticsListByWorkTime(centerMap4OutPut.get(workCenterExtDb.getWorkCenterCode()), workCenterExtDb.getWorkTime());

            List<MesWorkOrder> categoryCodeList4Order = (!CollectionUtils.isEmpty(categoryCodeMap4Order) && categoryCodeMap4Order.containsKey(workCenterExtDb.getCategoryCode())) ? categoryCodeMap4Order.get(workCenterExtDb.getCategoryCode()) : new ArrayList<>();
            List<MesOutPutStatistics> categoryCodeList4OutPut = (!CollectionUtils.isEmpty(categoryCodeMap4OutPut) && categoryCodeMap4OutPut.containsKey(workCenterExtDb.getCategoryCode())) ? categoryCodeMap4OutPut.get(workCenterExtDb.getCategoryCode()) : new ArrayList<>();
            if (!CollectionUtils.isEmpty(workOrderList4Center)) categoryCodeList4Order.addAll(workOrderList4Center);
            if (!CollectionUtils.isEmpty(outPutStatisticsList4Center)) categoryCodeList4OutPut.addAll(outPutStatisticsList4Center);

            categoryCodeMap4Order.put(workCenterExtDb.getCategoryCode(), categoryCodeList4Order);
            categoryCodeMap4OutPut.put(workCenterExtDb.getCategoryCode(), categoryCodeList4OutPut);

            if (!CollectionUtils.isEmpty(workOrderList4Center)) centerMap4OrderFilter.put(workCenterExtDb.getWorkCenterCode(), workOrderList4Center);
            if (!CollectionUtils.isEmpty(outPutStatisticsList4Center)) centerMap4OutPutFilter.put(workCenterExtDb.getWorkCenterCode(), sortOutPutStatisticsList(outPutStatisticsList4Center));
        }

        for (int i = 0; i < categoryCodeArr.length; i ++) {
            List<MesWorkOrder> workOrderList4Category = CollectionUtils.isEmpty(categoryCodeMap4Order) ? null : categoryCodeMap4Order.get(categoryCodeArr[i]);
            List<MesOutPutStatistics> outPutStatisticsList4Category = CollectionUtils.isEmpty(categoryCodeMap4OutPut) ? null : categoryCodeMap4OutPut.get(categoryCodeArr[i]);

            Double planQtyAmount = getWorkOrderList2AmountQty(workOrderList4Category);
            Double completeQtyAmount = getOutPutStatisticsList2AmountQty(outPutStatisticsList4Category);

            psData.addChartData(categoryNameArr[i], completeQtyAmount, planQtyAmount, getBigDecimalPercent(divThenMul100Format1RoundHalfUp(completeQtyAmount, planQtyAmount)));
        }

        dataMap.put(MesWorkOrder.class.getSimpleName(), centerMap4OrderFilter);
        dataMap.put(MesOutPutStatistics.class.getSimpleName(), centerMap4OutPutFilter);

        return psData;
    }

    private JxCenterWorkingBoardBusiModel getOntologyLineWorkingStatus(String organizeCode, List<MesWorkCenterExt> workCenterExtDbList, Map<String, Object> dataMap) {

        JxCenterWorkingBoardBusiModel psData = new JxCenterWorkingBoardBusiModel().title("本体产线状态");

        List<MesBusiConfig> busiConfigList = busiConfigService.getConfigListSortSeq(organizeCode, MesExtConstWords.BOARD_WORK_SHOW_FLAG);
        if (CollectionUtils.isEmpty(busiConfigList)) return psData;

        Map<String, MesWorkCenterExt> workCenterExtDbMap = workCenterExtDbList.stream().filter(o -> null != o).collect(Collectors.toMap(MesWorkCenterExt::getWorkCenterCode, o -> o));

        Map<String, List<MesOutPutStatistics>> centerMap4OutPutFilter = (Map<String, List<MesOutPutStatistics>>) dataMap.get(MesOutPutStatistics.class.getSimpleName());
        List<MesRepairTask> repairTaskList = (List<MesRepairTask>) dataMap.get(MesRepairTask.class.getSimpleName());

        for (MesBusiConfig busiConfig : busiConfigList) {
            if (null == busiConfig || !workCenterExtDbMap.containsKey(busiConfig.getCfgValue())) continue;
            MesWorkCenterExt workCenterExtDb = workCenterExtDbMap.get(busiConfig.getCfgValue());
            if (StringUtils.isEmpty(workCenterExtDb.getLineType()) || !workCenterExtDb.getLineType().equals(MesExtEnumUtil.LINE_TYPE.ONTOLOGY_LINE.getValueStr())) continue;
            dataMap.put(MesExtConstWords.WORK_TIME, workCenterExtDb.getWorkTime());
            dataMap.put(MesExtConstWords.SHIFT_CODE, workCenterExtDb.getShiftCode());
            dataMap.put(MesExtConstWords.WORK_CENTER_CODE, workCenterExtDb.getWorkCenterCode());
            MesOutPutStatistics outPutStatistics = (!CollectionUtils.isEmpty(centerMap4OutPutFilter) && centerMap4OutPutFilter.containsKey(workCenterExtDb.getWorkCenterCode())) ? centerMap4OutPutFilter.get(workCenterExtDb.getWorkCenterCode()).get(0) : getOutPutStatisticsByCenter(organizeCode, workCenterExtDb.getWorkCenterCode());
            List<MesOutPutStatistics> outPutStatisticsListFilter = (null == outPutStatistics || CollectionUtils.isEmpty(centerMap4OutPutFilter)) ? null : centerMap4OutPutFilter.get(workCenterExtDb.getWorkCenterCode());
            outPutStatisticsListFilter = CollectionUtils.isEmpty(outPutStatisticsListFilter) ? null : outPutStatisticsListFilter.stream().filter(o -> (null != o && o.getWorkOrderNo().equals(outPutStatistics.getWorkOrderNo()))).collect(Collectors.toList());
            psData.addChartData(workCenterExtDb.getWorkCenterCode(), null == outPutStatistics ? null : outPutStatistics.getPartNo(), getRepairRate(outPutStatisticsListFilter, repairTaskList, workCenterExtDb), proxyService1.queryCenterWorkingBoardData(dataMap).get("生产状态"));
        }

        return psData;
    }

    private String getRepairRate(List<MesOutPutStatistics> outPutStatisticsList, List<MesRepairTask> repairTaskList, MesWorkCenterExt workCenterExtDb) {
        return (CollectionUtils.isEmpty(outPutStatisticsList) || StringUtils.isEmpty(workCenterExtDb.getWorkTime())) ? getBigDecimalPercent(format1RounHalfUp(new Double(0))) : calcRepairRate(outPutStatisticsList, repairTaskList, workCenterExtDb);
    }

    private String calcRepairRate(List<MesOutPutStatistics> outPutStatisticsList, List<MesRepairTask> repairTaskList, MesWorkCenterExt workCenterExtDb) {
        Double repairQty = CollectionUtils.isEmpty(repairTaskList) ? new Double(0) : getListSizeD(repairTaskList.stream().filter(o -> (null != o && o.getWorkOrderNo().equals(outPutStatisticsList.get(0).getWorkOrderNo()) && o.getWorkTime().equals(workCenterExtDb.getWorkTime()))).collect(Collectors.toList()));
        Double qty = getOutPutStatisticsList2AmountQty(outPutStatisticsList);
        return getBigDecimalPercent(format1RounHalfUp(MathOperation.sub(new Double(100), MathOperation.div(qty, MathOperation.add(qty, repairQty)))));
    }

    private MesOutPutStatistics getOutPutStatisticsByCenter(String organizeCode, String workCenterCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(workCenterCode, MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.getValue(), MesExtConstWords.WORK_ORDER_TYPE, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
        return outPutStatisticsRepository.getByProperty(packBean);
    }
}
