liujie
2023-05-26 f9de931c4457c2a6bfe395879e3b2f2bfd7d9692
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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.*;
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.*;
import java.util.stream.Collectors;
 
@Controller
@Api(tags = "用户端-财务信息")
@RequestMapping("/api/tFinance")
public class FinanceController {
 
 
    @Autowired
    private ITOrderService orderService;
 
    @Autowired
    private ITGoodsService goodsService;
 
    @Autowired
    private ITUserService itUserService;
 
    @Autowired
    private ITQuoteService tQuoteService;
 
    @Autowired
    private TUserBankService userBankService;
 
 
    @ApiOperation(value = "用户端-invocie",notes="用户端-invocie")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "pageNumber", value = "pageNumber", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "time", value = "时间 (时间 - 时间)", required = false, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "订单id", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "state", value = "状态 1支付完 2未支付完", required = false, dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public Object list(int pageNumber,int pageSize,String time,Long id,Integer state,int userId) {
        Page<ReceivableVo> receivableVoPage = new Page<>(pageNumber, pageSize);
        List<ReceivableVo> receivable = orderService.getReceivable(receivableVoPage, time, id, state, userId);
        receivableVoPage.setRecords(receivable);
        return new SuccessTip(receivableVoPage);
    }
 
 
    @ApiOperation(value = "用户端-payImmediately",notes="用户端-payImmediately")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"),
    })
    @PostMapping(value = "/payImmediately")
    @ResponseBody
    public Object payImmediately(int userId) {
        // 找出所有未支付完成的订单
        List<TOrder> orders = orderService.getNoPayOrder(userId);
        BigDecimal all = new BigDecimal(0);
        ArrayList<TGoods> goods = new ArrayList<>();
        for (TOrder order : orders) {
            BigDecimal allTotal = order.getAllTotal();
            BigDecimal paid = order.getPaid();
            BigDecimal subtract = allTotal.subtract(paid);
            all = all.add(subtract);
            TGoods tGoods = goodsService.selectOne(new EntityWrapper<TGoods>().eq("order_id", order.getId()));
            goods.add(tGoods);
        }
 
        HashMap<String, Object> map = new HashMap<>();
        map.put("money",all);
        map.put("data",goods);
 
        return new SuccessTip(map);
    }
 
    @ApiOperation(value = "应收款票据详情",notes="应收款票据详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "number", value = "订单id", required = true, dataType = "Long"),
    })
    @GetMapping(value = "/listOfReceivablesInfo")
    @ResponseBody
    public Object listOfReceivablesInfo(Long number) {
        return new SuccessTip(orderService.listOfReceivablesInfo(number));
    }
 
    @ApiOperation(value = "支付调用",notes="支付调用")
    @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 = "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
    @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);
 
 
        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");
        }
    }
 
 
}