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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxOutPutStatisticsService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterStrategyService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOrgCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataScsjtsModel;
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.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * 生产实际台数
 * 查询条件: 更新时间
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategyScsjtsService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

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

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

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

    @Override
    public SxProdDataToSoftAdapterDataModel query(SxProdDataToSoftAdapterDataModel data) {

        List<MesOutPutStatistics> outPutStatisticsList = getOutPutStatisticsList(data);

        if (CollectionUtils.isEmpty(outPutStatisticsList)) return data;

        SxOrgCfgModel org = getOrganizeName(data.getOrganizeCode());

        Map<String, String> centerExtMap = getCenterExtMap(data.getOrganizeCode());

        Map<String, String> shiftMap = getShiftMap(data.getOrganizeCode());

        List<SxProdDataScsjtsModel> resultList = packResultList(outPutStatisticsList, org, centerExtMap, shiftMap);

        return data.obj(resultList);
    }

    private List<MesOutPutStatistics> getOutPutStatisticsList(SxProdDataToSoftAdapterDataModel data) {
        ISxOutPutStatisticsService outPutStatisticsService = (ISxOutPutStatisticsService) SpringContextsUtil.getBean("sxOutPutStatisticsService");
        return outPutStatisticsService.queryOutPutStatisticsList(packParam(data));
    }

    @Override
    public SxOrgCfgModel getOrganizeName(String organizeCode) { return strategyService.getOrganizeName(organizeCode); }

    @Override
    public Map<String, String> getCenterExtMap(String organizeCode) { return strategyService.getCenterExtMap(organizeCode); }

    @Override
    public Map<String, String> getShiftMap(String organizeCode) { return strategyService.getShiftMap(organizeCode); }

    private MesOutPutStatistics packParam(SxProdDataToSoftAdapterDataModel data) {
        MesOutPutStatistics param = new MesOutPutStatistics();
        param.setOrganizeCode(data.getOrganizeCode());
        param.setModifyDateTimeStart(data.getDate());
        param.setModifyDateTimeEnd(data.getDate());
        return param;
    }

    private List<SxProdDataScsjtsModel> packResultList(List<MesOutPutStatistics> outPutStatisticsList, SxOrgCfgModel org, Map<String, String> centerExtMap, Map<String, String> shiftMap) {
        List<SxProdDataScsjtsModel> resultList = null;
        for (MesOutPutStatistics item : outPutStatisticsList) {
            if (null == item) continue;
            SxProdDataScsjtsModel result = new SxProdDataScsjtsModel(org.getOrg(), org.getOrgName());
            BeanUtils.copyProperties(item, result);
            String[] centerArr = centerExtMap.get(result.getWorkCenterCode()).split(MesExtConstWords.AND);
            if (null != centerArr && centerArr.length >= 1) result.setWorkCenterName(centerArr[0]);
            if (null != centerArr && centerArr.length >= 2) result.setIsDont(centerArr[1]);
            if (null != centerArr && centerArr.length >= 3) result.setDontClass(centerArr[2]);
            result.setShiftName((StringUtils.isEmpty(result.getShiftCode()) || StringUtils.isEmpty(result.getWorkCenterCode())) ? null :
                    shiftMap.get(new StringJoiner(MesExtConstWords.AND).add(result.getWorkCenterCode()).add(result.getShiftCode()).toString()));
            result.setWorkOrderTypeName(StringUtils.isEmpty(result.getWorkOrderType()) ? null : MesExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(result.getWorkOrderType()));
            result.setPlanQtyInt(StringUtils.isEmpty(result.getPlanQty()) ? 0 : result.getPlanQty().intValue());
            result.setQtyInt(StringUtils.isEmpty(result.getQty()) ? 0 : result.getQty().intValue());
            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);
        }
        resultList = CollectionUtils.isEmpty(resultList) ? null :
                resultList.stream().filter(o -> null != o).sorted(Comparator.comparing(SxProdDataScsjtsModel::getModifyDatetime)).collect(Collectors.toList());
        return resultList;
    }

}
