|
@@ -1820,6 +1820,133 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, String> getActivityGoods(OrderAddBo bo) {
|
|
|
+ String key = "ORDER-" + "-" + bo.getUserId();
|
|
|
+ Long value = redisCache.getCacheObject(key);
|
|
|
+ if (Validator.isNotEmpty(value)) {
|
|
|
+ throw new CustomException("请勿频繁提交订单");
|
|
|
+ }
|
|
|
+ redisCache.setCacheObject(key, 1L, 5, TimeUnit.SECONDS);//5秒
|
|
|
+ List<OrderGoodsAddBo> goodsList = bo.getGoodsList();
|
|
|
+ if (goodsList == null || goodsList.size() < 1) {
|
|
|
+ throw new CustomException("商品列表为空");
|
|
|
+ }
|
|
|
+ Order add = BeanUtil.toBean(bo, Order.class);
|
|
|
+ validEntityBeforeSave(add);
|
|
|
+ add.setCreateTime(DateUtils.getNowTime());
|
|
|
+ add.setUpdateTime(DateUtils.getNowTime());
|
|
|
+
|
|
|
+
|
|
|
+ //生成订单号
|
|
|
+ String out_trade_no = DateUtils.getDateOrderSn();
|
|
|
+ BigDecimal totalPrice = new BigDecimal(0);
|
|
|
+ BigDecimal payPrice = new BigDecimal(0);
|
|
|
+ List<OrderGoods> freeList = new ArrayList<>();
|
|
|
+ //生成订单
|
|
|
+ for (OrderGoodsAddBo g : goodsList) {
|
|
|
+ //订单商品
|
|
|
+ Goods goods = iGoodsService.getOne(new LambdaQueryWrapper<Goods>().eq(Goods::getGoodsId, g.getGoodsId()));
|
|
|
+ if (goods.getGoodsStatus() != 1) {
|
|
|
+ throw new CustomException(goods.getGoodsName() + "商品已下架,请重新选择商品下单");
|
|
|
+ }
|
|
|
+ OrderGoods orderGoods = BeanUtil.toBean(g, OrderGoods.class);
|
|
|
+ orderGoods.setOrderSn(out_trade_no);
|
|
|
+ if (g.getGoodsInputData() != null) {
|
|
|
+ orderGoods.setGoodsInputData(JSON.toJSONString(g.getGoodsInputData()));
|
|
|
+ }
|
|
|
+ //成交价
|
|
|
+ orderGoods.setStatus(1);
|
|
|
+ orderGoods.setGoodsRealPrice(goods.getStandPrice());
|
|
|
+ orderGoods.setGoodsPrice(goods.getStandPrice());
|
|
|
+ orderGoods.setCreateTime(DateUtils.getNowTime());
|
|
|
+ orderGoods.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ //订单价格不计算优惠
|
|
|
+ totalPrice = totalPrice.add(goods.getStandPrice());
|
|
|
+ //暂不计算优惠券
|
|
|
+ payPrice = payPrice.add(goods.getStandPrice());
|
|
|
+ iOrderGoodsService.save(orderGoods);
|
|
|
+
|
|
|
+ boolean canRepeatBuy = false;
|
|
|
+ validUserBeforeBuy(goods, bo.getUserId());
|
|
|
+ //判断是否有购买过
|
|
|
+ Long oldOrderGoodsId = getHaveBuyGoods(g.getGoodsId(), bo.getUserId());
|
|
|
+ //视频商品安排班级
|
|
|
+ if (goods.getGoodsType() == 1) {
|
|
|
+ if (Validator.isNotEmpty(goods.getStudyCount())) {
|
|
|
+ orderGoods.setStudyCount(goods.getStudyCount() - 1); //默认分班消耗一次学习机会
|
|
|
+ } else {
|
|
|
+ orderGoods.setStudyCount(0L);//没配置则为0
|
|
|
+ }
|
|
|
+ Long gradeId = null;
|
|
|
+ //判断是否购买历史班级都过期
|
|
|
+ canRepeatBuy = this.canBuyRepeatGoods(g.getGoodsId(), bo.getUserId());
|
|
|
+ if (g.getGoodsInputData() != null) {
|
|
|
+ gradeId = g.getGoodsInputData().getGradeId();
|
|
|
+ if (Validator.isNotEmpty(oldOrderGoodsId)) {
|
|
|
+ //有指定选班模板且视频商品班级已过期,记录复购状态
|
|
|
+ orderGoods.setRebuyOrderGoodsId(oldOrderGoodsId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String requestId = IdUtil.simpleUUID();
|
|
|
+ RedisLockEntity redisLockEntity = new RedisLockEntity();
|
|
|
+ redisLockEntity.setLockKey(RedisLockEntity.KEY_LOCK_GRADE);
|
|
|
+ redisLockEntity.setRequestId(requestId);
|
|
|
+ if (redisCache.lock(redisLockEntity)) {
|
|
|
+ arrangeGrade(goods.getGoodsName(), goods.getGoodsId(), orderGoods.getOrderGoodsId(), gradeId, add.getUserId(), out_trade_no, goods.getBusinessId());
|
|
|
+ redisCache.unlockLua(redisLockEntity);
|
|
|
+ }
|
|
|
+ iOrderGoodsService.updateById(orderGoods);
|
|
|
+ newFreeBankGoods(goods.getGoodsId(), orderGoods.getOrderGoodsId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!canRepeatBuy && goods.getGoodsType() != 3 && goods.getGoodsType() != 4) {
|
|
|
+ this.checkBuyGoods(g.getGoodsId(), bo.getUserId(), goods.getGoodsType());
|
|
|
+ }
|
|
|
+ if (goods.getGoodsType() == 6) {
|
|
|
+ //直播商品
|
|
|
+ if (Validator.isNotEmpty(oldOrderGoodsId) && oldOrderGoodsId.longValue() > 0) {
|
|
|
+ throw new CustomException("直播商品不允许复购");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ iShoppingCartService.deleteByGoodsId(g.getGoodsId(), bo.getUserId());
|
|
|
+ //商品0元加入免费商品列表
|
|
|
+ freeList.add(orderGoods);
|
|
|
+ }
|
|
|
+
|
|
|
+ add.setOrderSn(out_trade_no);
|
|
|
+ add.setOrderFrom(Order.FROM_SMALL);
|
|
|
+ add.setPayPrice(payPrice);
|
|
|
+ add.setPayType(Order.PAY_WECHAT);
|
|
|
+ add.setOrderPrice(totalPrice);
|
|
|
+ //订单0元
|
|
|
+ //完全收费
|
|
|
+ add.setOrderStatus(Order.ORDER_STATUS_PAY);
|
|
|
+ add.setPayStatus(Order.PAY_FREE);
|
|
|
+ add.setStatus(1);
|
|
|
+ this.save(add);
|
|
|
+ //处理免费商品
|
|
|
+ for (OrderGoods orderGoods : freeList) {
|
|
|
+ dealFreeGoods(orderGoods, add);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> payResult = new HashMap<>();
|
|
|
+ payResult.put("orderSn", out_trade_no);
|
|
|
+
|
|
|
+ //活动商品领取记录
|
|
|
+ ActivityOrder record = new ActivityOrder();
|
|
|
+ record.setOrderSn(out_trade_no);
|
|
|
+ record.setUserId(add.getUserId());
|
|
|
+ record.setGoodsId(goodsList.get(0).getGoodsId());
|
|
|
+ record.setStatus(1);
|
|
|
+ record.setRemark("真题领取活动");
|
|
|
+ record.setType(2);
|
|
|
+ record.setCreateTime(DateUtils.getNowTime());
|
|
|
+ record.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ iActivityOrderService.save(record);
|
|
|
+ return payResult;
|
|
|
+ }
|
|
|
+
|
|
|
private String createGradeCode(Long goodsId, CourseBusinessVo business) {
|
|
|
|
|
|
Goods goods = iGoodsService.getById(goodsId);
|