Browse Source

建立定时任务更新模具询价单虚拟列的索引

sunyj 9 years ago
parent
commit
e7c8a4f46c

+ 50 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/controller/ScheduleController.java

@@ -0,0 +1,50 @@
+package com.uas.search.console.b2b.controller;
+
+import java.util.Date;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import com.uas.search.console.b2b.schedule.DailyTaskManager;
+import com.uas.search.console.b2b.schedule.model.DailyTaskInformation;
+import com.uas.search.console.b2b.schedule.model.DailyTaskLog;
+import com.uas.search.console.b2b.service.PurchaseInquiryMouldSimpleInfoService;
+
+@Controller
+@RequestMapping("/schedule")
+public class ScheduleController {
+	@Autowired
+	private PurchaseInquiryMouldSimpleInfoService purchaseInquiryMouldService;
+
+	private DailyTaskManager dailyTaskManager = new DailyTaskManager();
+
+	private Logger logger = LoggerFactory.getLogger(ScheduleController.class);
+
+	@RequestMapping("/updateOverdue")
+	@ResponseBody
+	public String updateOverdue(@RequestParam(required = true) String title,
+			@RequestParam(required = true) Integer hour, @RequestParam(required = true) Integer minute,
+			@RequestParam(required = true) Integer second) {
+		final DailyTaskInformation dailyTaskInformation = new DailyTaskInformation();
+		dailyTaskInformation.setTitle(title);
+		dailyTaskInformation.setHour(hour);
+		dailyTaskInformation.setMinute(minute);
+		dailyTaskInformation.setSecond(second);
+		dailyTaskInformation.setCommand(new Runnable() {
+			@Override
+			public void run() {
+				logger.info("Daily Task run...");
+				purchaseInquiryMouldService.updateOverdue();
+				DailyTaskLog dailyTaskLog = new DailyTaskLog(dailyTaskInformation, new Date(), "success");
+				dailyTaskManager.saveLog(dailyTaskLog);
+			}
+		});
+		dailyTaskManager.newDailyTask(dailyTaskInformation);
+		return "ok";
+	}
+}

+ 10 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/controller/SearchController.java

@@ -87,4 +87,14 @@ public class SearchController {
 		return innerSearchService.getObjectById(id, ClassAndTableNameUtils.toClass(tbName));
 		return innerSearchService.getObjectById(id, ClassAndTableNameUtils.toClass(tbName));
 	}
 	}
 
 
+	@RequestMapping("/objects")
+	@ResponseBody
+	public Object getObjects(String tableName, Integer page, Integer size) {
+		Table_name tbName = null;
+		if (!StringUtils.isEmpty(tableName)) {
+			tbName = Table_name.valueOf(tableName.toUpperCase());
+		}
+		return innerSearchService.getAllObjects(ClassAndTableNameUtils.toClass(tbName), page == null ? 0 : page,
+				size == null ? 0 : size);
+	}
 }
 }

+ 133 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/schedule/DailyTaskManager.java

@@ -0,0 +1,133 @@
+package com.uas.search.console.b2b.schedule;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+
+import com.alibaba.fastjson.JSONObject;
+import com.uas.search.console.b2b.core.util.PathUtils;
+import com.uas.search.console.b2b.schedule.model.DailyTaskInformation;
+import com.uas.search.console.b2b.schedule.model.DailyTaskLog;
+
+/**
+ * 管理定时任务
+ * 
+ * @author sunyj
+ * @since 2016年12月12日 上午11:40:00
+ */
+public class DailyTaskManager {
+
+	private List<DailyTaskInformation> dailyTaskInformations = new ArrayList<>();
+
+	private ScheduledExecutorService scheduledExecutorService;
+
+	private Logger logger = LoggerFactory.getLogger(DailyTaskManager.class);
+
+	/**
+	 * 一天的毫秒数
+	 */
+	private static final long MILLISECONDS_OF_ONE_DAY = 24 * 60 * 60 * 1000;
+
+	/**
+	 * 建立每天定时任务
+	 * 
+	 * @param dailyTaskInformation
+	 *            每天定时任务的信息
+	 */
+	public void newDailyTask(DailyTaskInformation dailyTaskInformation) {
+		if (dailyTaskInformation == null || StringUtils.isEmpty(dailyTaskInformation.getTitle())
+				|| dailyTaskInformation.getCommand() == null) {
+			throw new NullPointerException();
+		}
+		dailyTaskInformations.add(dailyTaskInformation);
+		recreateTasks();
+	}
+
+	/**
+	 * 重新创建所有任务
+	 */
+	private void recreateTasks() {
+		if (scheduledExecutorService != null) {
+			logger.info("Remove old daily tasks...");
+			scheduledExecutorService.shutdownNow();
+		}
+		if (!CollectionUtils.isEmpty(dailyTaskInformations)) {
+			// 线程数与任务数保持一致,这样保证任务间不会互相影响
+			scheduledExecutorService = Executors.newScheduledThreadPool(dailyTaskInformations.size());
+			for (DailyTaskInformation dailyTaskInformation : dailyTaskInformations) {
+				logger.info("New daily task: " + dailyTaskInformation);
+				newDailyTask(dailyTaskInformation.getCommand(), dailyTaskInformation.getHour(),
+						dailyTaskInformation.getMinute(), dailyTaskInformation.getSecond());
+			}
+		}
+	}
+
+	/**
+	 * 建立每天定时任务
+	 * 
+	 * @param command
+	 *            所执行的任务
+	 * @param hour
+	 *            执行时间中的小时
+	 * @param minute
+	 *            执行时间中的分钟
+	 * @param second
+	 *            执行时间中的秒
+	 */
+	private void newDailyTask(Runnable command, int hour, int minute, int second) {
+		Calendar now = Calendar.getInstance();
+		now.setTime(new Date());
+		Calendar initialDelayCalendar = (Calendar) now.clone();
+		initialDelayCalendar.set(Calendar.HOUR_OF_DAY, hour);
+		initialDelayCalendar.set(Calendar.MINUTE, minute);
+		initialDelayCalendar.set(Calendar.SECOND, second);
+		if (initialDelayCalendar.before(now)) {
+			initialDelayCalendar.add(Calendar.DAY_OF_MONTH, 1);
+		}
+		scheduledExecutorService.scheduleAtFixedRate(command,
+				initialDelayCalendar.getTimeInMillis() - now.getTimeInMillis(), MILLISECONDS_OF_ONE_DAY,
+				TimeUnit.MILLISECONDS);
+
+	}
+
+	/**
+	 * 保存日志到本地文件
+	 * 
+	 * @param log
+	 */
+	public void saveLog(DailyTaskLog log) {
+		if (log == null) {
+			throw new NullPointerException();
+		}
+		FileWriter fileWriter = null;
+		try {
+			fileWriter = new FileWriter(PathUtils.getAppPath() + "daily-task-log.log", true);
+			fileWriter.write(JSONObject.toJSONString(log) + "\n");
+			fileWriter.flush();
+			logger.info("Saved task log:" + JSONObject.toJSONString(log) + "\n");
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			if (fileWriter != null) {
+				try {
+					fileWriter.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+	}
+
+}

+ 95 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/schedule/model/DailyTaskInformation.java

@@ -0,0 +1,95 @@
+package com.uas.search.console.b2b.schedule.model;
+
+/**
+ * 每天定时任务
+ * 
+ * @author sunyj
+ * @since 2016年12月12日 下午3:18:42
+ */
+public class DailyTaskInformation {
+
+	/**
+	 * 任务标题
+	 */
+	private String title;
+
+	/**
+	 * 所执行的任务
+	 */
+	private Runnable command;
+
+	/**
+	 * 执行时间中的小时
+	 */
+	private int hour;
+
+	/**
+	 * 执行时间中的小时
+	 */
+	private int minute;
+
+	/**
+	 * 执行时间中的秒
+	 */
+	private int second;
+
+	public DailyTaskInformation() {
+		super();
+	}
+
+	public DailyTaskInformation(String title, Runnable command, int hour, int minute, int second) {
+		super();
+		this.title = title;
+		this.command = command;
+		this.hour = hour;
+		this.minute = minute;
+		this.second = second;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+
+	public Runnable getCommand() {
+		return command;
+	}
+
+	public void setCommand(Runnable command) {
+		this.command = command;
+	}
+
+	public int getHour() {
+		return hour;
+	}
+
+	public void setHour(int hour) {
+		this.hour = hour;
+	}
+
+	public int getMinute() {
+		return minute;
+	}
+
+	public void setMinute(int minute) {
+		this.minute = minute;
+	}
+
+	public int getSecond() {
+		return second;
+	}
+
+	public void setSecond(int second) {
+		this.second = second;
+	}
+
+	@Override
+	public String toString() {
+		return "DailyTaskInformation [title=" + title + ", command=" + command + ", hour=" + hour + ", minute=" + minute
+				+ ", second=" + second + "]";
+	}
+
+}

+ 69 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/schedule/model/DailyTaskLog.java

@@ -0,0 +1,69 @@
+package com.uas.search.console.b2b.schedule.model;
+
+import java.util.Date;
+
+/**
+ * 每天定时任务的执行日志
+ * 
+ * @author sunyj
+ * @since 2016年12月12日 下午3:22:30
+ */
+public class DailyTaskLog {
+
+	/**
+	 * 每天定时任务的信息
+	 */
+	private DailyTaskInformation dailyTaskInformation;
+
+	/**
+	 * 任务执行时间
+	 */
+	private Date executeTime;
+
+	/**
+	 * 任务执行日志
+	 */
+	private String log;
+
+	public DailyTaskLog() {
+		super();
+	}
+
+	public DailyTaskLog(DailyTaskInformation dailyTaskInformation, Date executeTime, String log) {
+		super();
+		this.dailyTaskInformation = dailyTaskInformation;
+		this.executeTime = executeTime;
+		this.log = log;
+	}
+
+	public DailyTaskInformation getDailyTaskInformation() {
+		return dailyTaskInformation;
+	}
+
+	public void setDailyTaskInformation(DailyTaskInformation dailyTaskInformation) {
+		this.dailyTaskInformation = dailyTaskInformation;
+	}
+
+	public Date getExecuteTime() {
+		return executeTime;
+	}
+
+	public void setExecuteTime(Date executeTime) {
+		this.executeTime = executeTime;
+	}
+
+	public String getLog() {
+		return log;
+	}
+
+	public void setLog(String log) {
+		this.log = log;
+	}
+
+	@Override
+	public String toString() {
+		return "DailyTaskLog [dailyTaskInformation=" + dailyTaskInformation + ", executeTime=" + executeTime + ", log="
+				+ log + "]";
+	}
+
+}

+ 15 - 3
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/InnerSearchService.java

@@ -1,6 +1,6 @@
 package com.uas.search.console.b2b.service;
 package com.uas.search.console.b2b.service;
 
 
-import com.uas.search.b2b.exception.SearchException;
+import com.uas.search.b2b.model.SPage;
 
 
 /**
 /**
  * search-console-b2b下自用搜索接口
  * search-console-b2b下自用搜索接口
@@ -11,11 +11,23 @@ import com.uas.search.b2b.exception.SearchException;
 public interface InnerSearchService {
 public interface InnerSearchService {
 
 
 	/**
 	/**
-	 * 根据id获取本地索引中的数据
+	 * 根据id获取本地指定实体类的索引中的数据
 	 * 
 	 * 
 	 * @param id
 	 * @param id
 	 * @param clazz
 	 * @param clazz
+	 *            指定的实体类
 	 * @return
 	 * @return
 	 */
 	 */
-	public <T> T getObjectById(Long id, Class<T> clazz) throws SearchException;
+	public <T> T getObjectById(Long id, Class<T> clazz);
+
+	/**
+	 * 分页获取本地指定实体类的索引中的所有数据
+	 * 
+	 * @param clazz
+	 *            指定的实体类
+	 * @param page
+	 * @param size
+	 * @return
+	 */
+	public <T> SPage<T> getAllObjects(Class<T> clazz, int page, int size);
 }
 }

+ 13 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/PurchaseInquiryMouldSimpleInfoService.java

@@ -0,0 +1,13 @@
+package com.uas.search.console.b2b.service;
+
+/**
+ * @author sunyj
+ * @since 2016年12月12日 下午4:25:54
+ */
+public interface PurchaseInquiryMouldSimpleInfoService {
+
+	/**
+	 * 更新虚拟列的索引
+	 */
+	public void updateOverdue();
+}

+ 4 - 15
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/IndexServiceImpl.java

@@ -200,7 +200,7 @@ public class IndexServiceImpl implements IndexService {
 		Long count = 0L;
 		Long count = 0L;
 		try {
 		try {
 			// 从本地路径读取器件数据
 			// 从本地路径读取器件数据
-			File file = new File(getDataPath(tableName));
+			File file = new File(SearchUtils.getDataPath(tableName));
 			if (!file.exists() || ArrayUtils.isEmpty(file.listFiles())) {
 			if (!file.exists() || ArrayUtils.isEmpty(file.listFiles())) {
 				logger.error("创建索引失败,原因:" + tableName.value() + "数据文件不存在!\n");
 				logger.error("创建索引失败,原因:" + tableName.value() + "数据文件不存在!\n");
 				return 0L;
 				return 0L;
@@ -301,11 +301,11 @@ public class IndexServiceImpl implements IndexService {
 		PrintWriter printWriter = null;
 		PrintWriter printWriter = null;
 		int count = 0;
 		int count = 0;
 		try {
 		try {
-			File file = new File(getDataPath(tableName));
+			File file = new File(SearchUtils.getDataPath(tableName));
 			if (!file.exists()) {
 			if (!file.exists()) {
 				file.mkdirs();
 				file.mkdirs();
 			}
 			}
-			printWriter = new PrintWriter(getDataPath(tableName) + "/" + fileIndex + ".txt");
+			printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
 			while (totalElements > size) {
 			while (totalElements > size) {
 				// 一个文件存放100000条数据,一旦超过,写入新的文件
 				// 一个文件存放100000条数据,一旦超过,写入新的文件
 				if (count > SINGLE_FILE_MAX_SIZE) {
 				if (count > SINGLE_FILE_MAX_SIZE) {
@@ -313,7 +313,7 @@ public class IndexServiceImpl implements IndexService {
 					printWriter.flush();
 					printWriter.flush();
 					printWriter.close();
 					printWriter.close();
 					fileIndex++;
 					fileIndex++;
-					printWriter = new PrintWriter(getDataPath(tableName) + "/" + fileIndex + ".txt");
+					printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
 				}
 				}
 				List<T> content = pageResult.getContent();
 				List<T> content = pageResult.getContent();
 				for (T element : content) {
 				for (T element : content) {
@@ -339,17 +339,6 @@ public class IndexServiceImpl implements IndexService {
 		return totalElements;
 		return totalElements;
 	}
 	}
 
 
-	/**
-	 * 获取指定单据类型在本地的数据存储路径
-	 * 
-	 * @param tableName
-	 *            单据类型
-	 * @return 数据存储路径
-	 */
-	private String getDataPath(Table_name tableName) {
-		return luceneProperties.getDataDir() + "/" + tableName.value();
-	}
-
 	@Override
 	@Override
 	public <T> T save(T obj) {
 	public <T> T save(T obj) {
 		if (obj != null) {
 		if (obj != null) {

+ 106 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/PurchaseInquiryMouldSimpleInfoServiceImpl.java

@@ -0,0 +1,106 @@
+package com.uas.search.console.b2b.service.impl;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.uas.search.b2b.model.SPage;
+import com.uas.search.b2b.service.SearchService.Table_name;
+import com.uas.search.console.b2b.core.util.ContextUtils;
+import com.uas.search.console.b2b.model.PurchaseInquiryMouldSimpleInfo;
+import com.uas.search.console.b2b.service.IndexService;
+import com.uas.search.console.b2b.service.InnerSearchService;
+import com.uas.search.console.b2b.service.PurchaseInquiryMouldSimpleInfoService;
+import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
+import com.uas.search.console.b2b.util.SearchUtils;
+
+/**
+ * @author sunyj
+ * @since 2016年12月12日 下午5:40:42
+ */
+@Service
+public class PurchaseInquiryMouldSimpleInfoServiceImpl implements PurchaseInquiryMouldSimpleInfoService {
+
+	/**
+	 * 默认的页码
+	 */
+	private static final int PAGE_INDEX = 1;
+
+	/**
+	 * 默认每页的大小
+	 */
+	private static final int PAGE_SIZE = 1000;
+
+	@Autowired
+	private IndexService indexService;
+
+	private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
+			InnerSearchService.class);
+
+	private Logger logger = LoggerFactory.getLogger(PurchaseInquiryMouldSimpleInfoServiceImpl.class);
+
+	@Override
+	public void updateOverdue() {
+		// 不能边更新索引边分页获取索引中的数据,因为索引更新后,分页顺序可能也会变化,索引要先把数据保存到本地,等待全部获取之后重建索引
+		Long startTime = new Date().getTime();
+		Table_name tableName = ClassAndTableNameUtils.toTableName(PurchaseInquiryMouldSimpleInfo.class);
+
+		int page = PAGE_INDEX;
+		SPage<PurchaseInquiryMouldSimpleInfo> sPage = innerSearchService
+				.getAllObjects(PurchaseInquiryMouldSimpleInfo.class, page, PAGE_SIZE);
+
+		// 数据库中数据的总数目
+		long totalElements = sPage.getTotalElement();
+		logger.info("发现数据:" + totalElements + "条");
+		int fileIndex = 1;
+		PrintWriter printWriter = null;
+		int count = 0;
+		try {
+			File file = new File(SearchUtils.getDataPath(tableName));
+			if (!file.exists()) {
+				file.mkdirs();
+			}
+			printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
+			while (true) {
+				// 一个文件存放100000条数据,一旦超过,写入新的文件
+				if (count > IndexServiceImpl.SINGLE_FILE_MAX_SIZE) {
+					count = 1;
+					printWriter.flush();
+					printWriter.close();
+					fileIndex++;
+					printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
+				}
+				List<PurchaseInquiryMouldSimpleInfo> content = sPage.getContent();
+				for (PurchaseInquiryMouldSimpleInfo purchaseInquiryMould : content) {
+					purchaseInquiryMould.setOverdue(purchaseInquiryMould.getOverdue());
+					printWriter.println(JSONObject.toJSONString(purchaseInquiryMould));
+					count++;
+				}
+				logger.info(String.format(tableName.value() + "...................%.2f%%",
+						(page - 1) * PAGE_SIZE * 100.0 / totalElements));
+
+				if (++page > sPage.getTotalPage()) {
+					break;
+				}
+				sPage = innerSearchService.getAllObjects(PurchaseInquiryMouldSimpleInfo.class, page, PAGE_SIZE);
+			}
+			printWriter.flush();
+			printWriter.close();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		}
+		Long endTime = new Date().getTime();
+		logger.info(String.format("修改数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0));
+		indexService.createIndexs(Arrays.asList(tableName), true);
+	}
+
+}

+ 117 - 50
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/SearchServiceImpl.java

@@ -2,6 +2,7 @@ package com.uas.search.console.b2b.service.impl;
 
 
 import java.io.IOException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Date;
 import java.util.List;
 import java.util.List;
@@ -56,14 +57,68 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
 
 
 	private Logger logger = LoggerFactory.getLogger(getClass());
 	private Logger logger = LoggerFactory.getLogger(getClass());
 
 
-	@SuppressWarnings("unchecked")
 	@Override
 	@Override
 	public SPage<Long> searchIds(String keyword, Table_name tableName, PageParams pageParams) throws SearchException {
 	public SPage<Long> searchIds(String keyword, Table_name tableName, PageParams pageParams) throws SearchException {
 		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(tableName);
 		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(tableName);
+		// 获取单据的id
+		List<Long> content = new ArrayList<>();
+		try {
+			SPage<ScoreDoc> scoreDocPage = search(indexSearcher, keyword, tableName, pageParams);
+			SPage<Long> sPage = convertSPage(scoreDocPage, Long.class);
+			for (ScoreDoc scoreDoc : scoreDocPage.getContent()) {
+				Document document = indexSearcher.doc(scoreDoc.doc);
+				content.add(Long.valueOf(document.get(ClassAndTableNameUtils.getIdField(tableName))));
+			}
+			sPage.setContent(content);
+			return sPage;
+		} catch (NumberFormatException | IOException e) {
+			throw new SearchException(e).setDetailedMessage(e);
+		} finally {
+			SearchUtils.releaseIndexSearcher(indexSearcher);
+		}
+	}
+
+	/**
+	 * 转换SPage
+	 * 
+	 * @param scoreDocPage
+	 * @param clazz
+	 * @return
+	 */
+	private <T> SPage<T> convertSPage(SPage<ScoreDoc> scoreDocPage, Class<T> clazz) {
+		return new SPage<>(scoreDocPage.getTotalPage(), scoreDocPage.getTotalElement(), scoreDocPage.getPage(),
+				scoreDocPage.getSize(), scoreDocPage.isFirst(), scoreDocPage.isLast());
+	}
+
+	/**
+	 * 根据关键词、单据类型、状态码搜索单据
+	 * 
+	 * @param indexSearcher
+	 * 
+	 * @param keyword
+	 *            不为空,模糊搜索关键词,可以是:单据编号、供应商uu(采购)、供应商名称(采购)、客户uu(销售)、客户名称(销售)、
+	 *            物料编号、 物料名称、物料规格
+	 * @param tableName
+	 *            不为空,单据类型
+	 * @param pageParams
+	 *            可为空,可能含有翻页信息,filters中可能有过滤信息,包括:1.状态、所属企业uu、其他状态(如已采纳、未采纳等),
+	 *            该部分参数的键为数据库表中相应的字段名称,值为字段对应的值,若值有多个,则使用com.uas.search.b2b.
+	 *            model.MultiValue,则;2.开始时间(Long)、截止时间(Long),
+	 *            这两个参数用于对时间范围进行筛选(包含开始和截止时间),键为com.uas.search.b2b.util.
+	 *            SearchConstants中的常量;3.排序方式(ArrayList(Sort)),键为com.uas.search.
+	 *            b2b.util.SearchConstants中的常量,值为List(com.uas.search.b2b.model.
+	 *            Sort)
+	 * @return 单据数据
+	 * @throws IOException
+	 */
+	@SuppressWarnings("unchecked")
+	private SPage<ScoreDoc> search(IndexSearcher indexSearcher, String keyword, Table_name tableName,
+			PageParams pageParams) throws IOException {
+
 		// 获取该表keyword可以搜索的域
 		// 获取该表keyword可以搜索的域
 		List<String> keywordFields = ClassAndTableNameUtils.getKeywordFields(tableName);
 		List<String> keywordFields = ClassAndTableNameUtils.getKeywordFields(tableName);
 
 
-		SPage<Long> sPage = new SPage<>();
+		SPage<ScoreDoc> sPage = new SPage<>();
 		BooleanQuery booleanQuery = new BooleanQuery();
 		BooleanQuery booleanQuery = new BooleanQuery();
 		Sort sort = null;
 		Sort sort = null;
 
 
@@ -180,59 +235,46 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
 		}
 		}
 		logger.info(booleanQuery.toString());
 		logger.info(booleanQuery.toString());
 
 
-		try {
-			TopDocs topDocs;
-			// 如果页码不为1
-			if (sPage.getPage() > 1) {
-				TopDocs previousTopDocs = null;
-				if (sort != null) {
-					previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize(), sort);
-				} else {
-					previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize());
-				}
-				int totalHits = previousTopDocs.totalHits;
-				ScoreDoc[] previousScoreDocs = previousTopDocs.scoreDocs;
-				if ((sPage.getPage() - 1) * sPage.getSize() >= totalHits) {
-					throw new SearchException("页码过大:元素总数量为" + totalHits);
-				}
-				if (sort != null) {
-					topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], booleanQuery,
-							sPage.getSize(), sort);
-				} else {
-					topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], booleanQuery,
-							sPage.getSize());
-				}
+		TopDocs topDocs;
+		// 如果页码不为1
+		if (sPage.getPage() > 1) {
+			TopDocs previousTopDocs = null;
+			if (sort != null) {
+				previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize(), sort);
 			} else {
 			} else {
-				sPage.setFirst(true);
-				if (sort != null) {
-					topDocs = indexSearcher.search(booleanQuery, sPage.getSize(), sort);
-				} else {
-					topDocs = indexSearcher.search(booleanQuery, sPage.getSize());
-				}
+				previousTopDocs = indexSearcher.search(booleanQuery, (sPage.getPage() - 1) * sPage.getSize());
 			}
 			}
-
-			int totalHits = topDocs.totalHits;
-			// 设置总元素个数、页数等信息
-			sPage.setTotalElement(totalHits);
-			int totalPage = (int) Math.ceil(totalHits / (1.0 * sPage.getSize()));
-			sPage.setTotalPage(totalPage);
-			if (totalPage == sPage.getPage()) {
-				sPage.setLast(true);
+			int totalHits = previousTopDocs.totalHits;
+			ScoreDoc[] previousScoreDocs = previousTopDocs.scoreDocs;
+			if ((sPage.getPage() - 1) * sPage.getSize() >= totalHits) {
+				throw new SearchException("页码过大:元素总数量为" + totalHits);
 			}
 			}
-
-			// 获取单据的id
-			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
-			List<Long> content = new ArrayList<>();
-			for (ScoreDoc scoreDoc : scoreDocs) {
-				Document document = indexSearcher.doc(scoreDoc.doc);
-				content.add(Long.valueOf(document.get(ClassAndTableNameUtils.getIdField(tableName))));
+			if (sort != null) {
+				topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], booleanQuery,
+						sPage.getSize(), sort);
+			} else {
+				topDocs = indexSearcher.searchAfter(previousScoreDocs[previousScoreDocs.length - 1], booleanQuery,
+						sPage.getSize());
 			}
 			}
-			sPage.setContent(content);
-		} catch (NumberFormatException | IOException e) {
-			throw new SearchException(e).setDetailedMessage(e);
-		} finally {
-			SearchUtils.releaseIndexSearcher(indexSearcher);
+		} else {
+			sPage.setFirst(true);
+			if (sort != null) {
+				topDocs = indexSearcher.search(booleanQuery, sPage.getSize(), sort);
+			} else {
+				topDocs = indexSearcher.search(booleanQuery, sPage.getSize());
+			}
+		}
+
+		int totalHits = topDocs.totalHits;
+		// 设置总元素个数、页数等信息
+		sPage.setTotalElement(totalHits);
+		int totalPage = (int) Math.ceil(totalHits / (1.0 * sPage.getSize()));
+		sPage.setTotalPage(totalPage);
+		if (totalPage == sPage.getPage()) {
+			sPage.setLast(true);
 		}
 		}
+
+		sPage.setContent(Arrays.asList(topDocs.scoreDocs));
 		logger.info(sPage + "\n");
 		logger.info(sPage + "\n");
 		return sPage;
 		return sPage;
 	}
 	}
@@ -255,6 +297,31 @@ public class SearchServiceImpl implements SearchService, InnerSearchService {
 		}
 		}
 	}
 	}
 
 
+	@Override
+	public <T> SPage<T> getAllObjects(Class<T> clazz, int page, int size) {
+		Table_name tableName = ClassAndTableNameUtils.toTableName(clazz);
+		IndexSearcher indexSearcher = SearchUtils.getIndexSearcher(tableName);
+		// 获取单据的id
+		List<T> content = new ArrayList<>();
+		try {
+			PageParams pageParams = new PageParams();
+			pageParams.setPage(page);
+			pageParams.setSize(size);
+			SPage<ScoreDoc> scoreDocPage = search(indexSearcher, null, tableName, pageParams);
+			SPage<T> sPage = convertSPage(scoreDocPage, clazz);
+			for (ScoreDoc scoreDoc : scoreDocPage.getContent()) {
+				Document document = indexSearcher.doc(scoreDoc.doc);
+				content.add(DocumentToObjectUtils.toObject(document, clazz));
+			}
+			sPage.setContent(content);
+			return sPage;
+		} catch (NumberFormatException | IOException e) {
+			throw new SearchException(e).setDetailedMessage(e);
+		} finally {
+			SearchUtils.releaseIndexSearcher(indexSearcher);
+		}
+	}
+
 	/**
 	/**
 	 * 获取排序字段的类型
 	 * 获取排序字段的类型
 	 * 
 	 * 

+ 30 - 29
search-console-b2b/src/main/java/com/uas/search/console/b2b/util/DocumentToObjectUtils.java

@@ -373,7 +373,7 @@ public class DocumentToObjectUtils {
 								.parseObject(
 								.parseObject(
 										document.get(ClassAndTableNameUtils.combineField(tableName,
 										document.get(ClassAndTableNameUtils.combineField(tableName,
 												PurcProofingapprovalSimpleInfo.ENTERPRISE_FIELD)),
 												PurcProofingapprovalSimpleInfo.ENTERPRISE_FIELD)),
-						EnterpriseSimpleInfo.class));
+										EnterpriseSimpleInfo.class));
 		return purcProofingapproval;
 		return purcProofingapproval;
 	}
 	}
 
 
@@ -577,8 +577,6 @@ public class DocumentToObjectUtils {
 		return purcReturn;
 		return purcReturn;
 	}
 	}
 
 
-
-
 	/**
 	/**
 	 * 将Document转换成PurchaseChanges
 	 * 将Document转换成PurchaseChanges
 	 * 
 	 * 
@@ -652,7 +650,7 @@ public class DocumentToObjectUtils {
 	 * @param document
 	 * @param document
 	 * @return
 	 * @return
 	 */
 	 */
-	public static PurchaseOrderSimpleInfo toPurchaseOrder(Document document) {
+	private static PurchaseOrderSimpleInfo toPurchaseOrder(Document document) {
 		if (document == null) {
 		if (document == null) {
 			return null;
 			return null;
 		}
 		}
@@ -694,7 +692,7 @@ public class DocumentToObjectUtils {
 	 * @param document
 	 * @param document
 	 * @return
 	 * @return
 	 */
 	 */
-	public static MakeOrderSimpleInfo toMakeOrder(Document document) {
+	private static MakeOrderSimpleInfo toMakeOrder(Document document) {
 		if (document == null) {
 		if (document == null) {
 			return null;
 			return null;
 		}
 		}
@@ -727,7 +725,7 @@ public class DocumentToObjectUtils {
 	 * @param document
 	 * @param document
 	 * @return
 	 * @return
 	 */
 	 */
-	public static PurchaseAcceptSimpleInfo toPurchaseAccept(Document document) {
+	private static PurchaseAcceptSimpleInfo toPurchaseAccept(Document document) {
 		if (document == null) {
 		if (document == null) {
 			return null;
 			return null;
 		}
 		}
@@ -765,7 +763,7 @@ public class DocumentToObjectUtils {
 	 * @param document
 	 * @param document
 	 * @return
 	 * @return
 	 */
 	 */
-	public static PurchaseApbillSimpleInfo toPurchaseApbill(Document document) {
+	private static PurchaseApbillSimpleInfo toPurchaseApbill(Document document) {
 		if (document == null) {
 		if (document == null) {
 			return null;
 			return null;
 		}
 		}
@@ -892,8 +890,7 @@ public class DocumentToObjectUtils {
 				PurchaseForecastItemSimpleInfo.class));
 				PurchaseForecastItemSimpleInfo.class));
 		return purchaseForecast;
 		return purchaseForecast;
 	}
 	}
-	
-	
+
 	/**
 	/**
 	 * 将Document转换成PurchaseInquiryMouldSimpleInfo对象
 	 * 将Document转换成PurchaseInquiryMouldSimpleInfo对象
 	 * 
 	 * 
@@ -908,23 +905,23 @@ public class DocumentToObjectUtils {
 		PurchaseInquiryMouldSimpleInfo purchaseInquiryMould = new PurchaseInquiryMouldSimpleInfo();
 		PurchaseInquiryMouldSimpleInfo purchaseInquiryMould = new PurchaseInquiryMouldSimpleInfo();
 		purchaseInquiryMould.setId(Long.valueOf(
 		purchaseInquiryMould.setId(Long.valueOf(
 				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ID_FIELD))));
 				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ID_FIELD))));
-		purchaseInquiryMould.setCode(
-				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.CODE_FIELD)));
+		purchaseInquiryMould.setCode(document
+				.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.CODE_FIELD)));
 		if (document.get(PurchaseInquiryMouldSimpleInfo.STATUS_FIELD) != null) {
 		if (document.get(PurchaseInquiryMouldSimpleInfo.STATUS_FIELD) != null) {
-			purchaseInquiryMould.setStatus(Short.valueOf(
-					document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.STATUS_FIELD))));
+			purchaseInquiryMould.setStatus(Short.valueOf(document
+					.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.STATUS_FIELD))));
 		}
 		}
 		if (document.get(PurchaseInquiryMouldSimpleInfo.ADOPTSTATUS_FIELD) != null) {
 		if (document.get(PurchaseInquiryMouldSimpleInfo.ADOPTSTATUS_FIELD) != null) {
-			purchaseInquiryMould.setAdoptstatus(Short.valueOf(
-					document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ADOPTSTATUS_FIELD))));
+			purchaseInquiryMould.setAdoptstatus(Short.valueOf(document.get(
+					ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ADOPTSTATUS_FIELD))));
 		}
 		}
 		if (document.get(PurchaseInquiryMouldSimpleInfo.SENDSTATUS_FIELD) != null) {
 		if (document.get(PurchaseInquiryMouldSimpleInfo.SENDSTATUS_FIELD) != null) {
-			purchaseInquiryMould.setSendstatus(Short.valueOf(
-					document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.SENDSTATUS_FIELD))));
+			purchaseInquiryMould.setSendstatus(Short.valueOf(document.get(
+					ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.SENDSTATUS_FIELD))));
 		}
 		}
 		if (document.get(PurchaseInquiryMouldSimpleInfo.OVERDUE_FIELD) != null) {
 		if (document.get(PurchaseInquiryMouldSimpleInfo.OVERDUE_FIELD) != null) {
-			purchaseInquiryMould.setOverdue(Short.valueOf(
-					document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.OVERDUE_FIELD))));
+			purchaseInquiryMould.setOverdue(Short.valueOf(document.get(
+					ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.OVERDUE_FIELD))));
 		}
 		}
 		if (document.get(PurchaseInquiryMouldSimpleInfo.VALID_FIELD) != null) {
 		if (document.get(PurchaseInquiryMouldSimpleInfo.VALID_FIELD) != null) {
 			purchaseInquiryMould.setValid(Short.valueOf(document
 			purchaseInquiryMould.setValid(Short.valueOf(document
@@ -935,24 +932,28 @@ public class DocumentToObjectUtils {
 		if (!StringUtils.isEmpty(dateString)) {
 		if (!StringUtils.isEmpty(dateString)) {
 			purchaseInquiryMould.setDate(new Date(Long.valueOf(dateString)));
 			purchaseInquiryMould.setDate(new Date(Long.valueOf(dateString)));
 		}
 		}
-		purchaseInquiryMould.setEnterprise(JSONObject.parseObject(
-				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ENTERPRISE_FIELD)),
-				EnterpriseSimpleInfo.class));
+		purchaseInquiryMould
+				.setEnterprise(
+						JSONObject
+								.parseObject(
+										document.get(ClassAndTableNameUtils.combineField(tableName,
+												PurchaseInquiryMouldSimpleInfo.ENTERPRISE_FIELD)),
+										EnterpriseSimpleInfo.class));
 		purchaseInquiryMould.setVend(JSONObject.parseObject(
 		purchaseInquiryMould.setVend(JSONObject.parseObject(
 				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.VEND_FIELD)),
 				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.VEND_FIELD)),
 				EnterpriseSimpleInfo.class));
 				EnterpriseSimpleInfo.class));
 		purchaseInquiryMould.setMouldItems(toSet(
 		purchaseInquiryMould.setMouldItems(toSet(
-				document.get(ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ITEMS_FIELD)),
-				PurchaseInquiryMouldItemSimpleInfo.class));
-		purchaseInquiryMould.setMouldDetails(toSet(
 				document.get(
 				document.get(
-						ClassAndTableNameUtils.combineField(tableName,
+						ClassAndTableNameUtils.combineField(tableName, PurchaseInquiryMouldSimpleInfo.ITEMS_FIELD)),
+				PurchaseInquiryMouldItemSimpleInfo.class));
+		purchaseInquiryMould
+				.setMouldDetails(toSet(
+						document.get(ClassAndTableNameUtils.combineField(tableName,
 								PurchaseInquiryMouldSimpleInfo.PRODDETAIL_FIELD)),
 								PurchaseInquiryMouldSimpleInfo.PRODDETAIL_FIELD)),
-				PurchaseInquiryMouldDetailSimpleInfo.class));
+						PurchaseInquiryMouldDetailSimpleInfo.class));
 		return purchaseInquiryMould;
 		return purchaseInquiryMould;
 	}
 	}
 
 
-
 	/**
 	/**
 	 * 将json字符串转为Set<T>对象
 	 * 将json字符串转为Set<T>对象
 	 * 
 	 * 
@@ -962,7 +963,7 @@ public class DocumentToObjectUtils {
 	 *            索引中存储的json数据
 	 *            索引中存储的json数据
 	 * @return Set<T>对象
 	 * @return Set<T>对象
 	 */
 	 */
-	public static <T> Set<T> toSet(String jsonString, Class<T> clazz) {
+	private static <T> Set<T> toSet(String jsonString, Class<T> clazz) {
 		if (StringUtils.isEmpty(jsonString)) {
 		if (StringUtils.isEmpty(jsonString)) {
 			return null;
 			return null;
 		}
 		}

+ 11 - 0
search-console-b2b/src/main/java/com/uas/search/console/b2b/util/SearchUtils.java

@@ -280,4 +280,15 @@ public class SearchUtils {
 		file.delete();
 		file.delete();
 		logger.info("Deleted... " + file.getPath());
 		logger.info("Deleted... " + file.getPath());
 	}
 	}
+
+	/**
+	 * 获取指定单据类型在本地的数据存储路径
+	 * 
+	 * @param tableName
+	 *            单据类型
+	 * @return 数据存储路径
+	 */
+	public static String getDataPath(Table_name tableName) {
+		return luceneProperties.getDataDir() + "/" + tableName.value();
+	}
 }
 }

+ 6 - 0
search-console-b2b/src/main/webapp/WEB-INF/views/console.html

@@ -19,6 +19,7 @@
 			<ol>
 			<ol>
 				<li><a target="_blank">search/object?id=115940&tableName=PURC$ORDERS</a></li>
 				<li><a target="_blank">search/object?id=115940&tableName=PURC$ORDERS</a></li>
 				<li><a target="_blank">search/object?id=21&tableName=MAKE$ORDERS</a></li>
 				<li><a target="_blank">search/object?id=21&tableName=MAKE$ORDERS</a></li>
+				<li><a target="_blank">search/objects?tableName=MAKE$ORDERS&page=1&size=6</a></li>
 			</ol>
 			</ol>
 
 
 			<strong><li class="title">索引修改</li></strong>
 			<strong><li class="title">索引修改</li></strong>
@@ -43,6 +44,11 @@
 				<li><a target="_blank">fileUpload</a></li>
 				<li><a target="_blank">fileUpload</a></li>
 			</ol>
 			</ol>
 
 
+			<strong><li class="title">定时任务</li></strong>
+			<ol>
+				<li><a target="_blank">schedule/updateOverdue?title=更新虚拟列索引:PURC$INQUIRYMOULD.im_overdue&hour=20&minute=16&second=0</a></li>
+			</ol>
+
 		</ol>
 		</ol>
 	</div>
 	</div>
 </body>
 </body>