JdbcServiceImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.uas.search.service.impl;
  2. import static com.uas.search.constant.SearchConstants.PCB_TABLE_NAME;
  3. import static com.uas.search.constant.SearchConstants.PRODUCTS_PRIVATE_TABLE_NAME;
  4. import static com.uas.search.constant.SearchConstants.PRODUCTS_USERS_TABLE_NAME;
  5. import static com.uas.search.constant.SearchConstants.TRADE_GOODS_TABLE_NAME;
  6. import com.uas.search.constant.SearchConstants;
  7. import com.uas.search.dao.GoodsDao;
  8. import com.uas.search.model.Brand;
  9. import com.uas.search.model.Component;
  10. import com.uas.search.model.Kind;
  11. import com.uas.search.model.PCB;
  12. import com.uas.search.model.ProductUsersSimpleInfo;
  13. import com.uas.search.model.Products;
  14. import com.uas.search.model.PropertyValue;
  15. import com.uas.search.model.TradeGoods;
  16. import com.uas.search.model.V_Products;
  17. import com.uas.search.service.JdbcService;
  18. import com.uas.search.util.CollectionUtils;
  19. import java.lang.annotation.Annotation;
  20. import java.lang.reflect.Field;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.Set;
  26. import java.util.stream.Collectors;
  27. import javax.persistence.Column;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.dao.EmptyResultDataAccessException;
  30. import org.springframework.jdbc.core.JdbcTemplate;
  31. import org.springframework.stereotype.Service;
  32. /**
  33. *
  34. * Created by wangyc on 2018/6/11.
  35. *
  36. * @version 2018/6/11 15:47 wangyc
  37. * @param <T>
  38. */
  39. @Service
  40. public class JdbcServiceImpl<T> implements JdbcService{
  41. private final JdbcTemplate jdbcTemplate;
  42. private final GoodsDao goodsDao;
  43. @Autowired
  44. public JdbcServiceImpl(JdbcTemplate jdbcTemplate, GoodsDao goodsDao) {
  45. this.jdbcTemplate = jdbcTemplate;
  46. this.goodsDao = goodsDao;
  47. }
  48. @Override
  49. public Long getTotalElements(String tablename) {
  50. return jdbcTemplate.queryForObject(String.format("select count(1) from %s", tablename), Long.class);
  51. }
  52. @Override
  53. public Long getMaxId(String tableName, String idColumn) {
  54. return jdbcTemplate.queryForObject(String.format("select max(%s) from %s", idColumn, tableName), Long.class);
  55. }
  56. @Override
  57. public Long getMinId(String tableName, String idColumn) {
  58. return jdbcTemplate.queryForObject(String.format("select max(%s) from %s", idColumn, tableName), Long.class);
  59. }
  60. @Override
  61. public List<Long> getLimitId(String tablename, String direction, String sortField, Long start, Integer size) {
  62. List<Long> ids = new ArrayList<>();
  63. Long startId = jdbcTemplate.queryForObject(String.format("select %s from %s order by %s limit %s,1", sortField,
  64. tablename, sortField, start), Long.class);
  65. ids.add(startId);
  66. try {
  67. Long endId = jdbcTemplate.queryForObject(String.format("select %s from %s order by %s limit %s,1", sortField,
  68. tablename, sortField, start + size - 1), Long.class);
  69. ids.add(endId);
  70. } catch (EmptyResultDataAccessException e) {
  71. Long endId = jdbcTemplate.queryForObject(String.format("select max(%s) from %s", sortField,
  72. tablename, sortField, start + size - 1), Long.class);
  73. ids.add(endId);
  74. ids.add(1L);
  75. }
  76. return ids;
  77. }
  78. @Override
  79. public String abstractTransform(Class className, String property) throws NoSuchFieldException {
  80. Field id = className.getDeclaredField("id");
  81. Annotation annotation = id.getAnnotation(Column.class);
  82. return ((Column) annotation).name();
  83. }
  84. @Override
  85. public List<T> getData(String tableName, Long startId, Long endId, String sortField, String direct) {
  86. String sql = String.format("select * from %s where %s between %d and %d order by %s %s", tableName, sortField,
  87. startId, endId, sortField, direct);
  88. List<T> data = null;
  89. // 器件索引
  90. if (SearchConstants.COMPONENT_TABLE_NAME.equals(tableName)) {
  91. data = queryComponents(sql, startId, endId, true);
  92. }
  93. // Product索引
  94. if (PRODUCTS_PRIVATE_TABLE_NAME.equals(tableName)) {
  95. data = queryVProducts(sql);
  96. }
  97. if (PRODUCTS_USERS_TABLE_NAME.equals(tableName)) {
  98. data = queryProductUser(sql);
  99. }
  100. // Goods索引
  101. if (TRADE_GOODS_TABLE_NAME.equals(tableName)) {
  102. data = queryGoods(sql);
  103. }
  104. // PCB索引
  105. if (PCB_TABLE_NAME.equals(tableName)) {
  106. data = queryPCB(sql);
  107. }
  108. return data;
  109. }
  110. /**
  111. * 获取Component
  112. * @param sql sql
  113. * @param needProperties 是否需要获取属性值
  114. * @return
  115. */
  116. private List<T> queryComponents(String sql, Long startId, Long endId, boolean needProperties) {
  117. List<Component> components = new ArrayList<>();
  118. if (needProperties) {
  119. String propertyValueSql = "select product$component.cmp_id,product$component.cmp_uuid,product$component.cmp_code,product$component.cmp_description,product$component.cmp_search_count,product$component.cmp_visit_count,product$component.searchweight,product$component.cmp_kiid,product$component.cmp_brid,product$kind.ki_id,product$kind.ki_name,product$kind.ki_level,product$kind.ki_isleaf,product$kind.ki_visit_count,product$kind.ki_search_count,product$brand.br_id,product$brand.br_uuid,product$brand.br_name_cn,product$brand.br_name_en,product$brand.br_visit_count,product$brand.br_search_count,product$brand.br_weight,product$propertyvalue.pv_id,product$propertyvalue.pv_propertyid,product$propertyvalue.pv_value,product$propertyvalue.pv_componentid,product$propertyvalue.pv_detno,product$property.pt_id,product$property.pt_label from product$component left join product$propertyvalue on product$propertyvalue.pv_componentid = product$component.cmp_id left join product$property on product$propertyvalue.pv_propertyid = product$property.pt_id left join product$kind on product$component.cmp_kiid = product$kind.ki_id left join product$brand on product$component.cmp_brid = product$brand.br_id where product$component.cmp_id between %d and %d";
  120. List<Component> componentList = jdbcTemplate.query(String.format(propertyValueSql, startId, endId), new Component());
  121. Map<Long, List<Component>> componentMap = componentList.stream().collect(Collectors.groupingBy(Component :: getId));
  122. for (Entry<Long, List<Component>> entery : componentMap.entrySet()) {
  123. List<Component> componentLists = componentMap.get(entery.getKey());
  124. Component finalComponent = componentLists.get(0);
  125. Set<PropertyValue> propertyValues = componentLists.stream().filter(
  126. c -> c.getPropertyValue() != null).map(c -> c.getPropertyValue()).collect(Collectors.toSet());
  127. finalComponent.setProperties(propertyValues);
  128. finalComponent.setPropertyValue(null);
  129. components.add(finalComponent);
  130. }
  131. return (List<T>)components;
  132. } else {
  133. return jdbcTemplate.query(sql.replace("where", "left join product$brand on product$brand.br_id=product$component.cmp_brid left join product$kind on product$kind.ki_id = product$component.cmp_kiid where"), new Component());
  134. }
  135. }
  136. /**
  137. * 获取V_Products
  138. * @param sql sql
  139. * @return
  140. */
  141. private List<T> queryVProducts(String sql) {
  142. return jdbcTemplate.query(sql, new V_Products());
  143. }
  144. private List<T> queryProductUser(String sql) {
  145. return jdbcTemplate.query(sql.replace("where", "left join v$product$private on product$users.pu_prid = v$product$private.pr_id where"), new ProductUsersSimpleInfo());
  146. }
  147. /**
  148. * 获取Goods
  149. * @param sql sql
  150. * @return
  151. */
  152. private List<T> queryGoods(String sql) {
  153. return jdbcTemplate.query(sql, new TradeGoods());
  154. }
  155. /**
  156. * 下载pcb
  157. * @param sql sql
  158. * @return
  159. */
  160. private List<T> queryPCB(String sql) {
  161. List<PCB> pcbs = jdbcTemplate.query(sql, new PCB());
  162. if (!CollectionUtils.isEmpty(pcbs)) {
  163. String kindSql = "select * from product$kind where ki_id = ";
  164. String brandSql = "select * from product$brand where br_id = ";
  165. String productSql = "select * from products where pr_id = ";
  166. for (PCB pcb : pcbs) {
  167. if (pcb.getKindId() != null) {
  168. List<Kind> kinds = jdbcTemplate.query(kindSql + pcb.getKindId(), new Kind());
  169. if (!CollectionUtils.isEmpty(kinds)) {
  170. pcb.setKind(kinds.get(0));
  171. }
  172. }
  173. if (pcb.getBrandId() != null) {
  174. List<Brand> brands = jdbcTemplate.query(brandSql + pcb.getBrandId(), new Brand());
  175. if (!CollectionUtils.isEmpty(brands)) {
  176. pcb.setBrand(brands.get(0));
  177. }
  178. }
  179. if (pcb.getProductId() != null) {
  180. List<Products> productses = jdbcTemplate.query(productSql + pcb.getProductId(), new Products());
  181. if (!CollectionUtils.isEmpty(productses)) {
  182. pcb.setProducts(productses.get(0));
  183. }
  184. }
  185. }
  186. }
  187. return (List<T>) pcbs;
  188. }
  189. }