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.pojo.bean.SxWorkCellTaktCollectRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.SxWorkCellTaktCollectRecordRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

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

    private SxWorkCellTaktCollectRecordRepository workCellTaktCollectRecordRepository;

    protected String targetField;

    public SxThirdPartyPlcCollectCellTaktStepService() {
        this.workCellTaktCollectRecordRepository = (SxWorkCellTaktCollectRecordRepository) SpringContextsUtil.getBean("sxWorkCellTaktCollectRecordRepository");
    }

    public SxThirdPartyPlcCollectCellTaktStepService targetField(String targetField) {
        this.targetField = targetField;
        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);
        Object targetValue = ((Map<String, Object>) stepResult.getData()).get(targetField);

        SxWorkCellTaktCollectRecord workCellTaktCollectRecord = new SxWorkCellTaktCollectRecord();
        workCellTaktCollectRecord.setWorkCenterCode(workCenterCode);
        workCellTaktCollectRecord.setWorkCellCode(workCellCode);
        workCellTaktCollectRecord.setWorkTakt(targetValue.toString());
        workCellTaktCollectRecord.setOrganizeCode(organizeCode);
        workCellTaktCollectRecord.setSystemSyncStatus(MesPcnEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(workCellTaktCollectRecord, MesPcnExtConstWords.JOB);
        workCellTaktCollectRecordRepository.insert(workCellTaktCollectRecord);

        return stepResult;
    }

}
