Browse Source

Merge remote-tracking branch 'origin/dev' into dev

shaoguo 7 months ago
parent
commit
594c9853a4

+ 67 - 12
src/main/java/com/sqx/modules/chat/controller/app/AppChatController.java

@@ -21,6 +21,7 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.time.LocalDateTime;
 import java.util.*;
 
 @RestController
@@ -52,14 +53,41 @@ public class AppChatController {
     @PostMapping("/insertChatConversation")
     @ApiOperation("发起聊天")
     public Result insertChatConversation(@RequestAttribute("userId")Long userId,@RequestBody ChatConversation chatConversation){
-        if (chatConversation.getUserId().equals(chatConversation.getFocusedUserId())){
-            return Result.error("发起会话失败");
+
+        // 1. 基础参数校验
+        if (chatConversation == null) {
+            return Result.error("发起会话失败:会话参数不能为空");
+        }
+        Long userId1 = chatConversation.getUserId();
+        Long focusedUserId = chatConversation.getFocusedUserId();
+        String patPushId = String.valueOf(chatConversation.getPostPushId());
+
+        if (userId1 == null || focusedUserId == null || patPushId == null || patPushId.trim().isEmpty()) {
+            return Result.error("发起会话失败:用户ID、关注用户ID、推送ID不能为空");
+        }
+
+        // 2. 校验:发起者不能是自己
+        if (userId1.equals(focusedUserId)) {
+            return Result.error("发起会话失败:不能与自己发起会话");
         }
-        UserEntity user =userService.selectUserById(userId);
+
+        // 3. 唯一性校验:根据user_id+focused_user_id+pat_push_id判断会话是否已存在
+        ChatConversation existConversation = chatConversationService.getOne(new QueryWrapper<ChatConversation>().eq("user_id",chatConversation.getUserId()).eq("focused_user_id",chatConversation.getFocusedUserId()).eq("post_push_id",chatConversation.getPostPushId()));
+        if (existConversation != null) {
+            chatConversation.setUpdateTime(String.valueOf(LocalDateTime.now()));
+            // 存在则直接返回已有会话ID
+            return Result.success("会话已存在").put("data", existConversation);
+        }
+
+        // 4. 校验发起用户是否存在
+        UserEntity user = userService.selectUserById(userId);
         if (user == null) {
             return Result.error("发起会话失败:用户不存在");
         }
-        if (user.getUserType()==2) {
+
+        // 5. 根据用户类型处理会话逻辑
+        // 5.1 VIP用户(userType=2):校验聊天次数
+        if (user.getUserType() == 2) {
             Integer vipConTimes = user.getVipConTimes();
             // 规则:-1=不限次数,0=无剩余次数,>0=剩余次数
             if (vipConTimes == null || vipConTimes == 0) {
@@ -67,19 +95,46 @@ public class AppChatController {
                 String msg = (vipConTimes == 0) ? "聊天次数已用尽,请开通/续费VIP" : "暂无聊天权限,请开通VIP";
                 return Result.error("发起会话失败:" + msg);
             }
-            Result insertResult = chatConversationService.insertChatConversations(chatConversation);
-            // 6. 会话创建成功后,扣减 1 次聊天次数(仅有限次数需要扣减)
-            if (vipConTimes > 0) { // 排除 -1(不限次数)的情况
-                user.setVipConTimes(vipConTimes - 1);
-                userService.updateById(user); // 保存扣减后的次数
 
+            // 5.1.1 创建会话
+            chatConversation.setCreateTime(String.valueOf(LocalDateTime.now()));
+            chatConversation.setUpdateTime(String.valueOf(LocalDateTime.now()));
+            boolean insertResult = chatConversationService.save(chatConversation);
+            if (!insertResult) {
+                return Result.error("发起会话失败:会话创建失败");
             }
-        }else if(user.getUserType()==1){
-        return chatConversationService.insertChatConversations(chatConversation);
+
+            // 5.1.2 扣减聊天次数(仅有限次数需要扣减,-1=不限次数不扣减)
+            if (vipConTimes > 0) {
+                user.setVipConTimes(vipConTimes - 1);
+                boolean updateResult = userService.updateById(user);
+                if (!updateResult ) {
+                    // 扣减次数失败则回滚事务(@Transactional保证)
+                    throw new RuntimeException("会话创建成功,但扣减聊天次数失败");
                 }
-        return Result.error("创建会话失败");
+            }
+
+            // 返回新创建的会话ID
+            return Result.success("会话创建成功").put("data", chatConversation);
+        }
+        // 5.2 普通用户(userType=1):直接创建会话
+        else if (user.getUserType() == 1) {
+            chatConversation.setCreateTime(String.valueOf(LocalDateTime.now()));
+            chatConversation.setUpdateTime(String.valueOf(LocalDateTime.now()));
+            boolean insertResult = chatConversationService.save(chatConversation);
+            if (!insertResult) {
+                return Result.error("发起会话失败:会话创建失败");
+            }
+
+            return Result.success("会话创建成功").put("data", chatConversation);
+        }
+        // 5.3 未知用户类型
+        else {
+            return Result.error("发起会话失败:用户类型不合法");
+        }
     }
 
+
     @Login
     @GetMapping("/selectChatContent")
     @ApiOperation("获取聊天记录")

+ 4 - 1
src/main/java/com/sqx/modules/firstLogin/control/HrController.java

@@ -36,13 +36,16 @@ private  HrService hrService;
     @PostMapping("addHr")
     @ApiOperation("hr设置名片信息")
     public Result addHr (@RequestAttribute("userId") Integer userId, @RequestBody HrEntity hr){
-        if(hr.getHrImg()==null ||hr.getHrPosition() == null|| hr.getHrImg().isEmpty() || hr.getHrPosition().isEmpty()||hr.getHrEmail()==null||hr.getHrEmail().isEmpty()) {
+        if(hr.getHrPosition() == null || hr.getHrPosition().isEmpty()||hr.getHrEmail()==null||hr.getHrEmail().isEmpty()) {
             return Result.error("请完整信息再次尝试");
         }
         HrEntity hrOld = hrService.getByUserId(String.valueOf(userId));
         if (hrOld!=null){
+            if (hr.getHrImg()!=null){
             hrOld.setHrImg(hr.getHrImg());
+            }
             hrOld.setHrPosition(hr.getHrPosition());
+            hrOld.setHrEmail(hr.getHrEmail());
             hrService.updateById(hrOld);
             return  Result.success().put("data",hrOld);
         }else {