From 4ed7d04b9b5bc7f1b91548a7a9daebe41475db82 Mon Sep 17 00:00:00 2001
From: sterben <1473352497@qq.com>
Date: Sun, 22 Oct 2023 03:21:43 +0800
Subject: [PATCH] =?UTF-8?q?[Feature#]=E5=AF=B9=E9=A3=9E=E4=B9=A6=E7=BB=B4?=
 =?UTF-8?q?=E5=BA=A6=E5=8D=87=E7=BA=A7=E5=A2=9E=E5=8A=A0=E4=B8=9A=E5=8A=A1?=
 =?UTF-8?q?=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../saber/biz/INotificationAttrsBiz.java      |  8 ++++++
 .../saber/biz/impl/NotificationAttrsBiz.java  | 26 +++++++++++++++++
 .../saber/biz/impl/NotificationRuleBiz.java   | 18 ++++++++++--
 .../NotificationAttrsController.java          | 23 +++++++++++++++
 .../impl/FeishuMessageChainServiceImpl.java   |  2 ++
 .../pojo/domain/NotificationFeishuAttrs.java  | 17 +++++++++--
 .../saber/pojo/model/FeishuAttrsRequest.java  | 13 +++++++++
 .../service/NotificationAttrsService.java     |  6 ++++
 .../saber/service/NotificationService.java    |  2 +-
 .../impl/NotificationAttrsServiceImpl.java    | 28 +++++++++++++++++++
 .../service/impl/NotificationServiceImpl.java | 10 +++++--
 11 files changed, 144 insertions(+), 9 deletions(-)
 create mode 100644 src/main/java/com/steadon/saber/biz/INotificationAttrsBiz.java
 create mode 100644 src/main/java/com/steadon/saber/biz/impl/NotificationAttrsBiz.java
 create mode 100644 src/main/java/com/steadon/saber/controller/NotificationAttrsController.java
 create mode 100644 src/main/java/com/steadon/saber/pojo/model/FeishuAttrsRequest.java

diff --git a/src/main/java/com/steadon/saber/biz/INotificationAttrsBiz.java b/src/main/java/com/steadon/saber/biz/INotificationAttrsBiz.java
new file mode 100644
index 0000000..8f7e7be
--- /dev/null
+++ b/src/main/java/com/steadon/saber/biz/INotificationAttrsBiz.java
@@ -0,0 +1,8 @@
+package com.steadon.saber.biz;
+
+import com.steadon.saber.pojo.SaberResponse;
+import com.steadon.saber.pojo.model.FeishuAttrsRequest;
+
+public interface INotificationAttrsBiz {
+    SaberResponse<String> updateFeishuAttrs(FeishuAttrsRequest request);
+}
\ No newline at end of file
diff --git a/src/main/java/com/steadon/saber/biz/impl/NotificationAttrsBiz.java b/src/main/java/com/steadon/saber/biz/impl/NotificationAttrsBiz.java
new file mode 100644
index 0000000..49b3e28
--- /dev/null
+++ b/src/main/java/com/steadon/saber/biz/impl/NotificationAttrsBiz.java
@@ -0,0 +1,26 @@
+package com.steadon.saber.biz.impl;
+
+import com.steadon.saber.biz.INotificationAttrsBiz;
+import com.steadon.saber.pojo.SaberResponse;
+import com.steadon.saber.pojo.domain.NotificationFeishuAttrs;
+import com.steadon.saber.pojo.model.FeishuAttrsRequest;
+import com.steadon.saber.service.NotificationAttrsService;
+import lombok.AllArgsConstructor;
+import org.springframework.stereotype.Service;
+
+@Service
+@AllArgsConstructor
+public class NotificationAttrsBiz implements INotificationAttrsBiz {
+
+    private NotificationAttrsService notificationAttrsService;
+
+    @Override
+    public SaberResponse<String> updateFeishuAttrs(FeishuAttrsRequest request) {
+        NotificationFeishuAttrs feishuAttrs = new NotificationFeishuAttrs();
+        feishuAttrs.loadDataFromReq(request);
+        feishuAttrs.updateStatus();
+        notificationAttrsService.updateFeishuAttrs(feishuAttrs);
+
+        return SaberResponse.success();
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/steadon/saber/biz/impl/NotificationRuleBiz.java b/src/main/java/com/steadon/saber/biz/impl/NotificationRuleBiz.java
index 66b32a9..14445cb 100644
--- a/src/main/java/com/steadon/saber/biz/impl/NotificationRuleBiz.java
+++ b/src/main/java/com/steadon/saber/biz/impl/NotificationRuleBiz.java
@@ -2,10 +2,12 @@ package com.steadon.saber.biz.impl;
 
 import com.steadon.saber.biz.INotificationRuleBiz;
 import com.steadon.saber.pojo.SaberResponse;
+import com.steadon.saber.pojo.domain.NotificationFeishuAttrs;
 import com.steadon.saber.pojo.domain.NotificationRule;
 import com.steadon.saber.pojo.domain.dto.NotificationRuleDto;
 import com.steadon.saber.pojo.model.NotificationRuleRequest;
 import com.steadon.saber.pojo.model.pageData.PageData;
+import com.steadon.saber.service.NotificationAttrsService;
 import com.steadon.saber.service.NotificationService;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
@@ -16,14 +18,24 @@ public class NotificationRuleBiz implements INotificationRuleBiz {
 
     private NotificationService notificationService;
 
+    private NotificationAttrsService notificationAttrsService;
+
     @Override
     public SaberResponse<NotificationRule> addNotificationRule(NotificationRuleRequest request) {
         NotificationRule notificationRule = new NotificationRule();
         notificationRule.loadDataFromReq(request);
         notificationRule.createStatus();
         notificationRule.updateStatus();
-
-        return SaberResponse.success(notificationService.addNotificationRule(notificationRule));
+        notificationService.addNotificationRule(notificationRule);
+
+        if (notificationRule.getFeishuStatus()) {
+            NotificationFeishuAttrs feishuAttrs = new NotificationFeishuAttrs();
+            feishuAttrs.setRuleId(notificationRule.getId());
+            feishuAttrs.createStatus();
+            feishuAttrs.updateStatus();
+            notificationAttrsService.addFeishuAttrs(feishuAttrs);
+        }
+        return SaberResponse.success(notificationRule);
     }
 
     @Override
@@ -32,7 +44,6 @@ public class NotificationRuleBiz implements INotificationRuleBiz {
         notificationRule.setId(ruleId);
         notificationRule.loadDataFromReq(request);
         notificationRule.updateStatus();
-
         notificationService.updateNotificationRule(notificationRule);
         return SaberResponse.success();
     }
@@ -40,6 +51,7 @@ public class NotificationRuleBiz implements INotificationRuleBiz {
     @Override
     public SaberResponse<String> deleteNotificationRule(Integer ruleId) {
         notificationService.deleteNotificationRule(ruleId);
+        notificationAttrsService.deleteFeishuAttrs(ruleId);
         return SaberResponse.success();
     }
 
diff --git a/src/main/java/com/steadon/saber/controller/NotificationAttrsController.java b/src/main/java/com/steadon/saber/controller/NotificationAttrsController.java
new file mode 100644
index 0000000..107ed98
--- /dev/null
+++ b/src/main/java/com/steadon/saber/controller/NotificationAttrsController.java
@@ -0,0 +1,23 @@
+package com.steadon.saber.controller;
+
+import com.steadon.saber.biz.INotificationAttrsBiz;
+import com.steadon.saber.pojo.SaberResponse;
+import com.steadon.saber.pojo.model.FeishuAttrsRequest;
+import lombok.AllArgsConstructor;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@AllArgsConstructor
+@RequestMapping("/saber")
+public class NotificationAttrsController {
+
+    private INotificationAttrsBiz iNotificationAttrsBiz;
+
+    @PostMapping("/feishu/attrs")
+    public SaberResponse<String> updateFeishuAttrs(@RequestBody FeishuAttrsRequest request) {
+        return iNotificationAttrsBiz.updateFeishuAttrs(request);
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/steadon/saber/handler/chain/impl/FeishuMessageChainServiceImpl.java b/src/main/java/com/steadon/saber/handler/chain/impl/FeishuMessageChainServiceImpl.java
index 47c1785..96b1b22 100644
--- a/src/main/java/com/steadon/saber/handler/chain/impl/FeishuMessageChainServiceImpl.java
+++ b/src/main/java/com/steadon/saber/handler/chain/impl/FeishuMessageChainServiceImpl.java
@@ -26,6 +26,7 @@ public class FeishuMessageChainServiceImpl implements MessageChainService {
 
     @Autowired
     private FeishuRobotService feishuRobotService;
+
     @Autowired
     private NotificationAttrsService notificationAttrsService;
 
@@ -34,6 +35,7 @@ public class FeishuMessageChainServiceImpl implements MessageChainService {
 
     @Value("${lancelot.feishu-app-id}")
     private String LANCELOT_APP_ID;
+
     @Value("${lancelot.feishu-app-secret}")
     private String LANCELOT_APP_SECRET;
 
diff --git a/src/main/java/com/steadon/saber/pojo/domain/NotificationFeishuAttrs.java b/src/main/java/com/steadon/saber/pojo/domain/NotificationFeishuAttrs.java
index 3005d5d..60af27b 100644
--- a/src/main/java/com/steadon/saber/pojo/domain/NotificationFeishuAttrs.java
+++ b/src/main/java/com/steadon/saber/pojo/domain/NotificationFeishuAttrs.java
@@ -1,7 +1,11 @@
 package com.steadon.saber.pojo.domain;
 
-import com.baomidou.mybatisplus.annotation.*;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.Version;
 import com.steadon.saber.handler.token.TokenHandler;
+import com.steadon.saber.pojo.model.FeishuAttrsRequest;
 import lombok.Data;
 
 import java.io.Serial;
@@ -26,7 +30,7 @@ public class NotificationFeishuAttrs implements Serializable {
     /**
      * 规则ID
      */
-    @TableId(value = "ruleId", type = IdType.AUTO)
+    @TableId(value = "ruleId")
     private Integer ruleId;
 
     /**
@@ -75,6 +79,15 @@ public class NotificationFeishuAttrs implements Serializable {
     @TableField("update_time")
     private LocalDateTime updateTime;
 
+    public void loadDataFromReq(FeishuAttrsRequest request) {
+        this.ruleId = request.getRuleId();
+        this.userId = request.getUserId();
+        this.chatId = request.getChatId();
+        this.templateId = request.getTemplateId();
+        this.feishuAppId = request.getFeishuAppId();
+        this.messageType = request.getMessageType();
+    }
+
     public void createStatus() {
         this.createBy = TokenHandler.getUsername();
         this.createTime = LocalDateTime.now();
diff --git a/src/main/java/com/steadon/saber/pojo/model/FeishuAttrsRequest.java b/src/main/java/com/steadon/saber/pojo/model/FeishuAttrsRequest.java
new file mode 100644
index 0000000..38cdf1e
--- /dev/null
+++ b/src/main/java/com/steadon/saber/pojo/model/FeishuAttrsRequest.java
@@ -0,0 +1,13 @@
+package com.steadon.saber.pojo.model;
+
+import lombok.Data;
+
+@Data
+public class FeishuAttrsRequest {
+    private Integer ruleId;
+    private String userId;
+    private String chatId;
+    private String templateId;
+    private String feishuAppId;
+    private String messageType;
+}
\ No newline at end of file
diff --git a/src/main/java/com/steadon/saber/service/NotificationAttrsService.java b/src/main/java/com/steadon/saber/service/NotificationAttrsService.java
index 2361a52..df8a773 100644
--- a/src/main/java/com/steadon/saber/service/NotificationAttrsService.java
+++ b/src/main/java/com/steadon/saber/service/NotificationAttrsService.java
@@ -5,4 +5,10 @@ import com.steadon.saber.pojo.domain.NotificationFeishuAttrs;
 public interface NotificationAttrsService {
 
     NotificationFeishuAttrs getFeishuAttrs(Integer ruleId);
+
+    void addFeishuAttrs(NotificationFeishuAttrs feishuAttrs);
+
+    void deleteFeishuAttrs(Integer ruleId);
+
+    void updateFeishuAttrs(NotificationFeishuAttrs feishuAttrs);
 }
diff --git a/src/main/java/com/steadon/saber/service/NotificationService.java b/src/main/java/com/steadon/saber/service/NotificationService.java
index de88e1b..ae4276f 100644
--- a/src/main/java/com/steadon/saber/service/NotificationService.java
+++ b/src/main/java/com/steadon/saber/service/NotificationService.java
@@ -11,7 +11,7 @@ public interface NotificationService {
 
     NotificationRule getNotificationRule(String ruleCode);
 
-    NotificationRule addNotificationRule(NotificationRule notificationRule);
+    void addNotificationRule(NotificationRule notificationRule);
 
     void updateNotificationRule(NotificationRule notificationRule);
 
diff --git a/src/main/java/com/steadon/saber/service/impl/NotificationAttrsServiceImpl.java b/src/main/java/com/steadon/saber/service/impl/NotificationAttrsServiceImpl.java
index 9e6e72b..0ce75da 100644
--- a/src/main/java/com/steadon/saber/service/impl/NotificationAttrsServiceImpl.java
+++ b/src/main/java/com/steadon/saber/service/impl/NotificationAttrsServiceImpl.java
@@ -2,8 +2,11 @@ package com.steadon.saber.service.impl;
 
 import com.steadon.saber.mapper.NotificationFeishuAttrsMapper;
 import com.steadon.saber.pojo.domain.NotificationFeishuAttrs;
+import com.steadon.saber.pojo.model.exception.SaberBaseException;
+import com.steadon.saber.pojo.model.resultEnum.ResultCode;
 import com.steadon.saber.service.NotificationAttrsService;
 import lombok.AllArgsConstructor;
+import org.springframework.dao.OptimisticLockingFailureException;
 import org.springframework.stereotype.Service;
 
 @Service
@@ -16,4 +19,29 @@ public class NotificationAttrsServiceImpl implements NotificationAttrsService {
     public NotificationFeishuAttrs getFeishuAttrs(Integer ruleId) {
         return notificationFeishuAttrsMapper.selectById(ruleId);
     }
+
+    @Override
+    public void addFeishuAttrs(NotificationFeishuAttrs feishuAttrs) {
+        if (notificationFeishuAttrsMapper.insert(feishuAttrs) == 0) {
+            throw new OptimisticLockingFailureException("Optimistic locking failed");
+        }
+    }
+
+    @Override
+    public void deleteFeishuAttrs(Integer ruleId) {
+        if (notificationFeishuAttrsMapper.deleteById(ruleId) == 0) {
+            throw new OptimisticLockingFailureException("Optimistic locking failed");
+        }
+    }
+
+    @Override
+    public void updateFeishuAttrs(NotificationFeishuAttrs feishuAttrs) {
+        NotificationFeishuAttrs selected = notificationFeishuAttrsMapper.selectById(feishuAttrs.getRuleId());
+        if (selected == null) {
+            throw new SaberBaseException(ResultCode.INVALID_PARAM, "rule not found");
+        }
+        if (notificationFeishuAttrsMapper.updateById(feishuAttrs) == 0) {
+            throw new OptimisticLockingFailureException("Optimistic locking failed");
+        }
+    }
 }
diff --git a/src/main/java/com/steadon/saber/service/impl/NotificationServiceImpl.java b/src/main/java/com/steadon/saber/service/impl/NotificationServiceImpl.java
index 0a265f6..2641f09 100644
--- a/src/main/java/com/steadon/saber/service/impl/NotificationServiceImpl.java
+++ b/src/main/java/com/steadon/saber/service/impl/NotificationServiceImpl.java
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.steadon.saber.handler.chain.model.MessageInfo;
+import com.steadon.saber.mapper.NotificationFeishuAttrsMapper;
 import com.steadon.saber.mapper.NotificationMapper;
 import com.steadon.saber.mapper.NotificationRuleMapper;
 import com.steadon.saber.mapper.NotificationUserMergeMapper;
@@ -19,6 +20,7 @@ import lombok.AllArgsConstructor;
 import org.springframework.dao.OptimisticLockingFailureException;
 import org.springframework.retry.annotation.Retryable;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
 
 @Service
@@ -31,9 +33,11 @@ public class NotificationServiceImpl implements NotificationService {
 
     private NotificationUserMergeMapper notificationUserMergeMapper;
 
+    private NotificationFeishuAttrsMapper notificationFeishuAttrsMapper;
+
     @Override
     @Retryable(retryFor = OptimisticLockingFailureException.class)
-    @Transactional(noRollbackFor = OptimisticLockingFailureException.class)
+    @Transactional(noRollbackFor = OptimisticLockingFailureException.class, isolation = Isolation.READ_COMMITTED)
     public void addNotification(MessageInfo messageInfo) {
         Notification notification = new Notification();
         notification.setTitle(messageInfo.getTitle());
@@ -72,11 +76,11 @@ public class NotificationServiceImpl implements NotificationService {
 
     @Override
     @Retryable(retryFor = OptimisticLockingFailureException.class)
-    public NotificationRule addNotificationRule(NotificationRule notificationRule) {
+    @Transactional(noRollbackFor = OptimisticLockingFailureException.class, isolation = Isolation.READ_COMMITTED)
+    public void addNotificationRule(NotificationRule notificationRule) {
         if (notificationRuleMapper.insert(notificationRule) == 0) {
             throw new OptimisticLockingFailureException("Optimistic locking failed");
         }
-        return notificationRule;
     }
 
     @Override
-- 
GitLab