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

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.*;

import cn.iocoder.yudao.module.pms.controller.admin.iotoperationplandev.vo.*;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationplandev.IotOperationPlanDevDO;
import cn.iocoder.yudao.module.pms.service.iotoperationplandev.IotOperationPlanDevService;

@Tag(name = "管理后台 - 运行记录计划设备")
@RestController
@RequestMapping("/rq/iot-operation-plan-dev")
@Validated
public class IotOperationPlanDevController {

    @Resource
    private IotOperationPlanDevService iotOperationPlanDevService;

    @PostMapping("/create")
    @Operation(summary = "创建运行记录计划设备")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan-dev:create')")
    public CommonResult<Long> createIotOperationPlanDev(@Valid @RequestBody IotOperationPlanDevSaveReqVO createReqVO) {
        return success(iotOperationPlanDevService.createIotOperationPlanDev(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新运行记录计划设备")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan-dev:update')")
    public CommonResult<Boolean> updateIotOperationPlanDev(@Valid @RequestBody IotOperationPlanDevSaveReqVO updateReqVO) {
        iotOperationPlanDevService.updateIotOperationPlanDev(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运行记录计划设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan-dev:delete')")
    public CommonResult<Boolean> deleteIotOperationPlanDev(@RequestParam("id") Long id) {
        iotOperationPlanDevService.deleteIotOperationPlanDev(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运行记录计划设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan-dev:query')")
    public CommonResult<IotOperationPlanDevRespVO> getIotOperationPlanDev(@RequestParam("id") Long id) {
        IotOperationPlanDevDO iotOperationPlanDev = iotOperationPlanDevService.getIotOperationPlanDev(id);
        return success(BeanUtils.toBean(iotOperationPlanDev, IotOperationPlanDevRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运行记录计划设备分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan-dev:query')")
    public CommonResult<PageResult<IotOperationPlanDevRespVO>> getIotOperationPlanDevPage(@Valid IotOperationPlanDevPageReqVO pageReqVO) {
        PageResult<IotOperationPlanDevDO> pageResult = iotOperationPlanDevService.getIotOperationPlanDevPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotOperationPlanDevRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出运行记录计划设备 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan-dev:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotOperationPlanDevExcel(@Valid IotOperationPlanDevPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotOperationPlanDevDO> list = iotOperationPlanDevService.getIotOperationPlanDevPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "运行记录计划设备.xls", "数据", IotOperationPlanDevRespVO.class,
                        BeanUtils.toBean(list, IotOperationPlanDevRespVO.class));
    }

}