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.IJxPlcExtService;
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;
import java.util.StringJoiner;

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

    private IJxWorkCellTaktCollectRecordService workCellTaktCollectRecordService;

    private IJxPlcExtService plcExtService;

    protected String targetField;

    protected String limit;

    protected String scanCellCode;

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

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

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

    public SxThirdPartyPlcCollectCellTaktStepService scanCellCode(String scanCellCode) {
        this.scanCellCode = scanCellCode;
        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(String.valueOf(MathOperation.div(new Double(targetValue), new Double(10))));

        String key = new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.CELL_TAKT).add(organizeCode).add(workCenterCode).add(scanCellCode).toString();
        String timeShiftQty = plcExtService.getCachedData(key, MesPcnExtConstWords.TIME_SHIFT_QTY);
        String partData = plcExtService.getCachedData(key, MesPcnExtConstWords.PART_NO);
        String[] timeShiftQtyArr = !StringUtils.isEmpty(timeShiftQty) ? timeShiftQty.split(MesPcnExtConstWords.AND) : null;
        String[] partDataArr = !StringUtils.isEmpty(partData) ? partData.split(MesPcnExtConstWords.AND) : null;
        workCellTaktCollectRecord.setWorkTime(null != timeShiftQtyArr ? timeShiftQtyArr[0] : null);
        workCellTaktCollectRecord.setShiftCode(null != timeShiftQtyArr ? timeShiftQtyArr[1] : null);
        workCellTaktCollectRecord.setWorkerQty(null != timeShiftQtyArr ? new Double(timeShiftQtyArr[2]) : new Double(1));
        workCellTaktCollectRecord.setPartNo(null != partDataArr ? partDataArr[0] : null);
        workCellTaktCollectRecord.setPartNameRdd(null != partDataArr ? partDataArr[1] : null);
        workCellTaktCollectRecord.setStandardWork(null != partDataArr ? new Double(partDataArr[2]) : new Double(0));
        workCellTaktCollectRecord.setScanCellCode(scanCellCode);

        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;
    }

}
