package org.springblade.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.common.base.BaseEntity;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.PaymentTypeEnum;
import org.springblade.common.enums.WithdrawalStateEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.platform.entity.BalanceRecord;
import org.springblade.modules.platform.service.IBalanceRecordService;
import org.springblade.modules.platform.vo.BalanceRecordVO;
import org.springblade.modules.platform.wrapper.BalanceRecordWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author pangqijun
 * Date 2023/7/10
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL + "/balance")
@Api(value = "余额相关接口", tags = "16.余额相关接口")
public class BalanceController {

    @Resource
    private IBalanceRecordService balanceRecordService;

    @GetMapping("/income/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "收益明细列表")
    public R<List<BalanceRecordVO>> incomeList(Query query) {
        LambdaQueryWrapper<BalanceRecord> wrapper = Wrappers.lambdaQuery(BalanceRecord.class).eq(BalanceRecord::getUserId, AuthUtil.getUserId());
        wrapper.orderByDesc(BaseEntity::getCreateTime);
        IPage<BalanceRecord> pages = balanceRecordService.page(Condition.getPage(query), wrapper);
        return R.data(BalanceRecordWrapper.build().listVO(pages.getRecords()));
    }

    @GetMapping("/recharge-record/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "充值记录列表")
    public R<List<BalanceRecordVO>> rechargeRecord(Query query) {
        LambdaQueryWrapper<BalanceRecord> wrapper = Wrappers.lambdaQuery(BalanceRecord.class).eq(BalanceRecord::getUserId, AuthUtil.getUserId());
        wrapper.eq(BalanceRecord::getType, 1).orderByDesc(BaseEntity::getCreateTime);
        IPage<BalanceRecord> pages = balanceRecordService.page(Condition.getPage(query), wrapper);
        List<BalanceRecordVO> vos = BalanceRecordWrapper.build().listVO(pages.getRecords());
        for (BalanceRecordVO vo : vos) {
            if (PaymentTypeEnum.ALI_PAY.getValue().equals(vo.getTradeMode())) {
                vo.setDescription("支付宝充值");
            } else {
                vo.setDescription("微信充值");
            }
        }
        return R.data(vos);
    }

    @GetMapping("/withdrawal-record/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "提现记录列表")
    public R<List<BalanceRecordVO>> withdrawalRecord(Query query) {
        LambdaQueryWrapper<BalanceRecord> wrapper = Wrappers.lambdaQuery(BalanceRecord.class).eq(BalanceRecord::getUserId, AuthUtil.getUserId());
        wrapper.eq(BalanceRecord::getType, 2).orderByDesc(BaseEntity::getCreateTime);
        IPage<BalanceRecord> pages = balanceRecordService.page(Condition.getPage(query), wrapper);
        List<BalanceRecordVO> vos = BalanceRecordWrapper.build().listVO(pages.getRecords());
        for (BalanceRecordVO vo : vos) {
            vo.setWithdrawalStatusDesc(WithdrawalStateEnum.getNameByValue(vo.getWithdrawalStatus()));
            if (PaymentTypeEnum.ALI_PAY.getValue().equals(vo.getTradeMode())) {
                vo.setDescription("提现到支付宝");
            } else {
                vo.setDescription("提现到微信");
            }
        }
        return R.data(vos);
    }
}
