admin
2021-04-19 eb7f3343af839a7c71f16e8ada2b25d5e2201c08
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package com.yeshi.buwan.service.imp.order;
 
import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.yeshi.buwan.dao.vip.OrderRecordDao;
import com.yeshi.buwan.domain.vip.OrderRecord;
import com.yeshi.buwan.domain.vip.OrderType;
import com.yeshi.buwan.domain.vip.UserVIPInfo;
import com.yeshi.buwan.domain.vip.VIPPriceType;
import com.yeshi.buwan.dto.order.PayWayInfoDTO;
import com.yeshi.buwan.exception.PPTVException;
import com.yeshi.buwan.exception.goldcorn.GoldCornException;
import com.yeshi.buwan.exception.order.OrderException;
import com.yeshi.buwan.exception.order.PayException;
import com.yeshi.buwan.exception.vip.VIPException;
import com.yeshi.buwan.pptv.PPTVVipManager;
import com.yeshi.buwan.service.inter.order.OrderService;
import com.yeshi.buwan.service.manager.GoldCornManager;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.RedisManager;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.user.VipUtil;
import com.yeshi.buwan.util.vip.VIPOrderUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.alipay.AlipayH5PayUtil;
import org.yeshi.utils.entity.wx.WXPayOrderInfoV3;
import org.yeshi.utils.wx.WXPayV3Util;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
@Service
public class OrderServiceImpl implements OrderService {
 
    private static Logger logger = LoggerFactory.getLogger("OrderService");
 
    private static Logger orderLogger = LoggerFactory.getLogger("order");
 
    @Resource
    private OrderRecordDao vipOrderRecordDao;
 
    @Resource
    private PPTVVipManager pptvVipManager;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private GoldCornManager goldCornManager;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OrderRecord createOrder(OrderRecord record) throws OrderException {
        if (record.getMoney() == null || (record.getType() == null && record.getVideoInfoId() == null) || record.getOrderType() == null || record.getUid() == null) {
            throw new OrderException(1, "参数不完整");
        }
 
        if (record.getState() == null) {
            record.setState(OrderRecord.STATE_NOT_PAY);
        }
 
        if (record.getCreateTime() == null) {
            record.setCreateTime(new Date());
        }
 
        if (record.getPayWay() == OrderRecord.PAY_WAY_WX) {
            if (record.getGoldCorn() != null && record.getGoldCorn() > 0) {
                if (record.getMoney().compareTo(new BigDecimal("0")) == 0) {
                    record.setPayWay(OrderRecord.PAY_WAY_GOLDCORN);
                } else {
                    record.setPayWay(OrderRecord.PAY_WAY_WX_GOLDCORN);
                }
            }
        } else if (record.getPayWay() == OrderRecord.PAY_WAY_ALIPAY) {
            if (record.getGoldCorn() != null && record.getGoldCorn() > 0) {
                if (record.getMoney().compareTo(new BigDecimal("0")) == 0) {
                    record.setPayWay(OrderRecord.PAY_WAY_GOLDCORN);
                } else {
                    record.setPayWay(OrderRecord.PAY_WAY_ALIPAY_GOLDCORN);
                }
            }
        }
 
 
        record.setMoneyPay(false);
        record.setGoldCornPay(false);
        Serializable id = vipOrderRecordDao.save(record);
        record.setId(id.toString());
        orderLogger.info("订单创建成功:id-{} 类型-{}", record.getId(), record.getOrderType().name());
        return record;
    }
 
    @Transactional(rollbackFor = Exception.class)
//    @GlobalTransactional(timeoutMills = 30000, name = "buwan-order", rollbackFor = Exception.class)
    @Override
    public PayWayInfoDTO payOrder(OrderRecord record) throws OrderException, GoldCornException, PayException, VIPException, PPTVException {
        String orderNo = VIPOrderUtil.getOutOrderNo(record.getOrderType(), record.getId());
        switch (record.getPayWay()) {
            case OrderRecord
                    .PAY_WAY_ALIPAY_GOLDCORN:
 
                try {
                    goldCornManager.consumeGoldCorn(record, record.getGoldCorn());
                } catch (Exception e) {
                    throw new GoldCornException(1, "影视豆扣除出错");
                }
                paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date());
 
                orderLogger.info("订单影视豆支付成功:id-{}", record.getId());
            case OrderRecord
                    .PAY_WAY_ALIPAY: {
                if (record.getMoney() != null && record.getMoney().compareTo(new BigDecimal(0)) == 0) {
                    return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId()));
                }
                //生成支付宝支付订单
                String form = VipUtil.getVipChargeAlipayForm(record.getId(), record.getOrderType(), orderNo, record.getMoney());
                //暂存2分钟
                String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                redisManager.cacheCommonString(id, form, 120);
                String payUrl = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + id;
                orderLogger.info("订单创建支付宝支付链接成功:id-{} 链接-{}", record.getId(), payUrl);
                return new PayWayInfoDTO(OrderRecord
                        .PAY_WAY_ALIPAY, payUrl);
            }
            case OrderRecord
                    .PAY_WAY_WX_GOLDCORN:
                try {
                    goldCornManager.consumeGoldCorn(record, record.getGoldCorn());
 
                } catch (Exception e) {
                    throw new GoldCornException(1, "影视豆扣除出错");
                }
                paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date());
                orderLogger.info("订单影视豆支付成功:id-{} 类型-{}", record.getId(), record.getOrderType().name());
            case OrderRecord
                    .PAY_WAY_WX: {
                if (record.getMoney() != null && record.getMoney().compareTo(new BigDecimal(0)) == 0) {
                    return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId()));
                }
                //生成微信支付订单
                try {
                    String title = "";
                    if (record.getOrderType() == OrderType.vip) {
                        title = "影视大全VIP-" + record.getType().getName();
                    } else {
                        title = "单片购买";
                    }
                    String payUrl = VipUtil.createWXOrder(record.getId(), record.getIpInfo(), orderNo, record.getMoney(), title);
                    orderLogger.info("订单创建微信支付链接成功:id-{} 链接-{}", record.getId(), payUrl);
                    return new PayWayInfoDTO(OrderRecord.PAY_WAY_WX, payUrl);
                } catch (Exception e) {
                    logger.error("生成微信支付订单出错", e);
                    throw new PayException(2, e.getMessage());
                }
            }
 
            case OrderRecord.PAY_WAY_GOLDCORN: {
                try {
                    goldCornManager.consumeGoldCorn(record, record.getGoldCorn());
                    orderLogger.info("订单影视豆支付成功:id-{}", record.getId());
                } catch (Exception e) {
                    throw new GoldCornException(1, "影视豆扣除出错");
                }
                paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date());
                return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId()));
            }
        }
        throw new OrderException(1, "支付方式不存在");
    }
 
    private int getPayState(OrderRecord record) {
        switch (record.getPayWay()) {
            case OrderRecord.PAY_WAY_ALIPAY:
            case OrderRecord.PAY_WAY_WX:
                if (record.getMoneyPay() != null && record.getMoneyPay())
                    return OrderRecord.STATE_PAY;
                else
                    return OrderRecord.STATE_NOT_PAY;
            case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN:
            case OrderRecord.PAY_WAY_WX_GOLDCORN:
                if (record.getMoneyPay() != null && record.getMoneyPay() && record.getGoldCornPay() != null && record.getGoldCornPay())
                    return OrderRecord.STATE_PAY;
                else
                    return OrderRecord.STATE_NOT_PAY;
            case OrderRecord.PAY_WAY_GOLDCORN:
                if (record.getGoldCornPay() != null && record.getGoldCornPay())
                    return OrderRecord.STATE_PAY;
                else
                    return OrderRecord.STATE_NOT_PAY;
        }
        return OrderRecord.STATE_NOT_PAY;
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OrderRecord paySuccess(String id, int payWay, BigDecimal payMoney, Date payTime) throws
            VIPException, PPTVException {
        orderLogger.info("订单支付成功目的执行:id-{},支付方式-{},支付金额-{},支付时间-{}", id, payWay, payMoney, payTime.getTime());
 
        Session session = vipOrderRecordDao.getSession();
 
        //修改记录
        Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(OrderRecord.class).setCacheable(false).setParameter(0, id);
        List<OrderRecord> list = query.list();
        if (list == null || list.size() == 0)
            throw new VIPException(10, "订单不存在");
 
        OrderRecord record = list.get(0);
        if (record.getState() != OrderRecord.STATE_NOT_PAY)
            throw new VIPException(1, "订单未处于待支付状态");
 
 
        if (payWay != OrderRecord.PAY_WAY_GOLDCORN) {
            record.setPayMoney(payMoney);
            record.setMoneyPay(true);
        } else {
            record.setGoldCornPay(true);
        }
 
        record.setPayTime(payTime);
        record.setUpdateTime(new Date());
        record.setState(getPayState(record));
 
        //已经支付成功
        if (record.getState() == OrderRecord.STATE_PAY) {
            session.createSQLQuery("insert into wk_vip_order_pay_success(id,create_time) value(?,now())").setParameter(0, id).executeUpdate();
        }
 
        if (record.getOrderType() == OrderType.vip) {
            Date[] expireDate = addExpireTime(session, record.getUid(), payTime, record.getType());
            record.setVipStartTime(expireDate[0]);
            record.setVipEndTime(expireDate[1]);
            if (expireDate == null) {
                throw new VIPException(2, "添加用户会员时间出错");
            }
            session.update(record);
 
 
            if (record.getState() == OrderRecord.STATE_PAY) {
                orderLogger.info("订单全部支付成功:id-{}", id);
                //购买VIP
                pptvVipManager.buyVIP(record);
                orderLogger.info("购买VIP成功:id-{}", id);
            } else {
                orderLogger.info("订单部分支付成功:id-{}", id);
            }
 
        } else if (record.getOrderType() == OrderType.video) {
            record.setVipStartTime(payTime);
            //7天之内有效
            record.setVipEndTime(new Date(payTime.getTime() + 1000 * 60 * 60L * 24 * 7));
            session.update(record);
            //单片购买成功(7天有效期)
            if (record.getState() == OrderRecord.STATE_PAY) {
                orderLogger.info("订单全部支付成功:id-{}", id);
                //购买单片
                pptvVipManager.buyVideo(record);
                orderLogger.info("购买单片成功:id-{}", id);
            } else {
                orderLogger.info("订单部分支付成功:id-{}", id);
            }
        }
 
        return record;
 
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cancelOrder(String id, String reason) throws OrderException, GoldCornException {
 
        Session session = vipOrderRecordDao.getSession();
        Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(OrderRecord.class).setCacheable(false).setParameter(0, id);
        List<OrderRecord> list = query.list();
        //订单不存在
        if (list == null || list.size() == 0)
            throw new OrderException(1, "订单不存在");
        OrderRecord record = list.get(0);
        if (record.getState() == OrderRecord.STATE_PAY) {
            throw new OrderException(1, "已支付成功的订单不能取消");
        }
 
        if (record.getPayWay() != OrderRecord.PAY_WAY_GOLDCORN && record.getMoneyPay() != null && record.getMoneyPay()) {
            throw new OrderException(2, "资金支付成功的订单不能取消");
        }
 
        //返回豆
        if (record.getGoldCornPay() != null && record.getGoldCornPay() && record.getGoldCorn() != null && record.getGoldCorn() > 0) {
            try {
                goldCornManager.drawbackGoldCorn(record);
            } catch (Exception e) {
                throw new GoldCornException(1, "影视豆退款失败");
            }
        }
 
        //设置订单状态
        record.setState(OrderRecord.STATE_CANCEL);
        record.setUpdateTime(new Date());
        record.setRemarks(reason);
        session.update(record);
        orderLogger.info("取消订单成功:id-{}", id);
    }
 
    @Override
    public List<OrderRecord> getCanCancelOrderList(int page, int pageSize) {
 
        OrderRecordDao.DaoQuery daoQuery = new OrderRecordDao.DaoQuery();
        daoQuery.state = OrderRecord.STATE_NOT_PAY;
        //30分钟未支付取消订单
        daoQuery.maxCreateTime = new Date(System.currentTimeMillis() - 1000 * 60 * 30);
        daoQuery.minCreateTime = new Date(daoQuery.maxCreateTime.getTime() - 1000 * 60 * 60L * 24);
 
        return vipOrderRecordDao.list(daoQuery);
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OrderRecord checkOrderPayState(String id) {
        OrderRecord record = vipOrderRecordDao.find(OrderRecord.class, id);
        if (record.getState() == OrderRecord.STATE_PAY)
            return record;
 
        switch (record.getPayWay()) {
            case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN:
            case OrderRecord.PAY_WAY_ALIPAY: {
                //支付宝
                AlipayTradeQueryResponse res = null;
                try {
                    res = AlipayH5PayUtil.queryOrder(VipUtil.getAlipayApp(), VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), null);
                    //支付成功
                    if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
                        try {
                            return paySuccess(id, OrderRecord.PAY_WAY_ALIPAY, new BigDecimal(res.getTotalAmount()), new Date());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (AlipayApiException e) {
                    e.printStackTrace();
                }
            }
 
            break;
            case OrderRecord.PAY_WAY_WX_GOLDCORN:
            case OrderRecord.PAY_WAY_WX: {
                //微信
                try {
                    WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), VipUtil.getWXAPP());
                    if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
                        paySuccess(id, record.getPayWay(), new BigDecimal(info.getAmount().getPayer_total()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR), new Date());
                        return record;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            break;
        }
        return record;
 
    }
 
 
    @Override
    public List<OrderRecord> listOrderRecord(String uid, OrderType orderType, Integer state, int page, int pageSize) {
 
        OrderRecordDao.DaoQuery query = new OrderRecordDao.DaoQuery();
        query.start = (page - 1) * pageSize;
        query.count = pageSize;
        query.uid = uid;
        query.state = state;
        query.orderType = orderType;
        return vipOrderRecordDao.list(query);
    }
 
    @Override
    public long countOrderRecord(String uid, OrderType orderType, Integer state) {
        OrderRecordDao.DaoQuery query = new OrderRecordDao.DaoQuery();
        query.uid = uid;
        query.state = state;
        query.orderType = orderType;
        return vipOrderRecordDao.count(query);
    }
 
    @Override
    public OrderRecord getOrderRecord(String id) {
        return vipOrderRecordDao.find(OrderRecord.class, id);
    }
 
 
    /**
     * 续期
     *
     * @param session
     * @param uid
     * @param payTime
     * @param type
     * @return
     */
    private Date[] addExpireTime(Session session, String uid, Date payTime, VIPPriceType type) {
        if (type == null || uid == null)
            return null;
        List list = session.createSQLQuery("select * from wk_user_vip i where i.uid=? for update").addEntity(UserVIPInfo.class).setParameter(0, uid).list();
        if (list == null || list.size() == 0) {
            //新增
            UserVIPInfo vip = new UserVIPInfo();
            vip.setUid(uid);
            vip.setCreateTime(new Date());
 
            Date[] expireDate = getExpireTime(payTime, null, type);
            vip.setExpireDate(expireDate[1]);
            session.save(vip);
            return expireDate;
        } else {
            //修改
            UserVIPInfo vipInfo = (UserVIPInfo) list.get(0);
            Date[] expireDate = getExpireTime(payTime, vipInfo.getExpireDate(), type);
            session.createQuery("update UserVIPInfo i set i.expireDate=? ,i.updateTime=? where i.uid=?").setParameter(0, expireDate[1]).setParameter(1, new Date()).setParameter(2, uid).executeUpdate();
 
            return expireDate;
        }
    }
 
    /**
     * 获取到期时间
     *
     * @param payTime
     * @param expireTime
     * @param type
     * @return
     */
    private Date[] getExpireTime(Date payTime, Date expireTime, VIPPriceType type) {
        Calendar calendar = Calendar.getInstance();
        if (expireTime != null) {
            //之前续期时间是否大于付款时间
            if (payTime.getTime() < expireTime.getTime())
                calendar.setTimeInMillis(expireTime.getTime());
            else
                calendar.setTimeInMillis(payTime.getTime());
        } else {
            calendar.setTimeInMillis(payTime.getTime());
        }
        long startTime = calendar.getTimeInMillis();
        if (type == VIPPriceType.day) {
            calendar.add(Calendar.DAY_OF_WEEK, 1);
        } else if (type == VIPPriceType.week) {
            calendar.add(Calendar.WEEK_OF_YEAR, 1);
        } else if (type == VIPPriceType.month) {
            calendar.add(Calendar.MONTH, 1);
        } else if (type == VIPPriceType.season) {
            calendar.add(Calendar.MONTH, 3);
        } else if (type == VIPPriceType.halfYear) {
            calendar.add(Calendar.MONTH, 6);
        } else if (type == VIPPriceType.year) {
            calendar.add(Calendar.YEAR, 1);
        }
        return new Date[]{new Date(startTime), new Date(calendar.getTimeInMillis())};
    }
 
 
}