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

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.account.vo.*;
import cn.iocoder.yudao.module.energy.convert.account.EnergyAccountConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.account.EnergyAccountDO;
import cn.iocoder.yudao.module.energy.dal.dataobject.account.EnergyAccountFlowDO;
import cn.iocoder.yudao.module.energy.dal.dataobject.account.EnergyProjectAccountDO;
import cn.iocoder.yudao.module.energy.service.account.EnergyAccountService;
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.math.BigDecimal;
import java.util.List;

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

@Tag(name = "管理后台 - 能源账户管理")
@RestController
@RequestMapping("/energy/account")
@Validated
public class EnergyAccountController {

    @Resource
    private EnergyAccountService accountService;

    @GetMapping("/page")
    @Operation(summary = "获得账户分页")
    @PreAuthorize("@ss.hasPermission('energy:account:query')")
    public CommonResult<PageResult<EnergyAccountRespVO>> getAccountPage(@Valid EnergyAccountPageReqVO pageReqVO) {
        PageResult<EnergyAccountDO> pageResult = accountService.getAccountPage(pageReqVO);
        return success(EnergyAccountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能源账户 Excel")
    @PreAuthorize("@ss.hasPermission('energy:account:export')")
    public void exportExcel(@Valid EnergyAccountPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<EnergyAccountDO> pageResult = accountService.getAccountPage(pageReqVO);
        List<EnergyAccountExcelVO> excelList = EnergyAccountConvert.INSTANCE.convertExcelList(pageResult.getList());
        ExcelUtils.write(response, "能源账户.xlsx", "数据", EnergyAccountExcelVO.class, excelList);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账户详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:account:query')")
    public CommonResult<EnergyAccountRespVO> getAccount(@RequestParam("id") Long id) {
        EnergyAccountDO account = accountService.getAccount(id);
        return success(EnergyAccountConvert.INSTANCE.convert(account));
    }

    @PostMapping("/recharge")
    @Operation(summary = "账户充值")
    @PreAuthorize("@ss.hasPermission('energy:account:recharge')")
    public CommonResult<Boolean> recharge(@RequestParam("customerId") Long customerId,
                                          @RequestParam("amount") BigDecimal amount,
                                          @RequestParam(value = "remark", required = false) String remark) {
        accountService.recharge(customerId, amount, null, null, remark);
        return success(true);
    }

    @PutMapping("/update-threshold")
    @Operation(summary = "更新预警阈值")
    @PreAuthorize("@ss.hasPermission('energy:account:update')")
    public CommonResult<Boolean> updateThreshold(@RequestParam("id") Long id,
                                                  @RequestParam("threshold") BigDecimal threshold) {
        accountService.updateThreshold(id, threshold);
        return success(true);
    }

    // ===== 项目账户 =====

    @GetMapping("/project/list")
    @Operation(summary = "获得项目账户列表")
    @Parameter(name = "accountId", description = "总账户ID", required = true)
    @PreAuthorize("@ss.hasPermission('energy:account:query')")
    public CommonResult<List<EnergyProjectAccountRespVO>> getProjectAccountList(@RequestParam("accountId") Long accountId) {
        List<EnergyProjectAccountDO> list = accountService.getProjectAccountsByAccountId(accountId);
        return success(cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList(list, EnergyAccountConvert.INSTANCE::convert));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得账户简单列表")
    public CommonResult<List<EnergyAccountSimpleVO>> getSimpleList() {
        List<EnergyAccountDO> list = accountService.getAccountList();
        return success(EnergyAccountConvert.INSTANCE.convertSimpleList(list));
    }

    @GetMapping("/summary")
    @Operation(summary = "获得账户汇总统计")
    @PreAuthorize("@ss.hasPermission('energy:account:query')")
    public CommonResult<EnergyAccountSummaryVO> getAccountSummary() {
        return success(accountService.getAccountSummary());
    }

    // ===== 流水查询 =====

    @GetMapping("/flow/page")
    @Operation(summary = "获得流水分页")
    @PreAuthorize("@ss.hasPermission('energy:account:query')")
    public CommonResult<PageResult<EnergyAccountFlowDO>> getFlowPage(@Valid EnergyAccountFlowPageReqVO pageReqVO) {
        return success(accountService.getFlowPage(pageReqVO));
    }
}
