Browse Source

多个关键词插入判断方法

yu_ber 4 years ago
parent
commit
258b678444

+ 16 - 27
mpwechatApp/src/main/java/com/liangjian11/wx/mp/controller/WxKeywordController.java

@@ -29,23 +29,12 @@ public class WxKeywordController {
     /**
      * 添加关键字
      * @param keyword
+     * @param appId
      * @return
      */
     @PostMapping("/add/{appid}")
-    public ResultInfo addKeyword(@PathVariable String appid,@RequestBody Keyword keyword){
-        if(StringUtils.isBlank(appid)){
-            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,2002,"appid不能为空");
-        }
-        //判断是否已经存在此关键字
-        if(keywordService.getKeywordByKeyword(keyword.getKeyword(),keyword.getParentId(),appid)>0){
-            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,333,"此关键字已存在");
-        }else {
-            keyword.setAppId(appid);
-            if(keywordService.insert(keyword)>0){
-                return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS,111,"添加关键字成功");
-            }
-            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,222,"添加关键字失败");
-        }
+    public ResultInfo addKeyword(@PathVariable String appId,@RequestBody Keyword keyword){
+        return keywordService.addKeyword(appId,keyword);
     }
 
     /**
@@ -75,6 +64,18 @@ public class WxKeywordController {
         return keywordService.getKeywordByParentId(parentId);
     }
 
+    /**
+     * 添加规则和关键字
+     * 如果输入的关键字相同,或者数据库已经存在此关键字,则不插入
+     * @param appid
+     * @param keywordList
+     * @return
+     */
+    @PostMapping("/adds/{appid}")
+    public ResultInfo insertList(@PathVariable String appid,@RequestBody List<Keyword> keywordList){
+        return keywordService.checkBatchInsert(keywordList,appid);
+    }
+
     /**
      * 更改规则发送消息关系
      * @param keywordMessageRef
@@ -92,19 +93,7 @@ public class WxKeywordController {
      */
     @PostMapping("/insert")
     public ResultInfo insert(@RequestBody KeywordMessageRef keywordMessageRef){
-        if(keywordMessageRef.getMessageId()==null){
-            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,2002,"推送消息ID不能为空");
-        }
-        if(keywordMessageRef.getKeyWordId()==null){
-            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,2002,"规则ID不能为空");
-        }
-        if(keywordMessageRefService.selectCount(keywordMessageRef.getKeyWordId(),keywordMessageRef.getMessageId())>0){
-            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,333,"规则对应发送消息关系已经存在,请勿重复添加");
-        }else if(keywordMessageRefService.insert(keywordMessageRef)>0){
-          return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS,111,"添加成功");
-        }else {
-          return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,222,"添加失败");
-        }
+        return keywordMessageRefService.addKeywordMessageRef(keywordMessageRef);
     }
 
     /**

+ 1 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/mapper/KeywordMapper.java

@@ -2,6 +2,7 @@ package com.liangjian11.wx.mp.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.liangjian11.wx.mp.modle.Keyword;
+import org.apache.ibatis.annotations.Insert;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 

+ 3 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/KeywordMessageRefService.java

@@ -2,6 +2,7 @@ package com.liangjian11.wx.mp.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.liangjian11.wx.mp.modle.KeywordMessageRef;
+import com.liangjian11.wx.mp.utils.ResultInfo;
 
 import java.util.List;
 
@@ -29,4 +30,6 @@ public interface KeywordMessageRefService extends IService<KeywordMessageRef> {
     int update(KeywordMessageRef keywordMessageRef);
 
     int delete(Integer id);
+
+    ResultInfo addKeywordMessageRef(KeywordMessageRef keywordMessageRef);
 }

+ 29 - 9
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/KeywordService.java

@@ -2,7 +2,9 @@ package com.liangjian11.wx.mp.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.liangjian11.wx.mp.modle.Keyword;
+import com.liangjian11.wx.mp.utils.ResultInfo;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -12,26 +14,44 @@ import java.util.List;
 public interface KeywordService extends IService<Keyword> {
     Keyword selectById(Integer Id);
 
+    int editKeyword(Keyword keyword);
+
+    int insert(Keyword keyword);
+
+    int delete(Integer id);
+
     /**
-     *
+     * 根据规则名id查询关键词
+     * @param parentId
+     * @return
+     */
+    List<Keyword> getKeywordByParentId(int parentId);
+
+    /**
+     * 查询数据库是否存在相同关键字
+     * @param rank 规则/关键词标识
      * @param keyword 关键词/规则名
      * @param parentId 关键词所属规则ID
      * @param appId appID
      * @return
      */
-    int getKeywordByKeyword(String keyword,Integer parentId,String appId);
+    int getKeywordByKeyword(String keyword,Integer parentId,String appId,Integer rank);
 
     /**
-     * 根据规则名id查询关键词
-     * @param parentId
+     * 添加关键字
+     * @param appId
+     * @param keyword
      * @return
      */
-    List<Keyword> getKeywordByParentId(int parentId);
-
-    int editKeyword(Keyword keyword);
+    ResultInfo addKeyword(String appId,Keyword keyword);
 
-    int insert(Keyword keyword);
+    /**
+     * 多行插入检查是否存在相同
+     * @param keywordList
+     * @param appId
+     * @return
+     */
+    ResultInfo checkBatchInsert(List<Keyword> keywordList, String appId);
 
-    int delete(Integer id);
 
 }

+ 33 - 15
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/impl/KeywordMessageRefServiceImpl.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.liangjian11.wx.mp.mapper.KeywordMessageRefMapper;
 import com.liangjian11.wx.mp.modle.KeywordMessageRef;
 import com.liangjian11.wx.mp.service.KeywordMessageRefService;
+import com.liangjian11.wx.mp.utils.ResultInfo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -20,21 +21,6 @@ public class KeywordMessageRefServiceImpl extends ServiceImpl<KeywordMessageRefM
     @Autowired
     private KeywordMessageRefMapper keywordMessageRefMapper;
 
-    @Override
-    public List<Integer> getMessageIdByKeywordId(Integer KeywordId) {
-        ArrayList<Integer> msgList = new ArrayList<>();
-        List<KeywordMessageRef> list = keywordMessageRefMapper.selectList(new QueryWrapper<KeywordMessageRef>().eq("KeywordId", KeywordId));
-        for (KeywordMessageRef keywordMessageRef : list) {
-            msgList.add(keywordMessageRef.getMessageId());
-        }
-        return msgList;
-    }
-
-    @Override
-    public int selectCount(int keyWordId, int messageId) {
-        return keywordMessageRefMapper.selectCount(new QueryWrapper<KeywordMessageRef>().eq("keyWordId",keyWordId).eq("messageId",messageId));
-    }
-
     @Override
     public int insert(KeywordMessageRef keywordMessageRef) {
         return keywordMessageRefMapper.insert(keywordMessageRef);
@@ -49,4 +35,36 @@ public class KeywordMessageRefServiceImpl extends ServiceImpl<KeywordMessageRefM
     public int delete(Integer id) {
         return keywordMessageRefMapper.deleteById(id);
     }
+
+    @Override
+    public ResultInfo addKeywordMessageRef(KeywordMessageRef keywordMessageRef) {
+        if(keywordMessageRef.getMessageId()==null){
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,2002,"推送消息ID不能为空");
+        }
+        if(keywordMessageRef.getKeyWordId()==null){
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,2002,"规则ID不能为空");
+        }
+        if(this.selectCount(keywordMessageRef.getKeyWordId(),keywordMessageRef.getMessageId())>0){
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,333,"规则对应发送消息关系已经存在,请勿重复添加");
+        }else if(this.insert(keywordMessageRef)>0){
+            return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS,111,"添加成功");
+        }else {
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,222,"添加失败");
+        }
+    }
+
+    @Override
+    public List<Integer> getMessageIdByKeywordId(Integer KeywordId) {
+        ArrayList<Integer> msgList = new ArrayList<>();
+        List<KeywordMessageRef> list = keywordMessageRefMapper.selectList(new QueryWrapper<KeywordMessageRef>().eq("KeywordId", KeywordId));
+        for (KeywordMessageRef keywordMessageRef : list) {
+            msgList.add(keywordMessageRef.getMessageId());
+        }
+        return msgList;
+    }
+
+    @Override
+    public int selectCount(int keyWordId, int messageId) {
+        return keywordMessageRefMapper.selectCount(new QueryWrapper<KeywordMessageRef>().eq("keyWordId",keyWordId).eq("messageId",messageId));
+    }
 }

+ 63 - 17
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/impl/KeywordServiceImpl.java

@@ -8,9 +8,12 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.liangjian11.wx.mp.mapper.KeywordMapper;
 import com.liangjian11.wx.mp.modle.Keyword;
 import com.liangjian11.wx.mp.service.KeywordService;
+import com.liangjian11.wx.mp.utils.ResultInfo;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -28,15 +31,6 @@ public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword> impl
         return keywordMapper.selectById(Id);
     }
 
-    @Override
-    public int getKeywordByKeyword(String keyword,Integer parentId,String appId) {
-        QueryWrapper<Keyword> wrapper = new QueryWrapper<>();
-        wrapper.eq("keyword",keyword);
-        wrapper.eq("parentId",parentId);
-        wrapper.eq("appId",appId);
-        return keywordMapper.selectCount(wrapper);
-    }
-
     @Override
     public int editKeyword(Keyword keyword) {
         return keywordMapper.updateById(keyword);
@@ -52,17 +46,69 @@ public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword> impl
         return keywordMapper.deleteById(id);
     }
 
+    @Override
+    public int getKeywordByKeyword(String keyword,Integer parentId,String appId,Integer rank) {
+        QueryWrapper<Keyword> wrapper = new QueryWrapper<>();
+        wrapper.eq("keyword",keyword);
+        wrapper.eq("parentId",parentId);
+        wrapper.eq("appId",appId);
+        wrapper.eq("rank",rank);
+        return keywordMapper.selectCount(wrapper);
+    }
+
+    @Override
+    public ResultInfo addKeyword(String appId, Keyword keyword) {
+        if(StringUtils.isBlank(appId)){
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,2002,"appid不能为空");
+        }
+        //判断是否已经存在此关键字
+        if(this.getKeywordByKeyword(keyword.getKeyword(),keyword.getParentId(),appId,keyword.getRank())>0){
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,333,"此关键字已存在");
+        }else {
+            keyword.setAppId(appId);
+            if(this.insert(keyword)>0){
+                return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS,111,"添加关键字成功");
+            }
+            return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,222,"添加关键字失败");
+        }
+    }
+
+    @Override
+    public ResultInfo checkBatchInsert(List<Keyword> keywordList, String appId) {
+        int num = 0;
+        //判断输入的关键词是否存在相同
+        if(keywordList.size()>2){
+            for(int i=0;i<keywordList.size()-1;i++){
+                for(int j=0;j<keywordList.size()-i-1;j++){
+                    if(keywordList.get(j).getKeyword().equals(keywordList.get(j+1).getKeyword())&&keywordList.get(j).getRank().equals(keywordList.get(j+1).getRank())){
+                        return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,444,"重复输入关键词"+keywordList.get(j).getKeyword());
+                    }
+                }
+            }
+        }
+        //判断输入的关键词在数据库中是否已经存在
+        for (Keyword keyword : keywordList) {
+            int i = this.getKeywordByKeyword(keyword.getKeyword(), keyword.getParentId(), appId, keyword.getRank());
+            if(i>0){
+              num++;
+          }
+            if(num>0){return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,333,"关键词或规则"+keyword.getKeyword()+"已经存在");}
+        }
+        //如果没有重复执行插入
+        if(num==0){
+            for (Keyword keyword : keywordList) {
+                keyword.setAppId(appId);
+                keywordMapper.insert(keyword);
+            }
+        }
+        return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS,111,"添加关键字成功");
+    }
+
+
     @Override
     public List<Keyword> getKeywordByParentId(int parentId) {
         return keywordMapper.selectList(new QueryWrapper<Keyword>().eq("parentId",parentId));
     }
 
-   /* @Override
-    public List<Keyword> getKeywordPageList(int start, int pageSize) {
-        IPage<Map<String, Object>> mapIPage = keywordMapper.selectMapsPage(new Page<Keyword>(start, pageSize), null);
-        List<Map<String, Object>> records = mapIPage.getRecords();
-        for (Map<String, Object> record : records) {
-        }
-        return  null;
-    }*/
+
 }