|
|
@@ -0,0 +1,157 @@
|
|
|
+package com.uas.ps.product.search.service;
|
|
|
+
|
|
|
+import com.uas.ps.core.page.PageInfo;
|
|
|
+import com.uas.ps.entity.Product;
|
|
|
+import com.uas.ps.entity.ProductUsers;
|
|
|
+import com.uas.ps.product.repository.ProductDao;
|
|
|
+import com.uas.ps.product.repository.ProductUsersDao;
|
|
|
+import com.uas.ps.product.search.model.SPage;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 搜索接口
|
|
|
+ *
|
|
|
+ * @author hejq
|
|
|
+ * @date 2018-08-01 10:38
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SearchServiceImpl implements SearchService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTempSearchService searchService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductDao productDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductUsersDao productUsersDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 物料信息表
|
|
|
+ */
|
|
|
+ final String PRODUCT_TABLE_NAME = "products";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 个人物料信息表
|
|
|
+ */
|
|
|
+ final String PRODUCT_USER_TABLE_NAME = "product$users";
|
|
|
+
|
|
|
+ private ConcurrentHashMap<String, Field> sortFields = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将id的SPage信息、数据合并
|
|
|
+ *
|
|
|
+ * @param idsPage
|
|
|
+ * id的SPage信息
|
|
|
+ * @param content
|
|
|
+ * 数据
|
|
|
+ * @return 合并后的结果
|
|
|
+ */
|
|
|
+ private <T> SPage<T> toSPage(SPage<Long> idsPage, List<T> content) {
|
|
|
+ if (idsPage == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SPage<T> result = new SPage<>();
|
|
|
+ result.setContent(content);
|
|
|
+ result.setFirst(idsPage.isFirst());
|
|
|
+ result.setLast(idsPage.isLast());
|
|
|
+ result.setPage(idsPage.getPage());
|
|
|
+ result.setSize(idsPage.getSize());
|
|
|
+ result.setTotalElement(idsPage.getTotalElement());
|
|
|
+ result.setTotalPage(idsPage.getTotalPage());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义排序规则
|
|
|
+ *
|
|
|
+ * @param content 数据集合
|
|
|
+ * @param targetCls 目标类
|
|
|
+ * @param properyName 字段名
|
|
|
+ * @param propertyList 字段
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ private <T> void sortByProperty(List<T> content, Class<T> targetCls, String properyName,
|
|
|
+ final List<Long> propertyList) {
|
|
|
+ final Field field = getPropertyField(targetCls, properyName);
|
|
|
+ if (null != field) {
|
|
|
+ Collections.sort(content, new Comparator<T>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int compare(T param1, T param2) {
|
|
|
+ try {
|
|
|
+ return Integer.compare(propertyList.indexOf((long) field.get(param1)),
|
|
|
+ propertyList.indexOf((long) field.get(param2)));
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字段列名
|
|
|
+ *
|
|
|
+ * @param targetCls 目标类
|
|
|
+ * @param properyName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Field getPropertyField(Class<?> targetCls, String properyName) {
|
|
|
+ Field field = sortFields.get(targetCls.toString());
|
|
|
+ if (null == field) {
|
|
|
+ Field[] fields = targetCls.getDeclaredFields();
|
|
|
+ for (Field f : fields) {
|
|
|
+ if (f.getName().equals(properyName)) {
|
|
|
+ field = f;
|
|
|
+ field.setAccessible(true);
|
|
|
+ sortFields.put(targetCls.toString(), f);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return field;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过关键词和分页信息查询物料信息
|
|
|
+ *
|
|
|
+ * @param keyword 搜索关键词
|
|
|
+ * @param pageInfo 分页信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SPage<Product> searchProducts(String keyword, PageInfo pageInfo) {
|
|
|
+ SPage<Long> idsPage = searchService.searchIds(keyword, PRODUCT_TABLE_NAME, pageInfo);
|
|
|
+ List<Product> content = productDao.findAll(idsPage.getContent());
|
|
|
+ sortByProperty(content, Product.class, "id", idsPage.getContent());
|
|
|
+ return toSPage(idsPage, content);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过关键词和分页信息查询个人物料信息
|
|
|
+ *
|
|
|
+ * @param keyword 搜索关键词
|
|
|
+ * @param pageInfo 分页信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SPage<ProductUsers> searchProductUsers(String keyword, PageInfo pageInfo) {
|
|
|
+ SPage<Long> idsPage = searchService.searchIds(keyword, PRODUCT_USER_TABLE_NAME, pageInfo);
|
|
|
+ List<ProductUsers> content = productUsersDao.findAll(idsPage.getContent());
|
|
|
+ sortByProperty(content, ProductUsers.class, "id", idsPage.getContent());
|
|
|
+ return toSPage(idsPage, content);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|