package com.jpsoft.shinestar.scheduled;

import com.jpsoft.shinestar.modules.base.entity.DeviceInfo;
import com.jpsoft.shinestar.modules.base.entity.ExecuteFailLog;
import com.jpsoft.shinestar.modules.base.service.DeviceInfoService;
import com.jpsoft.shinestar.modules.base.service.ExecuteFailLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@Slf4j
public class DeviceOnlineTask {
    @Autowired
    private ExecuteFailLogService executeFailLogService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    public void run(String deviceNo){
        List<ExecuteFailLog> list = executeFailLogService.findByDeviceNo(deviceNo,100);

        for (ExecuteFailLog efl : list) {
            if ("1".equals(efl.getType())) {
                //添加
                rabbitTemplate.convertAndSend("importImageExchange", "importImage", efl);
            }
            else if ("2".equals(efl.getType())) {
                //删除
                rabbitTemplate.convertAndSend("removeImageExchange", "removeImage", efl);
            }
        }
    }

    @Scheduled(cron = "0 0 * ? * *")
    public void run(){
        List<DeviceInfo> deviceInfoList = deviceInfoService.listOnline();

        for (DeviceInfo deviceInfo : deviceInfoList) {
            run(deviceInfo.getDeviceNo());
        }
    }
}