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

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.bill.vo.*;
import cn.iocoder.yudao.module.energy.controller.admin.detail.vo.HydrogenDetailRespVO;
import cn.iocoder.yudao.module.energy.convert.bill.EnergyBillConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.bill.EnergyBillDO;
import cn.iocoder.yudao.module.energy.service.bill.EnergyBillService;
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 java.util.Map;

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

@Tag(name = "管理后台 - 能源账单管理")
@RestController
@RequestMapping("/energy/bill")
@Validated
public class EnergyBillController {

    @Resource
    private EnergyBillService billService;

    @GetMapping("/page")
    @Operation(summary = "获得账单分页")
    @PreAuthorize("@ss.hasPermission('energy:bill:query')")
    public CommonResult<PageResult<EnergyBillRespVO>> getBillPage(@Valid EnergyBillPageReqVO pageReqVO) {
        PageResult<EnergyBillDO> pageResult = billService.getBillPage(pageReqVO);
        return success(EnergyBillConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能源账单 Excel")
    @PreAuthorize("@ss.hasPermission('energy:bill:export')")
    public void exportExcel(@Valid EnergyBillPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<EnergyBillDO> pageResult = billService.getBillPage(pageReqVO);
        List<EnergyBillExcelVO> excelList = EnergyBillConvert.INSTANCE.convertExcelList(pageResult.getList());
        ExcelUtils.write(response, "能源账单.xlsx", "数据", EnergyBillExcelVO.class, excelList);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:bill:query')")
    public CommonResult<EnergyBillRespVO> getBill(@RequestParam("id") Long id) {
        EnergyBillDO bill = billService.getBill(id);
        return success(EnergyBillConvert.INSTANCE.convert(bill));
    }

    @PostMapping("/generate")
    @Operation(summary = "生成账单")
    @PreAuthorize("@ss.hasPermission('energy:bill:create')")
    public CommonResult<Long> generateBill(@Valid @RequestBody EnergyBillGenerateReqVO reqVO) {
        return success(billService.generateBill(reqVO));
    }

    @PostMapping("/batch-generate")
    @Operation(summary = "批量生成账单")
    @PreAuthorize("@ss.hasPermission('energy:bill:create')")
    public CommonResult<List<Long>> batchGenerateBills(@Valid @RequestBody List<EnergyBillGenerateReqVO> reqVOs) {
        return success(billService.batchGenerateBills(reqVOs));
    }

    @PostMapping("/batch-generate-by-period")
    @Operation(summary = "按月批量生成账单")
    @PreAuthorize("@ss.hasPermission('energy:bill:create')")
    public CommonResult<Map<String, Integer>> batchGenerateByPeriod(@RequestParam("billPeriod") String billPeriod) {
        return success(billService.batchGenerateByPeriod(billPeriod));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单")
    @PreAuthorize("@ss.hasPermission('energy:bill:update')")
    public CommonResult<Boolean> updateBill(@Valid @RequestBody EnergyBillSaveReqVO reqVO) {
        billService.updateBill(reqVO);
        return success(true);
    }

    @PostMapping("/audit")
    @Operation(summary = "审核账单")
    @PreAuthorize("@ss.hasPermission('energy:bill:audit')")
    public CommonResult<Boolean> auditBill(@RequestParam("id") Long id,
                                            @RequestParam("approved") Boolean approved,
                                            @RequestParam(value = "remark", required = false) String remark) {
        billService.auditBill(id, approved, remark);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:bill:delete')")
    public CommonResult<Boolean> deleteBill(@RequestParam("id") Long id) {
        billService.deleteBill(id);
        return success(true);
    }

    @PostMapping("/adjustment/create")
    @Operation(summary = "创建调整行")
    @PreAuthorize("@ss.hasPermission('energy:bill:update')")
    public CommonResult<Long> createAdjustment(@Valid @RequestBody EnergyBillAdjustmentSaveReqVO reqVO) {
        return success(billService.createAdjustment(reqVO));
    }

    @DeleteMapping("/adjustment/delete")
    @Operation(summary = "删除调整行")
    @Parameter(name = "id", description = "调整行ID", required = true)
    @PreAuthorize("@ss.hasPermission('energy:bill:update')")
    public CommonResult<Boolean> deleteAdjustment(@RequestParam("id") Long id) {
        billService.deleteAdjustment(id);
        return success(true);
    }

    @GetMapping("/detail-list")
    @Operation(summary = "获得账单关联明细列表")
    @Parameter(name = "billId", description = "账单ID", required = true)
    @PreAuthorize("@ss.hasPermission('energy:bill:query')")
    public CommonResult<List<HydrogenDetailRespVO>> getBillDetailList(@RequestParam("billId") Long billId) {
        return success(billService.getBillDetailList(billId));
    }

    @GetMapping("/adjustment/list")
    @Operation(summary = "获得账单调整记录列表")
    @Parameter(name = "billId", description = "账单ID", required = true)
    @PreAuthorize("@ss.hasPermission('energy:bill:query')")
    public CommonResult<List<EnergyBillAdjustmentRespVO>> getAdjustmentList(@RequestParam("billId") Long billId) {
        return success(billService.getAdjustmentList(billId));
    }
}
