package com.zhongshu.card.server.core.controller.openAPI;

import com.github.microservice.net.ResultContent;
import com.github.microservice.pay.client.model.ledger.TransactionLogModel;
import com.github.microservice.pay.client.model.ledger.transaction.TransactionLogAggregateRetModel;
import com.github.microservice.types.payment.PaymentType;
import com.zhongshu.card.client.model.pay.*;
import com.zhongshu.card.client.openApi.RefundQueryParam;
import com.zhongshu.card.client.openApi.params.PayParam;
import com.zhongshu.card.client.openApi.params.RefundParam;
import com.zhongshu.card.server.core.model.pay.UnionFrictionlessPayFinishModel;
import com.zhongshu.card.server.core.service.openAPI.PayOpenApiService;
import com.zhongshu.card.server.core.service.pay.BalancePayService;
import com.zhongshu.card.server.core.service.pay.ChinaumsSenselessPayService;
import com.zhongshu.card.server.core.service.pay.PayAccountService;
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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/openAPI/v1/pay")
@Tag(name = "openAPI-支付管理")
public class PayOpenApiController {

    @Autowired
    PayOpenApiService payOpenApiService;

    @Operation(summary = "查询个人钱包账户余额", description = "查询个人钱包账户余额")
    @RequestMapping(value = "queryWallet", method = {RequestMethod.GET})
    public ResultContent<Long> queryWallet(@RequestParam("projectOid") String projectOid,
                                               @RequestParam("userId") String userId){
        return payOpenApiService.queryWallet(projectOid, userId);
    }

    @Operation(summary = "余额支付", description = "余额支付")
    @RequestMapping(value = "walletPay", method = {RequestMethod.POST})
    public ResultContent<List<TransactionLogModel>> walletPay(@RequestBody PayParam param){
       return payOpenApiService.walletPay(param.getProjectOid(), param.getOid(), param.getUserId(), param.getTotal(), param.getOrderNo(), param.getRemark());
    }

    @Operation(summary = "余额支付退款", description = "余额支付退款")
    @RequestMapping(value = "walletRefund", method = {RequestMethod.POST})
    public ResultContent<List<TransactionLogModel>> walletRefund(@RequestBody RefundParam param){
       return payOpenApiService.walletRefund(param.getProjectOid(), param.getOid(), param.getUserId(), param.getTotal(), param.getOrderNo(), param.getRemark());
    }

    @Operation(summary = "充值记录查询", description = "充值记录查询")
    @RequestMapping(value = "rechargeQuery", method = {RequestMethod.POST})
    public ResultContent<QueryRechargeOrderModel> rechargeQuery(@RequestBody QueryRechargeParam param){
        return payOpenApiService.rechargeQuery(param);
    }

    /**
     * 查询银联无感支付签约状态
     */
    @Operation(summary = "查询银联无感支付签约状态", description = "查询银联无感支付签约状态")
    @RequestMapping(value = "unionSenselessSignQuery", method = {RequestMethod.GET})
    public ResultContent<ChinaumsSenselessUserSignInfoModel> unionSenselessSignQuery(@RequestParam("projectOid") String projectOid,
                                                                                     @RequestParam("userId") String userId){
        return payOpenApiService.unionSenselessSignQuery(projectOid, userId);
    }

    @Operation(summary = "银联无感支付签约", description = "银联无感支付签约")
    @RequestMapping(value = "unionSenselessSign", method = {RequestMethod.POST})
    public ResultContent<Object> unionSenselessSign(@RequestBody UnionApplySignParam param){
        return payOpenApiService.unionSenselessSign(param);
    }

    @Operation(summary = "银联无感支付扣费", description = "银联无感支付扣费")
    @RequestMapping(value = "unionSenselessPay", method = {RequestMethod.POST})
    public ResultContent<Object> unionSenselessPay(@RequestBody PayParam param){
        return payOpenApiService.unionSenselessPay(param.getProjectOid(), param.getOid(), param.getUserId(), param.getTotal(), param.getOrderNo(), param.getRemark(), param.getSatInfo());
    }

    @Operation(summary = "银联无感支付退款", description = "银联无感支付退款")
    @RequestMapping(value = "unionSenselessRefund", method = {RequestMethod.POST})
    public ResultContent<UnionFrictionlessPayFinishModel> unionSenselessRefund(@RequestBody RefundParam param){
        return payOpenApiService.unionSenselessRefund(param.getProjectOid(), param.getOid(), param.getUserId(), param.getTotal(), param.getOrderNo(), param.getRefundOrderNo(), param.getRemark(), param.getTargetOrderId());
    }

    @Operation(summary = "银联无感支付退款查询", description = "银联无感支付退款查询")
    @RequestMapping(value = "unionSenselessRefundQuery", method = {RequestMethod.POST})
    public ResultContent<UnionFrictionlessPayFinishModel> unionSenselessRefundQuery(@RequestBody RefundQueryParam param){
        return payOpenApiService.unionSenselessRefundQuery(param.getProjectOid(), param.getOid(), param.getUserId(), param.getRefundOrderNo());
    }

    /**
     * 用户流水查询
     */
    @Operation(summary = "查询用户流水-分页查询", description = "查询用户流水-分页查询")
    @RequestMapping(value = {"queryTransLog"}, method = {RequestMethod.POST})
    public ResultContent<TransactionLogAggregateRetModel> queryTransLog(@RequestBody QueryTransLogParam param){
        Pageable pageable = PageRequest.of(param.getNumber(), param.getSize());
        return payOpenApiService.queryTransLog(pageable, param);
    }

}
