Browse Source

供应商推荐接口调整

dongbw 8 years ago
parent
commit
d228d892c6
1 changed files with 51 additions and 41 deletions
  1. 51 41
      src/main/java/com/uas/platform/b2b/service/impl/VendorsServiceImpl.java

+ 51 - 41
src/main/java/com/uas/platform/b2b/service/impl/VendorsServiceImpl.java

@@ -15,7 +15,6 @@ import com.uas.platform.b2b.erp.model.VendorRecommend;
 import com.uas.platform.b2b.model.Distribute;
 import com.uas.platform.b2b.model.Enterprise;
 import com.uas.platform.b2b.model.Product;
-import com.uas.platform.b2b.model.User;
 import com.uas.platform.b2b.model.Vendor;
 import com.uas.platform.b2b.model.VendorContact;
 import com.uas.platform.b2b.service.VendorService;
@@ -24,8 +23,6 @@ import com.uas.platform.b2b.temporary.model.PartnershipRecord;
 import com.uas.platform.core.model.Constant;
 import com.uas.platform.core.model.PageInfo;
 import com.uas.platform.core.model.Status;
-import com.uas.platform.core.persistence.criteria.PredicateFactory;
-import com.uas.platform.core.persistence.criteria.PredicateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.jpa.domain.Specification;
@@ -348,10 +345,10 @@ public class VendorsServiceImpl implements VendorService {
 	public com.uas.account.support.Page<VendorRecommend> getVendorRecommend(String productMatchCondition, String enterpriseMatchCondition, int page, int size) {
 		final Long enUU = SystemSession.getUser().getEnterprise().getUu();
 		// 先查询当前企业的采购物料(待购&&不在售)的标准物料标准料号
-		StringBuffer cmpCodesSql = new StringBuffer().append("select distinct(pr.pr_cmpcode) from products pr left join sec$enterprises en on pr.pr_enuu = en.en_uu " +
+		StringBuffer cmpCodesSql = new StringBuffer().append("select distinct(pr.pr_cmpcode) from products pr " +
 				" where pr.pr_ispurchase = 1 and pr.pr_issale != 1 and pr.pr_standard = 1 and pr.pr_enuu = ").append(enUU);
 		if (null != productMatchCondition && !"".equals(productMatchCondition)) {
-            cmpCodesSql.append(productMatchCondition);
+            cmpCodesSql.append(" and ").append(productMatchCondition);
         }
 
 		// 根据标准料号找出对应的供应商UU
@@ -368,12 +365,12 @@ public class VendorsServiceImpl implements VendorService {
 		vendorRecommendUusSql.append("select en_uu from sec$enterprises left join products on en_uu = pr_enuu " +
 				" where 1=1 and pr_issale = 1 and pr_standard = 1 and pr_cmpcode in (").append(cmpCodesSql).append(") and en_uu not in (")
 				.append(vendUUsSql).append(") and en_uu <> ").append(enUU);
-		vendorRecommendUuNumSql.append("select count(en_uu) from sec$enterprises left join products on en_uu = pr_enuu " +
+		vendorRecommendUuNumSql.append("select count(distinct(en_uu)) from sec$enterprises left join products on en_uu = pr_enuu " +
 				" where 1=1 and pr_issale = 1 and pr_standard = 1 and pr_cmpcode in (").append(cmpCodesSql).append(") and en_uu not in (")
 				.append(vendUUsSql).append(") and en_uu <> ").append(enUU);
 		if (null != enterpriseMatchCondition && !"".equals(enterpriseMatchCondition)) {
-			vendorRecommendUusSql.append(enterpriseMatchCondition);
-			vendorRecommendUuNumSql.append(enterpriseMatchCondition);
+			vendorRecommendUusSql.append(" and ").append(enterpriseMatchCondition);
+			vendorRecommendUuNumSql.append(" and ").append(enterpriseMatchCondition);
 		}
 		vendorRecommendUusSql.append(rownumSql);
 		System.out.println("SQL:" + vendorRecommendUusSql.toString());
@@ -381,30 +378,33 @@ public class VendorsServiceImpl implements VendorService {
 		Integer total = commonDao.queryForObject(vendorRecommendUuNumSql.toString(), Integer.class);
 		List<Long> vendUUs = commonDao.queryForList(vendorRecommendUusSql.toString(), Long.class);
 		List<VendorRecommend> content = new ArrayList<>();
-		for (Long vendUU : vendUUs) {
+		for (final Long vendUU : vendUUs) {
 			Enterprise vendor = enterPriseDao.findOne(vendUU);
 			if (null != vendor) {
-				if (null != vendor.getEnAdminuu()) {
-					User admin = userDao.findOne(vendor.getEnAdminuu());
-					vendor.setAdmin(admin);
-				}
-				PageInfo pageInfo = new PageInfo(1, 5);
-				final PredicateFactory<Product> factory = new PredicateFactory<>();
-				factory.add(PredicateUtils.eq("enUU", enUU, false));
-				factory.add(PredicateUtils.in("cmpCode", cmpCodes, false));
+//				if (null != vendor.getEnAdminuu()) {
+//					User admin = userDao.findOne(vendor.getEnAdminuu());
+//					vendor.setAdmin(admin);
+//				}
+				StringBuffer productIdsSql = new StringBuffer();
+				StringBuffer productNumSql = new StringBuffer();
+				productIdsSql.append("select pr.pr_id from v$products pr where pr.pr_enuu = ").append(vendUU)
+						.append(" and pr.pr_issale =1 and pr.pr_standard =1 and exists (select 1 from v$products p1 " +
+								"where p1.pr_cmpcode = pr.pr_cmpcode and p1.pr_ispurchase = 1 and p1.pr_issale != 1 " +
+								"and p1.pr_standard = 1 and p1.pr_b2bdisabled <> 1 and p1.pr_enuu = ")
+						.append(enUU).append(") and rownum <= 5 order by pr_id desc");
+				productNumSql.append("select count(1) from v$products pr where pr.pr_enuu = ").append(vendUU)
+						.append(" and pr.pr_issale =1 and pr.pr_standard =1 and exists (select 1 from v$products p1 " +
+								"where p1.pr_cmpcode = pr.pr_cmpcode and p1.pr_ispurchase = 1 and p1.pr_issale != 1 " +
+								"and p1.pr_standard = 1 and p1.pr_b2bdisabled <> 1 and p1.pr_enuu = ")
+						.append(enUU).append(")");
+				List<Long> prodIds = commonDao.queryForList(productIdsSql.toString(), Long.class);
+				Integer hitNums = commonDao.queryForObject(productNumSql.toString(), Integer.class);
 				// 获取前五条匹配物料
-				Page<Product> hitsProducts = productDao.findAll(new Specification<Product>() {
-					@Override
-					public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
-						query.where(factory.toPredicate(root, query, cb));
-						query.orderBy(cb.desc(root.get("id").as(Long.class)));
-						return null;
-					}
-				}, pageInfo);
+				List<Product> hitsProducts = productDao.findAll(prodIds);
 				VendorRecommend vendorRecommend = new VendorRecommend();
 				vendorRecommend.setEnterprise(vendor);
-				vendorRecommend.setHitProds(hitsProducts.getContent());
-				vendorRecommend.setHitNums(hitsProducts.getNumberOfElements());
+				vendorRecommend.setHitProds(hitsProducts);
+				vendorRecommend.setHitNums(hitNums);
 				content.add(vendorRecommend);
 			}
 		}
@@ -427,23 +427,33 @@ public class VendorsServiceImpl implements VendorService {
 		// UAS企业UU
 		Long enUU = SystemSession.getUser().getEnterprise().getUu();
 		// 先查询当前企业的采购物料(待购&&不在售)的标准物料标准料号
-		String cmpCodesSql = "select distinct(pr.pr_cmpcode) from products pr left join sec$enterprises en on pr.pr_enuu = en.en_uu " +
-				"where pr.pr_ispurchase = 1 and pr.pr_issale != 1 and pr.pr_standard = 1 and pr.pr_enuu = " + enUU +
-				" and " + productMatchCondition;
-		final List<String> cmpCodes = commonDao.queryForList(cmpCodesSql, String.class);
-		// rownum 控制
-		String rownumSql = " and rownum > " + (page - 1) * size + " and rownum <= " + page * size;
-		// 查找匹配物料的语句
-		String productsMatchSql = "select * from products where pr_enuu = " + vendUU + " and pr_cmpcode in (" + cmpCodesSql + ") and " + whereCondition;
-		// 查找所有物料的语句
-		String allProductsSql = "select * from products where pr_enuu = " + vendUU + " and pr_cmpcode is not null and " + whereCondition;
+		StringBuffer cmpCodesSql = new StringBuffer();
+		cmpCodesSql.append("select 1 from v$products where pr_cmpcode = p1.pr_cmpcode and pr_enuu = ")
+				.append(enUU).append(" and pr_issale <> 1 and pr_ispurchase = 1 and pr_standard = 1 and pr_b2bdisabled <> 1 ");
+		if (null != productMatchCondition) {
+			cmpCodesSql.append(" and ").append(productMatchCondition);
+		}
+//		final List<String> cmpCodes = commonDao.queryForList(cmpCodesSql.toString(), String.class);
+		// 查找匹配在售物料的语句
+		StringBuffer productsMatchSql = new StringBuffer();
+		productsMatchSql.append("select pr_id from (select pr_id from v$products p1 where exists (").append(cmpCodesSql)
+				.append(") and p1.pr_enuu = ").append(vendUU).append(" and p1.pr_issale = 1 and p1.pr_standard = 1 and p1.pr_b2bdisabled <> 1 and ")
+				.append(null != whereCondition ? whereCondition : "").append(" order by pr_id desc)");
+		// 查找不匹配但是在售物料的语句  union all 方便排序
+		StringBuffer notMatchProductsSql = new StringBuffer();
+		notMatchProductsSql.append("select pr_id from (select pr_id from v$products p1 where not exists (").append(cmpCodesSql)
+				.append(") and p1.pr_enuu = ").append(vendUU).append(" and p1.pr_issale = 1 and p1.pr_standard = 1 and p1.pr_b2bdisabled <> 1 and ")
+				.append(null != whereCondition ? whereCondition : "").append(" order by pr_id desc)");
 		// 物料id
 		StringBuffer idSql = new StringBuffer();
-		StringBuffer countSql = new StringBuffer();
-		idSql.append("select pr_id from (").append(productsMatchSql).append(" union ").append(allProductsSql).append(rownumSql).append(")");
-		countSql.append("select count(1) from (").append(productsMatchSql).append(" union ").append(allProductsSql).append(")");
+		String countSql = "select count(1) from v$products where pr_enuu = " + vendUU
+				+ " and pr_issale = 1 and pr_standard = 1 and pr_b2bdisabled <> 1";
+		// rownum 控制
+		String rownumSql = " where rownum > " + (page - 1) * size + " and rownum <= " + page * size;
+		idSql.append("select pr_id from (").append(productsMatchSql).append(" union all ").append(notMatchProductsSql)
+				.append(")").append(rownumSql);
 		System.out.println(idSql.toString());
-		Integer count = commonDao.queryForObject(countSql.toString(), Integer.class);
+		Integer count = commonDao.queryForObject(countSql, Integer.class);
 		List<Long> ids = commonDao.queryForList(idSql.toString(), Long.class);
 		List<Product> products = productDao.findAll(ids);
 		return new com.uas.account.support.Page<>(page, size, products, count);