package com.tmerclub.cloud.user.controller.app;

import com.tmerclub.cloud.api.payment.allinpay.order.resp.PayByBackSmsResp;
import com.tmerclub.cloud.api.payment.allinpay.order.resp.WithdrawApplyResp;
import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.user.dto.UserWithDrawDTO;
import com.tmerclub.cloud.user.service.UserWithdrawCashService;
import com.tmerclub.cloud.user.vo.DateUserWithdrawCashVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author gaozijie
 * @date 2023-04-24
 */
@RestController("appUserWithdrawController")
@RequestMapping("/userWithdraw")
@Tag(name = "app-用户提现")
public class UserWithdrawCashController {

    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;

    @Autowired
    private UserWithdrawCashService userWithdrawCashService;

    @PostMapping("/apply")
    @Operation(summary = "提现申请(通联)", description = "申请后需调用确认支付")
    public ServerResponseEntity<WithdrawApplyResp> withdrawApply(@RequestBody UserWithDrawDTO userWithDrawDTO) {
        userWithDrawDTO.setUserId(AuthUserContext.get().getUserId());
        WithdrawApplyResp resp = userWithdrawCashService.withdrawApply(userWithDrawDTO);
        return ServerResponseEntity.success(resp);
    }

    @PostMapping("/payConfirm")
    @Operation(summary = "提现确认支付(通联)", description = "确认支付")
    public ServerResponseEntity<PayByBackSmsResp> payConfirm(@RequestBody UserWithDrawDTO userWithDrawDTO) {
        userWithDrawDTO.setUserId(AuthUserContext.get().getUserId());
        PayByBackSmsResp resp = userWithdrawCashService.payConfirm(userWithDrawDTO);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/getTotalWithdrawAmount")
    @Operation(summary = "获取用户提现总金额")
    public ServerResponseEntity<BigDecimal> getTotalWithdrawAmount() {
        BigDecimal totalWithdrawAmount = userWithdrawCashService.getTotalWithdrawAmount(AuthUserContext.get().getUserId());
        return ServerResponseEntity.success(totalWithdrawAmount);
    }

    @GetMapping("/pageDateUserWithdrawCash")
    @Operation(summary = "分页获取按年月分组的用户提现记录")
    public ServerResponseEntity<PageVO<DateUserWithdrawCashVO>> pageDateUserWithdrawCash(PageDTO pageDTO) {
        PageVO<DateUserWithdrawCashVO> pageVO = userWithdrawCashService.pageDateUserWithdrawCashVOList(AuthUserContext.get().getUserId(), pageDTO);
        return ServerResponseEntity.success(pageVO);
    }
}
