package cn.iocoder.yudao.module.energy.controller.admin.detail;

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.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.energy.controller.admin.detail.vo.*;
import cn.iocoder.yudao.module.energy.convert.detail.HydrogenDetailConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.detail.EnergyHydrogenDetailDO;
import cn.iocoder.yudao.module.energy.service.detail.HydrogenDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 加氢明细管理")
@RestController
@RequestMapping("/energy/hydrogen-detail")
@Validated
public class HydrogenDetailController {

    @Resource
    private HydrogenDetailService hydrogenDetailService;

    @GetMapping("/page")
    @Operation(summary = "获得明细分页")
    @PreAuthorize("@ss.hasPermission('energy:hydrogen-detail:query')")
    public CommonResult<PageResult<HydrogenDetailRespVO>> getDetailPage(@Valid HydrogenDetailPageReqVO pageReqVO) {
        PageResult<EnergyHydrogenDetailDO> pageResult = hydrogenDetailService.getDetailPage(pageReqVO);
        return success(HydrogenDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出加氢明细 Excel")
    @PreAuthorize("@ss.hasPermission('energy:hydrogen-detail:export')")
    public void exportExcel(@Valid HydrogenDetailPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<EnergyHydrogenDetailDO> pageResult = hydrogenDetailService.getDetailPage(pageReqVO);
        List<HydrogenDetailExcelVO> excelList = HydrogenDetailConvert.INSTANCE.convertExcelList(pageResult.getList());
        ExcelUtils.write(response, "加氢明细.xlsx", "数据", HydrogenDetailExcelVO.class, excelList);
    }

    @GetMapping("/get")
    @Operation(summary = "获得明细详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:hydrogen-detail:query')")
    public CommonResult<HydrogenDetailRespVO> getDetail(@RequestParam("id") Long id) {
        EnergyHydrogenDetailDO detail = hydrogenDetailService.getDetail(id);
        return success(HydrogenDetailConvert.INSTANCE.convert(detail));
    }

    @PutMapping("/update")
    @Operation(summary = "更新明细")
    @PreAuthorize("@ss.hasPermission('energy:hydrogen-detail:update')")
    public CommonResult<Boolean> updateDetail(@Valid @RequestBody HydrogenDetailSaveReqVO reqVO) {
        hydrogenDetailService.updateDetail(reqVO);
        return success(true);
    }

    @PostMapping("/audit")
    @Operation(summary = "审核明细")
    @PreAuthorize("@ss.hasPermission('energy:hydrogen-detail:audit')")
    public CommonResult<Boolean> auditDetail(@RequestParam("id") Long id,
                                              @RequestParam("approved") Boolean approved,
                                              @RequestParam(value = "remark", required = false) String remark) {
        hydrogenDetailService.audit(id, approved, remark);
        return success(true);
    }

    @PostMapping("/batch-audit")
    @Operation(summary = "批量审核明细")
    @PreAuthorize("@ss.hasPermission('energy:hydrogen-detail:audit')")
    public CommonResult<Boolean> batchAuditDetail(@RequestParam("ids") List<Long> ids,
                                                   @RequestParam("approved") Boolean approved,
                                                   @RequestParam(value = "remark", required = false) String remark) {
        hydrogenDetailService.batchAudit(ids, approved, remark);
        return success(true);
    }
}
