package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.Threads;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.web.core.annotation.DeviceAdminRequired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author wjh
 * 2024/12/23
 */
@RestController
@RequestMapping("/app/device/admin")
public class AppDeviceAdminController extends BaseController {

    @Autowired
    private DeviceService deviceService;

    @ApiOperation("管理员通过MAC获取设备信息")
    @GetMapping("/byMac")
    @DeviceAdminRequired
    public AjaxResult getByMac(String mac) {
        return success(deviceService.selectByAnyMac(mac));
    }

    @ApiOperation("管理员开关设备")
    @DeviceAdminRequired
    @PutMapping("/{deviceId}/switch")
    public AjaxResult getExistMac(@PathVariable @ApiParam("设备ID") Long deviceId, @RequestParam @ApiParam("是否开启") Boolean open) {
        return toAjax(deviceService.switchDevice(deviceId, open,false,"管理员开关设备",false));
    }

    @ApiOperation("管理员修改设备电压系数")
    @DeviceAdminRequired
    @PutMapping("/{deviceId}/vxs")
    public AjaxResult updateVxs(@PathVariable @ApiParam("设备ID") Long deviceId, @RequestParam @ApiParam("电压系数") BigDecimal vxs) {
        return toAjax(deviceService.updateVxs(deviceId, vxs, "小程序管理员修改设备电压系数"));
    }

    @ApiOperation("管理员获取设备信息")
    @GetMapping("/get")
    @DeviceAdminRequired
    public AjaxResult adminGet(@RequestParam(required = false) String sn) {
        DeviceVO device = null;
        if (StringUtils.hasText(sn)) {
            device = deviceService.selectBySn(sn);
        } else {
            return error("请输入SN码");
        }
        return success(device);
    }

    @ApiOperation("管理员强制设备上报数据")
    @DeviceAdminRequired
    @PutMapping("/uploadData")
    public AjaxResult adminUploadData(@RequestParam String sn) {
        DeviceVO device = null;
        if (StringUtils.hasText(sn)) {
            device = deviceService.selectBySn(sn);
        } else {
            return error("请输入SN码");
        }
        int upload = deviceService.uploadData(device, "小程序管理员强制上报数据");
        if (upload != 1) {
            return error("上报数据失败");
        }
        Threads.sleep(3000);
        deviceService.pullDeviceInfo(device);
        return success(device);
    }

    @ApiOperation("管理员重启设备")
    @DeviceAdminRequired
    @PutMapping("/reboot")
    public AjaxResult adminReboot(@RequestParam String sn) {
        DeviceVO device = null;
        if (StringUtils.hasText(sn)) {
            device = deviceService.selectBySn(sn);
        } else {
            return error("请输入SN码");
        }
        return toAjax(deviceService.reboot(device, "小程序控制台重启设备"));
    }

    @ApiOperation("管理员开门")
    @DeviceAdminRequired
    @PutMapping("/openDoor")
    public AjaxResult openDoor(@RequestParam String sn) {
        DeviceVO device = null;
        if (StringUtils.hasText(sn)) {
            device = deviceService.selectBySn(sn);
        } else {
            return error("请输入SN码");
        }
        return toAjax(deviceService.openDoor(device, "小程序控制台开门"));
    }

    @ApiOperation("管理员开关")
    @PostMapping("/adminAll/{roomId}/switch")
    @DeviceAdminRequired
    public AjaxResult adminGet(@PathVariable @ApiParam("房间id") Long roomId ,@RequestParam @ApiParam("是否开启") Boolean open) {
        return toAjax(deviceService.roomAllSwitchDevice(roomId, open,"小程序房间开关"));
    }

}
