package cn.iocoder.yudao.module.product.controller.admin.slavedevice;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.slavedevice.vo.SlaveDevicePageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.slavedevice.vo.SlaveDeviceRespVO;
import cn.iocoder.yudao.module.product.convert.slavedevice.SlaveDeviceConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.slavedevice.SlaveDeviceDO;
import cn.iocoder.yudao.module.product.service.slavedevice.SlaveDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 从(子)设备信息")
@RestController
@RequestMapping("/product/slave-device")
@Validated
public class SlaveDeviceController {

    @Resource
    private SlaveDeviceService slaveDeviceService;

    @DeleteMapping("/delete")
    @ApiOperation("删除从(子)设备信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:slave-device:delete')")
    public CommonResult<Boolean> deleteSlaveDevice(@RequestParam("id") Long id) {
        slaveDeviceService.deleteSlaveDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得从(子)设备信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:slave-device:query')")
    public CommonResult<SlaveDeviceRespVO> getSlaveDevice(@RequestParam("id") Long id) {
        SlaveDeviceDO slaveDevice = slaveDeviceService.getSlaveDevice(id);
        return success(SlaveDeviceConvert.INSTANCE.convert(slaveDevice));
    }

    @GetMapping("/page")
    @ApiOperation("获得从(子)设备信息分页")
    @PreAuthorize("@ss.hasPermission('product:slave-device:query')")
    public CommonResult<PageResult<SlaveDeviceRespVO>> getSlaveDevicePage(@Valid SlaveDevicePageReqVO pageVO) {
        PageResult<SlaveDeviceDO> pageResult = slaveDeviceService.getSlaveDevicePage(pageVO);
        return success(SlaveDeviceConvert.INSTANCE.convertPage(pageResult));
    }

}
