package com.seer.teach.pay.app.wallet.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogOperation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ModuleEnum;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.pay.app.wallet.resp.AppCoinRechargeResp;
import com.seer.teach.pay.app.recharge.convert.AppCoinRechargeConvert;
import com.seer.teach.pay.app.recharge.service.IAppRechargeService;
import com.seer.teach.pay.entity.CoinRechargeEntity;
import com.seer.teach.pay.app.wallet.req.AppCoinRechargeReq;
import com.seer.teach.pay.service.ICoinRechargeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "App -钱包")
@Validated
@LogPrint
@DecryptionAnnotation
@EncryptionAnnotation
@RequiredArgsConstructor
@RequestMapping("/app/wallet/recharge")
@SaCheckLogin(type = StpUtil.TYPE)
public class AppCoinRechargeController {

    private final ICoinRechargeService coinRechargeService;

    private final IAppRechargeService appRechargeService;

    @Operation(summary = "创建充值支付订单")
    @PostMapping
    @SaCheckPermission("app:pay:recharge:create")
    @SaCheckLogin(type = StpUtil.TYPE)
    @LogOperation(module = ModuleEnum.MALL_WECHAT, subModule = "获取小程序的支付参数")
    public ResultBean<String> createWalletRechargePayOrder(@RequestBody @Validated AppCoinRechargeReq appCoinRechargeReq) {
        return ResultBean.success(appRechargeService.createWalletRechargePayOrder(appCoinRechargeReq));
    }


    @Operation(summary = "我的充值记录详情")
    @GetMapping("/records/{rechargeId}")
    @SaCheckPermission("app:pay:recharge:detail")
    public ResultBean<AppCoinRechargeResp> detail(
            @Parameter(description = "充值记录Id") @PathVariable("rechargeId") Integer rechargeId) {
        CoinRechargeEntity coinRecharge = coinRechargeService.myRechargeDetail(rechargeId);
        return ResultBean.success(AppCoinRechargeConvert.INSTANCE.convertOne(coinRecharge));
    }

    @Operation(summary = "我的充值记录列表")
    @GetMapping("/records/{size}/{current}")
    @SaCheckPermission("app:pay:recharge:list")
    public ResultBean<PageListBean<AppCoinRechargeResp>> page(
            @Parameter(description = "每页数量") @PathVariable("size") Integer size,
            @Parameter(description = "当前页") @PathVariable("current") Integer current,
            @Parameter(description = "充值状态，0:待支付, 1: 支付成功") @RequestParam(name = "payStatus", required = false) Integer payStatus) {
        IPage<CoinRechargeEntity> coinRechargeList = coinRechargeService.myRechargeList(size,current,payStatus);
        PageListBean<AppCoinRechargeResp> coinRechargeEntityPageListBean = PageConverterUtils.convertPageListBean(coinRechargeList, AppCoinRechargeConvert.INSTANCE::convertList);
        return ResultBean.success(coinRechargeEntityPageListBean);
    }
}