| @@ -11,7 +11,7 @@ public class NutritionQuestionResponse implements Serializable { | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** $column.columnComment */ | ||||
|     private Long id; | ||||
|     private String id; | ||||
|  | ||||
|     /** 标题 */ | ||||
|     @Excel(name = "标题") | ||||
|   | ||||
| @@ -2,6 +2,7 @@ package com.stdiet.custom.mapper; | ||||
|  | ||||
| import java.util.List; | ||||
| import com.stdiet.custom.domain.SysNutritionQuestion; | ||||
| import org.apache.ibatis.annotations.Param; | ||||
|  | ||||
| /** | ||||
|  * 营养知识小问答Mapper接口 | ||||
| @@ -64,4 +65,18 @@ public interface SysNutritionQuestionMapper | ||||
|      * @return | ||||
|      */ | ||||
|     public List<String> getNutritionQuestionListByKey(SysNutritionQuestion sysNutritionQuestion); | ||||
|  | ||||
|     /** | ||||
|      * 查询总数 | ||||
|      * @return | ||||
|      */ | ||||
|     public int getNutritionQuestionCount(); | ||||
|  | ||||
|     /** | ||||
|      * 分页查询 | ||||
|      * @param start | ||||
|      * @param pageSize | ||||
|      * @return | ||||
|      */ | ||||
|     public List<SysNutritionQuestion> getNutritionQuestionListByPage(@Param("start")int start, @Param("pageSize")int pageSize); | ||||
| } | ||||
| @@ -1,10 +1,13 @@ | ||||
| package com.stdiet.custom.mapper; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
| import com.stdiet.custom.domain.SysRecipesPlan; | ||||
| import com.stdiet.custom.domain.SysRecipesPlanListInfo; | ||||
| import org.apache.ibatis.annotations.Param; | ||||
|  | ||||
| import javax.xml.crypto.Data; | ||||
|  | ||||
| /** | ||||
|  * 食谱计划Mapper接口 | ||||
|  * | ||||
| @@ -87,7 +90,7 @@ public interface SysRecipesPlanMapper | ||||
|      * @param customerId | ||||
|      * @return | ||||
|      */ | ||||
|     SysRecipesPlan getLastDayRecipesPlan(@Param("customerId")Long customerId, @Param("orderId")Long orderId); | ||||
|     SysRecipesPlan getLastDayRecipesPlan(@Param("customerId")Long customerId, @Param("orderTime") Date orderTime); | ||||
|  | ||||
|     /** | ||||
|      * 根据订单ID查询食谱计划 | ||||
|   | ||||
| @@ -67,4 +67,10 @@ public interface ISysNutritionQuestionService | ||||
|      */ | ||||
|     public Map<String, Object> getNutritionQuestionListByKey(SysNutritionQuestion sysNutritionQuestion, int pageNum, int pageSize); | ||||
|  | ||||
|     /** | ||||
|      * 重新生成知识问答索引 | ||||
|      * @return | ||||
|      */ | ||||
|     public boolean regenerateNutritionQuestionIndex(); | ||||
|  | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| package com.stdiet.custom.service; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
|  | ||||
| import com.stdiet.custom.domain.SysRecipesPlan; | ||||
| @@ -102,7 +103,7 @@ public interface ISysRecipesPlanService | ||||
|      * @param customerId | ||||
|      * @return | ||||
|      */ | ||||
|     SysRecipesPlan getLastDayRecipesPlan(Long customerId, Long orderId); | ||||
|     SysRecipesPlan getLastDayRecipesPlan(Long customerId,  Date orderTime); | ||||
|  | ||||
|     /** | ||||
|      * 通过outId查询食谱计划简要 | ||||
|   | ||||
| @@ -7,11 +7,13 @@ import java.util.Map; | ||||
|  | ||||
| import com.stdiet.common.utils.DateUtils; | ||||
| 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.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Service; | ||||
| import com.stdiet.custom.mapper.SysNutritionQuestionMapper; | ||||
| import com.stdiet.custom.domain.SysNutritionQuestion; | ||||
| @@ -29,7 +31,8 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     @Autowired | ||||
|     private SysNutritionQuestionMapper sysNutritionQuestionMapper; | ||||
|  | ||||
|     public static final String index_path = "D:\\develop\\LuceneIndex\\nutritionQuestion"; | ||||
|     @Value("${lucene.index.nutritionQuestion}") | ||||
|     public String index_path; | ||||
|     //private static final String index_path = ""; | ||||
|  | ||||
|     //建立索引的字段名称 | ||||
| @@ -85,7 +88,10 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     public int updateSysNutritionQuestion(SysNutritionQuestion sysNutritionQuestion) | ||||
|     { | ||||
|         sysNutritionQuestion.setUpdateTime(DateUtils.getNowDate()); | ||||
|         return sysNutritionQuestionMapper.updateSysNutritionQuestion(sysNutritionQuestion); | ||||
|         if(sysNutritionQuestionMapper.updateSysNutritionQuestion(sysNutritionQuestion) > 0){ | ||||
|             return updateNutritionQuestionIndex(sysNutritionQuestion) ? 1 : 0; | ||||
|         } | ||||
|         return 0; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -97,7 +103,17 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     @Override | ||||
|     public int deleteSysNutritionQuestionByIds(Long[] ids) | ||||
|     { | ||||
|         return sysNutritionQuestionMapper.deleteSysNutritionQuestionByIds(ids); | ||||
|         if(sysNutritionQuestionMapper.deleteSysNutritionQuestionByIds(ids) > 0){ | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             try { | ||||
|                 return luceneIndexUtils.deleteOneByPrimaryIds(ids) ? 1 : 0; | ||||
|             }catch (Exception e){ | ||||
|                 e.printStackTrace(); | ||||
|                 return 0; | ||||
|             } | ||||
|         } | ||||
|         return 0; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -109,7 +125,17 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     @Override | ||||
|     public int deleteSysNutritionQuestionById(Long id) | ||||
|     { | ||||
|         return sysNutritionQuestionMapper.deleteSysNutritionQuestionById(id); | ||||
|         if(sysNutritionQuestionMapper.deleteSysNutritionQuestionById(id) > 0){ | ||||
|             try { | ||||
|                 //建立索引 | ||||
|                 LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|                 return luceneIndexUtils.deleteOneByPrimaryKey(id) ? 1 : 0; | ||||
|             }catch (Exception e){ | ||||
|                 e.printStackTrace(); | ||||
|                 return 0; | ||||
|             } | ||||
|         } | ||||
|         return 0; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -119,19 +145,59 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|     @Override | ||||
|     public Map<String, Object> getNutritionQuestionListByKey(SysNutritionQuestion sysNutritionQuestion, int pageNum, int pageSize){ | ||||
|         //return sysNutritionQuestionMapper.getNutritionQuestionListByKey(sysNutritionQuestion); | ||||
|         Map<String, Object> result = new HashMap<>(); | ||||
|         int total = 0; | ||||
|         List<NutritionQuestionResponse> nutritionQuestionResponsesList = new ArrayList<>(); | ||||
|         try{ | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             return luceneIndexUtils.queryByKeyword(sysNutritionQuestion.getKey(), index_field_array, pageNum, pageSize); | ||||
|             Map<String, Object> indexMap = luceneIndexUtils.queryByKeyword(sysNutritionQuestion.getKey(), index_field_array, pageNum, pageSize); | ||||
|             total = (int)indexMap.get("total"); | ||||
|             List<Document> documentList = (List<Document>)indexMap.get("data"); | ||||
|             if(documentList != null && documentList.size() > 0){ | ||||
|                 for (Document document : documentList) { | ||||
|                     nutritionQuestionResponsesList.add(documentToNutritionQuestion(document)); | ||||
|                 } | ||||
|             } | ||||
|         }catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         Map<String, Object> result = new HashMap<>(); | ||||
|         result.put("total", 0); | ||||
|         result.put("data", new ArrayList<>()); | ||||
|         result.put("total", total); | ||||
|         result.put("data", nutritionQuestionResponsesList); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 重新生成知识问答索引 | ||||
|      * @return | ||||
|      */ | ||||
|     public boolean regenerateNutritionQuestionIndex(){ | ||||
|         int pageSize = 1000; | ||||
|         int total = sysNutritionQuestionMapper.getNutritionQuestionCount(); | ||||
|         try { | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             if(luceneIndexUtils.deleteAllIndex()){ | ||||
|                 if(total > 0){ | ||||
|                     //总页数 | ||||
|                     int totalPage = total/pageSize + (total%pageSize > 0 ? 1 : 0); | ||||
|                     int start = 0; | ||||
|                     for(int i = 1; i <= totalPage; i++){ | ||||
|                         start = (i-1) * pageSize; | ||||
|                         List<SysNutritionQuestion> list = sysNutritionQuestionMapper.getNutritionQuestionListByPage(start,pageSize); | ||||
|                         luceneIndexUtils.addIndexs(nutritionQuestionToDocument(list)); | ||||
|                     } | ||||
|                 } | ||||
|                 return true; | ||||
|             } | ||||
|         }catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|             return false; | ||||
|         } | ||||
|         return false; | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 建立索引 | ||||
|      * @param sysNutritionQuestion | ||||
| @@ -141,14 +207,62 @@ public class SysNutritionQuestionServiceImpl implements ISysNutritionQuestionSer | ||||
|         try{ | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             Document document = new Document(); | ||||
|             for (String fieldName : index_field_array) { | ||||
|                 document.add(new TextField(fieldName, ReflectUtils.getFieldValue(sysNutritionQuestion, fieldName)+"", Field.Store.YES)); | ||||
|             } | ||||
|             Document document = nutritionQuestionToDocument(sysNutritionQuestion); | ||||
|             return luceneIndexUtils.addIndexOne(document); | ||||
|         }catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 更新索引 | ||||
|      * @param sysNutritionQuestion | ||||
|      * @return | ||||
|      */ | ||||
|     private boolean updateNutritionQuestionIndex(SysNutritionQuestion sysNutritionQuestion){ | ||||
|         try{ | ||||
|             //建立索引 | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(index_path); | ||||
|             Document document = nutritionQuestionToDocument(sysNutritionQuestion); | ||||
|             return luceneIndexUtils.updateByPrimaryId(sysNutritionQuestion.getId(), document); | ||||
|         }catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * document转为NutritionQuestionResponse | ||||
|      * @param document | ||||
|      * @return | ||||
|      */ | ||||
|     private NutritionQuestionResponse documentToNutritionQuestion(Document document){ | ||||
|         NutritionQuestionResponse nutritionQuestionResponse = new NutritionQuestionResponse(); | ||||
|         for (String fieldName : index_field_array) { | ||||
|             ReflectUtils.setFieldValue(nutritionQuestionResponse, fieldName, document.get(fieldName)); | ||||
|         } | ||||
|         return nutritionQuestionResponse; | ||||
|     } | ||||
|  | ||||
|     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)); | ||||
|         } | ||||
|         return document; | ||||
|     } | ||||
|  | ||||
|     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); | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
| } | ||||
| @@ -229,7 +229,7 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { | ||||
|      */ | ||||
|     private List<SysRecipesPlan> generatePlan(SysOrder sysOrder, LocalDate serverStartDate, LocalDate serverEndDate, List<SysOrderPause> pauseList) { | ||||
|         //查询在上一个订单最后一条食谱计划 | ||||
|         SysRecipesPlan beforeOrderLastPlan = getLastDayRecipesPlan(sysOrder.getCusId(), sysOrder.getOrderId()); | ||||
|         SysRecipesPlan beforeOrderLastPlan = getLastDayRecipesPlan(sysOrder.getCusId(), sysOrder.getOrderTime()); | ||||
|         int startNumDay = 0; | ||||
|         //之前是否存在食谱 | ||||
|         if(beforeOrderLastPlan != null){ | ||||
| @@ -409,11 +409,10 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { | ||||
|     /** | ||||
|      * 根据客户ID查询最后一天食谱计划 | ||||
|      * @param customerId 客户ID | ||||
|      * @param orderId 不属于该订单ID的食谱 | ||||
|      * @return | ||||
|      */ | ||||
|     public SysRecipesPlan getLastDayRecipesPlan(Long customerId, Long orderId){ | ||||
|         return sysRecipesPlanMapper.getLastDayRecipesPlan(customerId, orderId); | ||||
|     public SysRecipesPlan getLastDayRecipesPlan(Long customerId, Date orderTime){ | ||||
|         return sysRecipesPlanMapper.getLastDayRecipesPlan(customerId, orderTime); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| package com.stdiet.custom.utils; | ||||
|  | ||||
| import com.stdiet.common.utils.StringUtils; | ||||
| import com.stdiet.custom.service.impl.SysNutritionQuestionServiceImpl; | ||||
| import org.apache.lucene.document.Document; | ||||
| import org.apache.lucene.index.*; | ||||
| import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; | ||||
| @@ -61,7 +60,7 @@ public class LuceneIndexUtils { | ||||
|      * @return | ||||
|      * @throws IOException | ||||
|      */ | ||||
|     public int addIndexs(ArrayList<Document> documents) throws IOException { | ||||
|     public int addIndexs(List<Document> documents) throws IOException { | ||||
|  | ||||
|         //流 读取索引库 加装工具 | ||||
|         indexWriter = new IndexWriter(directory,config); | ||||
| @@ -143,12 +142,12 @@ public class LuceneIndexUtils { | ||||
|      * @return | ||||
|      * @throws IOException | ||||
|      */ | ||||
|     public boolean deleteOne(Term term) throws IOException { | ||||
|  | ||||
|     public boolean deleteOneByPrimaryKey(Long id) throws IOException { | ||||
|  | ||||
|         //流 读取索引库 加装工具 | ||||
|         indexWriter = new IndexWriter(directory,config); | ||||
|  | ||||
|         TermQuery term = new TermQuery(new Term(default_primary_key, id+"")); | ||||
|         try { | ||||
|             indexWriter.deleteDocuments(term); | ||||
|             indexWriter.commit(); | ||||
| @@ -163,6 +162,34 @@ public class LuceneIndexUtils { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 删除索引库中一条数据 | ||||
|      * @param term | ||||
|      * @return | ||||
|      * @throws IOException | ||||
|      */ | ||||
|     public boolean deleteOneByPrimaryIds(Long[] ids) throws IOException { | ||||
|  | ||||
|         //流 读取索引库 加装工具 | ||||
|         indexWriter = new IndexWriter(directory,config); | ||||
|  | ||||
|         try { | ||||
|             for (Long id : ids) { | ||||
|                 TermQuery term = new TermQuery(new Term(default_primary_key, id+"")); | ||||
|                 indexWriter.deleteDocuments(term); | ||||
|                 indexWriter.commit(); | ||||
|             } | ||||
|             return true; | ||||
|         }catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|             indexWriter.rollback(); | ||||
|             return false; | ||||
|         }finally { | ||||
|             indexWriter.close(); | ||||
|             indexWriter= null; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 修改索引库中一条数据 | ||||
|      * 注意:此处修改为  根据查找条件修改  如果有则修改 没有则新添 多条则修改一条(索引库的底层其实做法是 先删除,后修改- -) | ||||
| @@ -171,14 +198,14 @@ public class LuceneIndexUtils { | ||||
|      * @return | ||||
|      * @throws IOException | ||||
|      */ | ||||
|     public boolean updateOne(Term term, Document document) throws IOException { | ||||
|     public boolean updateByPrimaryId(Long id, Document document) throws IOException { | ||||
|  | ||||
|         try { | ||||
|  | ||||
|             //流 读取索引库 加装工具 | ||||
|             indexWriter = new IndexWriter(directory,config); | ||||
|  | ||||
|             indexWriter.updateDocument(term,document); | ||||
|             indexWriter.updateDocument(new Term(default_primary_key, id+""),document); | ||||
|             indexWriter.commit(); | ||||
|             return true; | ||||
|         }catch (Exception e){ | ||||
| @@ -281,39 +308,4 @@ public class LuceneIndexUtils { | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     public static void main(String[] args) throws IOException { | ||||
|         try{ | ||||
|             LuceneIndexUtils luceneIndexUtils = LuceneIndexUtils.getLuceneIndexUtils(SysNutritionQuestionServiceImpl.index_path); | ||||
|             /*for (int i = 1 ; i < 100; i++){ | ||||
|                 Document document = new Document(); | ||||
|                 document.add(new TextField("id",i+"", Field.Store.YES)); | ||||
|                 document.add(new TextField("title","什么食物是脂肪杀手?", Field.Store.YES)); | ||||
|                 document.add(new TextField("content","黄瓜", Field.Store.YES)); | ||||
|                 document.add(new TextField("key","脂肪|杀手|食物", Field.Store.YES)); | ||||
|                 luceneIndexUtils.addIndexOne(document); | ||||
|  | ||||
|             }*/ | ||||
|             /*Document document = new Document(); | ||||
|             document.add(new TextField("id","1", Field.Store.YES)); | ||||
|             document.add(new TextField("title","什么食物是脂肪杀手?", Field.Store.YES)); | ||||
|             document.add(new TextField("content","黄瓜", Field.Store.YES)); | ||||
|             document.add(new TextField("key","脂肪|杀手|食物", Field.Store.YES)); | ||||
|             luceneIndexUtils.addIndexOne(document);*/ | ||||
|  | ||||
|             String[] columns = {"key","content","title"}; | ||||
|             Map<String,Object> map = luceneIndexUtils.queryByKeyword("猝死", SysNutritionQuestionServiceImpl.index_field_array, 1,10); | ||||
|  | ||||
|             System.out.println(Long.parseLong(map.get("total").toString())); | ||||
|  | ||||
|             for(Document document : (List<Document>)map.get("data")){ | ||||
|                 System.out.println(document.get("id")+"-"+document.get("key")); | ||||
|             } | ||||
|  | ||||
|             //LuceneIndexUtils.printAnalysisResult("什么食物是脂肪杀手?"); | ||||
|         }catch (Exception e){ | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -111,4 +111,15 @@ | ||||
|         order by id desc | ||||
|     </select> | ||||
|  | ||||
|     <!-- 查询总数 --> | ||||
|     <select id="getNutritionQuestionCount" resultType="int"> | ||||
|       select count(id) from sys_nutrition_question where del_flag = 0 | ||||
|     </select> | ||||
|  | ||||
|  | ||||
|     <!-- 分页查询--> | ||||
|     <select id="getNutritionQuestionListByPage" resultMap="SysNutritionQuestionResult"> | ||||
|       select * from sys_nutrition_question where del_flag = 0 order by id asc limit #{start},#{pageSize} | ||||
|     </select> | ||||
|  | ||||
| </mapper> | ||||
| @@ -63,9 +63,11 @@ | ||||
|     </select> | ||||
|  | ||||
|     <!-- 根据用户ID查询是否存在该用户该订单之前的最后一条食谱 --> | ||||
|     <select id="getLastDayRecipesPlan"  resultMap="SysRecipesPlanResult" parameterType="Long"> | ||||
|         <include refid="selectSysRecipesPlanVo"/> | ||||
|         where cus_id = #{customerId} and del_flag = 0 and #{orderId} > order_id order by end_num_day DESC limit 1 | ||||
|     <select id="getLastDayRecipesPlan"  resultMap="SysRecipesPlanResult"> | ||||
|         select srp.* | ||||
|         from sys_recipes_plan srp | ||||
|         left join sys_order so on so.order_id = srp.order_id and so.del_flag = 0 | ||||
|         where srp.cus_id = #{customerId} and srp.del_flag = 0 and #{orderTime} > so.order_time order by srp.end_num_day DESC limit 1 | ||||
|     </select> | ||||
|  | ||||
|     <insert id="insertSysRecipesPlan" parameterType="SysRecipesPlan" useGeneratedKeys="true" keyProperty="id"> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user