package com.izouma.dingdong.service;

import com.izouma.dingdong.config.KsherPaySdk;
import com.izouma.dingdong.config.WxPayProperties;
import com.izouma.dingdong.domain.FeeRecord;
import com.izouma.dingdong.domain.OrderInfo;
import com.izouma.dingdong.enums.PayMethod;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.utils.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class ConsumptionService {

    @Value("${ksher.appid}")
    private String appid;

    @Value("${ksher.private_key}")
    private String privateKey;

    private WxPayProperties properties;

    /*
    支付类型
    CNY 人民币
    THB 泰株
     */
    private String feeType = "CNY";

    public ConsumptionService(WxPayProperties properties) {
        this.properties = properties;
    }

    private KsherPaySdk getSdk() {
        return new KsherPaySdk(appid, privateKey);
    }

    /*
    app支付 只能微信和支付宝
    */
    public String appPay(OrderInfo orderInfo) {
        //ksher支付SDK
        KsherPaySdk ksherPaySdk = getSdk();
        //商家产生的唯一no
        String mchOrderNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
        //总数 分为单位
        Integer total = orderInfo.getRealAmount().multiply(BigDecimal.valueOf(100)).intValue();
        //支付方式
        String channel = "wechat";
        if (PayMethod.ALI_PAY.equals(orderInfo.getPayMethod())) {
            channel = "alipay";
        }

        String subOpenid = null;
        if (orderInfo.getUser().getOpenId() != null) {
            subOpenid = orderInfo.getUser().getOpenId();
        }
        String request = "";
        try {
            //调app支付
            request = ksherPaySdk.AppPay(mchOrderNo, feeType, channel, subOpenid, properties.getAppId(), total, orderInfo
                    .getId()
                    .toString());
            // success(mchOrderNo, channel, total, request);
        } catch (Exception e) {
            log.info("支付异常", e);
            throw new BusinessException(e.getMessage());
        }
        return request;
    }

    /*
    聚合支付
     */
    public String cardPay(OrderInfo orderInfo) {
        //ksher支付SDK
        KsherPaySdk ksherPaySdk = getSdk();
        //商家产生的唯一no
        String mchOrderNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
        //支付方式
        String channelList = "ktbcard,alipay,wechat";
        //商家方的订单号
        String mchCode = orderInfo.getOrderNo();
        //总数 分为单位
        Integer total = orderInfo.getRealAmount().multiply(BigDecimal.valueOf(100)).intValue();
        //设备
        String device = "H5";
        //内容
        String productName = "购买商品";

        String request = "";
        try {
            request = ksherPaySdk.GatewayPay(mchOrderNo, feeType, channelList, mchCode, null,
                    null, productName, null, device, total, orderInfo
                            .getId()
                            .toString());
//            success(mchOrderNo, channelList, total, request);
        } catch (Exception e) {
            log.info("支付异常", e);
            throw new BusinessException(e.getMessage());
        }
        return request;
    }


    /*
    小程序支付 只能微信和支付宝
    */
    public String miniproPay(OrderInfo orderInfo) {
        //ksher支付SDK
        KsherPaySdk ksherPaySdk = getSdk();
        //商家产生的唯一no
        String mchOrderNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
        //总数 分为单位
        Integer total = orderInfo.getRealAmount().multiply(BigDecimal.valueOf(100)).intValue();
        //支付方式
        String channel = "wechat";
        if (PayMethod.ALI_PAY.equals(orderInfo.getPayMethod())) {
            channel = "alipay";
        }

        String subOpenid;
        if (orderInfo.getUser().getOpenId() != null) {
            subOpenid = orderInfo.getUser().getOpenId();
        } else {
            throw new BusinessException("openId为空");
        }

        String request = "";
        try {
            //调小程序支付
            request = ksherPaySdk.MiniproPay(mchOrderNo, feeType, channel, subOpenid, properties.getAppId(), total, orderInfo
                    .getId()
                    .toString());
//            success(mchOrderNo, channel, total, request);
        } catch (Exception e) {
            log.info("支付异常", e);
            throw new BusinessException(e.getMessage());
        }
        return request;
    }

    /*
    订单退款
     */
    public void refund(FeeRecord feeRecord) {
        //ksher支付SDK
        KsherPaySdk ksherPaySdk = getSdk();
        String mchRefundNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
        try {
            //全部退款
            String request = ksherPaySdk.OrderRefund(mchRefundNo, feeType, feeRecord.getMchNo(), feeRecord.getTotalFee(), feeRecord
                    .getTotalFee());
//            success(mchRefundNo, feeRecord.getChannel(), feeRecord.getTotalFee(), request);
        } catch (Exception e) {
            log.info("退款异常", e);
            throw new BusinessException(e.getMessage());
        }
    }
}
