package cn.iocoder.yudao.module.pms.controller.admin.qhse.measure;

import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureRecordPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureRecordRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureRecordSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureRecordDO;
import cn.iocoder.yudao.module.pms.service.qhse.measure.IotMeasureRecordService;
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 = "管理后台 - 计量器具-使用记录")
@RestController
@RequestMapping("/rq/iot-measure-record")
@Validated
public class IotMeasureRecordController {

    @Resource
    private IotMeasureRecordService iotMeasureRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建计量器具-使用记录")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-record:create')")
    public CommonResult<Long> createIotMeasureRecord(@Valid @RequestBody IotMeasureRecordSaveReqVO createReqVO) {
        return success(iotMeasureRecordService.createIotMeasureRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计量器具-使用记录")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-record:update')")
    public CommonResult<Boolean> updateIotMeasureRecord(@Valid @RequestBody IotMeasureRecordSaveReqVO updateReqVO) {
        iotMeasureRecordService.updateIotMeasureRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计量器具-使用记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-record:delete')")
    public CommonResult<Boolean> deleteIotMeasureRecord(@RequestParam("id") Long id) {
        iotMeasureRecordService.deleteIotMeasureRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计量器具-使用记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-record:query')")
    public CommonResult<IotMeasureRecordRespVO> getIotMeasureRecord(@RequestParam("id") Long id) {
        IotMeasureRecordDO iotMeasureRecord = iotMeasureRecordService.getIotMeasureRecord(id);
        return success(BeanUtils.toBean(iotMeasureRecord, IotMeasureRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计量器具-使用记录分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-record:query')")
    public CommonResult<PageResult<IotMeasureRecordRespVO>> getIotMeasureRecordPage(@Valid IotMeasureRecordPageReqVO pageReqVO) {
        PageResult<IotMeasureRecordDO> pageResult = iotMeasureRecordService.getIotMeasureRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMeasureRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计量器具-使用记录 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMeasureRecordExcel(@Valid IotMeasureRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMeasureRecordDO> list = iotMeasureRecordService.getIotMeasureRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计量器具-使用记录.xls", "数据", IotMeasureRecordRespVO.class,
                        BeanUtils.toBean(list, IotMeasureRecordRespVO.class));
    }

}