营养小知识修改优化
This commit is contained in:
		| @@ -18,6 +18,8 @@ public class SysNutritionQuestion extends BaseEntity | ||||
|     /** $column.columnComment */ | ||||
|     private Long id; | ||||
|  | ||||
|     private Long[] ids; | ||||
|  | ||||
|     /** 标题 */ | ||||
|     @Excel(name = "标题") | ||||
|     private String title; | ||||
|   | ||||
| @@ -1,9 +1,11 @@ | ||||
| package com.stdiet.custom.dto.response; | ||||
|  | ||||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||||
| import com.stdiet.common.annotation.Excel; | ||||
| import lombok.Data; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.util.Date; | ||||
|  | ||||
| @Data | ||||
| public class NutritionQuestionResponse implements Serializable { | ||||
| @@ -14,14 +16,14 @@ public class NutritionQuestionResponse implements Serializable { | ||||
|     private String id; | ||||
|  | ||||
|     /** 标题 */ | ||||
|     @Excel(name = "标题") | ||||
|     private String title; | ||||
|  | ||||
|     /** 内容 */ | ||||
|     @Excel(name = "内容") | ||||
|     private String content; | ||||
|  | ||||
|     /** 关键词,逗号隔开 */ | ||||
|     @Excel(name = "关键词,逗号隔开") | ||||
|     private String key; | ||||
|  | ||||
|     /** 小程序是否显示,0不显示 1显示 */ | ||||
|     private String showFlag; | ||||
| } | ||||
|   | ||||
| @@ -79,4 +79,12 @@ public interface SysNutritionQuestionMapper | ||||
|      * @return | ||||
|      */ | ||||
|     public List<SysNutritionQuestion> getNutritionQuestionListByPage(@Param("start")int start, @Param("pageSize")int pageSize); | ||||
|  | ||||
|     /** | ||||
|      * 更新微信展示状态 | ||||
|      * @param wxShow 是否展示  0不展示 1展示 | ||||
|      * @param ids id数组 | ||||
|      * @return | ||||
|      */ | ||||
|     public int updateWxShowByIds(@Param("wxShow")Integer wxShow, @Param("array") Long[] ids); | ||||
| } | ||||
| @@ -73,4 +73,12 @@ public interface ISysNutritionQuestionService | ||||
|      */ | ||||
|     public boolean regenerateNutritionQuestionIndex(); | ||||
|  | ||||
|     /** | ||||
|      * 更新微信展示状态 | ||||
|      * @param wxShow 是否展示  0不展示 1展示 | ||||
|      * @param ids id数组 | ||||
|      * @return | ||||
|      */ | ||||
|     public int updateWxShowByIds(Integer wxShow,  Long[] ids); | ||||
|  | ||||
| } | ||||
| @@ -6,12 +6,17 @@ import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| import com.stdiet.common.utils.DateUtils; | ||||
| import com.stdiet.common.utils.StringUtils; | ||||
| import com.stdiet.common.utils.reflect.ReflectUtils; | ||||
| import com.stdiet.custom.dto.response.NutritionQuestionResponse; | ||||
| import com.stdiet.custom.utils.LuceneIndexUtils; | ||||
| import org.apache.lucene.document.Document; | ||||
| import org.apache.lucene.document.Field; | ||||
| import org.apache.lucene.document.TextField; | ||||
| import org.apache.lucene.index.Term; | ||||
| import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; | ||||
| import org.apache.lucene.search.*; | ||||
| import org.apache.lucene.util.Version; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Service; | ||||
| @@ -35,10 +40,19 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     public String index_path; | ||||
|  | ||||
|     //建立索引的字段名称 | ||||
|     public static final String[] index_field_array = {"id", "title", "content", "key"}; | ||||
|     public static final String[] index_field_array = {"id", "title", "content", "key", "showFlag"}; | ||||
|     //查询字段 | ||||
|     public static final String[] index_select_field_array = {"title", "content", "key"}; | ||||
|  | ||||
|     public static Map<String, Integer> nutritionQuestionBoostMap = null; | ||||
|  | ||||
|     static{ | ||||
|         nutritionQuestionBoostMap = new HashMap<>(); | ||||
|         nutritionQuestionBoostMap.put("key", 100); | ||||
|         nutritionQuestionBoostMap.put("title", 80); | ||||
|         nutritionQuestionBoostMap.put("content", 60); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 查询营养知识小问答 | ||||
|      * | ||||
| @@ -74,7 +88,7 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     { | ||||
|         sysNutritionQuestion.setCreateTime(DateUtils.getNowDate()); | ||||
|         if(sysNutritionQuestionMapper.insertSysNutritionQuestion(sysNutritionQuestion) > 0){ | ||||
|             return createNutritionQuestionIndex(sysNutritionQuestion) ? 1 : 0; | ||||
|             return createNutritionQuestionIndex(sysNutritionQuestion.getId()) ? 1 : 0; | ||||
|         } | ||||
|         return 0; | ||||
|     } | ||||
| @@ -90,7 +104,7 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     { | ||||
|         sysNutritionQuestion.setUpdateTime(DateUtils.getNowDate()); | ||||
|         if(sysNutritionQuestionMapper.updateSysNutritionQuestion(sysNutritionQuestion) > 0){ | ||||
|             return updateNutritionQuestionIndex(sysNutritionQuestion) ? 1 : 0; | ||||
|             return updateNutritionQuestionIndex(sysNutritionQuestion.getId()) ? 1 : 0; | ||||
|         } | ||||
|         return 0; | ||||
|     } | ||||
| @@ -152,7 +166,24 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|         try{ | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             Map<String, Object> indexMap = luceneIndexUtils.queryByKeyword(sysNutritionQuestion.getKey(), index_select_field_array, pageNum, pageSize); | ||||
|  | ||||
|             //建立查询语句 | ||||
|             BooleanQuery booleanQuery = new BooleanQuery(); | ||||
|  | ||||
|             //装配 | ||||
|             MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44, index_select_field_array, luceneIndexUtils.analyzer); | ||||
|  | ||||
|             //解析输入关键字 | ||||
|             Query keywordQuery = StringUtils.isEmpty(sysNutritionQuestion.getKey()) ? new WildcardQuery(new Term(LuceneIndexUtils.default_primary_key, "*")) : queryParser.parse(sysNutritionQuestion.getKey()); | ||||
|  | ||||
|             booleanQuery.add(keywordQuery, BooleanClause.Occur.MUST); | ||||
|  | ||||
|             if(sysNutritionQuestion.getShowFlag() != null){ | ||||
|                 Query showFlagQuery = new TermQuery(new Term("showFlag", sysNutritionQuestion.getShowFlag().intValue()+"")); | ||||
|                 booleanQuery.add(showFlagQuery, BooleanClause.Occur.MUST); | ||||
|             } | ||||
|  | ||||
|             Map<String, Object> indexMap = luceneIndexUtils.queryByKeyword(booleanQuery, StringUtils.isNotEmpty(sysNutritionQuestion.getKey()), pageNum, pageSize); | ||||
|             total = (int)indexMap.get("total"); | ||||
|             List<Document> documentList = (List<Document>)indexMap.get("data"); | ||||
|             if(documentList != null && documentList.size() > 0){ | ||||
| @@ -204,7 +235,8 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|      * @param sysNutritionQuestion | ||||
|      * @return | ||||
|      */ | ||||
|     private boolean createNutritionQuestionIndex(SysNutritionQuestion sysNutritionQuestion){ | ||||
|     private boolean createNutritionQuestionIndex(Long id){ | ||||
|         SysNutritionQuestion sysNutritionQuestion = selectSysNutritionQuestionById(id); | ||||
|         try{ | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
| @@ -221,8 +253,9 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|      * @param sysNutritionQuestion | ||||
|      * @return | ||||
|      */ | ||||
|     private boolean updateNutritionQuestionIndex(SysNutritionQuestion sysNutritionQuestion){ | ||||
|     private boolean updateNutritionQuestionIndex(Long id){ | ||||
|         try{ | ||||
|             SysNutritionQuestion sysNutritionQuestion = selectSysNutritionQuestionById(id); | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             Document document = nutritionQuestionToDocument(sysNutritionQuestion); | ||||
| @@ -247,10 +280,19 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|         return nutritionQuestionResponse; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * | ||||
|      * @param sysNutritionQuestion | ||||
|      * @return | ||||
|      */ | ||||
|     private Document nutritionQuestionToDocument(SysNutritionQuestion sysNutritionQuestion){ | ||||
|         Document document = new Document(); | ||||
|         for (String fieldName : index_field_array) { | ||||
|             document.add(new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES)); | ||||
|             TextField field = new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES); | ||||
|             if(nutritionQuestionBoostMap.containsKey(fieldName)){ | ||||
|                 field.setBoost(nutritionQuestionBoostMap.get(fieldName).floatValue()); | ||||
|             } | ||||
|             document.add(field); | ||||
|         } | ||||
|         return document; | ||||
|     } | ||||
| @@ -258,12 +300,24 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     private List<Document> nutritionQuestionToDocument(List<SysNutritionQuestion> sysNutritionQuestionList){ | ||||
|         List<Document> result = new ArrayList<>(); | ||||
|         for (SysNutritionQuestion sysNutritionQuestion : sysNutritionQuestionList) { | ||||
|             Document document = new Document(); | ||||
|             for (String fieldName : index_field_array) { | ||||
|                 document.add(new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES)); | ||||
|             } | ||||
|             result.add(document); | ||||
|             result.add(nutritionQuestionToDocument(sysNutritionQuestion)); | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 更新微信展示状态 | ||||
|      * @param wxShow 是否展示  0不展示 1展示 | ||||
|      * @param ids id数组 | ||||
|      * @return | ||||
|      */ | ||||
|     public int updateWxShowByIds(Integer wxShow,  Long[] ids){ | ||||
|         if(sysNutritionQuestionMapper.updateWxShowByIds(wxShow, ids) > 0){ | ||||
|             for (Long id : ids) { | ||||
|                 updateNutritionQuestionIndex(id); | ||||
|             } | ||||
|             return 1; | ||||
|         } | ||||
|         return 0; | ||||
|     } | ||||
| } | ||||
| @@ -26,29 +26,39 @@ import java.util.Map; | ||||
| public class LuceneIndexUtils { | ||||
|  | ||||
|     //分词器 | ||||
|     private IKAnalyzer analyzer = null; | ||||
|     public static IKAnalyzer analyzer = null; | ||||
|     //分词器工具 | ||||
|     private static IndexWriterConfig config = null; | ||||
|     //索引库 | ||||
|     private Directory directory = null; | ||||
|     //分词器工具 | ||||
|     private IndexWriterConfig config = null; | ||||
|     //流 | ||||
|     private IndexWriter indexWriter= null; | ||||
|     //获得读取对象 | ||||
|     private IndexSearcher indexSearcher = null; | ||||
|  | ||||
|     public static final String default_primary_key = "id"; | ||||
|  | ||||
|     public static final Integer max_select_count = 1000; | ||||
|     public static final Integer max_select_count = 10000; | ||||
|  | ||||
|     static{ | ||||
|         //分词器 | ||||
|         analyzer = new IKAnalyzer(); | ||||
|         //使用智能分词 | ||||
|         analyzer.setUseSmart(true); | ||||
|         //工具装配分词器 | ||||
|         config = new IndexWriterConfig(Version.LUCENE_44, analyzer); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public static LuceneIndexUtils getLuceneIndexUtils(String indexPath){ | ||||
|         LuceneIndexUtils luceneIndexUtils = new LuceneIndexUtils(); | ||||
|         try { | ||||
|             //分词器 | ||||
|             luceneIndexUtils.analyzer = new IKAnalyzer(); | ||||
|             //使用智能分词 | ||||
|             luceneIndexUtils.analyzer.setUseSmart(true); | ||||
|             //索引库 | ||||
|             luceneIndexUtils.directory = FSDirectory.open(new File(indexPath)); | ||||
|             //工具装配分词器 | ||||
|             luceneIndexUtils.config = new IndexWriterConfig(Version.LUCENE_44, luceneIndexUtils.analyzer); | ||||
|             //读索引库流 | ||||
|             IndexReader reader = DirectoryReader.open(luceneIndexUtils.directory); | ||||
|             //获得读取对象 | ||||
|             luceneIndexUtils.indexSearcher = new IndexSearcher(reader); | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
| @@ -220,34 +230,21 @@ public class LuceneIndexUtils { | ||||
|  | ||||
|     /** | ||||
|      * 查询索引库的数据(根据输入关键字) | ||||
|      * @param keyword | ||||
|      * @return | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public Map<String, Object> queryByKeyword(String keyword, String[] columns, int pageNum, int pageSize) throws Exception { | ||||
|     public Map<String, Object> queryByKeyword(BooleanQuery query, Boolean boostSort, int pageNum, int pageSize) throws Exception { | ||||
|         Map<String, Object> result = new HashMap<>(); | ||||
|  | ||||
|         //指定读取索引库的列数据 | ||||
|         //String[] columns = {"articleId","articleName","articleImage","articleContent"}; | ||||
|  | ||||
|         //装配 | ||||
|         MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44, columns, analyzer); | ||||
|  | ||||
|         //解析输入关键字 | ||||
|         Query query = StringUtils.isEmpty(keyword) ? new WildcardQuery(new Term(default_primary_key, "*")) : queryParser.parse(keyword); | ||||
|  | ||||
|         //读索引库流 | ||||
|         IndexReader reader = DirectoryReader.open(directory); | ||||
|  | ||||
|         //获得读取对象 | ||||
|         IndexSearcher indexSearcher = new IndexSearcher(reader); | ||||
|  | ||||
|         //排序 | ||||
|         SortField sortField = new SortField(default_primary_key, SortField.Type.INT, true); | ||||
|         Sort sort = new Sort(sortField); | ||||
|         Sort sort = null; | ||||
|         if(boostSort == null || !boostSort){ | ||||
|             SortField sortField = new SortField(default_primary_key, SortField.Type.INT, true); | ||||
|             sort = new Sort(sortField); | ||||
|         } | ||||
|  | ||||
|         //装配解析结果 指定读取量级 | ||||
|         TopDocs search = indexSearcher.search(query, max_select_count, sort); | ||||
|         TopDocs search = sort == null ? indexSearcher.search(query, max_select_count) : indexSearcher.search(query, max_select_count, sort); | ||||
|  | ||||
|         //获得数据地址数组 | ||||
|         ScoreDoc[] scoreDocs = search.scoreDocs; | ||||
| @@ -265,6 +262,7 @@ public class LuceneIndexUtils { | ||||
|                 end = scoreDocs.length; | ||||
|             } | ||||
|             for (int i = start; i < end; i++) { | ||||
|                 //System.out.println(scoreDocs[i].score); | ||||
|                 //进集合 装配 | ||||
|                 list.add(indexSearcher.doc(scoreDocs[i].doc)); | ||||
|             } | ||||
|   | ||||
| @@ -24,7 +24,9 @@ | ||||
|         <result property="title"    column="title"    /> | ||||
|         <result property="content"    column="content"    /> | ||||
|         <result property="key"    column="key"    /> | ||||
|         <result property="titleContentIndex"    column="title_content_index"    /> | ||||
|         <result property="key"    column="key"    /> | ||||
|         <result property="showFlag"    column="show_flag"    /> | ||||
|         <result property="createTime"    column="create_time"    /> | ||||
|     </resultMap> | ||||
|  | ||||
|     <sql id="selectSysNutritionQuestionVo"> | ||||
| @@ -98,7 +100,7 @@ | ||||
|  | ||||
|     <!-- 根据关键词查询对应知识问题 --> | ||||
|     <select id="getNutritionQuestionListByKey" parameterType="SysNutritionQuestion" resultMap="SysNutritionQuestionResultExtended"> | ||||
|         select id, title, `key`, content where del_flag = 0 | ||||
|         select id, title, `key`, content, show_flag, create_time where del_flag = 0 | ||||
|         <if test="showFlag != null "> and show_flag = #{showFlag}</if> | ||||
|         <if test="key != null and key != ''"> | ||||
|           and (title like CONCAT('%',#{key},'%') or key like CONCAT('%',#{key},'%') or titleContentIndex like CONCAT('%',#{key},'%')) | ||||
| @@ -117,4 +119,14 @@ | ||||
|       select * from sys_nutrition_question where del_flag = 0 order by id asc limit #{start},#{pageSize} | ||||
|     </select> | ||||
|  | ||||
|  | ||||
|     <update id="updateWxShowByIds"> | ||||
|         update sys_nutrition_question set show_flag = #{wxShow} where id in | ||||
|         <foreach item="id" collection="array" open="(" separator="," close=")"> | ||||
|             #{id} | ||||
|         </foreach> | ||||
|     </update> | ||||
|  | ||||
|  | ||||
|  | ||||
| </mapper> | ||||
		Reference in New Issue
	
	Block a user