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.SxProdDataCqrsModel;
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.base.enumutil.CommonEnumUtil;
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;

/**
 * 出勤人数
 * 查询条件: 更新时间
 * 使用时间: 单位为Min,四舍五入保留四位小数,开始时间-结束时间
 */
@Slf4j
public class SxProdDataToSoftAdapterStrategyCqrsService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategyCqrsService(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> centerMap = getCenterMap(data.getOrganizeCode());

        Map<String, String> shiftProdMap = getShiftProdMap(data.getOrganizeCode());

        List<SxProdDataCqrsModel> resultList = packResultList(shiftProdCenterRecordList, org, centerMap, shiftProdMap);

        return data.obj(resultList);
    }

    private List<MesShiftProdCenterRecord> getShiftProdCenterRecordList(SxProdDataToSoftAdapterDataModel data) {
        ISxShiftProdCenterRecordService shiftProdCenterRecordService = (ISxShiftProdCenterRecordService) SpringContextsUtil.getBean("sxShiftProdCenterRecordService");
        List<MesShiftProdCenterRecord> shiftProdCenterRecordList = shiftProdCenterRecordService.queryAllShiftProdCenterRecordListByModifyDatetime(packParam(data));
        return CollectionUtils.isEmpty(shiftProdCenterRecordList) ? null :
                shiftProdCenterRecordList.stream().filter(o -> (null != o && (CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() == o.getIsDeleted() ||
                        !o.getCreateDatetime().substring(0, 10).equals(o.getModifyDatetime().substring(0, 10))))).collect(Collectors.toList());
    }

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

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

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

    @Override
    public Double getBetweenMin(String startTime, String endTime, int scale) { return strategyService.getBetweenMin(startTime, endTime, scale); }

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

    private List<SxProdDataCqrsModel> packResultList(List<MesShiftProdCenterRecord> shiftProdCenterRecordList, SxOrgCfgModel org, Map<String, String> centerMap, Map<String, String> shiftProdMap) {
        List<SxProdDataCqrsModel> resultList = null;
        for (MesShiftProdCenterRecord item : shiftProdCenterRecordList) {
            if (null == item) continue;
            SxProdDataCqrsModel result = new SxProdDataCqrsModel(org.getOrg(), org.getOrgName());
            BeanUtils.copyProperties(item, result);
            result.setWorkCenterName(StringUtils.isEmpty(result.getWorkCenterCode()) ? null : centerMap.get(result.getWorkCenterCode()));
            result.setProdName(StringUtils.isEmpty(result.getProdCode()) ? null : shiftProdMap.get(result.getProdCode()));
            result.setProdTypeName(StringUtils.isEmpty(result.getProdType()) ? null : MesExtEnumUtil.SHIFT_PROD_TYPE.valueOfDescription(result.getProdType()));
            result.setIntervalMin(formatPoint(getBetweenMin(result.getStartTime(), result.getEndTime(), 4), null));
            result.setWorkerQtyInt(StringUtils.isEmpty(result.getWorkerQty()) ? 0 : result.getWorkerQty().intValue());
            result.setModifyDate(!StringUtils.isEmpty(result.getModifyDatetime()) ? result.getModifyDatetime().substring(0, 10) : null);
            result.setIsDeletedName(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == item.getIsDeleted() ? "Y" : "N");
            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);
        }
        resultList = CollectionUtils.isEmpty(resultList) ? null :
                resultList.stream().filter(o -> null != o).sorted(Comparator.comparing(SxProdDataCqrsModel::getModifyDatetime)).collect(Collectors.toList());
        return resultList;
    }

}
