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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.BalancePageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.BalanceDO;
import cn.iocoder.yudao.module.system.service.biz.BalanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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


@Tag(name = "管理后台 - 长者余额")
@RestController
@RequestMapping("/elderly/balance")
@Validated
public class BalanceController {

    @Resource
    private BalanceService balanceService;

    @PostMapping("/recharge")
    @Operation(summary = "充值余额")
    @TenantIgnore
    public CommonResult<Boolean> rechargeBalance(@Valid @RequestBody BalanceDO createReqVO) {
        balanceService.rechargeBalance(createReqVO);
        return success(true);
    }

    @PutMapping("/refund")
    @Operation(summary = "余额退款")
    @TenantIgnore
    public CommonResult<Boolean> refund(@Valid @RequestBody BalanceDO updateReqVO) {
        balanceService.refund(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者余额流水详情")
    @Parameter(name = "elderId", description = "长者ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<BalanceDO> getBalance(@RequestParam("elderId") Long elderId) {
        BalanceDO balance = balanceService.getBalance(elderId);
        return success(balance);
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者余额分页")
    @TenantIgnore
    public CommonResult<PageResult<BalanceDO>> getBalancePage(@Valid BalancePageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<BalanceDO> pageResult = balanceService.getBalancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BalanceDO.class));
    }


    @GetMapping("/getBalanceAmountByElderId")
    @Operation(summary = "获得长者余额")
    @TenantIgnore
    public CommonResult<BalanceDO>  getBalanceAmountByElderId(@RequestParam("elderId") Long elderId) {
        BalanceDO balance = balanceService.getBalanceAmountByElderId(elderId);
        return success(balance);
    }





    @GetMapping("/test")
    public CommonResult<List<Map<String, BigDecimal>>> test() {
        return success(balanceService.test());
    }



}