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.ISxRepairRecordExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesRepairRecordExt;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOrgCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataBlslModel;
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.MesRepairRecord;
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 SxProdDataToSoftAdapterStrategyBlslService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    public SxProdDataToSoftAdapterStrategyBlslService(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<MesRepairRecordExt> repairRecordExtList = getRepairRecordExtList(data);

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

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

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

        List<SxProdDataBlslModel> resultList = packResultList(repairRecordExtList, org, centerExtMap);

        return data.obj(resultList);
    }

    @Override
    public Object exec(SxProdDataToSoftAdapterDataModel data) {

        Map<String, List<SxProdDataBlslModel>> dataMap = getDataMap(data);

        return getBlslResultList(dataMap);

    }

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

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

    private List<MesRepairRecordExt> getRepairRecordExtList(SxProdDataToSoftAdapterDataModel data) {
        ISxRepairRecordExtService repairRecordExtService = (ISxRepairRecordExtService) SpringContextsUtil.getBean("sxRepairRecordExtService");
        return repairRecordExtService.queryRepairRecordExtListByCreateDatetime(packParam(data));
    }

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

    private List<SxProdDataBlslModel> packResultList(List<MesRepairRecordExt> repairRecordExtList, SxOrgCfgModel org, Map<String, String> centerExtMap) {
        List<SxProdDataBlslModel> resultList = null;
        for (MesRepairRecordExt item : repairRecordExtList) {
            if (null == item) continue;
            SxProdDataBlslModel result = new SxProdDataBlslModel(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 (StringUtils.isEmpty(result.getSerialNumber())) result.setSerialNumber(MesExtConstWords.EMPTY);
            if (StringUtils.isEmpty(result.getDefectType())) result.setDefectType(MesExtConstWords.EMPTY);
            //result.setRepairJudgeName(StringUtils.isEmpty(result.getRepairJudge()) ? null : MesExtEnumUtil.REPAIR_JUDGE.valueOfDescription(result.getRepairJudge()));
            result.setRepairStatusName(MesExtEnumUtil.REPAIR_STATUS.REPAIRED.getDescription());
            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);
        }
        return resultList;
    }

    private Map<String, List<SxProdDataBlslModel>> getDataMap(SxProdDataToSoftAdapterDataModel data) {
        List<SxProdDataBlslModel> resultList = (List<SxProdDataBlslModel>) data.getObj();
        return resultList.stream().filter(o -> null != o).collect(Collectors.groupingBy(o -> new StringJoiner(MesExtConstWords.AND)
                .add(o.getWorkCenterCode()).add(o.getWorkOrderNo()).add(o.getSerialNumber()).add(o.getDefectType()).add(o.getDefectCode())
                .add(o.getRepairJudgeName()).add(o.getDcCode()).add(o.getRepairCode()).add(o.getCreateDate()).add(o.getModifyDate()).toString()));
    }

    private Object getBlslResultList(Map<String, List<SxProdDataBlslModel>> dataMap) {
        List<SxProdDataBlslModel> resultList = null;

        for(Map.Entry<String, List<SxProdDataBlslModel>> entry : dataMap.entrySet()) {
            if (null == entry) continue;

            List<SxProdDataBlslModel> itemList = entry.getValue();

            SxProdDataBlslModel result = new SxProdDataBlslModel(itemList.get(0));
            result.setStatistics(itemList.size());
            if (StringUtils.isEmpty(result.getSerialNumber())) result.setSerialNumber(MesExtConstWords.EMPTY);
            if (StringUtils.isEmpty(result.getDefectType())) result.setDefectType(MesExtConstWords.EMPTY);
            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);

        }

        resultList = CollectionUtils.isEmpty(resultList) ? null :
                resultList.stream().sorted(Comparator.comparing(SxProdDataBlslModel::getWorkCenterCode)
                        .thenComparing(SxProdDataBlslModel::getWorkOrderNo)
                        .thenComparing(SxProdDataBlslModel::getSerialNumber)
                        .thenComparing(SxProdDataBlslModel::getDefectType)
                        .thenComparing(SxProdDataBlslModel::getDefectCode)
                        .thenComparing(SxProdDataBlslModel::getRepairJudge)
                        .thenComparing(SxProdDataBlslModel::getDcCode)
                        .thenComparing(SxProdDataBlslModel::getRepairCode)
                        .thenComparing(SxProdDataBlslModel::getCreateDate)
                        .thenComparing(SxProdDataBlslModel::getModifyDate)).collect(Collectors.toList());

        return resultList;
    }

}
