package cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.slave;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.slave.vo.IotYfVarTempSalvePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.slave.vo.IotYfVarTempSalveRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.slave.vo.IotYfVarTempSalveSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.var.slave.IotYfVarTempSalveDO;
import cn.iocoder.yudao.module.pms.service.yanfan.var.slave.IotVarTempSalveService;
import org.springframework.web.bind.annotation.*;
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 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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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


@Tag(name = "管理后台 - yf变量模板设备从机对象")
@RestController
@RequestMapping("/rq/iot-var-temp-salve")
@Validated
public class IotYfVarTempSalveController {

    @Resource
    private IotVarTempSalveService iotVarTempSalveService;

    @PostMapping("/create")
    @Operation(summary = "创建yf变量模板设备从机对象")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp-salve:create')")
    public CommonResult<Long> createIotVarTempSalve(@Valid @RequestBody IotYfVarTempSalveSaveReqVO createReqVO) {
        return success(iotVarTempSalveService.createIotVarTempSalve(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新yf变量模板设备从机对象")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp-salve:update')")
    public CommonResult<Boolean> updateIotVarTempSalve(@Valid @RequestBody IotYfVarTempSalveSaveReqVO updateReqVO) {
        iotVarTempSalveService.updateIotVarTempSalve(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf变量模板设备从机对象")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp-salve:delete')")
    public CommonResult<Boolean> deleteIotVarTempSalve(@RequestParam("id") Long id) {
        iotVarTempSalveService.deleteIotVarTempSalve(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf变量模板设备从机对象")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp-salve:query')")
    public CommonResult<IotYfVarTempSalveRespVO> getIotVarTempSalve(@RequestParam("id") Long id) {
        IotYfVarTempSalveDO iotVarTempSalve = iotVarTempSalveService.getIotVarTempSalve(id);
        return success(BeanUtils.toBean(iotVarTempSalve, IotYfVarTempSalveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf变量模板设备从机对象分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp-salve:query')")
    public CommonResult<PageResult<IotYfVarTempSalveRespVO>> getIotVarTempSalvePage(@Valid IotYfVarTempSalvePageReqVO pageReqVO) {
        PageResult<IotYfVarTempSalveDO> pageResult = iotVarTempSalveService.getIotVarTempSalvePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfVarTempSalveRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf变量模板设备从机对象 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp-salve:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotVarTempSalveExcel(@Valid IotYfVarTempSalvePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfVarTempSalveDO> list = iotVarTempSalveService.getIotVarTempSalvePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf变量模板设备从机对象.xls", "数据", IotYfVarTempSalveRespVO.class,
                        BeanUtils.toBean(list, IotYfVarTempSalveRespVO.class));
    }

}
