Explorar el Código

Merge branch 'sit' of http://8.152.195.41:3000/alien/alien_cloud into second-trade

wxd hace 3 semanas
padre
commit
dd08ebe560

+ 10 - 0
alien-config/src/main/java/shop/alien/config/redis/BaseRedisService.java

@@ -113,6 +113,16 @@ public class BaseRedisService {
     public void setString(String key, String value, Long timeOut) {
         set(key, value, timeOut);
     }
+
+    /**
+     * 判断是否存在指定key
+     * @param key
+     * @return
+     */
+    public boolean hasKey(String key) {
+        Boolean exists = stringRedisTemplate.hasKey(key);
+        return exists != null && exists;
+    }
     
     /**
      * 添加String值, 如果key不存在则设置成功并返回true,否则返回false

+ 4 - 0
alien-entity/src/main/java/shop/alien/entity/store/vo/WebSocketVo.java

@@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 import java.util.Date;
+import java.util.List;
 
 @Data
 @JsonInclude
@@ -35,6 +36,9 @@ public class WebSocketVo {
     @ApiModelProperty(value = "消息id")
     private Integer messageId;
 
+    @ApiModelProperty(value = "消息idList")
+    private List<Integer> messageIdList;
+
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     @ApiModelProperty(value = "创建时间")
     private Date createdTime;

+ 29 - 0
alien-second/src/main/java/shop/alien/second/controller/SecondTradeRecordController.java

@@ -189,4 +189,33 @@ public class SecondTradeRecordController {
             return R.fail("获取评价列表失败: " + e.getMessage());
         }
     }
+
+    @ApiOperation("加入定位")
+    @ApiOperationSupport(order = 13)
+    @ApiImplicitParams({@ApiImplicitParam(name = "otherUserId", value = "对方userId", dataType = "Integer", paramType = "query", required = true)})
+    @GetMapping("/locationShareAdd")
+    public R<String> locationShareAdd(Integer otherUserId) throws Exception {
+        log.info("SecondTradeRecordController.locationShareAdd?otherUserId={}", otherUserId);
+        secondTradeRecordService.locationShareAdd(otherUserId);
+        return R.data("操作成功");
+    }
+
+    @ApiOperation("退出定位")
+    @ApiOperationSupport(order = 14)
+    @ApiImplicitParams({@ApiImplicitParam(name = "otherUserId", value = "对方userId", dataType = "Integer", paramType = "query", required = true)})
+    @GetMapping("/locationShareDel")
+    public R<String> locationShareDel(Integer otherUserId) throws Exception {
+        log.info("SecondTradeRecordController.locationShareDel?otherUserId={}", otherUserId);
+        secondTradeRecordService.locationShareDel(otherUserId);
+        return R.success("操作成功");
+    }
+
+    @ApiOperation("是否在定位里面")
+    @ApiOperationSupport(order = 15)
+    @ApiImplicitParams({@ApiImplicitParam(name = "otherUserId", value = "对方userId", dataType = "Integer", paramType = "query", required = true)})
+    @GetMapping("/locationShareHas")
+    public R<Boolean> locationShareHas(Integer otherUserId) throws Exception {
+        log.info("SecondTradeRecordController.locationShareHas?otherUserId={}", otherUserId);
+        return R.data(secondTradeRecordService.locationShareHas(otherUserId));
+    }
 }

+ 6 - 0
alien-second/src/main/java/shop/alien/second/service/SecondTradeRecordService.java

@@ -52,4 +52,10 @@ public interface SecondTradeRecordService extends IService<SecondTradeRecord> {
      * @return 卖家评价分页列表,包含买家信息、评价内容、评分、风控评分等级等
      */
     IPage<SellerEvaluationVo> getSellerEvaluationList(Integer sellerId, Integer pageNum, Integer pageSize) throws Exception;
+
+    void locationShareAdd(Integer otherUserId) throws Exception;
+
+    void locationShareDel(Integer otherUserId) throws Exception;
+
+    boolean locationShareHas(Integer otherUserId) throws Exception;
 }

+ 119 - 5
alien-second/src/main/java/shop/alien/second/service/impl/SecondTradeRecordServiceImpl.java

@@ -1,13 +1,16 @@
 package shop.alien.second.service.impl;
 
+import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
+import com.alibaba.nacos.common.utils.CollectionUtils;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import io.swagger.models.auth.In;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -15,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import shop.alien.config.properties.RiskControlProperties;
+import shop.alien.config.redis.BaseRedisService;
 import shop.alien.entity.result.BusinessException;
 import shop.alien.entity.second.*;
 import shop.alien.entity.second.vo.SecondEntrustUserDTO;
@@ -33,10 +37,7 @@ import shop.alien.second.service.SecondTradeRecordService;
 import shop.alien.util.common.JwtUtil;
 
 import java.time.*;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -64,6 +65,7 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
     private final LifeUserMapper lifeUserMapper;
     private final AlienStoreFeign alienStoreFeign;
     private final StoreDictionaryMapper storeDictionaryMapper;
+    private final BaseRedisService baseRedisService;
     @Autowired
     private RiskControlProperties riskControlProperties;
 
@@ -1023,7 +1025,119 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
             throw new Exception("获取卖家评价列表失败: " + e.getMessage());
         }
     }
-    
+
+    @Override
+    public void locationShareAdd(Integer otherUserId) throws Exception {
+        try {
+            Integer userId = Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId");
+            String key = getKey(userId, otherUserId);
+
+            if (baseRedisService.hasKey(key)) {
+                JSONArray array = JSONArray.parseArray(baseRedisService.getString(key));
+                if (!array.contains(userId)) array.add(userId);
+                baseRedisService.setString(key, array.toJSONString());
+            } else {
+                JSONArray array = new JSONArray();
+                array.add(userId);
+                baseRedisService.setString(key, array.toJSONString());
+            }
+        } catch (Exception e) {
+            log.error("SecondTradeRecordServiceImpl.locationShareAdd(): Error Msg={}", e.getMessage(), e);
+            throw new Exception("添加位置共享失败: " + e.getMessage());
+        }
+    }
+
+    @Override
+    public void locationShareDel(Integer otherUserId) throws Exception {
+        try {
+            Integer userId = Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId");
+            String key = getKey(userId, otherUserId);
+
+            // 存在key,则删除
+            if (baseRedisService.hasKey(key)) {
+                JSONArray array = JSONArray.parseArray(baseRedisService.getString(key));
+                array.remove(userId);
+                if (array.isEmpty()) {
+                    baseRedisService.delete(key);
+                    LifeUser user = lifeUserMapper.selectById(userId);
+                    LifeUser otherUser = lifeUserMapper.selectById(otherUserId);
+                    LambdaQueryWrapper<LifeMessage> wrapper = new LambdaQueryWrapper<>();
+                    wrapper.eq(LifeMessage::getType, 10);
+                    wrapper.isNull(LifeMessage::getContent);
+                    wrapper.apply("((receiver_id = 'user_" + otherUser.getUserPhone() + "' and sender_id = 'user_" + user.getUserPhone() + "') or (receiver_id = 'user_" + user.getUserPhone() + "' and sender_id = 'user_" + otherUser.getUserPhone() + "'))");
+                    List<Integer> messageIds = lifeMessageMapper.selectList(wrapper).stream().map(LifeMessage::getId).collect(Collectors.toList());
+
+                    if (CollectionUtils.isNotEmpty(messageIds)) {
+                        // 修改消息状态
+                        JSONObject messageJson = new JSONObject();
+                        messageJson.put("cardType", 2);
+                        LambdaUpdateWrapper<LifeMessage> updateWrapper = new LambdaUpdateWrapper<>();
+                        updateWrapper.in(LifeMessage::getId, messageIds);
+                        updateWrapper.set(LifeMessage::getContent, messageJson.toJSONString());
+                        lifeMessageMapper.update(null, updateWrapper);
+
+                        // 发起交易人信息
+                        String phoneId = JwtUtil.getCurrentUserInfo().getString("userType") + "_" + JwtUtil.getCurrentUserInfo().getString("phone");
+
+                        // 获取交易对方信息
+                        LifeUser lifeUser = lifeUserMapper.selectById(otherUserId);
+                        String receiverId = "user_" + lifeUser.getUserPhone();
+
+                        // 给买家与卖家发送交易消息
+                        WebSocketVo webSocketVo = new WebSocketVo();
+                        webSocketVo.setSenderId(phoneId);
+                        webSocketVo.setReceiverId(receiverId);
+                        webSocketVo.setCategory("message");
+                        webSocketVo.setType("10");
+                        webSocketVo.setIsRead(0);
+                        webSocketVo.setText(messageJson.toJSONString());
+                        webSocketVo.setMessageIdList(messageIds);
+
+                        alienStoreFeign.sendMsgToClientByPhoneId(phoneId, JSONObject.from(webSocketVo).toJSONString());
+                        alienStoreFeign.sendMsgToClientByPhoneId(receiverId, JSONObject.from(webSocketVo).toJSONString());
+                    }
+                } else {
+                    baseRedisService.setString(key, array.toJSONString());
+                }
+            }
+        } catch (Exception e) {
+            log.error("SecondTradeRecordServiceImpl.locationShareDel(): Error Msg={}", e.getMessage(), e);
+            throw new Exception("删除位置共享失败: " + e.getMessage());
+        }
+    }
+
+    @Override
+    public boolean locationShareHas(Integer otherUserId) throws Exception {
+        try {
+            Integer userId = Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId");
+            String key = getKey(userId, otherUserId);
+
+            // 是否存在定位信息
+            if (baseRedisService.hasKey(key) && !JSONArray.parseArray(baseRedisService.getString(key)).isEmpty()) {
+                return true;
+            } else {
+                baseRedisService.delete(key);
+            }
+
+        } catch (Exception e) {
+            log.error("SecondTradeRecordServiceImpl.locationShareHas(): Error Msg={}", e.getMessage(), e);
+            throw new Exception("获取位置共享状态失败: " + e.getMessage());
+        }
+        return false;
+    }
+
+    private String getKey(Integer userId, Integer otherUserId) {
+        // 组合userId和otherUserId作为Redis的key
+        // 使用排序方式确保key的一致性(较小的ID在前)
+        String key;
+        if (userId.compareTo(otherUserId) < 0) {
+            key = "second_location_share:" + userId + ":" + otherUserId;
+        } else {
+            key = "second_location_share:" + otherUserId + ":" + userId;
+        }
+        return key;
+    }
+
     /**
      * 计算用户风控评分等级
      * 根据用户违规次数计算等级:O(无记录) < A(1-2次) < B(3-5次) < C(6-9次) < D(10-14次) < E(≥15次)