
package com.yfd.platform.component.iec61850.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.component.utils.OptimizedThreadPool;
import com.yfd.platform.modules.auxcontrol.domain.GatewayDevice;
import com.yfd.platform.modules.auxcontrol.mapper.GatewayDeviceMapper;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author
 * @date 2019-01-07
 */
@Component
@RequiredArgsConstructor
public class IEC61850ClientRunner implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(IEC61850ClientRunner.class);
    private final GatewayDeviceMapper gatewayDeviceMapper;
    private final IEC61850Service iec61850Service;

    /**
     * 项目启动时激活启用的IEC104 与各子站的连接任务
     *
     * @param applicationArguments /
     */
    @Override
    @Async
    public void run(ApplicationArguments applicationArguments) throws Exception {
        LambdaQueryWrapper<GatewayDevice> queryWrapper = new LambdaQueryWrapper<>();
        // 没有停用
        queryWrapper.ne(GatewayDevice::getStatus, "00");
        queryWrapper.eq(GatewayDevice::getDeviceType, "30");
        queryWrapper.eq(GatewayDevice::getProtocol, "IEC61850");
        queryWrapper.select(GatewayDevice::getId, GatewayDevice::getDeviceCode, GatewayDevice::getDeviceName,
                GatewayDevice::getIpAddr, GatewayDevice::getIecAddr, GatewayDevice::getUrcb, GatewayDevice::getBrcb);
        queryWrapper.orderByAsc(GatewayDevice::getDeviceCode);
        List<GatewayDevice> list = gatewayDeviceMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
//            for (GatewayDevice systemDevice : list) {
//                OptimizedThreadPool threadPool = OptimizedThreadPool.getInstance();
//                threadPool.execute(() -> iec61850Service.connect(systemDevice));
//            }
        }
    }

}
