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

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
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.constant.AllinpayConstant;
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("multishopAllinpayOrderController")
@RequestMapping("/m/allinpay/order")
public class AllinpayOrderController {

    @Autowired
    private AllinpayService allinpayService;

    @GetMapping("/queryBalance")
    @Operation(summary = "查询账户余额")
    public ServerResponseEntity<QueryBalanceResp> queryBalance() {
        String bizUserId = String.valueOf(AuthUserContext.get().getTenantId());
        QueryBalanceResp queryBalanceResp = allinpayService.queryBalance(AllinpayConstant.SHOP + bizUserId, SysTypeEnum.MULTISHOP.value());
        return ServerResponseEntity.success(queryBalanceResp);
    }

    @GetMapping("/queryInExpDetail")
    @Operation(summary = "查询账户收支明细")
    public ServerResponseEntity<PageVO<InExpDetail>> queryInExpDetail(PageDTO pageDTO,
                                                                      QueryInExpDetailDTO queryInExpDetailDTO) {
        String bizUserId = String.valueOf(AuthUserContext.get().getTenantId());
        PageVO<InExpDetail> pageVO = allinpayService.queryInExpDetail(AllinpayConstant.SHOP + 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().getTenantId());
        String url = allinpayService.payBySms(AllinpayConstant.SHOP + bizUserId, payBySmsDTO);
        return ServerResponseEntity.success(url);
    }

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

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