package com.yolo.keyboard.controller.admin.tenantbalance;

import com.yolo.keyboard.dal.dataobject.tenantbalancetransaction.TenantBalanceTransactionDO;
import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.tenantbalance.vo.*;
import com.yolo.keyboard.dal.dataobject.tenantbalance.TenantBalanceDO;
import com.yolo.keyboard.service.tenantbalance.TenantBalanceService;

@Tag(name = "管理后台 - 租户余额")
@RestController
@RequestMapping("/keyboard/tenant-balance")
@Validated
public class TenantBalanceController {

    @Resource
    private TenantBalanceService tenantBalanceService;

    @PostMapping("/create")
    @Operation(summary = "创建租户余额")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:create')")
    public CommonResult<Long> createTenantBalance(@Valid @RequestBody TenantBalanceSaveReqVO createReqVO) {
        return success(tenantBalanceService.createTenantBalance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户余额")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:update')")
    public CommonResult<Boolean> updateTenantBalance(@Valid @RequestBody TenantBalanceSaveReqVO updateReqVO) {
        tenantBalanceService.updateTenantBalance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户余额")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:delete')")
    public CommonResult<Boolean> deleteTenantBalance(@RequestParam("id") Long id) {
        tenantBalanceService.deleteTenantBalance(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户余额")
                @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:delete')")
    public CommonResult<Boolean> deleteTenantBalanceList(@RequestParam("ids") List<Long> ids) {
        tenantBalanceService.deleteTenantBalanceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户余额")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:query')")
    public CommonResult<TenantBalanceRespVO> getTenantBalance(@RequestParam("id") Long id) {
        TenantBalanceDO tenantBalance = tenantBalanceService.getTenantBalance(id);
        return success(BeanUtils.toBean(tenantBalance, TenantBalanceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户余额分页")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:query')")
    public CommonResult<PageResult<TenantBalanceRespVO>> getTenantBalancePage(@Valid TenantBalancePageReqVO pageReqVO) {
        PageResult<TenantBalanceDO> pageResult = tenantBalanceService.getTenantBalancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantBalanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户余额 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantBalanceExcel(@Valid TenantBalancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantBalanceDO> list = tenantBalanceService.getTenantBalancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户余额.xls", "数据", TenantBalanceRespVO.class,
                        BeanUtils.toBean(list, TenantBalanceRespVO.class));
    }

    @PostMapping("/addAmount")
    @Operation(summary = "添加租户余额")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:add')")
    public CommonResult<Boolean>  addTenantBalance(@Valid @RequestBody TenantBalanceAddReqVO addReqVO) {
        tenantBalanceService.addTenantBalance(addReqVO);
        return success(true);
    }

    @GetMapping("/get-self-amount")
    @Operation(summary = "获得自己的余额")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:self-amount')")
    public CommonResult<TenantBalanceRespVO> getTenantBalance() {
        TenantBalanceDO tenantBalance = tenantBalanceService.getSelfBalance();
        return success(BeanUtils.toBean(tenantBalance, TenantBalanceRespVO.class));
    }

    @GetMapping("/get-self-subordinate-amount-page")
    @Operation(summary = "获得自己下级余额的分页")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:self-subordinate')")
    public CommonResult<PageResult<TenantBalanceRespVO>> getSelfSubordinate(@Valid TenantBalancePageReqVO pageReqVO) {
        PageResult<TenantBalanceRespVO> tenantBalancePage = tenantBalanceService.getSelfSubordinateTenantBalancePage(pageReqVO);
        return success(BeanUtils.toBean(tenantBalancePage, TenantBalanceRespVO.class));
    }

    @PostMapping("/withdraw")
    @Operation(summary = "租户提现")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:withdraw')")
    public CommonResult<Boolean> withdraw(@Valid @RequestBody TenantBalanceWithdrawReqVO withdrawReqVO) {
        tenantBalanceService.withdraw(withdrawReqVO);
        return success(true);
    }


    // ==================== 子表（租户积分记录） ====================

    @GetMapping("/tenant-balance-transaction/page")
    @Operation(summary = "获得租户积分记录分页")
    @Parameter(name = "tenantId", description = "租户 Id")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:query')")
    public CommonResult<PageResult<TenantBalanceTransactionDO>> getTenantBalanceTransactionPage(PageParam pageReqVO,
                                                                                                @RequestParam("tenantId") Long tenantId) {
        return success(tenantBalanceService.getTenantBalanceTransactionPage(pageReqVO, tenantId));
    }

    @PostMapping("/tenant-balance-transaction/create")
    @Operation(summary = "创建租户积分记录")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:create')")
    public CommonResult<Long> createTenantBalanceTransaction(@Valid @RequestBody TenantBalanceTransactionDO tenantBalanceTransaction) {
        return success(tenantBalanceService.createTenantBalanceTransaction(tenantBalanceTransaction));
    }

    @PutMapping("/tenant-balance-transaction/update")
    @Operation(summary = "更新租户积分记录")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:update')")
    public CommonResult<Boolean> updateTenantBalanceTransaction(@Valid @RequestBody TenantBalanceTransactionDO tenantBalanceTransaction) {
        tenantBalanceService.updateTenantBalanceTransaction(tenantBalanceTransaction);
        return success(true);
    }

    @DeleteMapping("/tenant-balance-transaction/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除租户积分记录")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:delete')")
    public CommonResult<Boolean> deleteTenantBalanceTransaction(@RequestParam("id") Long id) {
        tenantBalanceService.deleteTenantBalanceTransaction(id);
        return success(true);
    }

    @DeleteMapping("/tenant-balance-transaction/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户积分记录")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:delete')")
    public CommonResult<Boolean> deleteTenantBalanceTransactionList(@RequestParam("ids") List<Long> ids) {
        tenantBalanceService.deleteTenantBalanceTransactionListByIds(ids);
        return success(true);
    }

    @GetMapping("/tenant-balance-transaction/get")
    @Operation(summary = "获得租户积分记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-balance:query')")
    public CommonResult<TenantBalanceTransactionDO> getTenantBalanceTransaction(@RequestParam("id") Long id) {
        return success(tenantBalanceService.getTenantBalanceTransaction(id));
    }
}