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.ISxShiftProdCenterRecordService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOrgCfgModel;
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.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 停开机记录通用统计查询
 * 查询条件: 创建时间
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategySpcrStatisticsService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategySpcrStatisticsService(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<MesShiftProdCenterRecord> shiftProdCenterRecordList = getShiftProdCenterRecordList(data);

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

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

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

        List<SxProdDataSpcrStatisticsModel> resultList = packResultList(shiftProdCenterRecordList, org, centerExtMap);

        return data.obj(resultList);
    }

    private List<MesShiftProdCenterRecord> getShiftProdCenterRecordList(SxProdDataToSoftAdapterDataModel data) {
        ISxShiftProdCenterRecordService shiftProdCenterRecordService = (ISxShiftProdCenterRecordService) SpringContextsUtil.getBean("sxShiftProdCenterRecordService");
        return shiftProdCenterRecordService.queryShiftProdCenterRecordListByCreateDatetime(packParam(data));
    }

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

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

    private MesShiftProdCenterRecord packParam(SxProdDataToSoftAdapterDataModel data) {
        MesShiftProdCenterRecord param = new MesShiftProdCenterRecord();
        param.setOrganizeCode(data.getOrganizeCode());
        param.setCreateDateTimeStart(data.getDate());
        param.setCreateDateTimeEnd(data.getDate());
        return param;
    }

    private List<SxProdDataSpcrStatisticsModel> packResultList(List<MesShiftProdCenterRecord> shiftProdCenterRecordList, SxOrgCfgModel org, Map<String, String> centerExtMap) {
        List<SxProdDataSpcrStatisticsModel> resultList = null;
        for (MesShiftProdCenterRecord item : shiftProdCenterRecordList) {
            if (null == item) continue;
            SxProdDataSpcrStatisticsModel result = new SxProdDataSpcrStatisticsModel(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]);
            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);
        }
        return resultList;
    }

}
