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

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.payment.allinpay.order.resp.PayByBackSmsResp;
import com.tmerclub.cloud.api.payment.allinpay.order.resp.QueryBalanceResp;
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.payment.allinpay.order.resp.GetOrderStatusResp;
import com.tmerclub.cloud.payment.allinpay.order.resp.InExpDetail;
import com.tmerclub.cloud.payment.allinpay.order.resp.ResendPaySmsResp;
import com.tmerclub.cloud.payment.dto.PayByBackSmsDTO;
import com.tmerclub.cloud.payment.dto.PayByPwdDTO;
import com.tmerclub.cloud.payment.dto.PayBySmsDTO;
import com.tmerclub.cloud.payment.dto.QueryInExpDetailDTO;
import com.tmerclub.cloud.payment.service.AllinpayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author gaozijie
 * @date 2023-03-28
 */
@Tag(name = "通联支付-订单接口(用户端)")
@RestController
@RequestMapping("/allinpay/order")
public class AllinpayOrderController {

    @Autowired
    private AllinpayService allinpayService;

    @GetMapping("/queryBalance")
    @Operation(summary = "查询账户余额")
    public ServerResponseEntity<QueryBalanceResp> queryBalance() {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        String bizUserId = String.valueOf(uidInfoBO.getUserId());
        QueryBalanceResp queryBalanceResp = allinpayService.queryBalance(bizUserId, uidInfoBO.getSysType());
        return ServerResponseEntity.success(queryBalanceResp);
    }

    @GetMapping("/queryInExpDetail")
    @Operation(summary = "查询账户收支明细")
    public ServerResponseEntity<PageVO<InExpDetail>> queryInExpDetail(PageDTO pageDTO,
                                                                      QueryInExpDetailDTO queryInExpDetailDTO) {
        String bizUserId = String.valueOf(AuthUserContext.get().getUserId());
        PageVO<InExpDetail> pageVO = allinpayService.queryInExpDetail(bizUserId, pageDTO, queryInExpDetailDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @PostMapping("/payConfirmByFrontSms")
    @Operation(summary = "确认支付[前台+短信验证码])", description = "通联支付申请后，前端再调用此接口获取通联页面链接")
    public ServerResponseEntity<String> payConfirmByFrontSms(@RequestBody PayBySmsDTO payBySmsDTO) {
        String bizUserId = String.valueOf(AuthUserContext.get().getUserId());
        String url = allinpayService.payBySms(bizUserId, payBySmsDTO);
        return ServerResponseEntity.success(url);
    }

    @PostMapping("/payConfirmByFrontPwd")
    @Operation(summary = "确认支付[前台+密码验证版]", description = "通联支付申请后，前端再调用此接口获取通联页面链接")
    public ServerResponseEntity<String> payConfirmByFrontPwd(@RequestBody PayByPwdDTO payByPwdDTO) {
        String bizUserId = String.valueOf(AuthUserContext.get().getUserId());
        String url = allinpayService.payByPwd(bizUserId, payByPwdDTO);
        return ServerResponseEntity.success(url);
    }

    @PostMapping("/payConfirmByBackSms")
    @Operation(summary = "确认支付[后台+短信验证码]", description = "通联支付申请后，后端再调用此接口获取通联页面链接")
    public ServerResponseEntity<PayByBackSmsResp> payConfirmByBackSms(@RequestBody PayByBackSmsDTO payByBackSmsDTO) {
        String bizUserId = String.valueOf(AuthUserContext.get().getUserId());
        PayByBackSmsResp resp = allinpayService.payByBackSms(bizUserId, payByBackSmsDTO);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/resendPaySms")
    @Operation(summary = "重新发送支付短信验证码")
    public ServerResponseEntity<ResendPaySmsResp> resendPaySms(String bizOrderNo) {
        ResendPaySmsResp resp = allinpayService.resendPaySms(bizOrderNo);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/getOrderStatus")
    @Operation(summary = "查询订单状态")
    public ServerResponseEntity<GetOrderStatusResp> getOrderStatus(String bizOrderNo) {
        String bizUserId = String.valueOf(AuthUserContext.get().getUserId());
        GetOrderStatusResp resp = allinpayService.getOrderStatus(bizUserId, bizOrderNo);
        return ServerResponseEntity.success(resp);
    }
}
