package cn.iocoder.yudao.module.pms.controller.admin.tdparams;

import cn.iocoder.yudao.module.pms.controller.admin.tdparams.vo.IotTdParamsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.tdparams.vo.IotTdParamsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.tdparams.vo.IotTdParamsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.tdparams.IotTdParamsDO;
import cn.iocoder.yudao.module.pms.service.tdparams.IotTdParamsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 设备模型参数")
@RestController
@RequestMapping("/rq/iot-td-params")
@Validated
public class IotTdParamsController {

    @Resource
    private IotTdParamsService iotTdParamsService;

    @PostMapping("/create")
    @Operation(summary = "创建设备模型参数")
    @PreAuthorize("@ss.hasPermission('rq:iot-td-params:create')")
    public CommonResult<Long> createIotTdParams(@Valid @RequestBody IotTdParamsSaveReqVO createReqVO) {
        return success(iotTdParamsService.createIotTdParams(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备模型参数")
    @PreAuthorize("@ss.hasPermission('rq:iot-td-params:update')")
    public CommonResult<Boolean> updateIotTdParams(@Valid @RequestBody IotTdParamsSaveReqVO updateReqVO) {
        iotTdParamsService.updateIotTdParams(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备模型参数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-td-params:delete')")
    public CommonResult<Boolean> deleteIotTdParams(@RequestParam("id") Long id) {
        iotTdParamsService.deleteIotTdParams(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备模型参数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-td-params:query')")
    public CommonResult<IotTdParamsRespVO> getIotTdParams(@RequestParam("id") Long id) {
        IotTdParamsDO iotTdParams = iotTdParamsService.getIotTdParams(id);
        return success(BeanUtils.toBean(iotTdParams, IotTdParamsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备模型参数分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-td-params:query')")
    public CommonResult<PageResult<IotTdParamsRespVO>> getIotTdParamsPage(@Valid IotTdParamsPageReqVO pageReqVO) {
        PageResult<IotTdParamsDO> pageResult = iotTdParamsService.getIotTdParamsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotTdParamsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备模型参数 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-td-params:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotTdParamsExcel(@Valid IotTdParamsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotTdParamsDO> list = iotTdParamsService.getIotTdParamsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备模型参数.xls", "数据", IotTdParamsRespVO.class,
                        BeanUtils.toBean(list, IotTdParamsRespVO.class));
    }

}
