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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxPartExtService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterStrategyService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOrgCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataPfzbModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterDataModel;
import cn.estsh.i3plus.pojo.base.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCategory;
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 SxProdDataToSoftAdapterStrategyPfzbService implements ISxProdDataToSoftAdapterStrategyService {

    private ISxProdDataToSoftAdapterStrategyService strategyService;

    private SxProdDataToSoftAdapterCfgModel cfgModel;

    private ISxPartExtService partExtService;

    public SxProdDataToSoftAdapterStrategyPfzbService(ISxProdDataToSoftAdapterStrategyService strategyService, SxProdDataToSoftAdapterCfgModel cfgModel) {
        this.strategyService = strategyService;
        this.cfgModel = cfgModel;

        partExtService = (ISxPartExtService) SpringContextsUtil.getBean("sxPartExtService");
    }

    @Override
    public SxProdDataToSoftAdapterCfgModel getCfgModel() {
        return cfgModel;
    }

    @Override
    public ISxProdDataToSoftAdapterStrategyService getStrategyService() {
        return strategyService;
    }

    @Override
    public SxProdDataToSoftAdapterDataModel query(SxProdDataToSoftAdapterDataModel data) {

        List<MesPartExt> partExtList = getPartExtList(data);

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

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

        List<SxProdDataPfzbModel> resultList = packResultList(partExtList, org);

        return data.obj(resultList);
    }

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

    private List<MesPartExt> getPartExtList(SxProdDataToSoftAdapterDataModel data) {
        ISxPartExtService partExtService = (ISxPartExtService) SpringContextsUtil.getBean("sxPartExtService");
        return partExtService.queryPartExtListByModifyDatetime(packParam(data));
    }

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

    private List<SxProdDataPfzbModel> packResultList(List<MesPartExt> partExtList, SxOrgCfgModel org) {
        List<MesPartCategory> partCategoryList = partExtService.queryPartCategoryList(org.getOrganizeCode());

        List<MesPartCategory> partCategory1List = CollectionUtils.isEmpty(partCategoryList) ? null :
                partCategoryList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCategoryCode()) && !StringUtils.isEmpty(o.getCategoryName()) &&
                        !StringUtils.isEmpty(o.getCategoryType()) && o.getCategoryType().equals(MesEnumUtil.PART_CATEGORY_TYPE.CATEGORY_ONE.getValue()))).collect(Collectors.toList());
        List<MesPartCategory> partCategory3List = CollectionUtils.isEmpty(partCategoryList) ? null :
                partCategoryList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCategoryCode()) && !StringUtils.isEmpty(o.getCategoryName()) &&
                        !StringUtils.isEmpty(o.getCategoryType()) && o.getCategoryType().equals(MesEnumUtil.PART_CATEGORY_TYPE.CATEGORY_THREE.getValue()))).collect(Collectors.toList());

        Map<String, String> category1Map = CollectionUtils.isEmpty(partCategory1List) ? null :
                partCategory1List.stream().filter(o -> null != o).collect(Collectors.toMap(MesPartCategory::getCategoryCode, MesPartCategory::getCategoryName));
        Map<String, String> category3Map = CollectionUtils.isEmpty(partCategory3List) ? null :
                partCategory3List.stream().filter(o -> null != o).collect(Collectors.toMap(MesPartCategory::getCategoryCode, MesPartCategory::getCategoryName));

        //List<MesPartProduceType> partProduceTypeList = partExtService.queryPartProduceTypeList(org.getOrganizeCode());
        //Map<String, String> partProduceTypeMap = CollectionUtils.isEmpty(partProduceTypeList) ? null :
                //partProduceTypeList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getPptCode()) && !StringUtils.isEmpty(o.getPptName()))).collect(Collectors.toMap(MesPartProduceType::getPptCode, MesPartProduceType::getPptName));

        List<SxProdDataPfzbModel> resultList = null;
        for (MesPartExt item : partExtList) {
            if (null == item) continue;
            SxProdDataPfzbModel result = new SxProdDataPfzbModel(org.getOrg(), org.getOrgName());
            BeanUtils.copyProperties(item, result);
            //result.setPptName((!StringUtils.isEmpty(result.getPptCode()) &&!CollectionUtils.isEmpty(partProduceTypeMap) && partProduceTypeMap.containsKey(result.getPptCode())) ? partProduceTypeMap.get(result.getPptCode()) : null);
            result.setCategoryCode1Name((!StringUtils.isEmpty(result.getCategoryCode1()) && !CollectionUtils.isEmpty(category1Map) && category1Map.containsKey(result.getCategoryCode1())) ? category1Map.get(result.getCategoryCode1()) : null);
            result.setCategoryCode3Name((!StringUtils.isEmpty(result.getCategoryCode3()) && !CollectionUtils.isEmpty(category3Map) && category3Map.containsKey(result.getCategoryCode3())) ? category3Map.get(result.getCategoryCode3()) : null);
            result.setStandardWorkStr(formatPoint(StringUtils.isEmpty(result.getStandardWork()) ? new Double(0) : result.getStandardWork(), 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(SxProdDataPfzbModel::getModifyDatetime)).collect(Collectors.toList());
        return resultList;
    }

}
