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

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.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceCheckRecordDO;
import cn.iocoder.yudao.module.mes.service.energydevice.EnergyDeviceCheckRecordService;
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("/mes/energy-device-check-record")
@Validated
public class EnergyDeviceCheckRecordController {

    @Resource
    private EnergyDeviceCheckRecordService energyDeviceCheckRecordService;


    @PostMapping("/create")
    @Operation(summary = "创建抄表记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device-check-record:create')")
    public CommonResult<Long> createEnergyDeviceCheckRecord(@Valid @RequestBody EnergyDeviceCheckRecordSaveReqVO createReqVO) {
        return success(energyDeviceCheckRecordService.createEnergyDeviceCheckRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新抄表记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device-check-record:update')")
    public CommonResult<Boolean> updateEnergyDeviceCheckRecord(@Valid @RequestBody EnergyDeviceCheckRecordSaveReqVO updateReqVO) {
        energyDeviceCheckRecordService.updateEnergyDeviceCheckRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除抄表记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:energy-device-check-record:delete')")
    public CommonResult<Boolean> deleteEnergyDeviceCheckRecord(@RequestParam("id") Long id) {
        energyDeviceCheckRecordService.deleteEnergyDeviceCheckRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得抄表记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:energy-device-check-record:query')")
    public CommonResult<EnergyDeviceCheckRecordRespVO> getEnergyDeviceCheckRecord(@RequestParam("id") Long id) {
        EnergyDeviceCheckRecordDO energyDeviceCheckRecord = energyDeviceCheckRecordService.getEnergyDeviceCheckRecord(id);
        return success(BeanUtils.toBean(energyDeviceCheckRecord, EnergyDeviceCheckRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得抄表记录分页")
    @PreAuthorize("@ss.hasPermission('mes:energy-device-check-record:query')")
    public CommonResult<PageResult<EnergyDeviceCheckRecordRespVO>> getEnergyDeviceCheckRecordPage(@Valid EnergyDeviceCheckRecordPageReqVO pageReqVO) {
        PageResult<EnergyDeviceCheckRecordDO> pageResult = energyDeviceCheckRecordService.getEnergyDeviceCheckRecordPage(pageReqVO);
        PageResult<EnergyDeviceCheckRecordRespVO> pageResult1 =
                new PageResult<>(energyDeviceCheckRecordService.buildVOList(pageResult.getList()),pageResult.getTotal());
        return success(pageResult1);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出抄表记录 Excel")
    @PreAuthorize("@ss.hasPermission('mes:energy-device-check-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnergyDeviceCheckRecordExcel(@Valid EnergyDeviceCheckRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EnergyDeviceCheckRecordDO> list = energyDeviceCheckRecordService.getEnergyDeviceCheckRecordPage(pageReqVO).getList();

        // 导出 Excel
        ExcelUtils.write(response, "抄表记录.xls", "数据", EnergyDeviceCheckRecordRespVO.class,
                        energyDeviceCheckRecordService.buildVOList(list));
    }

}