package com.bohuikeji.delivery.backstage.pay;

import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.constants.payConst;
import com.bohuikeji.delivery.backstage.entity.TCustomerPay;
import com.bohuikeji.delivery.backstage.entity.TPay;
import com.bohuikeji.delivery.backstage.entity.discount.TDiscountPay;
import com.bohuikeji.frame.module.admin.entity.vo.AliReturnPayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 支付回调操作
 */
@Service
@Slf4j
public class PayServices {

    /**
     * 客户下单支付记录
     * @return
     */
    public static TPay orderPaySrevice(JSONObject jsonObject, AliReturnPayVo req,TPay pay) {//pay.getPayExpectPrice().multiply(new BigDecimal(100)).longValue()
        //根据判断进入百度支付业务处理
        if(jsonObject != null && jsonObject.getInteger("payType")!=null){
            log.info("进入百度回调业务处理");
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("userId",jsonObject.getLong("userId"));
            jsonObj.put("orderId",jsonObject.getLong("orderId"));
            pay.setPayFactPrice(new BigDecimal(jsonObject.getIntValue("totalMoney")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            pay.setActualPrice(pay.getPayFactPrice());
            pay.setPayChannel(payConst.getAllPayChannel(jsonObject));
            pay.setPayChannelNum(jsonObj.toString());
            pay.setPayStatus("已支付");
            pay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("msg")!=null){
            JSONObject msgjson= JSONObject.parseObject(jsonObject.getString("msg"));
            log.info("进入抖音支付回调业务处理");
            pay.setPayFactPrice(new BigDecimal(msgjson.getIntValue("total_amount")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            pay.setActualPrice(pay.getPayFactPrice());
            pay.setPayChannel(payConst.getAllPayChannel(msgjson));
            pay.setPayChannelNum(msgjson.getString("order_id"));
            pay.setPayStatus("已支付");
            pay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("biz_type")!=null){
            log.info("进入快手支付回调业务处理");
            pay.setPayFactPrice(new BigDecimal(jsonObject.getJSONObject("data").getIntValue("order_amount")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            pay.setActualPrice(pay.getPayFactPrice());
            pay.setPayChannel(payConst.getAllPayChannel(jsonObject.getJSONObject("data")));
            pay.setPayChannelNum(jsonObject.getJSONObject("data").getString("ks_order_no"));
            pay.setPayStatus("已支付");
            pay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("trade_type")!=null){//进入微信业务处理 jsonObject.getJSONObject("amount").getString("total")).divide(new BigDecimal(100))
            log.info("进入微信支付回调业务处理");
            pay.setPayFactPrice(new BigDecimal(jsonObject.getJSONObject("amount").getString("total")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            pay.setActualPrice(new BigDecimal(jsonObject.getJSONObject("amount").getString("payer_total")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            pay.setPayChannel("微信");
            pay.setPayChannelNum(jsonObject.getString("transaction_id"));
            pay.setPayStatus("已支付");
            pay.setPayTime(LocalDateTime.now());
        }
        if(req != null){
            log.info("进入支付宝支付回调业务处理");
            pay.setPayFactPrice(new BigDecimal(req.getTotal_amount()));
            pay.setActualPrice(new BigDecimal(req.getReceipt_amount()));//获取支付宝优惠后的金额
            pay.setPayChannel("支付宝");
            pay.setPayChannelNum(req.getTrade_no());
            pay.setPayStatus("已支付");
            pay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject==null && req==null){
            log.info("进入钱包支付业务处理");
            pay.setPayFactPrice(pay.getPayExpectPrice());//获取微信支付金额由分转成元/真鸡儿啰嗦
            pay.setActualPrice(pay.getPayExpectPrice());
            pay.setPayChannel("钱包");
            pay.setPayChannelNum(null);
            pay.setPayStatus("已支付");
            pay.setPayTime(LocalDateTime.now());
        }
        return pay;
    }

    /**
     * 客户购买优惠卷支付记录
     * @param jsonObject
     * @param req
     * @param discountPay
     * @return
     */
    public static TDiscountPay getDisPaySrevice(JSONObject jsonObject, AliReturnPayVo req, TDiscountPay discountPay) {//pay.getPayExpectPrice().multiply(new BigDecimal(100)).longValue()
        //根据判断进入百度支付业务处理
        if(jsonObject != null && jsonObject.getInteger("payType")!=null){
            log.info("进入百度回调业务处理");
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("userId",jsonObject.getLong("userId"));
            jsonObj.put("orderId",jsonObject.getLong("orderId"));
            discountPay.setDisPrice(new BigDecimal(jsonObject.getIntValue("totalMoney")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元  做除法时设置舍入
            discountPay.setPayChannel(payConst.getAllPayChannel(jsonObject));
            discountPay.setPayChannelNum(jsonObj.toString());
            discountPay.setPayStatus("已支付");
            discountPay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getInteger("way")!=null){
            log.info("进入抖音支付回调业务处理");
            discountPay.setDisPrice(new BigDecimal(jsonObject.getIntValue("total_amount")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/做除法时设置舍入
            discountPay.setPayChannel(payConst.getAllPayChannel(jsonObject));
            discountPay.setPayChannelNum(jsonObject.getString("order_id"));
            discountPay.setPayStatus("已支付");
            discountPay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("biz_type")!=null){
            log.info("进入快手支付回调业务处理");
            discountPay.setDisPrice(new BigDecimal(jsonObject.getJSONObject("data").getIntValue("order_amount")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/做除法时设置舍入
            discountPay.setPayChannel(payConst.getAllPayChannel(jsonObject.getJSONObject("data")));
            discountPay.setPayChannelNum(jsonObject.getJSONObject("data").getString("ks_order_no"));
            discountPay.setPayStatus("已支付");
            discountPay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("trade_type")!=null){//进入微信业务处理 jsonObject.getJSONObject("amount").getString("total")).divide(new BigDecimal(100))
            log.info("进入微信支付回调业务处理");
            discountPay.setDisPrice(new BigDecimal(jsonObject.getJSONObject("amount").getString("payer_total")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            discountPay.setPayChannel("微信");
            discountPay.setPayChannelNum(jsonObject.getString("transaction_id"));
            discountPay.setPayStatus("已支付");
            discountPay.setPayTime(LocalDateTime.now());
        }
        if(req != null){
            log.info("进入支付宝购买回调业务处理");
            discountPay.setDisPrice(new BigDecimal(req.getReceipt_amount()));//获取微信支付金额由分转成元/真鸡儿啰嗦
            discountPay.setPayChannel("支付宝");
            discountPay.setPayChannelNum(req.getTrade_no());
            discountPay.setPayStatus("已支付");
            discountPay.setPayTime(LocalDateTime.now());
        }
        return discountPay;
    }


    /**
     * 客户钱包充值
     * @param jsonObject
     * @param req
     * @param customerPay
     * @return
     */
    public static TCustomerPay getCustomerPaySrevice(JSONObject jsonObject, AliReturnPayVo req, TCustomerPay customerPay) {//pay.getPayExpectPrice().multiply(new BigDecimal(100)).longValue()
        //根据判断进入百度支付业务处理
        if(jsonObject != null && jsonObject.getInteger("payType")!=null){
            log.info("进入百度钱包充值回调业务处理");
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("userId",jsonObject.getLong("userId"));
            jsonObj.put("orderId",jsonObject.getLong("orderId"));
            customerPay.setPayFactPrice(new BigDecimal(jsonObject.getIntValue("totalMoney")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            customerPay.setPayChannel(payConst.getAllPayChannel(jsonObject));
            customerPay.setPayChannelNum(jsonObj.toString());
            customerPay.setPayStatus("已支付");
            customerPay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getInteger("way")!=null){
            log.info("进入抖音钱包充值回调业务处理");
            customerPay.setPayFactPrice(new BigDecimal(jsonObject.getIntValue("total_amount")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            customerPay.setPayChannel(payConst.getAllPayChannel(jsonObject));
            customerPay.setPayChannelNum(jsonObject.getString("order_id"));
            customerPay.setPayStatus("已支付");
            customerPay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("biz_type")!=null){
            log.info("进入快手钱包充值回调业务处理");
            customerPay.setPayFactPrice(new BigDecimal(jsonObject.getJSONObject("data").getIntValue("order_amount")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));//获取微信支付金额由分转成元/真鸡儿啰嗦
            customerPay.setPayChannel(payConst.getAllPayChannel(jsonObject.getJSONObject("data")));
            customerPay.setPayChannelNum(jsonObject.getJSONObject("data").getString("ks_order_no"));
            customerPay.setPayStatus("已支付");
            customerPay.setPayTime(LocalDateTime.now());
        }
        if(jsonObject != null && jsonObject.getString("trade_type")!=null){//进入微信业务处理 jsonObject.getJSONObject("amount").getString("total")).divide(new BigDecimal(100))
            log.info("进入微信钱包充值回调业务处理");
            customerPay.setPayFactPrice(new BigDecimal(jsonObject.getJSONObject("amount").getString("payer_total")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
            customerPay.setPayChannel("微信");
            customerPay.setPayChannelNum(jsonObject.getString("transaction_id"));
            customerPay.setPayStatus("已支付");
            customerPay.setPayWay("微信");
            customerPay.setPayTime(LocalDateTime.now());
        }
        if(req != null){
            log.info("进入支付宝钱包充值回调业务处理");
            customerPay.setPayFactPrice(new BigDecimal(req.getTotal_amount()));
            customerPay.setPayChannel("支付宝");
            customerPay.setPayChannelNum(req.getTrade_no());
            customerPay.setPayStatus("已支付");
            customerPay.setPayWay("支付宝");
            customerPay.setPayTime(LocalDateTime.now());
        }
        return customerPay;
    }
}
