package net.vtstar.zhongtong.avi.gateway.painting.job;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.domain.Enum.EquipStatusEnum;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import net.vtstar.scada.base.gather.job.GatherJob;
import net.vtstar.zhongtong.avi.gateway.painting.domain.PaintingPretreatmentMachine;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 前处理温度数据采集JOB
 */
@Slf4j
@Component
public class PaintPretreatmentMachineJob extends GatherJob {

    private static final String NAME = "VEHICLE_PRETREATMENT_ELECTROPHORESIS";

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

        log.info("JobName: {}", context.getJobDetail().getKey().getName());
        super.gatherByModbus(NAME, PaintingPretreatmentMachine.class);
    }

    @Override
    public boolean handleObject(EquipmentData equipmentData) {
        PaintingPretreatmentMachine data = (PaintingPretreatmentMachine) equipmentData;
        String state = data.getStatus();
        if (state == null || "0".equals(state)) {
            return false;
        }
        if (null == data.getCataphoresisTemperature() || null == data.getDegreaseTemperature() || null == data.getLhrsTemperature()
                || null == data.getPhosphatizingTemperature()){
            return false;
        }
        equipmentData.setEquipStatus(EquipStatusEnum.RUN);
        return true;
    }

    @Override
    public Object handleValue(String fieldName, Object value) {
        if (null == value){
            return null;
        }
        return value;
    }

    @Override
    protected void postHandle(EquipmentData equipmentData) {
        ((PaintingPretreatmentMachine)equipmentData).setCreateTime(new Date());
    }
}
