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

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.iotdevicerunlog.vo.IotDeviceRunLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicerunlog.vo.IotDeviceRunLogRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicerunlog.vo.IotDeviceRunLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicerunlog.IotDeviceRunLogDO;
import cn.iocoder.yudao.module.pms.service.iotdevicerunlog.IotDeviceRunLogService;
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-run-log")
@Validated
public class IotDeviceRunLogController {

    @Resource
    private IotDeviceRunLogService iotDeviceRunLogService;

    @PostMapping("/create")
    @Operation(summary = "创建设备运行数据记录 中间表")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-run-log:create')")
    public CommonResult<Long> createIotDeviceRunLog(@Valid @RequestBody IotDeviceRunLogSaveReqVO createReqVO) {
        return success(iotDeviceRunLogService.createIotDeviceRunLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备运行数据记录 中间表")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-run-log:update')")
    public CommonResult<Boolean> updateIotDeviceRunLog(@Valid @RequestBody IotDeviceRunLogSaveReqVO updateReqVO) {
        iotDeviceRunLogService.updateIotDeviceRunLog(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得设备运行数据记录 中间表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-run-log:query')")
    public CommonResult<IotDeviceRunLogRespVO> getIotDeviceRunLog(@RequestParam("id") Long id) {
        IotDeviceRunLogDO iotDeviceRunLog = iotDeviceRunLogService.getIotDeviceRunLog(id);
        return success(BeanUtils.toBean(iotDeviceRunLog, IotDeviceRunLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备运行数据记录 中间表分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-run-log:query')")
    public CommonResult<PageResult<IotDeviceRunLogRespVO>> getIotDeviceRunLogPage(@Valid IotDeviceRunLogPageReqVO pageReqVO) {
        PageResult<IotDeviceRunLogDO> pageResult = iotDeviceRunLogService.getIotDeviceRunLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeviceRunLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备运行数据记录 中间表 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-run-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceRunLogExcel(@Valid IotDeviceRunLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceRunLogDO> list = iotDeviceRunLogService.getIotDeviceRunLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备运行数据记录 中间表.xls", "数据", IotDeviceRunLogRespVO.class,
                        BeanUtils.toBean(list, IotDeviceRunLogRespVO.class));
    }

}