package net.vtstar.zhongtong.avi.equipment.job;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.service.EquipDataService;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import net.vtstar.scada.base.redis.service.SimpleRedisService;
import net.vtstar.scada.base.redis.util.EquipDataUtil;
import net.vtstar.zhongtong.avi.equipment.domain.EquipInfo;
import net.vtstar.zhongtong.avi.equipment.mapper.EquipmentDataMapper;
import net.vtstar.zhongtong.avi.equipment.service.BasicInfoService;
import net.vtstar.zhongtong.avi.equipment.service.EquipStatusRealTimeService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
@DisallowConcurrentExecution
public class CleanMachineStatusDataJob extends QuartzJobBean {


    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;
    @Autowired
    private BasicInfoService basicInfoService;
    @Autowired
    private EquipStatusRealTimeService realTimeService;

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

        log.info("_______________________________________________________________");
        List<EquipInfo> allEquip = basicInfoService.findAllEquip();
        log.info("开始清洗设备实时状态数据 allEquip.size: {}", allEquip.size());
        for (EquipInfo info : allEquip) {
            if ("PLC".equals(info.getEquipType()) || "PAINTING_ROBOT_ERROR".equals(info.getEquipType())) {
                continue;
            }
            if (null != info.getRemarks()) {
                continue;
            }
            EquipmentData data;
            data = (EquipmentData) valueOperations.get(EquipDataUtil.getLastStatusKey(info.getAlias()));
            if (null == data || null == data.getEquipStatus()) {
                realTimeService.saveEquip(info.getWorkshopName(), info.getEquipCode(), info.getAlias(), "关机");
                continue;
            }
            String state = "";
            switch (data.getEquipStatus().name()) {
                case "RUN":
                    state = "运行";
                    break;
                case "IDLE":
                    state = "待机";
                    break;
                case "PAUSE":
                    state = "暂停";
                    break;
                case "ALARM":
                    state = "报警";
                    break;
                case "SHUTDOWN":
                    state = "关机";
                    break;
                default:
                    state = "关机";
                    break;
            }
            realTimeService.saveEquip(info.getWorkshopName(), info.getEquipCode(), info.getAlias(), state);
        }
        log.info("_______________________________________________________________");
    }
}
