package com.jjt.biz.controller;

import com.jjt.biz.domain.TwinDevice;
import com.jjt.biz.service.ITwinDeviceService;
import com.jjt.common.annotation.Log;
import com.jjt.common.core.controller.BaseController;
import com.jjt.common.core.domain.AjaxResult;
import com.jjt.common.core.page.TableDataInfo;
import com.jjt.common.enums.BusinessType;
import com.jjt.common.utils.poi.ExcelUtil;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备管理Controller
 *
 * @author wukai
 * @date 2025-01-15
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/biz/device")
public class TwinDeviceController extends BaseController {
    @Resource
    private ITwinDeviceService twinDeviceService;

    /**
     * 查询设备管理列表
     */
    @ApiOperation("查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('biz:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(TwinDevice twinDevice) {
        startPage();
        List<TwinDevice> list = twinDeviceService.selectTwinDeviceList(twinDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @ApiOperation("导出设备管理列表")
    @PreAuthorize("@ss.hasPermi('biz:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwinDevice twinDevice) {
        List<TwinDevice> list = twinDeviceService.selectTwinDeviceList(twinDevice);
        ExcelUtil<TwinDevice> util = new ExcelUtil<TwinDevice>(TwinDevice.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @ApiOperation("获取设备管理详细信息")
    @PreAuthorize("@ss.hasPermi('biz:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId) {
        return success(twinDeviceService.selectTwinDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备管理
     */
    @ApiOperation("新增设备管理")
    @PreAuthorize("@ss.hasPermi('biz:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwinDevice twinDevice) {
        return toAjax(twinDeviceService.insertTwinDevice(twinDevice));
    }

    /**
     * 修改设备管理
     */
    @ApiOperation("修改设备管理")
    @PreAuthorize("@ss.hasPermi('biz:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwinDevice twinDevice) {
        return toAjax(twinDeviceService.updateTwinDevice(twinDevice));
    }

    /**
     * 删除设备管理
     */
    @ApiOperation("删除设备管理")
    @PreAuthorize("@ss.hasPermi('biz:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds) {
        return toAjax(twinDeviceService.deleteTwinDeviceByDeviceIds(deviceIds));
    }
}
