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


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.DeviceSignal;
import com.yfd.platform.modules.auxcontrol.mapper.DeviceSignalMapper;
import com.yfd.platform.modules.auxcontrol.service.IDeviceSignalService;
import com.yfd.platform.modules.basedata.domain.SubstationModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 变电站-辅控设备-信号 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2025-04-23
 */
@RestController
@RequestMapping("/auxcontrol/device-signal")
@Api(value = "DeviceSignalController", tags = "变电站辅控设备信号")
public class DeviceSignalController {


    @Resource
    private IDeviceSignalService deviceSignalService;

    /**********************************
     * 用途说明: 分页查询变电站辅控设备信号
     * 参数说明
     * mainDeviceId    主设备设备ID
     * componentId    部件ID
     * signalName  信号名称
     * pageNum 当前页
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回分页查询结果
     ***********************************/
    @GetMapping("/page")
    @ApiOperation("分页查询变电站辅控设备信号")
    public ResponseResult getDeviceSignalPage(String systemcode,String meterDeviceId,String mainDeviceId, String mainComponentId, String signalName, Page<DeviceSignal> page) {
        //参数校验  辅控设备ID不能为空
        if (StrUtil.isBlank(mainComponentId)) {
            return ResponseResult.error("参数为空");
        }
        Page<DeviceSignal> deviceSignalPage = deviceSignalService.getDeviceSignalPage(systemcode,meterDeviceId,mainDeviceId,mainComponentId, signalName, page);
        return ResponseResult.successData(deviceSignalPage);
    }


    /**********************************
     * 用途说明: 查询遥信遥测数据
     * 参数说明
     * areaid    区域ID
     * 返回值说明:  变电站辅控设备信号集合
     ***********************************/
    @GetMapping("/queryYxYcData")
    @ApiOperation("查询遥信遥测数据")
    public ResponseResult queryYxYcData(String mainDeviceId, String componentId) {
        LambdaQueryWrapper<DeviceSignal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(mainDeviceId), DeviceSignal::getMainDeviceId, mainDeviceId).eq(StrUtil.isNotBlank(componentId), DeviceSignal::getMainComponentId, componentId);
        List<DeviceSignal> list = deviceSignalService.list(queryWrapper);
        return ResponseResult.successData(list);
    }

    /**********************************
     * 用途说明: 查询信号信息
     * 参数说明
     * signalId    区域ID
     * 返回值说明:  变电站辅控设备信号集合
     ***********************************/
    @GetMapping("/querySignalDataById")
    @ApiOperation("根据信号id查询信号信息")
    public ResponseResult querySignalDataById(String signalId) {
        Map<String, Object> map = deviceSignalService.querySignalDataById(signalId);
        return ResponseResult.successData(map);
    }

    /***********************************
     * 用途说明：新增变电站辅控设备信号
     * 参数说明
     * deviceSignal     变电站辅控设备信号
     * 返回值说明:  com.yfd.platform.config.ResponseResult 返回新增成功或者失败
     ***********************************/
    @Log(module = "变电站辅控设备信号", value = "新增变电站辅控设备信号！", type = "1")
    @PostMapping("/addDeviceSignal")
    @ApiOperation("新增变电站辅控设备信号")
    @ResponseBody
    public ResponseResult addDeviceSignal(@RequestBody DeviceSignal deviceSignal) {
        //对象不能为空
        if (deviceSignal == null) {
            return ResponseResult.error("参数为空");
        }
        boolean isOK = deviceSignalService.addDeviceSignal(deviceSignal);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 修改变电站辅控设备信号
     * 参数说明  deviceSignal 变电站辅控设备信号
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回修改成功或者失败
     ***********************************/
    @Log(module = "变电站辅控设备信号", value = "修改变电站辅控设备信号",type = "1")
    @PostMapping("/updateDeviceSignal")
    @ApiOperation("修改变电站辅控设备信号")
    public ResponseResult updateDeviceSignal(@RequestBody DeviceSignal deviceSignal) {
        if (ObjectUtil.isEmpty(deviceSignal)) {
            return ResponseResult.error("参数为空");
        }
        boolean isOK = deviceSignalService.updateDeviceSignal(deviceSignal);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

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


    /**********************************
     * 用途说明: 批量删除变电站辅控设备信号
     * 参数说明  ids 变电站辅控设备信号id数组
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回批量删除成功或失败
     ***********************************/
    @Log(module = "变电站辅控设备信号", value = "批量删除变电站辅控设备信号",type = "1")
    @PostMapping("/deleteDeviceSignalByIds")
    @ApiOperation("批量删除变电站辅控设备信号")
    public ResponseResult deleteDeviceByIds(@RequestParam String ids) {
        if (StrUtil.isBlank(ids)) {
            return ResponseResult.error("参数为空");
        }
        List<String> idList = StrUtil.split(ids, ",");
        boolean isOK = deviceSignalService.removeByIds(idList);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }


}
