package com.skr.mdm;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.entity.DeviceEquipmentDO;
import com.skr.mdm.enums.EquipmentStateEnum;
import com.skr.mdm.netty.WebsocketServer;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.skr.mdm.dao")
@Slf4j
public class MdmClientApplication implements CommandLineRunner {

    @Value("${websocket.port}")
    private Integer port;

    @Autowired
    private WebsocketServer websocketServer;
    @Autowired
    private DeviceEquipmentDao equipmentDao;

    public static void main(String[] args) {
        SpringApplication.run(MdmClientApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // websocket在线的设备改为http在线（http离线机制处理）
        new LambdaUpdateChainWrapper<>(equipmentDao)
                .set(DeviceEquipmentDO::getEquipmentState, EquipmentStateEnum.HTTP_ONLINE.getState())
                .eq(DeviceEquipmentDO::getEquipmentState, EquipmentStateEnum.WS_ONLINE.getState())
                .update();
        websocketServer.run(port);
    }
}
