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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxThirdPartyPlcCollectDataStepService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxWorkCellTaktCollectRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.SxWorkCellTaktCollectRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 第三方PLC数据采集定时任务---步骤执行---写节拍
 */
@Slf4j
public class SxThirdPartyPlcCollectCellTaktStepService implements ISxThirdPartyPlcCollectDataStepService {

    private IJxWorkCellTaktCollectRecordService workCellTaktCollectRecordService;

    protected String targetField;

    protected String limit;

    public SxThirdPartyPlcCollectCellTaktStepService() {
        this.workCellTaktCollectRecordService = (IJxWorkCellTaktCollectRecordService) SpringContextsUtil.getBean("jxWorkCellTaktCollectRecordService");
    }

    public SxThirdPartyPlcCollectCellTaktStepService targetField(String targetField) {
        this.targetField = targetField;
        return this;
    }

    public SxThirdPartyPlcCollectCellTaktStepService limit(String limit) {
        this.limit = limit;
        return this;
    }

    @Override
    public StepResult exec(StepResult stepResult) {

        String organizeCode = (String) ((Map<String, Object>) stepResult.getData()).get(MesPcnExtConstWords.ORGANIZE_CODE);
        String workCenterCode = (String) ((Map<String, Object>) stepResult.getData()).get(MesPcnExtConstWords.WORK_CENTER_CODE);
        String workCellCode = (String) ((Map<String, Object>) stepResult.getData()).get(MesPcnExtConstWords.WORK_CELL_CODE);
        String targetValue = (String) ((Map<String, Object>) stepResult.getData()).get(targetField);

        if (!StringUtils.isEmpty(limit) && filter2Limit(targetValue)) return stepResult.isCompleted(false).msg(String.format("读取plcCode:%s 值:%s 不匹配LIMIT区间[%s]!", targetField, targetValue, limit));

        SxWorkCellTaktCollectRecord workCellTaktCollectRecord = new SxWorkCellTaktCollectRecord();
        workCellTaktCollectRecord.setWorkCenterCode(workCenterCode);
        workCellTaktCollectRecord.setWorkCellCode(workCellCode);
        workCellTaktCollectRecord.setWorkTakt(targetValue);
        workCellTaktCollectRecord.setOrganizeCode(organizeCode);
        workCellTaktCollectRecord.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(workCellTaktCollectRecord, MesPcnExtConstWords.JOB);
        workCellTaktCollectRecordService.insertWorkCellTaktCollectRecordNoRollback(workCellTaktCollectRecord);

        return stepResult;
    }

    private Boolean filter2Limit(String targetValue) {
        if (!limit.contains(MesPcnExtConstWords.COMMA)) return false;
        String[] limitArr = limit.split(MesPcnExtConstWords.COMMA);
        if (limitArr.length != 2) return false;
        try {
            if (!StringUtils.isEmpty(limitArr[0]) && MathOperation.compareTo(Double.parseDouble(targetValue), Double.parseDouble(limitArr[0])) < 0) return true;
            if (!StringUtils.isEmpty(limitArr[1]) && MathOperation.compareTo(Double.parseDouble(targetValue), Double.parseDouble(limitArr[1])) > 0) return true;
        } catch (NumberFormatException e) {
            return true;
        }
        return false;
    }

}
