zhijiezhao 2 سال پیش
والد
کامیت
dba5e3287d

+ 5 - 0
pom.xml

@@ -81,6 +81,11 @@
             <artifactId>spring-security-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>2.1.6</version>
+        </dependency>
     </dependencies>
 
     <profiles>

+ 37 - 5
src/main/java/com/caimei365/manager/controller/caimei/keyword/KeyWordApi.java

@@ -1,15 +1,17 @@
 package com.caimei365.manager.controller.caimei.keyword;
 
+import com.alibaba.excel.EasyExcel;
 import com.caimei365.manager.entity.PaginationVo;
 import com.caimei365.manager.entity.ResponseJson;
 import com.caimei365.manager.entity.caimei.KeyWord;
 import com.caimei365.manager.service.caimei.KeyWordService;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import com.caimei365.manager.service.caimei.listener.KeywordListener;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.IOException;
+import java.io.InputStream;
 
 
 /**
@@ -23,6 +25,9 @@ public class KeyWordApi {
     @Resource
     private KeyWordService keyWordService;
 
+    @Resource
+    private KeywordListener keywordListener;
+
     /**
      * 关键词列表
      * keyword 关键词
@@ -38,7 +43,7 @@ public class KeyWordApi {
                                                               Integer fromSearch,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                               @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
-        return keyWordService.getKeyWordList(keyword, searchTimeCode, fromSearch,beginTime, endTime, labelStatus, pageNum, pageSize);
+        return keyWordService.getKeyWordList(keyword, searchTimeCode, fromSearch, beginTime, endTime, labelStatus, pageNum, pageSize);
     }
 
     /**
@@ -85,4 +90,31 @@ public class KeyWordApi {
     public ResponseJson insertLabel(String keyword) {
         return keyWordService.insertLabel(keyword);
     }
+
+    /**
+     * 导入标签
+     *
+     * @param keywordFile
+     * @return
+     */
+    @PostMapping("/import/label")
+    public ResponseJson doImport(MultipartFile keywordFile) {
+        try {
+            InputStream inputStream = keywordFile.getInputStream();
+            EasyExcel.read(inputStream, KeyWord.class, keywordListener).sheet().doRead();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return ResponseJson.success();
+    }
+
+    /**
+     * 删除标签
+     * @param id
+     * @return
+     */
+    @GetMapping("/delete/label")
+    public ResponseJson delLabel(String id){
+        return keyWordService.deleteLabel(id);
+    }
 }

+ 7 - 1
src/main/java/com/caimei365/manager/dao/KeyWordDao.java

@@ -16,7 +16,13 @@ public interface KeyWordDao {
 
     List<KeyWord> findRecommendList(String keyword, String beginTime, String endTime);
 
-    void updateRecommendFlag(String s);
+    void updateRecommendFlag(String id);
 
     void insertLabel(String keyword);
+
+    void insertLabelByExcel(String keyword);
+
+    Integer findKeywordExist(String keyword);
+
+    void updateDelFlag(String id);
 }

+ 15 - 1
src/main/java/com/caimei365/manager/entity/caimei/KeyWord.java

@@ -1,5 +1,7 @@
 package com.caimei365.manager.entity.caimei;
 
+import com.alibaba.excel.annotation.ExcelIgnore;
+import com.alibaba.excel.annotation.ExcelProperty;
 import lombok.Data;
 import org.springframework.format.annotation.DateTimeFormat;
 
@@ -13,31 +15,43 @@ import java.util.Date;
 public class KeyWord implements Serializable {
     private static final long serialVersionUID = 1L;
 
+    @ExcelIgnore
     private Integer id;
 
+    @ExcelProperty(value = "关键词", index = 0)
     private String keyword;
     /**
      * 标签库数据来源(1:手动添加;2:用户关键词统计;3:导入)
      */
+    @ExcelIgnore
     private Integer fromSearch;
     /**
      * 0:未加入关键词库;1:已加入关键词库
      */
+    @ExcelIgnore
     private Integer labelStatus;
     /**
      * 关键词出现次数
      */
+    @ExcelIgnore
     private Integer frequency;
 
+    /**
+     * 添加到标签库的时间
+     */
+    @ExcelIgnore
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date addTime;
     /**
      * 关键词推荐时间
      */
+    @ExcelIgnore
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date recommendTime;
-
     /**
      * 最近一次搜索时间
      */
+    @ExcelIgnore
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date searchTime;
 }

+ 2 - 0
src/main/java/com/caimei365/manager/service/caimei/KeyWordService.java

@@ -19,4 +19,6 @@ public interface KeyWordService {
     ResponseJson ignoreLabel(String id);
 
     ResponseJson insertLabel(String keyword);
+
+    ResponseJson deleteLabel(String id);
 }

+ 20 - 2
src/main/java/com/caimei365/manager/service/caimei/impl/KeyWordServiceImpl.java

@@ -79,8 +79,26 @@ public class KeyWordServiceImpl implements KeyWordService {
 
     @Override
     public ResponseJson insertLabel(String keyword) {
-        keyWordDao.insertLabel(keyword);
-        return ResponseJson.success();
+        Integer keywordExist = keyWordDao.findKeywordExist(keyword);
+        if (null == keywordExist) {
+            keyWordDao.insertLabel(keyword);
+            return ResponseJson.success();
+        } else {
+            return ResponseJson.success("关键词已存在");
+        }
     }
 
+    @Override
+    public ResponseJson deleteLabel(String id) {
+        Assert.notBlank(id);
+        if (id.contains(",")) {
+            String[] split = id.split(",");
+            for (String s : split) {
+                keyWordDao.updateDelFlag(s);
+            }
+        } else {
+            keyWordDao.updateDelFlag(id);
+        }
+        return ResponseJson.success();
+    }
 }

+ 63 - 0
src/main/java/com/caimei365/manager/service/caimei/listener/KeywordListener.java

@@ -0,0 +1,63 @@
+package com.caimei365.manager.service.caimei.listener;
+
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.event.AnalysisEventListener;
+import com.alibaba.fastjson.JSON;
+import com.caimei365.manager.dao.KeyWordDao;
+import com.caimei365.manager.entity.caimei.KeyWord;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.List;
+
+@Slf4j
+@Service
+public class KeywordListener extends AnalysisEventListener<KeyWord> {
+
+    @Resource
+    private KeyWordDao keyWordDao;
+
+    /**
+     * 批处理阈值
+     */
+    private static final int BATCH_COUNT = 20;
+    List<KeyWord> list = new ArrayList<>(BATCH_COUNT);
+
+    /**
+     * 数据解析
+     *
+     * @param keyword
+     * @param analysisContext
+     */
+    @Override
+    public void invoke(KeyWord keyword, AnalysisContext analysisContext) {
+        log.info("解析到一条数据:{}", JSON.toJSONString(keyword));
+        list.add(keyword);
+        if (list.size() >= BATCH_COUNT) {
+            importItemInfo(list);
+            list.clear();
+        }
+    }
+
+    @Override
+    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
+        importItemInfo(list);
+        log.info("所有数据解析完成!");
+    }
+
+    /**
+     * 插入数据
+     *
+     * @param infoList
+     */
+    public void importItemInfo(List<KeyWord> infoList) {
+        infoList.forEach(k -> {
+            Integer id = keyWordDao.findKeywordExist(k.getKeyword());
+            if (null == id) {
+                keyWordDao.insertLabelByExcel(k.getKeyword());
+            }
+        });
+    }
+}

+ 30 - 10
src/main/resources/mapper/KeyWordDao.xml

@@ -2,28 +2,40 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.caimei365.manager.dao.KeyWordDao">
     <insert id="insertLabel">
-        insert into cm_user_search_frequency(fromSearch, keyword, frequency)
-        values (1, #{keyword}, 0)
+        insert into cm_user_search_frequency(fromSearch, keyword, frequency, trueStatus, addTime)
+        values (1, #{keyword}, 0, 1, now())
+    </insert>
+
+    <insert id="insertLabelByExcel">
+        insert into cm_user_search_frequency(fromSearch, keyword, frequency, trueStatus, addTime)
+        values (3, #{keyword}, 0, 1, now())
     </insert>
 
     <update id="updateLabelStatus">
         update cm_user_search_frequency
-        set trueStatus = 1
+        set trueStatus = 1,
+            fromSearch = 2,
+            addTime    = now()
         where id = #{id}
     </update>
 
     <update id="updateRecommendFlag">
         update cm_user_search_frequency
-        set recommendFlag = 1,
-            fromSearch    = 2
+        set recommendFlag = 1
+        where id = #{id}
+    </update>
+
+    <update id="updateDelFlag">
+        update cm_user_search_frequency
+        set delStatus = 0
         where id = #{id}
     </update>
 
     <select id="findList" resultType="com.caimei365.manager.entity.caimei.KeyWord">
-        SELECT keywordId as id, COUNT(keyWord) AS frequency, cusf.keyword as keyword,
+        SELECT cusf.id AS id, COUNT(cls.keyWordId) AS frequency, cusf.keyword as keyword,addTime,
         cusf.searchTime as searchTime,cusf.trueStatus as labelStatus,cusf.fromSearch as fromSearch
-        FROM cm_label_source cls
-        LEFT JOIN cm_user_search_frequency cusf ON cls.keywordid = cusf.id
+        FROM cm_user_search_frequency cusf
+        LEFT JOIN cm_label_source cls ON cls.keywordid = cusf.id
         WHERE cusf.delStatus = 1
         <if test="beginTime !=null and beginTime !=''">
             AND saveTime BETWEEN #{beginTime} AND #{endTime}
@@ -37,7 +49,8 @@
         <if test="fromSearch != null">
             AND fromSearch = #{fromSearch}
         </if>
-        GROUP BY keywordId
+        GROUP BY cusf.id
+        ORDER BY frequency DESC
     </select>
 
     <select id="findRecommendList" resultType="com.caimei365.manager.entity.caimei.KeyWord">
@@ -49,8 +62,15 @@
         <if test="beginTime !=null and beginTime !=''">
             AND recommendTime BETWEEN #{beginTime} AND #{endTime}
         </if>
-        <if test="keyWord !=null and keyWord !=''">
+        <if test="keyword !=null and keyword !=''">
             AND keyword LIKE CONCAT('%',#{keyword},'%')
         </if>
     </select>
+
+    <select id="findKeywordExist" resultType="java.lang.Integer">
+        select id
+        from cm_user_search_frequency
+        where keyword = #{keyword}
+          and delstatus = 1
+    </select>
 </mapper>