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;

/**
 * 勤务时间(作业时间)
 * 勤务时间=总勤务时间-非作业时间
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategyQwsjService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategyQwsjService(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 getQwsjResultList(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()) &&
                !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> getQwsjResultList(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));

            List<SxProdDataSpcrStatisticsModel> zqwsjList = itemList.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());
            List<SxProdDataSpcrStatisticsModel> fzysjList = itemList.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());
            Double zqwsjStatistics = zqwsjList.stream().filter(o -> null != o).mapToDouble(o -> MathOperation.mul(getBetweenMin(o.getStartTime(), o.getEndTime(), 4), o.getWorkerQty())).sum();
            Double fzysjStatistics = fzysjList.stream().filter(o -> null != o).mapToDouble(o -> MathOperation.mul(getBetweenMin(o.getStartTime(), o.getEndTime(), 4), o.getWorkerQty())).sum();
            Double statistics = MathOperation.sub(zqwsjStatistics, fzysjStatistics);
            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;
    }
}
