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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartDivulgeCheckRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpPartDivulgeCheckRecordModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartDivulgeCheckRecordRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION: 24小时泄露检测保存检测结果工步
 * @USER: xinwang.yi
 * @DATE: 2021-12-22 9:59
 */
@Slf4j
@Service("epPartDivulgeCheckSaveResultStepService")
public class EpPartDivulgeCheckSaveResultStepService extends BaseStepService {

    @Autowired
    private MesPartDivulgeCheckRecordRepository partDivulgeCheckRecordRepository;

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

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("epPartDivulgeCheckSaveResultStepService[execute] 参数：{}", reqBean);
        StationResultBean resultBean = new StationResultBean();
        Map<String, Object> dataMap = reqBean.getDataMap();
        EpPartDivulgeCheckRecordModel partDivulgeCheckRecordModel = (EpPartDivulgeCheckRecordModel) dataMap.get(MesPcnExtConstWords.CHECK_RECORD);
        if(partDivulgeCheckRecordModel == null){
            this.sendMessage(reqBean, resultBean, "上次检测数据丢失,记录未保存成功" , MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getSuccessComplete();
        }

        MesPartDivulgeCheckRecord partDivulgeCheckRecord = new MesPartDivulgeCheckRecord();
        BeanUtils.copyProperties(partDivulgeCheckRecordModel, partDivulgeCheckRecord);
        partDivulgeCheckRecord.setOrganizeCode(reqBean.getOrganizeCode());
        ConvertBean.serviceModelInitialize(partDivulgeCheckRecord, reqBean.getUserInfo());
        partDivulgeCheckRecord.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        partDivulgeCheckRecord.setSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        partDivulgeCheckRecordRepository.insert(partDivulgeCheckRecord);

        List<EpPartDivulgeCheckRecordModel> partDivulgeCheckRecordModels = getLastSelectOrder(reqBean);
//        if(StringUtils.isEmpty(lastSelectOrder)){
//            List<EpPartDivulgeCheckRecordModel> partDivulgeCheckRecordModels = new ArrayList<>();
//            partDivulgeCheckRecordModels.add(partDivulgeCheckRecordModel);
//            putLastSelectOrder(reqBean, partDivulgeCheckRecordModels);
//        }
        if(CollectionUtils.isEmpty(partDivulgeCheckRecordModels)){
            partDivulgeCheckRecordModels = new ArrayList<>();
        }

//        List<EpPartDivulgeCheckRecordModel> partDivulgeCheckRecordModels = JSONArray.parseArray(lastSelectOrder, EpPartDivulgeCheckRecordModel.class).stream().collect(Collectors.toList());
        partDivulgeCheckRecordModels.add(partDivulgeCheckRecordModel);
        if(partDivulgeCheckRecordModels.size() > 50){
            int size = partDivulgeCheckRecordModels.size();
            List<EpPartDivulgeCheckRecordModel> collectTop10 = partDivulgeCheckRecordModels.stream().skip(size - 10).collect(Collectors.toList());
            putLastSelectOrder(reqBean, collectTop10);
        }else{
            putLastSelectOrder(reqBean, partDivulgeCheckRecordModels);
        }
        return StepResult.getSuccessComplete();
    }

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

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

    public void putLastSelectOrder(StationRequestBean reqBean, List<EpPartDivulgeCheckRecordModel> epPartDivulgeCheckRecordModels) {
        redisMesPcn.putObject(getLastSelectOrderKey(reqBean), epPartDivulgeCheckRecordModels, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
    }
}
