liujie
2023-05-26 f9de931c4457c2a6bfe395879e3b2f2bfd7d9692
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,15 @@
    @Autowired
    private ITGoodsService goodsService;
    @Autowired
    private ITUserService itUserService;
    @Autowired
    private ITQuoteService tQuoteService;
    @Autowired
    private TUserBankService userBankService;
    @ApiOperation(value = "用户端-invocie",notes="用户端-invocie")
@@ -105,17 +118,119 @@
    @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) {
                    bigDecimal = bigDecimal.add(order.getAllTotal());
                }
                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");
                    long l = bigDecimal.multiply(new BigDecimal(100)).longValue();
                    Boolean pay = PaymentProcessor.pay(tUser.getUserNumber(), cardToken, l);
                    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();
                    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 = PaymentProcessor.pay(tUser.getUserNumber(), id1.toString(), l);
                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);
                }
            }
            return new SuccessTip();
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorTip(500, "ERROR");
        }
    }
}