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

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.domain.Enum.EquipStatusEnum;
import net.vtstar.scada.base.redis.util.EquipDataUtil;
import net.vtstar.zhongtong.avi.equipment.service.EquipStatusRealTimeService;
import net.vtstar.zhongtong.avi.gateway.parts.domain.SteelCncCuttingMachine;
import net.vtstar.zhongtong.avi.utils.PingIpUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 型钢数控下料状态采集JOB
 */
@Slf4j
@Component
public class SteelCncCuttingMachineStateJob extends QuartzJobBean {

    @Value("${zt.ip.steelCncCutting1}")
    private String ipAddress1;
    @Value("${zt.ip.steelCncCutting2}")
    private String ipAddress2;
    @Value("${zt.ip.steelCncCutting3}")
    private String ipAddress3;
    @Value("${zt.ip.steelCncCutting4}")
    private String ipAddress4;
    @Value("${zt.ip.pingtimes}")
    private int pingTimes;
    @Value("${zt.ip.timeout}")
    private int timeOut;

    @Autowired
    private EquipStatusRealTimeService realTimeService;

    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<String> ipAddressList = Arrays.asList(ipAddress1, ipAddress2, ipAddress3, ipAddress4);
        for (String ipAddress : ipAddressList) {
            String equipName = "";
            String equipCode = "";
            String equipFactory = "";
            switch (ipAddress){
                case "10.1.4.107":
                    equipName = "自动数控锯切机";
                    equipCode = "076103-14";
                    equipFactory = "制件车间05东北方向靠里";
                    break;
                case "10.1.4.129":
                    equipName = "自动转角锯切机";
                    equipCode = "076103-25";
                    equipFactory = "制件车间05东北方向靠外";
                    break;
                case "10.1.4.106":
                    equipName = "自动转角锯切机";
                    equipCode = "076103-13";
                    equipFactory = "制件车间05中间南侧";
                    break;
                case "10.1.4.120":
                    equipName = "自动数控锯切机";
                    equipCode = "076103-17";
                    equipFactory = "制件车间05中间北侧";
                    break;
            }
            SteelCncCuttingMachine machine = new SteelCncCuttingMachine();
            machine.setAlias(equipName);
            machine.setEquipCode(equipCode);
            machine.setCreateTime(new Date());
            machine.setStatus("1");
            machine.setEquipFactory(equipFactory);
            if (!PingIpUtils.ping(ipAddress, pingTimes, timeOut)) {
                machine.setStateString("关机");
                realTimeService.saveEquip("制件车间", equipCode, equipName, "关机");
                machine.setEquipStatus(EquipStatusEnum.SHUTDOWN);
                String key = EquipDataUtil.getLastStatusKey(machine.getEquipCode());
                valueOperations.set(key, machine);
                continue;
            }
            machine.setStateString("开机");
            realTimeService.saveEquip("制件车间", equipCode, equipName, "开机");
            machine.setEquipStatus(EquipStatusEnum.RUN);
            String key = EquipDataUtil.getLastStatusKey(machine.getEquipCode());
            valueOperations.set(key, machine);
        }

    }
}
