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.platform.common.tool.MathOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Slf4j
public class JxCenterWorkingBoardProxyEnergyRateService extends JxCenterWorkingBoardCommonService 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) && (StringUtils.isEmpty(shiftCode) || 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) && (StringUtils.isEmpty(shiftCode) || o.getShiftCode().equals(shiftCode)) && o.getStartTime().compareTo(curTime) <= 0)).collect(Collectors.toList());

        List<MesShiftProdCenterRecord> prodList = getShiftProdCenterRecordListByFlag(filterList2ShiftProd, true);
        Double serviceTime = CollectionUtils.isEmpty(prodList) ? new Double(0) : prodList.stream().filter(o -> null != o).mapToDouble(o -> getStatisticsTime(o, curTime)).sum();
        dataMap.put("serviceTime", serviceTime);

        dataMap.put("standardTime", getStandardTime(filterList2OutPut, partStandardWorkMap));

        if (!dataMap.containsKey("onlyCalcRate")) {
            List<MesShiftProdCenterRecord> stopList = getShiftProdCenterRecordListByFlag(filterList2ShiftProd, false);
            Double nonWorkingTime = CollectionUtils.isEmpty(stopList) ? new Double(0) : stopList.stream().filter(o -> null != o).mapToDouble(o -> getStatisticsTime(o, curTime)).sum();
            dataMap.put("nonWorkingTime", nonWorkingTime);

            Double workingTime = MathOperation.sub(serviceTime, nonWorkingTime);
            dataMap.put("workingTime", workingTime);
        }

        return dataMap;
    }

}
