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

import com.github.microservice.net.ResultContent;
import com.github.microservice.pay.client.model.chinaSenseless.SatInfo;
import com.github.microservice.pay.client.model.ledger.TransactionLogModel;
import com.github.microservice.pay.client.model.ledger.transaction.TransactionLogAggregateRetModel;
import com.github.microservice.pay.client.ret.ResultState;
import com.zhongshu.card.client.model.pay.*;
import com.zhongshu.card.server.core.model.pay.UnionFrictionlessPayFinishModel;
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.OrgOverviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PayOpenApiService {

    @Autowired
    BalancePayService balancePayService;

    @Autowired
    ChinaumsSenselessPayService chinaumsSenselessPayService;

    @Autowired
    OrgOverviewService orgOverviewService;

    /**
     * 查询钱包余额
     */
    public ResultContent<Long> queryWallet(String projectOid, String userId){
        com.github.microservice.pay.client.ret.ResultContent<Long> resultContent = balancePayService.queryUserBalance(projectOid, userId);
        if (resultContent.getState() == ResultState.Success){
            return ResultContent.buildContent(resultContent.getContent());
        }
        return ResultContent.buildFail(resultContent.getMsg());
    }

    /**
     * 余额支付
     */
    public ResultContent<List<TransactionLogModel>> walletPay(String projectOid, String oid, String userId, BigDecimal total, String orderNo, String remark){
        com.github.microservice.pay.client.ret.ResultContent resultContent = balancePayService.balancePay(projectOid, oid, userId, total, orderNo, remark);
        if (resultContent.getState() == ResultState.Success){
            return ResultContent.buildContent(resultContent.getContent());
        }
        return ResultContent.buildFail(resultContent.getMsg());
    }

    /**
     * 余额支付退款
     */
    public ResultContent<List<TransactionLogModel>> walletRefund(String projectOid, String oid, String userId, BigDecimal total, String orderNo, String remark){
        com.github.microservice.pay.client.ret.ResultContent<List<TransactionLogModel>> resultContent = balancePayService.refund(projectOid, oid, userId, orderNo, total, remark);
        if (resultContent.getState() == ResultState.Success){
            return ResultContent.buildContent(resultContent.getContent());
        }
        return ResultContent.buildFail(resultContent.getMsg());
    }

    /**
     * 充值记录查询
     */
    public ResultContent<QueryRechargeOrderModel> rechargeQuery(QueryRechargeParam param){

        Assert.notNull(param.getYear(), "参数校验失败：year is null");
        Assert.notNull(param.getMonth(), "参数校验失败：month is null");
        Assert.notNull(param.getProjectOid(), "参数校验失败：projectOid is null");
        Assert.notNull(param.getUserId(), "参数校验失败：userId is null");

        com.github.microservice.pay.client.ret.ResultContent resultContent = balancePayService.queryRechargeOrder(param);
        if (resultContent.getState() == ResultState.Success){
            return ResultContent.buildContent(resultContent.getContent());
        }
        return ResultContent.buildFail(resultContent.getMsg());
    }

    /**
     * 查询银联无感支付签约状态
     */
    public ResultContent<ChinaumsSenselessUserSignInfoModel> unionSenselessSignQuery(String projectOid, String userId){
        Assert.notNull(projectOid, "参数校验失败：projectOid is null");
        Assert.notNull(userId, "参数校验失败：userId is null");
        return chinaumsSenselessPayService.queryUserSignInfo(userId, projectOid);
    }

    /**
     * 银联无感支付签约
     */
    public ResultContent unionSenselessSign(UnionApplySignParam param){
        Assert.notNull(param.getInvokeScene(), "参数校验失败：invokeScene is null");
        Assert.notNull(param.getMchntWxMpAppId(), "参数校验失败：mchntWxMpAppId is null");
        Assert.notNull(param.getMchntWxMpPath(), "参数校验失败：mchntWxMpPath is null");
        Assert.notNull(param.getProjectOid(), "参数校验失败：projectOid is null");
        Assert.notNull(param.getUserId(), "参数校验失败：userId is null");
        return chinaumsSenselessPayService.signApply(param);
    }

    /**
     * 银联无感支付
     */
    public ResultContent unionSenselessPay(String projectOid, String oid, String userId, BigDecimal total, String orderNo, String remark, List<SatInfoModel> satInfo){
        Assert.notNull(projectOid, "参数校验失败：projectOid is null");
        Assert.notNull(oid, "参数校验失败：oid is null");
        Assert.notNull(userId, "参数校验失败：userId is null");
        Assert.notNull(total, "参数校验失败：total is null");
        Assert.notNull(orderNo, "参数校验失败：orderNo is null");
        return chinaumsSenselessPayService.senselessPay(projectOid, oid, userId, total, orderNo, remark, satInfo);
    }

    /**
     * 银联无感支付退款
     */
    public ResultContent<UnionFrictionlessPayFinishModel> unionSenselessRefund(String projectOid, String oid, String userId, BigDecimal total, String orderNo, String refundOrderNo, String remark, String targetOrderId){
        Assert.notNull(projectOid, "参数校验失败：projectOid is null");
        Assert.notNull(oid, "参数校验失败：oid is null");
        Assert.notNull(userId, "参数校验失败：userId is null");
        Assert.notNull(total, "参数校验失败：total is null");
        Assert.notNull(orderNo, "参数校验失败：orderNo is null");
        Assert.notNull(refundOrderNo, "参数校验失败：refundOrderNo is null");
        Assert.notNull(targetOrderId, "参数校验失败：targetOrderId is null");
        return chinaumsSenselessPayService.refund(projectOid, oid, userId, total, orderNo, refundOrderNo, remark, targetOrderId);
    }

    /**
     * 银联无感支付退款查询
     */
    public ResultContent<UnionFrictionlessPayFinishModel> unionSenselessRefundQuery(String projectOid, String oid, String userId, String refundOrderNo){
        return chinaumsSenselessPayService.refundQuery(projectOid, userId, oid, refundOrderNo, false);
    }

    /**
     * 用户流水查询
     */
    public ResultContent<TransactionLogAggregateRetModel> queryTransLog(Pageable pageable, QueryTransLogParam param){
        return orgOverviewService.queryAccountTreeFlow(pageable, param);
    }

}
