package com.ruoyi.web.controller.system;

import com.ruoyi.common.biz.EntityController;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.sql.SQLException;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2023-08-14
 */
@RestController
@RequestMapping("/system/device")
public class SysDeviceController extends EntityController<SysDevice, Long, ISysDeviceService> {
//    @Resource
//    private TaosClient taosClient;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public Mono<TableDataInfo> list(ServerWebExchange exchange, SysDevice cond) {
        return super.list(exchange, cond);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public Mono<Void> export(ServerHttpResponse response, SysDevice cond) {
        return super.export(response, cond, SysDevice.class, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public Mono<AjaxResult> getInfo(@PathVariable("id") Long id) {
        return super.getInfo(id);
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public Mono<AjaxResult> insert(@RequestBody SysDevice data) {
        return startMono(() -> {
            return service.insert(data).map(this::toAjax);
        });
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public Mono<AjaxResult> update(@RequestBody SysDevice data) {
        return startMono(() -> {
            return service.update(data).map(this::toAjax);
        });
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Mono<AjaxResult> deleteByIds(@PathVariable Long[] ids) {
        return startMono(() -> {
            return service.deleteByIds(ids).map(this::toAjax);
        });
    }

//    @PreAuthorize("@ss.hasPermi('system:device:list')")
//    @GetMapping("findLogs")
//    public Mono<TableDataInfo> findLogs( Integer pageSize, Integer pageNum, String deviceNo) {
//        try {
//            TableDataInfo tableDataInfo = new TableDataInfo();
//            pageNum = (pageNum - 1) * pageSize;
//            tableDataInfo.setRows(taosClient.getLogsByPage(deviceNo, pageNum, pageSize));
//            tableDataInfo.setCode(HttpStatus.OK.value());
//            tableDataInfo.setTotal(taosClient.getTotalRecordCount(deviceNo));
//            return Mono.just(tableDataInfo);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
