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.api.busi.bu3.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOrgCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataJhsctsModel;
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.pojo.mes.bean.MesWorkOrder;
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.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 计划生产台数
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategyJhsctsService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategyJhsctsService(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<MesWorkOrder> workOrderList = getWorkOrderList(data);

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

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

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

        List<SxProdDataJhsctsModel> resultList = packResultList(workOrderList, org, centerExtMap);

        return data.obj(resultList);
    }

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

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

    private List<MesWorkOrder> getWorkOrderList(SxProdDataToSoftAdapterDataModel data) {
        ISxWorkOrderExtService workOrderExtService = (ISxWorkOrderExtService) SpringContextsUtil.getBean("sxWorkOrderExtService");
        return workOrderExtService.queryWorkOrderListByModifyDatetime(packParam(data));
    }

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

    private List<SxProdDataJhsctsModel> packResultList(List<MesWorkOrder> workOrderList, SxOrgCfgModel org, Map<String, String> centerExtMap) {
        List<SxProdDataJhsctsModel> resultList = null;
        for (MesWorkOrder item : workOrderList) {
            if (null == item || StringUtils.isEmpty(item.getWorkCenterCode())) continue;
            SxProdDataJhsctsModel result = new SxProdDataJhsctsModel(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.setWorkOrderNo(item.getOrderNo());
            result.setWorkOrderTypeName(StringUtils.isEmpty(result.getWorkOrderType()) ? null : MesExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(result.getWorkOrderType()));
            result.setQtyInt(StringUtils.isEmpty(result.getQty()) ? 0 : result.getQty().intValue());
            result.setCompleteQtyInt(StringUtils.isEmpty(result.getCompleteQty()) ? 0 : result.getCompleteQty().intValue());
            result.setStartTimeYmd(!StringUtils.isEmpty(result.getStartTime()) ? result.getStartTime().substring(0, 10) : null);
            result.setCreateDate(!StringUtils.isEmpty(result.getCreateDatetime()) ? result.getCreateDatetime().substring(0, 10) : null);
            result.setModifyDate(!StringUtils.isEmpty(result.getModifyDatetime()) ? result.getModifyDatetime().substring(0, 10) : null);
            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);
        }
        resultList = CollectionUtils.isEmpty(resultList) ? null :
                resultList.stream().filter(o -> null != o).sorted(Comparator.comparing(SxProdDataJhsctsModel::getModifyDatetime)).collect(Collectors.toList());
        return resultList;
    }

}
