package com.ruoyi.web.bst;

import com.ruoyi.bst.device.domain.enums.DeviceUnLockType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.iot.constants.IotConstants;

@RestController
@RequestMapping("/bst/device/iot")
public class DeviceIotController extends BaseController {

    @Autowired
    private DeviceIotService deviceIotService;

    @Autowired
    private DeviceService deviceService;

    private DeviceVO getDevice(Long id, String sn) {
        if (id != null) {
            return deviceService.selectDeviceById(id, true);
        } else if (sn != null) {
            return deviceService.selectDeviceBySn(sn, true);
        } else {
            throw new ServiceException("SN和ID不能同时为空");
        }
    }

    @PreAuthorize("@ss.hasPermi('bst:device:unlock')")
    @Log(title = "管理员开锁", businessType = BusinessType.OTHER)
    @PutMapping("/unlock")
    public AjaxResult unlock(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = this.getDevice(id, sn);
        return toAjax(deviceIotService.unlock(device, DeviceUnLockType.ADMIN, "管理员开锁", true));
    }

    @PreAuthorize("@ss.hasPermi('bst:device:lock')")
    @Log(title = "管理员锁车", businessType = BusinessType.OTHER)
    @PutMapping("/lock")
    public AjaxResult lock(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = this.getDevice(id, sn);
        return toAjax(deviceIotService.lock(device, true, "管理员锁车", true));
    }

    @PreAuthorize("@ss.hasPermi('bst:device:ring')")
    @Log(title = "管理员响铃寻车", businessType = BusinessType.OTHER)
    @PutMapping("/ring")
    public AjaxResult ring(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = this.getDevice(id, sn);
        return toAjax(deviceIotService.play(device, IotConstants.PLAY_WARNING, "管理员响铃寻车"));
    }

    @PreAuthorize("@ss.hasPermi('bst:device:reboot')")
    @Log(title = "管理员重启", businessType = BusinessType.OTHER)
    @PutMapping("/reboot")
    public AjaxResult reboot(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = this.getDevice(id, sn);
        return toAjax(deviceIotService.reboot(device, "管理员重启"));
    }

    @PreAuthorize("@ss.hasPermi('bst:device:unlockSeat')")
    @Log(title = "管理员开坐垫锁", businessType = BusinessType.OTHER)
    @PutMapping("/unlockSeat")
    public AjaxResult unlockSeat(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = this.getDevice(id, sn);
        return toAjax(deviceIotService.unlockSeat(device, "管理员开坐垫锁"));
    }

    @PreAuthorize("@ss.hasPermi('bst:device:refresh')")
    @Log(title = "管理员刷新", businessType = BusinessType.OTHER)
    @PutMapping("/refresh")
    public AjaxResult refresh(@RequestParam(required = false) Long id, @RequestParam(required = false) String sn) {
        DeviceVO device = this.getDevice(id, sn);
        deviceIotService.refresh(device, IotConstants.ONLINE_TYPE_COMMAND);
        return success(device);
    }
}
