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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.annotation.Log;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.auxcontrol.domain.GatewayDevice;
import com.yfd.platform.modules.auxcontrol.domain.GatewayDeviceRequest;
import com.yfd.platform.modules.auxcontrol.service.IGatewayDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 变电站-通讯网关设备 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2025-04-23
 */
@RestController
@RequestMapping("/auxcontrol/gateway-device")
@Api(value = "GatewayDeviceController", tags = "变电站通讯网关设备")
public class GatewayDeviceController {

    @Resource
    private IGatewayDeviceService gatewayDeviceService;

    /**********************************
     * 用途说明: 分页查询变电站网关设备
     * 参数说明
     * deviceName    设备名称
     * deviceModel   设备型号
     *deviceType     设备类型
     *status         设备状态
     * pageNum 当前页
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回分页查询结果
     ***********************************/
    @GetMapping("/page")
    @ApiOperation("分页查询变电站网关设备")
    public ResponseResult getGatewayDevicePage(String stationId, String deviceName, String deviceModel,
                                               String deviceType,
                                               String status, Page<GatewayDevice> page) {
        //分页查询
        Page<GatewayDevice> deviceAlarmParameterPage = gatewayDeviceService.getGatewayDevicePage(stationId, deviceName,
                deviceModel, deviceType, status, page);
        return ResponseResult.successData(deviceAlarmParameterPage);
    }

    @GetMapping("/getGatewayList")
    @ApiOperation("查询网关机列表")
    public ResponseResult getGatewayList(String stationId) {
        List<Map<String, Object>> meterDeviceList = gatewayDeviceService.getGatewayList(stationId);
        return ResponseResult.successData(meterDeviceList);
    }

    /***********************************
     * 用途说明：新增变电站网关设备
     * 参数说明
     * systemDevice     变电站网关
     * 返回值说明:  com.yfd.platform.config.ResponseResult 返回新增成功或者失败
     ***********************************/
    @Log(module = "数据变电站网关设备", value = "新增变电站网关设备！", type = "1")
    @PostMapping("/addGatewayDevice")
    @ApiOperation("新增变电站网关设备")
    @ResponseBody
    public ResponseResult addGatewayDevice(@RequestBody GatewayDevice gatewayDevice) {
        //对象不能为空
        if (ObjectUtil.isEmpty(gatewayDevice)) {
            return ResponseResult.error("参数为空");
        }
        boolean isOk = gatewayDeviceService.addGatewayDevice(gatewayDevice);
        if (isOk) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 修改变电站网关设备
     * 参数说明
     * systemDevice 变电站网关设备
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回修改成功或者失败
     ***********************************/
    @Log(module = "数据变电站网关设备", value = "修改变电站网关设备", type = "1")
    @PostMapping("/updateGatewayDevice")
    @ApiOperation("修改变电站网关设备")
    public ResponseResult updateGatewayDevice(@RequestBody GatewayDevice gatewayDevice) {
        //对象不能为空
        if (ObjectUtil.isEmpty(gatewayDevice)) {
            return ResponseResult.error("参数为空");
        }
        boolean isOk = gatewayDeviceService.updateGatewayDevice(gatewayDevice);
        if (isOk) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 根据ID删除变电站网关设备
     * 参数说明  id 变电站网关设备ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回删除成功或者失败
     ***********************************/
    @Log(module = "数据变电站网关设备", value = "根据ID删除变电站网关设备", type = "1")
    @PostMapping("/deleteGatewayDeviceById")
    @ApiOperation("根据ID删除变电站网关设备")
    public ResponseResult deleteGatewayDeviceById(@RequestParam String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("参数为空");
        }
        boolean isOk = gatewayDeviceService.removeById(id);
        if (isOk) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 根据ID删除变电站网关设备
     * 参数说明  ids 变电站网关设备ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回删除成功或者失败
     ***********************************/
    @Log(module = "数据变电站系统设备", value = "根据ID删除变电站网关设备", type = "1")
    @PostMapping("/batchDeleteGateway")
    @ApiOperation("根据ID批量删除变电站网关设备")
    public ResponseResult batchDeleteGateway(@RequestBody GatewayDeviceRequest gatewayDeviceRequest) {
        if (gatewayDeviceRequest.getIds() == null || gatewayDeviceRequest.getIds().size() <= 0) {
            return ResponseResult.error("参数为空");
        }
        boolean isOk = gatewayDeviceService.removeByIds(gatewayDeviceRequest.getIds());
        if (isOk) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

}
