package org.dromara.app;

import com.wzj.soopin.transaction.business.IChargeBusiness;
import com.wzj.soopin.transaction.domain.bo.ChargeBO;
import com.wzj.soopin.transaction.enums.PaymentClientEnum;
import com.wzj.soopin.transaction.enums.PaymentMethodEnum;
import com.wzj.soopin.transaction.kit.CashierSupport;
import com.wzj.soopin.transaction.kit.dto.PayParam;
import com.wzj.soopin.transaction.kit.params.dto.CashierParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.ResultCode;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 买家端,收银台接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@RestController
@Tag(name = "收银台接口")
@RequestMapping("/app/payment")
@RequiredArgsConstructor
public class AppPayController {

    private final IChargeBusiness chargeBusiness;
    private final CashierSupport cashierSupport;


    //    @ApiImplicitParams({
//            @ApiImplicitParam(name = "client", value = "客户端类型", paramType = "path", allowableValues = "PC,H5,WECHAT_MP,APP")
//    })
    @PostMapping(value = "/tradeDetail")
    @Operation(summary = "获取支付详情")
    public R paymentParams(@RequestBody @Validated PayParam payParam) {

        CashierParam cashierParam = cashierSupport.cashierParam(payParam);
        return R.ok( cashierParam);
    }


    @ApiImplicitParams({
        @ApiImplicitParam(name = "paymentMethod", value = "支付方式", paramType = "path", allowableValues = "WECHAT,ALIPAY"),
        @ApiImplicitParam(name = "paymentClient", value = "调起方式", paramType = "path", allowableValues = "APP,NATIVE,JSAPI,H5,MP")
    })
    @PostMapping(value = "/pay")
    @Operation(summary = "支付")
    public R payment(
        HttpServletRequest request,
        HttpServletResponse response,
        @RequestBody @Validated PayParam payParam, @RequestBody ChargeBO chargeBO) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(payParam.getPaymentMethod());
        PaymentClientEnum paymentClientEnum = PaymentClientEnum.valueOf(payParam.getPaymentClient());

        try {

            LoginUser user = LoginHelper.getLoginUser();
            if (user == null) {
                return R.notLogin();
            }
            //先生成充值订单
            chargeBO.setMemberId(user.getUserId());
            chargeBO.setMethod(paymentMethodEnum.getCode());
            chargeBusiness.charge(chargeBO);
            payParam.setSn(chargeBO.getCode());
            return cashierSupport.payment(paymentMethodEnum, paymentClientEnum, request, response, payParam);
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
        return null;


    }

    @Operation(summary = "支付回调")
    @RequestMapping(value = "/callback/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public R<Object> callback(HttpServletRequest request, @PathVariable String paymentMethod) {

        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);

        cashierSupport.callback(paymentMethodEnum, request);

        return R.ok(ResultCode.PAY_SUCCESS);
    }

    @Operation(summary = "支付异步通知")
    @RequestMapping(value = "/notify/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public void notify(HttpServletRequest request, @PathVariable String paymentMethod) {

        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);

        cashierSupport.notify(paymentMethodEnum, request);

    }

    @Operation(summary = "查询支付结果")
    @GetMapping(value = "/result")
    public R<Boolean> paymentResult(PayParam payParam) {
        return R.ok(cashierSupport.paymentResult(payParam));
    }
}
