Browse Source

提升批量删除个人物料的速度

yuj 7 years ago
parent
commit
7cf963fab1

+ 4 - 4
src/main/java/com/uas/platform/b2c/core/utils/StringUtilB2C.java

@@ -217,14 +217,14 @@ public class StringUtilB2C {
 	 * @return 连接的字符串
 	 */
 	public static <T> String joinListUseContact(List<T> tList, String contactStr) {
-		String str = "";
+		StringBuffer str = new StringBuffer();
 		for (T t : tList) {
-			str = str +  t.toString() + contactStr;
+			str.append(t.toString()).append(contactStr);
 		}
 		if(!StringUtils.isEmpty(str)) {
-			str = str.substring(0, str.length() - 1);
+			return str.substring(0, str.length() - 1);
 		}
-		return str;
+		return str.toString();
 	}
 
 	/**

+ 10 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/dao/ProductPersonDao.java

@@ -35,6 +35,16 @@ public interface ProductPersonDao extends JpaRepository<ProductPerson, Long>, Jp
     @Query(value = "select p.productId from ProductPerson p where p.userUU = :useruu and p.productId in (:prids)")
     List<Long> findProductIdInProductPerson(@Param("prids") List<Long> prids, @Param("useruu") Long useruu);
 
+    /**
+     * 获取已存在个人物料库的主键
+     *
+     * @param prids 物料的id
+     * @param useruu 个人用户
+     * @return
+     */
+    @Query(value = "select p.id from ProductPerson p where p.userUU = :useruu and p.productId in (:prids)")
+    List<Long> findIdsInProductPerson(@Param("prids") List<Long> prids, @Param("useruu") Long useruu);
+
     /**
      * 根据productId查找物料信息
      * @param productId

+ 7 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/service/ProductService.java

@@ -104,6 +104,13 @@ public interface ProductService {
      */
     ResultMap deleteBatch(String type, Integer isPerson);
 
+    /**
+     * 批量删除个人物料
+     * @param type
+     * @return
+     */
+    ResultMap deletePersonalProductByBatch(String type);
+
     /**
      * 单个匹配
      *

+ 43 - 15
src/main/java/com/uas/platform/b2c/prod/commodity/service/impl/ProductServiceImpl.java

@@ -601,7 +601,7 @@ public class ProductServiceImpl implements ProductService {
         if (CollectionUtils.isEmpty(ids)) {
             return new ResultMap(CodeType.NO_INFO, "没有传入有效信息");
         }
-        if (null != isPerson && isPerson.intValue() == IntegerConstant.YES_SHORT.intValue()) {
+        if (IntegerConstant.YES_SHORT.equals(isPerson)) {
             String sql = "delete from product$users where pu_prid in (:ids) and pu_useruu = " + SystemSession.getUser().getUserUU();
             Map<String, List<Long>> map = new HashedMap();
             map.put("ids", ids);
@@ -679,27 +679,19 @@ public class ProductServiceImpl implements ProductService {
      */
     @Override
     public ResultMap deleteBatch(String type, Integer isPerson) {
-        Integer standard;
+        //如果是删除个人物料
+        if (IntegerConstant.YES_SHORT.equals(isPerson)) {
+            ResultMap resultMap = deletePersonalProductByBatch(type);
+            return resultMap;
+        }
+        Integer standard = ProductConstant.STANDARD.equals(type) ? IntegerConstant.YES_SHORT : IntegerConstant.NO_SHORT;
         List<ProductPerson> personList;
         Integer fail = 0;
         Integer success = 0;
         Integer isHave = 0;
         Long uu = SystemSession.getUser().getEnterprise().getUu();
-        if (ProductConstant.STANDARD.equals(type)) {
-            standard = IntegerConstant.YES_SHORT;
-        } else {
-            standard = IntegerConstant.NO_SHORT;
-        }
         List<Product> products = productDao.findProductByEnuuAndStandardAndEnabled(uu, standard, IntegerConstant.YES_SHORT);
         for (Product product : products) {
-            if (null != isPerson && isPerson.intValue() == IntegerConstant.YES_SHORT.intValue()) {
-                List<ProductPerson> productPersonList = productPersonDao.findByProductIdAndUserUU(product.getId(),SystemSession.getUser().getUserUU());
-                if (!CollectionUtils.isEmpty(productPersonList)) {
-                    Long ppid = productPersonList.get(0).getId();
-                    productPersonDao.delete(ppid);
-                }
-                continue;
-            }
             try {
                 personList = productPersonDao.findByProductId(product.getId());
                 if (CollectionUtils.isNotEmpty(personList)) {
@@ -773,6 +765,42 @@ public class ProductServiceImpl implements ProductService {
         return new ResultMap(status, message);
     }
 
+    /**
+     * 批量删除个人物料
+     *
+     * @param type
+     * @return
+     */
+    @Override
+    public ResultMap deletePersonalProductByBatch(String type) {
+        Integer standard;
+        Integer success = 0;
+        Long uu = SystemSession.getUser().getEnterprise().getUu();
+        if (ProductConstant.STANDARD.equals(type)) {
+            standard = IntegerConstant.YES_SHORT;
+        } else {
+            standard = IntegerConstant.NO_SHORT;
+        }
+        List<Long> prids = productDao.findPridsByEnuuAndStardand(uu, standard);
+        List<Long> personPrIds = null;
+        if (CollectionUtils.isNotEmpty(prids)) {
+            personPrIds = productPersonDao.findIdsInProductPerson(prids, SystemSession.getUser().getUserUU());
+            if (CollectionUtils.isNotEmpty(personPrIds)) {
+                success = personPrIds.size();
+                String ids = StringUtilB2C.joinListUseContact(personPrIds, SplitChar.COMMA);
+                jdbcTemplate.update("delete from product$users where pu_id in (" + ids +")");
+            }
+        }
+        String message = "";
+        if (IntegerConstant.NO_SHORT.equals(success)) {
+            message = "没有需要删除的信息";
+        } else {
+            message = "成功删除" + success + "条";
+        }
+        return new ResultMap(CodeType.OK, message);
+    }
+
+
     @Override
     public Product match(Long id) {
         Product product = productDao.findOne(id);

+ 18 - 0
src/test/java/com/uas/platform/b2c/Test.java

@@ -1,5 +1,7 @@
 package com.uas.platform.b2c;
 
+import com.uas.platform.b2c.core.constant.SplitChar;
+import com.uas.platform.b2c.core.utils.StringUtilB2C;
 import com.uas.platform.b2c.prod.commodity.model.Product;
 import com.uas.platform.b2c.prod.commodity.service.ProductService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,4 +52,20 @@ public class Test {
         System.arraycopy(list.toArray(), 0, list2, 0, list.size());
         System.out.println(list2);
     }
+
+    @org.junit.Test
+    public void stringBufferTest() {
+        List<Long> list = new ArrayList<>();
+        list.add(1l);
+        list.add(2l);
+        String s1 = StringUtilB2C.joinListUseContact(list, SplitChar.COMMA);
+        System.out.println(s1);
+        StringBuffer st = new StringBuffer();
+        for (Long aLong : list) {
+            st.append(aLong);
+            String s = st.substring(0, 1);
+            System.out.println(s);
+        }
+        System.out.println(st.toString());
+    }
 }