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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicemaxrunlog.vo.IotDeviceMaxRunLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicemaxrunlog.vo.IotDeviceMaxRunLogRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicemaxrunlog.vo.IotDeviceMaxRunLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicemaxrunlog.IotDeviceMaxRunLogDO;
import cn.iocoder.yudao.module.pms.service.iotdevicemaxrunlog.IotDeviceMaxRunLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设备运行数据记录最大值")
@RestController
@RequestMapping("/pms/iot-device-max-run-log")
@Validated
public class IotDeviceMaxRunLogController {

    @Resource
    private IotDeviceMaxRunLogService iotDeviceMaxRunLogService;

    @PostMapping("/create")
    @Operation(summary = "创建设备运行数据记录最大值")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-max-run-log:create')")
    public CommonResult<Long> createIotDeviceMaxRunLog(@Valid @RequestBody IotDeviceMaxRunLogSaveReqVO createReqVO) {
        return success(iotDeviceMaxRunLogService.createIotDeviceMaxRunLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备运行数据记录最大值")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-max-run-log:update')")
    public CommonResult<Boolean> updateIotDeviceMaxRunLog(@Valid @RequestBody IotDeviceMaxRunLogSaveReqVO updateReqVO) {
        iotDeviceMaxRunLogService.updateIotDeviceMaxRunLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备运行数据记录最大值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-device-max-run-log:delete')")
    public CommonResult<Boolean> deleteIotDeviceMaxRunLog(@RequestParam("id") Long id) {
        iotDeviceMaxRunLogService.deleteIotDeviceMaxRunLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备运行数据记录最大值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-max-run-log:query')")
    public CommonResult<IotDeviceMaxRunLogRespVO> getIotDeviceMaxRunLog(@RequestParam("id") Long id) {
        IotDeviceMaxRunLogDO iotDeviceMaxRunLog = iotDeviceMaxRunLogService.getIotDeviceMaxRunLog(id);
        return success(BeanUtils.toBean(iotDeviceMaxRunLog, IotDeviceMaxRunLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备运行数据记录最大值分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-max-run-log:query')")
    public CommonResult<PageResult<IotDeviceMaxRunLogRespVO>> getIotDeviceMaxRunLogPage(@Valid IotDeviceMaxRunLogPageReqVO pageReqVO) {
        PageResult<IotDeviceMaxRunLogDO> pageResult = iotDeviceMaxRunLogService.getIotDeviceMaxRunLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeviceMaxRunLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备运行数据记录最大值 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-max-run-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceMaxRunLogExcel(@Valid IotDeviceMaxRunLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceMaxRunLogDO> list = iotDeviceMaxRunLogService.getIotDeviceMaxRunLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备运行数据记录最大值.xls", "数据", IotDeviceMaxRunLogRespVO.class,
                        BeanUtils.toBean(list, IotDeviceMaxRunLogRespVO.class));
    }

}