Browse Source

直播中奖,直接发货接口,直播间进行点赞更新

yuhongqi 18 hours ago
parent
commit
7c720050da

+ 53 - 0
fs-live-socket/src/main/java/com/fs/live/controller/LiveDataController.java

@@ -0,0 +1,53 @@
+package com.fs.live.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.fs.common.core.controller.BaseController;
+import com.fs.common.core.domain.R;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.utils.DateUtils;
+import com.fs.live.domain.LiveUserLike;
+import com.fs.live.service.ILiveDataService;
+import com.fs.live.service.ILiveUserLikeService;
+import com.fs.live.websocket.bean.SendMsgVo;
+import com.fs.live.websocket.service.WebSocketServer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import springfox.documentation.annotations.ApiIgnore;
+
+import javax.websocket.server.PathParam;
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
+@RestController
+@RequestMapping("/app/live/liveData")
+public class LiveDataController extends BaseController {
+
+    @Autowired
+    private ILiveDataService liveDataService;
+    @Autowired
+    private WebSocketServer webSocketServer;
+    @Autowired
+    private RedisCache redisCache;
+    @Autowired
+    private ILiveUserLikeService liveUserLikeService;
+
+    /**
+     * 点赞
+     * */
+    @GetMapping("/like/{liveId}")
+    public R like(@PathVariable("liveId") Long liveId) {
+        //直播间总点赞数
+        Long increment = redisCache.increment("live:like:" + liveId, 1);
+
+        SendMsgVo sendMsgVo = new SendMsgVo();
+        sendMsgVo.setLiveId(liveId);
+        sendMsgVo.setCmd("likeDetail");
+        sendMsgVo.setData(JSON.toJSONString(increment));
+        webSocketServer.broadcastLikeMessage(liveId, JSONObject.toJSONString(sendMsgVo));
+        return R.ok().put("like",increment);
+    }
+}

+ 14 - 0
fs-live-socket/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -426,6 +426,20 @@ public class WebSocketServer {
         });
     }
 
+    /**
+     * 广播点赞消息
+     * @param liveId   直播间ID
+     * @param message  消息内容
+     */
+    public void broadcastLikeMessage(Long liveId, String message) {
+        ConcurrentHashMap<Long, Session> room = getRoom(liveId);
+        room.forEach((k, v) -> {
+            if (v.isOpen()) {
+                sendWithRetry(v,message,7);
+            }
+        });
+    }
+
     private void sendWithRetry(Session session, String message, int maxRetries) {
         int attempts = 0;
         while (attempts < maxRetries) {

+ 2 - 0
fs-service-system/src/main/java/com/fs/live/service/ILiveOrderService.java

@@ -208,4 +208,6 @@ public interface ILiveOrderService {
     LiveOrderComputeDTO computedReward(long l, LiveOrderComputedParam param);
 
     R createRewardLiveOrder(LiveOrder liveOrder);
+
+    R payConfirmReward(LiveOrder liveOrder);
 }

+ 51 - 0
fs-service-system/src/main/java/com/fs/live/service/impl/LiveOrderServiceImpl.java

@@ -2247,6 +2247,57 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
         }
     }
 
+    @Override
+    @Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
+    public R payConfirmReward(LiveOrder liveOrder) {
+        Long orderId = liveOrder.getOrderId();
+        if(orderId==null) return R.error("订单ID不存在");
+        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
+        try {
+            liveOrder = baseMapper.selectLiveOrderByOrderId(String.valueOf(orderId));
+            if(liveOrder==null || !liveOrder.getStatus().equals(1)){
+                throw new CustomException("当前订单未找到或者订单状态不为待支付! orderId:" + orderId);
+            }
+            String payCode = liveOrder.getOrderCode();
+
+            LiveOrderPayment storePayment = liveOrderPaymentMapper.selectLiveOrderPaymentByPaymentCode(payCode);
+            if (storePayment!=null){
+                if(storePayment.getStatus().equals(0)){
+                    LiveOrderPayment paymentMap=new LiveOrderPayment();
+                    paymentMap.setPaymentId(storePayment.getPaymentId());
+                    paymentMap.setStatus(1);
+                    paymentMap.setPayTime(new Date());
+                    liveOrderPaymentMapper.updateLiveOrderPayment(paymentMap);
+                }
+            }
+            else{
+                log.info("支付单号不存在:"+payCode);
+                throw new CustomException("当前支付记录未找到!");
+            }
+            if(liveOrder.getStatus()!=1){
+                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+                return R.error("当前订单未找到或者订单状态不为待支付!");
+            }
+            //增加用户购买次数
+            userService.incPayCount(Long.valueOf(liveOrder.getUserId()));
+
+            liveOrder.setStatus(2);
+            liveOrder.setPayTime(LocalDateTime.now());
+            baseMapper.updateLiveOrder(liveOrder);
+            return R.ok("支付成功");
+        }catch (Exception e){
+            log.info("抽奖订单支付错误:"+e.getMessage());
+            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
+            LiveOrderPaymentError err = new LiveOrderPaymentError();
+            err.setOrderNo(String.valueOf(orderId));
+            err.setStatus(0);
+            err.setMsg("抽奖订单支付错误:"+e.getMessage());
+            err.setCreateTime(DateUtils.getNowDate());
+            liveOrderPaymentErrorMapper.insertLiveOrderPaymentError(err);
+            return R.error("支付失败");
+        }
+    }
+
 
     @Override
     @Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)

+ 15 - 0
fs-user-app/src/main/java/com/fs/app/controller/LiveOrderController.java

@@ -221,6 +221,21 @@ public class LiveOrderController extends AppBaseController
         return liveOrderService.createRewardLiveOrder(liveOrder);
     }
 
+    /**
+     * 新增订单
+     */
+    @Login
+    @Log(title = "提交中奖订单", businessType = BusinessType.INSERT)
+    @PostMapping("/payConfirmReward")
+    @RepeatSubmit
+    public R payConfirmReward(@RequestBody LiveOrder liveOrder)
+    {
+        log.info("新增订单: {}", JSON.toJSONString(liveOrder));
+
+        liveOrder.setUserId(getUserId());
+        return liveOrderService.payConfirmReward(liveOrder);
+    }
+
     /**
      * 修改订单
      */