From 871efa21e6c95520e9825ae1f2338c9a919fdd5d Mon Sep 17 00:00:00 2001
From: liujie <liujie>
Date: 星期日, 15 十月 2023 17:57:48 +0800
Subject: [PATCH] 用户端代码

---
 src/main/java/com/stylefeng/guns/modular/system/controller/FinanceController.java |  153 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 137 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/stylefeng/guns/modular/system/controller/FinanceController.java b/src/main/java/com/stylefeng/guns/modular/system/controller/FinanceController.java
index b026bf1..bbb57e2 100644
--- a/src/main/java/com/stylefeng/guns/modular/system/controller/FinanceController.java
+++ b/src/main/java/com/stylefeng/guns/modular/system/controller/FinanceController.java
@@ -1,33 +1,37 @@
 package com.stylefeng.guns.modular.system.controller;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.amazonaws.services.dynamodbv2.xspec.B;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
 import com.stripe.exception.StripeException;
 import com.stripe.model.PaymentIntent;
-import com.stylefeng.guns.modular.system.model.ReceivableVo;
-import com.stylefeng.guns.modular.system.model.TGoods;
-import com.stylefeng.guns.modular.system.model.TOrder;
-import com.stylefeng.guns.modular.system.model.TPrice;
-import com.stylefeng.guns.modular.system.service.ITGoodsService;
-import com.stylefeng.guns.modular.system.service.ITOrderService;
+import com.stylefeng.guns.modular.system.model.*;
+import com.stylefeng.guns.modular.system.service.*;
+import com.stylefeng.guns.modular.system.service.impl.TQuoteServiceImpl;
 import com.stylefeng.guns.modular.system.utils.PaymentProcessor;
 import com.stylefeng.guns.modular.system.utils.Server;
+import com.stylefeng.guns.modular.system.utils.StripePayUtils;
+import com.stylefeng.guns.modular.system.utils.tips.ErrorTip;
 import com.stylefeng.guns.modular.system.utils.tips.SuccessTip;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
+import org.apache.poi.ss.formula.functions.T;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Controller
 @Api(tags = "用户端-财务信息")
@@ -40,6 +44,18 @@
 
     @Autowired
     private ITGoodsService goodsService;
+
+    @Autowired
+    private ITUserService itUserService;
+
+    @Autowired
+    private ITQuoteService tQuoteService;
+
+    @Autowired
+    private TUserBankService userBankService;
+
+    @Autowired
+    private ITPriceService priceService;
 
 
     @ApiOperation(value = "用户端-invocie",notes="用户端-invocie")
@@ -75,7 +91,7 @@
         BigDecimal all = new BigDecimal(0);
         ArrayList<TGoods> goods = new ArrayList<>();
         for (TOrder order : orders) {
-            BigDecimal allTotal = order.getAllTotal();
+            BigDecimal allTotal = priceService.selectList(new EntityWrapper<TPrice>().eq("order_id", order.getId())).stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
             BigDecimal paid = order.getPaid();
             BigDecimal subtract = allTotal.subtract(paid);
             all = all.add(subtract);
@@ -105,17 +121,122 @@
     @ApiImplicitParams({
             @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
             @ApiImplicitParam(name = "id", value = "userid", required = true, dataType = "int"),
-            @ApiImplicitParam(name = "orderId", value = "orderId", required = true, dataType = "Long"),
+            @ApiImplicitParam(name = "quoteId", value = "需求id", required = false, dataType = "Long"),
+            @ApiImplicitParam(name = "cardId", value = "cardId", required = false, dataType = "Integer"),
+            @ApiImplicitParam(name = "type", value = "type 1额度支付  2全款支付", required = true, dataType = "int"),
+            @ApiImplicitParam(name = "category", value = " 1需求支付  2所有支付", required = true, dataType = "int"),
     })
     @GetMapping(value = "/payInfo")
     @ResponseBody
-    public Object payInfo(int id,Long orderId) throws StripeException {
-        PaymentIntent paymentIntent = PaymentProcessor.charge("cus_NvFsNut8zDF9pc", 1L);
+    @Transactional(propagation = Propagation.REQUIRES_NEW)
+    public Object payInfo(int id, Long quoteId, Integer cardId, int type, int category) throws StripeException {
+        TUser tUser = itUserService.selectById(id);
 
-        Server.aaa();
-        return new SuccessTip();
+
+        try {
+            if (category == 1) {
+                TQuote quote = tQuoteService.selectById(quoteId);
+                // 找出状态为0的订单
+                List<TOrder> orders = orderService.selectList(new EntityWrapper<TOrder>().eq("e_zip_z", quote.getOrderId()).eq("status", 0));
+                // 总金额
+                BigDecimal bigDecimal = new BigDecimal(0);
+                for (TOrder order : orders) {
+                    List<TPrice> prices = priceService.selectList(new EntityWrapper<TPrice>().eq("order_id", order.getId()));
+                    bigDecimal = bigDecimal.add(prices.stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO,BigDecimal::add));
+                }
+                if (type == 1) {
+                    // 可用额度
+                    BigDecimal bigDecimal1 = null;
+                    String residueLimit = tUser.getResidueLimit();
+                    if (residueLimit == null) {
+                        bigDecimal1 = new BigDecimal(residueLimit);
+                    } else {
+                        bigDecimal1 = new BigDecimal(residueLimit);
+                    }
+                    // 剩余额度
+                    BigDecimal subtract = bigDecimal1.subtract(bigDecimal);
+                    tUser.setResidueLimit(subtract.toString());
+                    orders.forEach(e -> {
+                        e.setStatus("2");
+                        e.setPayTime(new Date());
+                    });
+                    orderService.updateBatchById(orders);
+                    itUserService.updateById(tUser);
+                    quote.setState(4);
+                    tQuoteService.updateById(quote);
+                } else {
+                    if (tUser.getUserNumber() == null) {
+                        return new ErrorTip(5006, "Please bind a card first");
+                    }
+                    // 找出这张卡
+                    TUserBank tUserBank = userBankService.selectById(cardId);
+                    String cardToken = tUserBank.getCardToken();
+                    // 修改默认支付卡
+                    StripePayUtils.updateStripeDefaultCard(tUser.getUserNumber(), cardToken);
+//                    String method = PaymentProcessor.getMethod(tUser.getUserNumber());
+//                    JSONObject jsonObject = JSON.parseObject(method);
+//                    Object id1 = jsonObject.get("id");
+                    Boolean pay = StripePayUtils.charge(tUser.getUserNumber(), bigDecimal, "","Continental Cargo Association, Inc.");
+                    if (pay) {
+                        orders.forEach(e -> {
+                            e.setStatus("2");
+                            e.setPaid(e.getAllTotal());
+                            e.setPayTime(new Date());
+                        });
+                        orderService.updateBatchById(orders);
+                        quote.setState(4);
+                        tQuoteService.updateById(quote);
+                    }
+                }
+            } else {
+                if (tUser.getUserNumber() == null) {
+                    return new ErrorTip(5006, "Please bind a card first");
+                }
+                // 找出所有未支付完成的订单
+                List<TOrder> orders = orderService.getNoPayOrder(id);
+                HashSet<TQuote> tQuotes = new HashSet<>();
+                BigDecimal all = new BigDecimal(0);
+                for (TOrder order : orders) {
+                    String s = order.geteZipZ();
+                    TQuote quote = tQuoteService.selectOne(new EntityWrapper<TQuote>().eq("order_id", s));
+                    quote.setState(4);
+                    tQuotes.add(quote);
+//                    BigDecimal allTotal = order.getAllTotal();
+                    List<TPrice> prices = priceService.selectList(new EntityWrapper<TPrice>().eq("order_id", order.getId()));
+                    BigDecimal allTotal = prices.stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
+                    BigDecimal paid = order.getPaid();
+                    BigDecimal subtract = allTotal.subtract(paid);
+                    all = all.add(subtract);
+                }
+                // 找出这张卡
+                TUserBank tUserBank = userBankService.selectById(cardId);
+                String cardToken = tUserBank.getCardToken();
+                // 修改默认支付卡
+                StripePayUtils.updateStripeDefaultCard(tUser.getUserNumber(), cardToken);
+                String method = PaymentProcessor.getMethod(tUser.getUserNumber());
+                JSONObject jsonObject = JSON.parseObject(method);
+                Object id1 = jsonObject.get("id");
+                long l = all.multiply(new BigDecimal(100)).longValue();
+                Boolean pay = StripePayUtils.charge(tUser.getUserNumber(), all, "","Continental Cargo Association, Inc.");
+                if (pay) {
+                    orders.forEach(e -> {
+                        e.setStatus("2");
+                        e.setPaid(e.getAllTotal());
+                        e.setPayTime(new Date());
+                    });
+                    orderService.updateBatchById(orders);
+                    List<TQuote> collect = tQuotes.stream().collect(Collectors.toList());
+                    tQuoteService.updateBatchById(collect);
+                }else {
+                    return new ErrorTip(5010, "payment failure");
+                }
+            }
+            return new SuccessTip();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ErrorTip(500, "ERROR");
+        }
     }
-
 
 
 }

--
Gitblit v1.7.1