package cn.iocoder.yudao.module.system.controller.admin.tenantbalance;

import org.springframework.web.bind.annotation.*;
import javax.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 javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.tenantbalance.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantbalance.TenantBalanceDO;
import cn.iocoder.yudao.module.system.service.tenantbalance.TenantBalanceService;

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

    @Resource
    private TenantBalanceService tenantBalanceService;

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

    @PutMapping("/update")
    @Operation(summary = "更新租户余额")
    @PreAuthorize("@ss.hasPermission('system: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('system: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('system: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('system: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('system:tenant-balance:query')")
    public CommonResult<PageResult<TenantBalanceRespVO>> getTenantBalancePage(@Valid TenantBalancePageReqVO pageReqVO) {
        PageResult<TenantBalanceRespVO> tenantBalancePage = tenantBalanceService.getTenantBalancePage(pageReqVO);
        return success(BeanUtils.toBean(tenantBalancePage, TenantBalanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户余额 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantBalanceExcel(@Valid TenantBalancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantBalanceRespVO> 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);
    }

    @PostMapping("/transfer")
    @Operation(summary = "租户余额转账")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:transfer')")
    public CommonResult<Boolean>  transferToTenant(@Valid @RequestBody TenantBalanceTransferReqVO transferReqVO) {
        tenantBalanceService.transferToTenant(transferReqVO);
        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));
    }


}