package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep;

import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpPartDivulgeCheckRecordModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseModuleService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.mes.pcn.util.PojoAttrUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesStateMachineStatus;
import cn.estsh.i3plus.pojo.mes.model.AttrBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION: 24小时泄露检测展示组件
 * @USER: xinwang.yi
 * @DATE: 2021-12-21 13:09
 */
@Slf4j
@Service("epPartDivulgeCheckModuleService")
public class EpPartDivulgeCheckModuleService extends BaseModuleService {

    @Resource(name = MesPcnExtConstWords.REDIS_RES_PCN)
    private ImppRedis redisMesPcn;

    @Override
    public void init(StationRequestBean reqBean) {
        this.sendMessage(reqBean, null, "", MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        doInit(reqBean);
    }

    private void doInit(StationRequestBean reqBean){
        StationResultBean resultBean = new StationResultBean();
        String err;

        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.MODULE_CONTENT.getValue());
        resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM.getValue());
        resultBean.setCustomPageName(MesPcnExtConstWords.PART_DIVULGE_CHECK);
        resultBean.setDataAttrList(packDataAttrList(reqBean));
        resultBean.setSpecialDisplayData(getStepColIndent(reqBean));
        Map<String, Object> dataMap = reqBean.getDataMap();
        if(CollectionUtils.isEmpty(reqBean.getDataMap())){
            dataMap = new LinkedHashMap();
            dataMap.put(MesPcnExtConstWords.RADIO, 1);
        }
        dataMap.put(MesPcnExtConstWords.DIVULGE_VALUE_CAPITAL, "");
        resultBean.setResultMap(dataMap);
//        String result = getLastSelectOrder(reqBean);
        List<EpPartDivulgeCheckRecordModel> partDivulgeCheckRecordModels = getLastSelectOrder(reqBean);
//        List<EpPartDivulgeCheckRecordModel> partDivulgeCheckRecordModels = JSONArray.parseArray(result, EpPartDivulgeCheckRecordModel.class);
        if(!CollectionUtils.isEmpty(partDivulgeCheckRecordModels)){
            partDivulgeCheckRecordModels = partDivulgeCheckRecordModels.stream().sorted(Comparator.comparing(EpPartDivulgeCheckRecordModel::getCheckTime)).collect(Collectors.toList());
            Collections.reverse(partDivulgeCheckRecordModels);
            //取前10的数据
            if(partDivulgeCheckRecordModels.size() > 10){
                List<EpPartDivulgeCheckRecordModel> PartDivulgeCheckRecordModelsTop10 = partDivulgeCheckRecordModels.stream().limit(10).collect(Collectors.toList());
                resultBean.setResultList(PartDivulgeCheckRecordModelsTop10);
            }else{
                resultBean.setResultList(partDivulgeCheckRecordModels);
            }
        }
        this.sendMessage(reqBean, resultBean);
    }

    private List<AttrBean> packDataAttrList(StationRequestBean reqBean) {
        List<AttrBean> attrBeanList = new ArrayList<>();
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PACKAGENO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "包装条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.CHECK_REUST_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "检测结果"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.DIVULGE_VALUE, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "泄露值"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.OUT_WC_TIME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "下线时间"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.CHECK_TIME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "检测时间"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PART_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料编码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PART_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料名称"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.WORK_ORDER_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单号"));
        return attrBeanList;
    }

    private String getLastSelectOrderKey(StationRequestBean reqBean) {
        return getCacheKey(reqBean, MesPcnExtConstWords.OPERATOR_DATA);
    }

    private List<EpPartDivulgeCheckRecordModel> getLastSelectOrder(StationRequestBean reqBean) {
        return (List<EpPartDivulgeCheckRecordModel>) redisMesPcn.getObject(getLastSelectOrderKey(reqBean));
    }

    @Override
    public boolean execute(StationRequestBean reqBean) {
        return true;
    }

    @Override
    public boolean execStateModule(StationRequestBean reqBean, List<MesStateMachineStatus> states, Map<String, String> wcpcMap) {
        doInit(reqBean);
        return true;
    }
}
