package com.yn.bftl.thirdparty.modules.unionpay.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.yn.bftl.common.modules.order.enums.PosPaymentType;
import com.yn.bftl.thirdparty.modules.log.service.LoggerService;
import com.yn.bftl.thirdparty.modules.unionpay.dto.PosAppRechargeDTO;
import com.yn.bftl.thirdparty.modules.unionpay.dto.PosPayResultDTO;
import com.yn.bftl.thirdparty.modules.unionpay.dto.PosRefundResultDTO;
import com.yn.bftl.thirdparty.modules.unionpay.service.PosPayService;
import com.yn.bftl.thirdparty.modules.unionpay.service.impl.PosPayServiceImpl;
import com.yn.bftl.thirdparty.modules.unionpay.vo.PosPayVO;
import com.yn.bftl.thirdparty.modules.unionpay.vo.PosRefundSalesVO;
import com.yn.bftl.thirdparty.modules.unionpay.vo.PosRefundVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Author：Vance
 * @Description：pos控制层
 * @Date：2022/9/10 10:20
 */
@Api(value = "PosMemberController", tags = "用户端-POS")
@RestController
@RequestMapping("/member/pos")
@Validated
@RequiredArgsConstructor
public class PosMemberController {

    private final PosPayService posPayService;
    private final LoggerService loggerService;

    @ApiOperation("POS-下单")
    @PostMapping("/app_order")
    public PosPayVO appOrder(@NotNull @RequestParam Long orderId,
                             @NotBlank @RequestParam String hardwareSn,
                             @NotNull @RequestParam PosPaymentType payType) {
        return posPayService.appOrder(orderId, hardwareSn, payType);
    }

    @ApiOperation("POS-退款（撤销-只能当天）")
    @PostMapping("/refund")
    public PosRefundVO refund(@NotNull @RequestParam Long orderId) {
        return posPayService.refund(orderId);
    }

    @ApiOperation("POS-退款（退货-隔日退货）")
    @PostMapping("/refund_sales")
    public PosRefundSalesVO refundSales(@NotNull @RequestParam Long orderId) {
        return posPayService.refundSales(orderId);
    }

    @ApiOperation("POS-支付回调")
    @PostMapping("/call_back")
    public Boolean callBack(@RequestBody PosPayResultDTO dto) {
        loggerService.unionpay("[银联POS回调]银联POS回调信息", (JSONObject) JSONObject.toJSON(dto), PosPayServiceImpl.class.getName(), "payCallBack", "posUnionpay");
        return posPayService.payCallBack(dto);
    }

    @ApiOperation("POS-退款、退货回调")
    @PostMapping("/refund_call_back")
    public Boolean refundCallBack(@RequestBody PosRefundResultDTO dto) {
        loggerService.unionpay("[银联POS退款、退货回调]银联POS退款、退货回调信息", (JSONObject) JSONObject.toJSON(dto), PosPayServiceImpl.class.getName(), "refundCallBack", "posUnionpay");
        return posPayService.refundCallBack(dto);
    }

    @ApiOperation("POS-充值下单")
    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    public PosPayVO appRecharge(@Valid @RequestBody PosAppRechargeDTO posAppRechargeDTO) {
        return posPayService.appRecharge(posAppRechargeDTO);
    }

    @ApiOperation("POS-充值退款（撤销-只能当天）")
    @PostMapping("/recharge_refund")
    public PosRefundVO rechargeRefund(@NotNull @RequestParam Long topUpOrderId) {
        return posPayService.rechargeRefund(topUpOrderId);
    }

    @ApiOperation("POS-充值支付回调")
    @PostMapping("/recharge/call_back")
    public Boolean rechargeCallBack(@RequestBody PosPayResultDTO dto) {
        loggerService.unionpay("[银联POS充值回调]银联POS充值回调信息", (JSONObject) JSONObject.toJSON(dto), PosPayServiceImpl.class.getName(), "rechargeCallBack", "posUnionpay");
        return posPayService.rechargeCallBack(dto);
    }

    @ApiOperation("POS-充值退款回调")
    @PostMapping("/recharge_refund_call_back")
    public Boolean rechargeRefundCallBack(@RequestBody PosRefundResultDTO dto) {
        loggerService.unionpay("[银联POS充值退款回调]银联POS充值退款回调信息", (JSONObject) JSONObject.toJSON(dto), PosPayServiceImpl.class.getName(), "rechargeRefundCallBack", "posUnionpay");
        return posPayService.rechargeRefundCallBack(dto);
    }
}
