package cn.iocoder.yudao.module.mes.controller.admin.energyrecord;

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.mes.controller.admin.energyrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.energyrecord.EnergyRecordDO;
import cn.iocoder.yudao.module.mes.service.energyrecord.EnergyRecordService;

@Tag(name = "管理后台 - 能源设备历史记录")
@RestController
@RequestMapping("/mes/energy-record")
@Validated
public class EnergyRecordController {

    @Resource
    private EnergyRecordService energyRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建能源设备历史记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-record:create')")
    public CommonResult<Long> createEnergyRecord(@Valid @RequestBody EnergyRecordSaveReqVO createReqVO) {
        return success(energyRecordService.createEnergyRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新能源设备历史记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-record:update')")
    public CommonResult<Boolean> updateEnergyRecord(@Valid @RequestBody EnergyRecordSaveReqVO updateReqVO) {
        energyRecordService.updateEnergyRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除能源设备历史记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:energy-record:delete')")
    public CommonResult<Boolean> deleteEnergyRecord(@RequestParam("id") Long id) {
        energyRecordService.deleteEnergyRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得能源设备历史记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:energy-record:query')")
    public CommonResult<EnergyRecordRespVO> getEnergyRecord(@RequestParam("id") Long id) {
        EnergyRecordDO energyRecord = energyRecordService.getEnergyRecord(id);
        return success(BeanUtils.toBean(energyRecord, EnergyRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得能源设备历史记录分页")
    @PreAuthorize("@ss.hasPermission('mes:energy-record:query')")
    public CommonResult<PageResult<EnergyRecordRespVO>> getEnergyRecordPage(@Valid EnergyRecordPageReqVO pageReqVO) {
        PageResult<EnergyRecordDO> pageResult = energyRecordService.getEnergyRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EnergyRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能源设备历史记录 Excel")
    @PreAuthorize("@ss.hasPermission('mes:energy-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnergyRecordExcel(@Valid EnergyRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EnergyRecordDO> list = energyRecordService.getEnergyRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "能源设备历史记录.xls", "数据", EnergyRecordRespVO.class,
                        BeanUtils.toBean(list, EnergyRecordRespVO.class));
    }

}