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

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxCenterWorkingBoardService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
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.platform.common.tool.TimeTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    private IJxCenterWorkingBoardService proxyService;

    public JxCenterWorkingBoardProxyEnergyRateService() {}

    public JxCenterWorkingBoardProxyEnergyRateService(IJxCenterWorkingBoardService proxyService) {
        this.proxyService = proxyService;
    }

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

        if (null != proxyService) proxyService.queryCenterWorkingBoardData(dataMap);

        String workCenterCode = (String) dataMap.get(MesExtConstWords.WORK_CENTER_CODE);
        String workTime = (String) dataMap.get(MesExtConstWords.WORK_TIME);
        String shiftCode = (String) dataMap.get(MesExtConstWords.SHIFT_CODE);
        String curTime = (String) dataMap.get("curTime");
        List<MesShiftProdCenterRecord> shiftProdCenterRecordList = (List<MesShiftProdCenterRecord>) dataMap.get(MesShiftProdCenterRecord.class.getSimpleName());
        List<MesOutPutStatistics> outPutStatisticsList = (List<MesOutPutStatistics>) dataMap.get(MesOutPutStatistics.class.getSimpleName());
        Map<String, String> partStandardWorkMap = (Map<String, String>) dataMap.get(MesExtConstWords.STANDARD_WORK);

        List<MesOutPutStatistics> filterList2OutPut = CollectionUtils.isEmpty(outPutStatisticsList) ? null :
                outPutStatisticsList.stream().filter(o -> (null != o && o.getWorkCenterCode().equals(workCenterCode) && o.getWorkTime().equals(workTime) && o.getShiftCode().equals(shiftCode))).collect(Collectors.toList());
        List<MesShiftProdCenterRecord> filterList2ShiftProd = CollectionUtils.isEmpty(shiftProdCenterRecordList) ? null :
                shiftProdCenterRecordList.stream().filter(o -> (null != o && o.getWorkCenterCode().equals(workCenterCode) && o.getWorkTime().equals(workTime) && o.getShiftCode().equals(shiftCode) && o.getStartTime().compareTo(curTime) <= 0)).collect(Collectors.toList());

        List<MesShiftProdCenterRecord> stopList = getShiftProdDataListByFlag(filterList2ShiftProd, false);
        List<MesShiftProdCenterRecord> prodList = getShiftProdDataListByFlag(filterList2ShiftProd, true);

        Double serviceTime = CollectionUtils.isEmpty(prodList) ? new Double(0) : prodList.stream().filter(o -> null != o).mapToDouble(o -> getStatisticsTime(o, curTime)).sum();
        Double nonWorkingTime = CollectionUtils.isEmpty(stopList) ? new Double(0) : stopList.stream().filter(o -> null != o).mapToDouble(o -> getStatisticsTime(o, curTime)).sum();
        Double standardTime = CollectionUtils.isEmpty(filterList2OutPut) ? new Double(0) : filterList2OutPut.stream().filter(o -> null != o).mapToDouble(o -> getStatisticsTime(o, partStandardWorkMap)).sum();
        Double workingTime = MathOperation.sub(serviceTime, nonWorkingTime);

        dataMap.put("serviceTime", serviceTime);
        dataMap.put("nonWorkingTime", nonWorkingTime);
        dataMap.put("standardTime", standardTime);
        dataMap.put("workingTime", workingTime);

        return dataMap;
    }

    private List<MesShiftProdCenterRecord> getShiftProdDataListByFlag(List<MesShiftProdCenterRecord> shiftProdCenterRecordList, Boolean flag) {
        if (CollectionUtils.isEmpty(shiftProdCenterRecordList)) return null;
        if (!flag) return shiftProdCenterRecordList.stream().filter(o -> (null != o && (MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_STOP_CENTER.getValue() == o.getProdType() || MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_STOP_CENTER.getValue() == o.getProdType()))).collect(Collectors.toList());
        else return shiftProdCenterRecordList.stream().filter(o -> (null != o && (MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_PROD.getValue() == o.getProdType() || MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_PROD.getValue() == o.getProdType()))).collect(Collectors.toList());
    }

    private Double getStatisticsTime(MesShiftProdCenterRecord shiftProdCenterRecord, String curTime) {
        try {
            String endTime = shiftProdCenterRecord.getEndTime().compareTo(curTime) <= 0 ? shiftProdCenterRecord.getEndTime() : curTime;
            return MathOperation.mul(new Double(TimeTool.getSecoundsBetweenTime(2, shiftProdCenterRecord.getStartTime(), endTime)), shiftProdCenterRecord.getWorkerQty());
        } catch (ParseException e) {
            return new Double(0);
        }
    }

    private Double getStatisticsTime(MesOutPutStatistics outPutStatistics, Map<String, String> partStandardWorkMap) {
        return MathOperation.mul(outPutStatistics.getQty(), ((!CollectionUtils.isEmpty(partStandardWorkMap) && partStandardWorkMap.containsKey(outPutStatistics.getPartNo())) ? Double.parseDouble(partStandardWorkMap.get(outPutStatistics.getPartNo())) : new Double(0)));
    }

}
