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

import cn.estsh.i3plus.ext.mes.api.base.IShiftProdService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterStrategyService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProd;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOrgCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataFzylsyjhModel;
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.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.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 非作业率事业计划
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategyFzylsyjhService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategyFzylsyjhService(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<MesShiftProd> shiftProdList = getShiftProdList(data);

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

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

        List<SxProdDataFzylsyjhModel> resultList = packResultList(shiftProdList, org);

        return data.obj(resultList);
    }

    private List<MesShiftProd> getShiftProdList(SxProdDataToSoftAdapterDataModel data) {
        IShiftProdService shiftProdService = (IShiftProdService) SpringContextsUtil.getBean("shiftProdService");
        return shiftProdService.getShiftProdList(data.getOrganizeCode());
    }

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

    private List<SxProdDataFzylsyjhModel> packResultList(List<MesShiftProd> shiftProdList, SxOrgCfgModel org) {
        List<SxProdDataFzylsyjhModel> resultList = null;
        for (MesShiftProd item : shiftProdList) {
            if (null == item) continue;
            SxProdDataFzylsyjhModel result = new SxProdDataFzylsyjhModel(org.getOrg(), org.getOrgName());
            BeanUtils.copyProperties(item, result);
            result.setProdCategoryName(StringUtils.isEmpty(result.getProdCategory()) ? null : MesExtEnumUtil.SHIFT_PROD_CATEGORY.valueOfDescription(result.getProdCategory()));
            result.setIsProdTimeName(StringUtils.isEmpty(result.getIsProdTime()) ? null : MesExtEnumUtil.SHIFT_PROD_IS_PROD_TIME.valueOfDescription(result.getIsProdTime()));
            result.setProdTypeName(StringUtils.isEmpty(result.getProdType()) ? null : MesExtEnumUtil.SHIFT_PROD_TYPE.valueOfDescription(result.getProdType()));
            result.setPlanRateStr(formatPoint(StringUtils.isEmpty(result.getPlanRate()) ? new Double(0) : result.getPlanRate(), "%.8f"));
            result.setCreateDate(!StringUtils.isEmpty(result.getCreateDatetime()) ? result.getCreateDatetime().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(SxProdDataFzylsyjhModel::getCreateDatetime)).collect(Collectors.toList());
        return resultList;
    }

}
