package com.yfd.platform.modules.auxcontrol.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.component.iec104.client.IIEC104Service;
import com.yfd.platform.component.iec61850.client.IEC61850Service;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.auxcontrol.domain.DeviceSignal;
import com.yfd.platform.modules.auxcontrol.domain.GatewayDevice;
import com.yfd.platform.modules.auxcontrol.domain.MeterDevice;
import com.yfd.platform.modules.auxcontrol.service.IDeviceSignalService;
import com.yfd.platform.modules.auxcontrol.service.IGatewayDeviceService;
import com.yfd.platform.modules.auxcontrol.service.IMeterDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 变电站-系统运行日志 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2024-04-05
 */
@RestController
@RequestMapping("/iecclient")
@Api(value = "IEC104Controller", tags = "IEC104服务")
public class IECController {

    //变电站-辅控设备-信号 服务类
    @Resource
    private IDeviceSignalService deviceSignalService;
    //变电站辅控设备 服务类

    @Resource
    private IGatewayDeviceService gatewayDeviceService;

    @Resource
    private IMeterDeviceService meterDeviceService;


    @Resource
    private IIEC104Service iec104Service;

    @Resource
    private IEC61850Service iec61850Service;



    /**********************************
     * 用途说明: 发送遥控遥调命令
     * 参数说明
     * systemcode    系统编号
     ***********************************/
    @GetMapping("/sendCommand")
    @ApiOperation("发送遥控遥调命令")
    public ResponseResult sendCommand(String systemcode, String signalid, String type, String value) throws Exception {
        if (StrUtil.isBlank(signalid)) {
            return ResponseResult.error("信号ID不能为空！");
        }
        DeviceSignal deviceSignal = deviceSignalService.getById(signalid);
        if (ObjUtil.isNotEmpty(deviceSignal)) {
            LambdaQueryWrapper<MeterDevice> queryWrapper = new LambdaQueryWrapper<>();
            //型号对应的监控设备id
            queryWrapper.eq(MeterDevice::getDeviceId, deviceSignal.getMeterDeviceId());
            //查找信号对应监控设备
            MeterDevice meterDevice = meterDeviceService.getOne(queryWrapper);
            LambdaQueryWrapper<GatewayDevice> queryWrapper2 = new LambdaQueryWrapper<>();
            //IP地址
            queryWrapper2.eq(GatewayDevice::getIpAddr, meterDevice.getNetdeviceIp());
            //查找信号对应网关机设备
            GatewayDevice netDevice = gatewayDeviceService.getOne(queryWrapper2);
            if (netDevice == null) {
                return ResponseResult.error("组件未关联信号！");
            }
            if ("IEC104".equals(netDevice.getProtocol())) {
                iec104Service.sendCommand(systemcode, signalid, type, value);
            } else if ("IEC61850".equals(netDevice.getProtocol())) {
                iec61850Service.sendCommand(systemcode, signalid, type, value);
            }
            return ResponseResult.success();
        } else {
            return ResponseResult.error("组件未关联信号！");
        }
    }
}
