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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterStrategyService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataSpcrStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterDataModel;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * 总勤务时间
 * 总勤务时间=常规生产时间（包含停机时间）*人数 +非常规生产时间（包含停机时间）*人数
 * 作业类型: 常规生产, 非常规生产
 * 生产日期+班次+生产线分组汇总
 * 总加班时间: 单位为Min,四舍五入保留四位小数,开始时间-结束时间=加班时间,加班时间*加班人数=总加班时间
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategyZqwsjService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategyZqwsjService(ISxProdDataToSoftAdapterStrategyService strategyService, SxProdDataToSoftAdapterCfgModel cfgModel) {
        this.strategyService = strategyService;
        this.cfgModel = cfgModel;
    }

    @Override
    public SxProdDataToSoftAdapterCfgModel getCfgModel() {
        return cfgModel;
    }

    @Override
    public ISxProdDataToSoftAdapterStrategyService getStrategyService() {
        return strategyService;
    }

    @Override
    public Object exec(SxProdDataToSoftAdapterDataModel data) {

        Map<String, List<SxProdDataSpcrStatisticsModel>> dataMap = getDataMap(data);

        if (CollectionUtils.isEmpty(dataMap)) return null;

        return getZqwsjResultList(dataMap);

    }

    @Override
    public Double getBetweenMin(String startTime, String endTime, int scale) { return strategyService.getBetweenMin(startTime, endTime, scale); }

    private Map<String, List<SxProdDataSpcrStatisticsModel>> getDataMap(SxProdDataToSoftAdapterDataModel data) {
        List<SxProdDataSpcrStatisticsModel> resultList = (List<SxProdDataSpcrStatisticsModel>) data.getObj();

        List<SxProdDataSpcrStatisticsModel> dataList = resultList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getProdType()) &&
                (MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_PROD.getValue() == o.getProdType() || MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_PROD.getValue() == o.getProdType()) &&
                !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getShiftCode()) && !StringUtils.isEmpty(o.getWorkCenterCode()) &&
                !StringUtils.isEmpty(o.getStartTime()) && !StringUtils.isEmpty(o.getEndTime()) && !StringUtils.isEmpty(o.getWorkerQty()))).collect(Collectors.toList());

        return CollectionUtils.isEmpty(dataList) ? null :
                dataList.stream().filter(o -> null != o).collect(Collectors.groupingBy(o -> new StringJoiner(MesExtConstWords.AND).add(o.getWorkTime()).add(o.getShiftCode()).add(o.getWorkCenterCode()).toString()));

    }

    private List<SxProdDataSpcrStatisticsModel> getZqwsjResultList(Map<String, List<SxProdDataSpcrStatisticsModel>> dataMap) {

        List<SxProdDataSpcrStatisticsModel> resultList = null;

        for(Map.Entry<String, List<SxProdDataSpcrStatisticsModel>> entry : dataMap.entrySet()) {
            if (null == entry) continue;

            List<SxProdDataSpcrStatisticsModel> itemList = entry.getValue();

            SxProdDataSpcrStatisticsModel result = new SxProdDataSpcrStatisticsModel(itemList.get(0));

            Double statistics = itemList.stream().filter(o -> null != o).mapToDouble(o -> MathOperation.mul(getBetweenMin(o.getStartTime(), o.getEndTime(), 4), o.getWorkerQty())).sum();
            result.setStatistics(formatPoint(statistics, null));

            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);

        }

        resultList = CollectionUtils.isEmpty(resultList) ? null :
                resultList.stream().sorted(Comparator.comparing(SxProdDataSpcrStatisticsModel::getWorkTime)
                        .thenComparing(SxProdDataSpcrStatisticsModel::getShiftCode)
                        .thenComparing(SxProdDataSpcrStatisticsModel::getWorkCenterCode)).collect(Collectors.toList());

        return resultList;
    }
}
