Browse Source

调整批量删除的逻辑

yujia 7 years ago
parent
commit
662777a6b8

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

@@ -11,14 +11,8 @@ import com.uas.platform.core.model.Type;
 import org.apache.commons.collections.CollectionUtils;
 import org.springframework.util.StringUtils;
 
-import java.io.*;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Random;
-import java.util.Set;
+import java.io.UnsupportedEncodingException;
+import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -432,5 +426,20 @@ public class StringUtilB2C {
 		}
 	}
 
-
+	/**
+	 * 从list数组中删除指定的信息
+	 * @param list
+	 * @param target
+	 */
+	public static <T> void removeFromList(List<T> list, T target) {
+		if (CollectionUtils.isEmpty(list) || target == null) {
+			return ;
+		}
+		for(int i = list.size() - 1; i >= 0; i--){
+			T item = list.get(i);
+			if(target.equals(item)){
+				list.remove(item);
+			}
+		}
+	}
 }

+ 2 - 2
src/main/java/com/uas/platform/b2c/prod/commodity/controller/ProductController.java

@@ -178,13 +178,13 @@ public class ProductController {
      * @param ids the ids
      */
     @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
-	public void deleteBatch(@PathVariable("ids") String ids , Integer isPerson) {
+	public ResultMap deleteBatch(@PathVariable("ids") String ids , Integer isPerson) {
 		String[] idstrs = ids.split(SplitChar.COMMA);
 		List<Long> idsLong = new ArrayList<Long>();
 		for (String id : idstrs) {
 			idsLong.add(Long.parseLong(id));
 		}
-		productService.deleteBatch(idsLong, isPerson);
+		return productService.deleteBatch(idsLong, isPerson);
 	}
 
     /**

+ 8 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/dao/ProductMatchResultDao.java

@@ -3,8 +3,13 @@ package com.uas.platform.b2c.prod.commodity.dao;
 import com.uas.platform.b2c.prod.commodity.model.ProductMatchResult;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 /**
  * 匹配结果类操作层
  * @author hulh
@@ -12,4 +17,7 @@ import org.springframework.stereotype.Repository;
 @Repository
 public interface ProductMatchResultDao extends JpaRepository<ProductMatchResult, Long>, JpaSpecificationExecutor<ProductMatchResult> {
 
+    @Modifying
+    @Query(value = "delete from ProductMatchResult p where p.prid in (:productIds)")
+    void deleteByProductids(@Param("productIds") List<Long> productIds);
 }

+ 1 - 1
src/main/java/com/uas/platform/b2c/prod/commodity/service/GoodsService.java

@@ -688,7 +688,7 @@ public interface GoodsService {
      * @param goodses
      * @return
      */
-    List<Goods> deleteGoods(List<Goods> goodses);
+    void deleteGoods(List<Goods> goodses);
 
     /**
      * 整理在售的信息

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

@@ -88,7 +88,7 @@ public interface ProductService {
      *
      * @param ids the ids 主键
      */
-    void deleteBatch(List<Long> ids, Integer isPerson);
+    ResultMap deleteBatch(List<Long> ids, Integer isPerson);
 
     /**
      * 批量删除产品

+ 36 - 71
src/main/java/com/uas/platform/b2c/prod/commodity/service/impl/GoodsServiceImpl.java

@@ -3455,82 +3455,47 @@ public class GoodsServiceImpl implements GoodsService {
     }
 
     @Override
-    public List<Goods> deleteGoods(List<Goods> goodses) {
-        List<Order> orders = new ArrayList<>();
-        List<GoodsHistory> histories = new ArrayList<>();
-        List<Product> products = new ArrayList<>();
-        List<Goods> goodses1 = new ArrayList<>();
-        List<Long> gids = new ArrayList<>();
-        Set<String> uuids = new HashSet<>();
-        for (Goods goodse : goodses) {
-            Goods goods = goodsDao.findOne(goodse.getId());
-            if (goods == null) {
-                return null;
-            }
-            boolean isExistOrder = validateExistOrder(goods.getBatchCode());
-            if (isExistOrder) {
-                throw new IllegalOperatorException("存在商品已经被购买了,不能删除");
-            }
-            Set<String> set = new HashSet<>();
-            set.add(goods.getBatchCode());
-            recommendProductService.deleteProductsWhenSellerUpdateReserve(goods.getStoreid(), set);
-
-            orders.addAll(detailService.updateOrderDetailsByGoods(goods));
+    public void deleteGoods(List<Goods> goodses) {
+        try {
+            executor.submitTask(new Runnable() {
+                @Override
+                public void run() {
+                    List<Order> orders = new ArrayList<>();
+                    List<GoodsHistory> histories = new ArrayList<>();
+                    List<Goods> goodses1 = new ArrayList<>();
+                    for (Goods goodse : goodses) {
+                        if (goodse.getStatus().equals(Status.AVAILABLE.value()) || goodse.getStatus().equals(Status.UNAVAILABLE.value())) {
+                            goodse.setStatus(Status.REMOVED.value());
+                            GoodsHistory goodsHistoryRemoved = goodsHistoryService.converTGoodsHist(goodse, OperateType.Down.getPhrase(), false);
+                            histories.add(goodsHistoryRemoved);
+                        }
+                        //删除库存信息
+                        goodse.setStatus(Status.DELETED.value());
+                        GoodsHistory goodsHistoryDelete = goodsHistoryService.converTGoodsHist(goodse, OperateType.DELETE.getPhrase(),false);
+                        histories.add(goodsHistoryDelete);
+                        goodses1.add(goodse);
+                    }
+                    if (CollectionUtils.isNotEmpty(histories)) {
+                        goodsHistoryService.save(histories);
+                    }
+                    if (CollectionUtils.isNotEmpty(goodses1)) {
+                        goodsDao.delete(goodses1);
+                    }
 
-            if (goods.getStatus().equals(Status.AVAILABLE.value()) || goods.getStatus().equals(Status.UNAVAILABLE.value())) {
-                goods.setStatus(Status.REMOVED.value());
-                GoodsHistory goodsHistoryRemoved = goodsHistoryService.converTGoodsHist(goods, OperateType.Down.getPhrase(), false);
-                histories.add(goodsHistoryRemoved);
-            }
-            //删除库存信息
-            goods.setStatus(Status.DELETED.value());
-            GoodsHistory goodsHistoryDelete = goodsHistoryService.converTGoodsHist(goods, OperateType.DELETE.getPhrase(),false);
-            histories.add(goodsHistoryDelete);
-            goodses1.add(goods);
-            gids.add(goods.getId());
-            if (!StringUtils.isEmpty(goods.getUuid())) {
-                uuids.add(goods.getUuid());
-            }
-            if (goods.getProductid() != null) {
-                Product product = productDao.findOne(goods.getProductid());
-                if (product != null) {
-                    ProductPrivate productPrivate = productPrivateService.findByPrId(product.getId());
-                    if (productPrivate == null) {
-                        productPrivate = new ProductPrivate();
-                        productPrivate.setPrId(product.getId());
-                        productPrivate.setB2cEnabled(IntegerConstant.YES_INT);
+                    for (Goods goodse : goodses) {
+                        Set<String> set = new HashSet<>();
+                        set.add(goodse.getBatchCode());
+                        recommendProductService.deleteProductsWhenSellerUpdateReserve(goodse.getStoreid(), set);
+                        orders.addAll(detailService.updateOrderDetailsByGoods(goodse));
                     }
-                    Integer count = productPrivate.getBatchCount();
-                    if (null == count){
-                        productPrivate.setBatchCount(0);
-                    }else {
-                        productPrivate.setBatchCount(--count);
-                        if (productPrivate.getBatchCount() < 1) {
-                            productPrivate.setBatchCount(0);
-                        }
+                    if (CollectionUtils.isNotEmpty(orders)) {
+                        orderService.save(orders);
                     }
-                    productPrivateDao.save(productPrivate);
-                    products.add(product);
                 }
-            }
-        }
-        if (CollectionUtils.isNotEmpty(orders)) {
-            orderService.save(orders);
-        }
-        if (CollectionUtils.isNotEmpty(histories)) {
-            goodsHistoryService.save(histories);
-        }
-        if (CollectionUtils.isNotEmpty(products)) {
-            productDao.save(products);
-        }
-        if (CollectionUtils.isNotEmpty(goodses1)) {
-            goodsDao.delete(goodses1);
-        }
-
-        for (String uuid : uuids) {
-            updateComponentTradeInfos(uuid);
+            });
+        } catch (InterruptedException e) {
+            e.printStackTrace();
         }
-        return goodses;
     }
 
     @Override

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

@@ -527,70 +527,75 @@ public class ProductServiceImpl implements ProductService {
         return exportGoodsByProduct(productList);
     }
 
+    @javax.transaction.Transactional
     @Override
-    public void deleteBatch(List<Long> ids, Integer isPerson) {
-        for (Long id : ids) {
-            if (null != isPerson && isPerson.intValue() == IntegerConstant.YES_SHORT.intValue()) {
-                List<ProductPerson> productPersonList = productPersonDao.findByProductIdAndUserUU(id,SystemSession.getUser().getUserUU());
-                if (!CollectionUtils.isEmpty(productPersonList)) {
-//                    Map<String,Object> requestMap = new HashMap<>();
-//                    requestMap.put("userUU",productPersonList.get(0).getUserUU());
-//                    requestMap.put("productId",productPersonList.get(0).getProductId());
-//                    String res  = restTemplate.postForEntity( productServiceIp+"/product/assign/delete", null, String.class, requestMap).getBody();
-                    Long ppid = productPersonList.get(0).getId();
-                    productPersonDao.delete(ppid);
+    public ResultMap deleteBatch(List<Long> ids, Integer isPerson) {
+        if (CollectionUtils.isEmpty(ids)) {
+            return new ResultMap(CodeType.NO_INFO, "没有传入有效信息");
+        }
+        if (null != isPerson && isPerson.intValue() == IntegerConstant.YES_SHORT.intValue()) {
+            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);
+            namedParameterJdbcTemplate.update(sql, map);
+            return ResultMap.success("删除成功");
+        } else {
+            String message = "";
+            List<Goods> goodsList = goodsDao.findByProductIds(ids);
+            String codes = OrderStatus.UNAVAILABLE.getCodes() + "-" + Status.TOBECONFIRMED.value();
+            List<OrderDetail> orderDetailList = orderDetailService.findOrderDetailByProductids(ids);
+            for (OrderDetail detail : orderDetailList) {
+                Order order = detail.getOrder();
+                int status = order.getStatus() == null ? detail.getStatus() : order.getStatus();
+                if (codes.indexOf(String.valueOf(status)) < 0) {
+                    // 区分是否是假单,or_orderids 不为空
+                    if (StringUtils.isEmpty(order.getOrderids())) {
+                        if (!message.contains("存在已下单的在售产品信息,不能删除")) {
+                            message += "存在已下单的在售产品信息,不能删除";
+                        }
+                        StringUtilB2C.removeFromList(ids, detail.getProductId());
+                    }
                 }
-                continue;
             }
-            Product product = productDao.findOne(id);
-            if (product == null)
-                throw new IllegalOperatorException("选择的产品不存在,请重新选择");
-            //查看是否存在正常未取消的订单
-            List<Goods> goodses = goodsDao.findByProductId(product.getId());
-            Boolean isExistOrder = false;
-            String codes = OrderStatus.UNAVAILABLE.getCodes() + "-" + Status.TOBECONFIRMED.value();
-            for (Goods goods : goodses) {
-                List<OrderDetail> orderList = orderDetailService.findOrderDetailByBatchCode(goods.getBatchCode());
-                for (OrderDetail detail : orderList) {
-                    Order order = detail.getOrder();
-                    int status = order.getStatus() == null ? detail.getStatus() : order.getStatus();
-                    if (codes.indexOf(String.valueOf(status)) < 0) {
-                        // 区分是否是假单,or_orderids 不为空
-                       if (StringUtils.isEmpty(order.getOrderids())) {
-                           isExistOrder = true;
-                           break;
-                       }
 
+            List<ProductPerson> productPersons = productPersonDao.findByProductIds(ids);
+            for (ProductPerson productPerson : productPersons) {
+                if (!message.contains("存在物料已被个人收录,不能删除")) {
+                    if (org.apache.commons.lang.StringUtils.isNotEmpty(message)) {
+                        message += ",";
                     }
+                    message += "存在物料已被个人收录,不能删除";
                 }
-                if (isExistOrder) {
-                   break;
-                }
+                StringUtilB2C.removeFromList(ids, productPerson.getProductId());
             }
-            if (!isExistOrder) {
-                //是否被个人物料库收录
-                int count = productPersonDao.countByProductId(id);
-                if ( count > 0 ){
-                    throw new IllegalOperatorException("该产品已被个人物料库收录,不能删除");
-                }
-                //删除外键关联的匹配结果列表,不然删除product失败
-                Set<ProductMatchResult> productMatchResultSet =  product.getMatchresults();
-                productMatchResultDao.delete(productMatchResultSet);
-                if (CollectionUtils.isNotEmpty(goodses)) {
-                    goodsService.deleteGoods(goodses);
-                }
-                ProductPrivate productPrivate = productPrivateService.findByPrId(product.getId());
-                if (productPrivate == null) {
-                    productPrivate = new ProductPrivate();
-                    productPrivate.setPrId(product.getId());
-                }
+            //删除匹配结果信息
+            productMatchResultDao.deleteByProductids(ids);
+
+            //更新在售产品信息
+            List<Goods> goodses = goodsService.findByProductids(ids);
+            goodsService.deleteGoods(goodses);
+
+            //调整物料为禁用状态。
+            List<ProductPrivate> productPrivates = productPrivateService.findByPrIds(ids);
+            for (ProductPrivate productPrivate : productPrivates) {
                 productPrivate.setB2cEnabled(IntegerConstant.NO_SHORT);
-                productPrivateDao.save(productPrivate);
-                //productDao.delete(id);  不允许删除
-            } else {
-                throw new
-                        IllegalOperatorException("该产品中存在已下单的在售产品信息,不能删除");
+                StringUtilB2C.removeFromList(ids, productPrivate.getPrId());
+            }
+            ProductPrivate pPrivate = null;
+            for (Long id : ids) {
+                pPrivate = new ProductPrivate(id);
+                if (CollectionUtils.isEmpty(productPrivates)) {
+                    productPrivates = new ArrayList<>();
+                }
+                productPrivates.add(pPrivate);
+            }
+            if (CollectionUtils.isNotEmpty(productPrivates)) {
+                productPrivateDao.save(productPrivates);
+            }
+            if (StringUtils.isEmpty(message)) {
+                message = "删除成功";
             }
+            return ResultMap.success(message);
         }
     }
 
@@ -613,7 +618,6 @@ public class ProductServiceImpl implements ProductService {
             standard = IntegerConstant.NO_SHORT;
         }
         List<Product> products = productDao.findProductByEnuuAndStandardAndEnabled(uu, standard, IntegerConstant.YES_SHORT);
-        Set<String> uuids = new HashSet<>();
         for (Product product : products) {
             if (null != isPerson && isPerson.intValue() == IntegerConstant.YES_SHORT.intValue()) {
                 List<ProductPerson> productPersonList = productPersonDao.findByProductIdAndUserUU(product.getId(),SystemSession.getUser().getUserUU());
@@ -656,7 +660,6 @@ public class ProductServiceImpl implements ProductService {
                     Set<ProductMatchResult> productMatchResultSet =  product.getMatchresults();
                     productMatchResultDao.delete(productMatchResultSet);
                     goodsService.deleteGoods(goodses);
-                    //productDao.delete(product.getId());
                     ProductPrivate productPrivate = productPrivateService.findByPrId(product.getId());
                     if (null == productPrivate) {
                         productPrivate = new ProductPrivate();
@@ -671,9 +674,6 @@ public class ProductServiceImpl implements ProductService {
                     }
                     productPrivate.setB2cEnabled(IntegerConstant.NO_SHORT);
                     productPrivateDao.save(productPrivate);
-                    if (!StringUtils.isEmpty(product.getCmpUuId())) {
-                        uuids.add(product.getCmpUuId());
-                    }
                 } else {
                     fail++;
                 }
@@ -691,15 +691,12 @@ public class ProductServiceImpl implements ProductService {
             if(success != 0) {
                 message += ",";
             }
-            message += "删除失败" + (fail-isHave) + "条,原因:该产品可能已经被其他单据或个人物料库引用,无法删除";
+            message += "删除失败" + (fail - isHave) + "条,原因:该产品可能已经被其他单据或个人物料库引用,无法删除";
         }
         // 如果是全部都不能删除,才能设置失败的状态码
         if ((products.size() != 0) && (products.size() ==  fail)) {
             status = CodeType.ERROR_STATE.code();
         }
-        for (String uuid : uuids) {
-            goodsService.updateComponentTradeInfos(uuid);
-        }
         return new ResultMap(status, message);
     }
 
@@ -1422,9 +1419,9 @@ public class ProductServiceImpl implements ProductService {
         if (product == null)
             throw new IllegalOperatorException("此物料信息不存在,请重新确认");
         ProductDetail productDetail = productDetailDao.findByProductId(productId);
-        if (productDetail == null)
+        if (productDetail == null) {
             productDetail = new ProductDetail();
-
+        }
         productDetail.setBreakUp(goods.getBreakUp());
         productDetail.setProduceDate(goods.getProduceDate());
         productDetail.setProductId(productId);
@@ -1437,8 +1434,9 @@ public class ProductServiceImpl implements ProductService {
         productDetail.setMinPackQty(goods.getMinPackQty());
         productDetail.setPackaging(goods.getPackaging());
 
-        if (enterpriseService.getCurrencyByRegisterAddress().getData() == null)
+        if (enterpriseService.getCurrencyByRegisterAddress().getData() == null) {
             throw new IllegalOperatorException("注册地址为空,无法确认币别信息,请前往-优软云补充注册地址信息");
+        }
         String currency = enterpriseService.getCurrencyByRegisterAddress().getData().toString();
         List<GoodsQtyPrice> qtyPrice = FastjsonUtils.fromJsonArray(goods.getQtyPrice(), GoodsQtyPrice.class);
         if (!CollectionUtils.isEmpty(qtyPrice)) {

+ 9 - 1
src/main/java/com/uas/platform/b2c/trade/order/dao/OrderDetailDao.java

@@ -1,7 +1,6 @@
 package com.uas.platform.b2c.trade.order.dao;
 
 import com.uas.platform.b2c.trade.order.model.OrderDetail;
-import net.jpountz.util.Native;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.jpa.repository.Query;
@@ -75,4 +74,13 @@ public interface OrderDetailDao extends JpaSpecificationExecutor<OrderDetail>, J
 	 */
 	@Query(nativeQuery = true,value = "select sum(detail_number) from trade$order_detail where go_batch  = :batches and detail_status in (501, 504, 502, 406, 404, 520, 405, 503, 514, 506)")
 	Double getPCBSaleQtyByBatchcodesAndStatus(@Param("batches") String batches);
+
+
+	/**
+	 * 订单明细
+	 * @param ids 订单主键
+	 * @return
+	 */
+	@Query(value = "select o from OrderDetail o where o.productId in (:ids)")
+	List<OrderDetail> getOrderDetailByProductids(@Param("ids") List<Long> ids);
 }

+ 1 - 1
src/main/java/com/uas/platform/b2c/trade/order/model/OrderDetail.java

@@ -38,7 +38,7 @@ import java.util.Objects;
  */
 @Entity
 @Table(name = "trade$order_detail")
-public class OrderDetail extends Document implements Serializable{
+public class OrderDetail extends Document implements Serializable {
 
 	/**
 	  * 

+ 7 - 0
src/main/java/com/uas/platform/b2c/trade/order/service/OrderDetailService.java

@@ -130,4 +130,11 @@ public interface OrderDetailService {
 	 * @return
 	 */
 	List<OrderDetail> findOrderDetailByBatchCode(String batchcode);
+
+	/**
+	 * 根据物料id获取订单明细
+	 * @param ids;
+	 * @return  List<OrderDetail>
+	 */
+	List<OrderDetail> findOrderDetailByProductids(List<Long> ids);
 }

+ 18 - 1
src/main/java/com/uas/platform/b2c/trade/order/service/impl/OrderDetailServiceImpl.java

@@ -527,7 +527,7 @@ public class OrderDetailServiceImpl implements OrderDetailService {
 		List<OrderDetail> orderDetailList = orderDetailDao.findByBatchCodeAndStatus(goods.getBatchCode(), Status.TOBECONFIRMED.value());
 		if(goods.getStatus().intValue() == Status.UNAVAILABLE.value() || goods.getStatus().intValue() == Status.REMOVED.value() || goods.getStatus().intValue() == Status.GOODS_DELETE.value() || goods.getStatus().intValue() == Status.NO_SHELVE.value()) {
 			return deleteOrderDetailsByGoods(orderDetailList);
-		}else {
+		} else {
 			List<Order> orders = new ArrayList<>();
 			for (OrderDetail detail : orderDetailList) {
 				Order order = updateOrderDetail(goods, detail);
@@ -602,4 +602,21 @@ public class OrderDetailServiceImpl implements OrderDetailService {
 		List<OrderDetail> details = orderDetailDao.findByBatchCode(batchcode);
 		return details;
 	}
+
+
+	/**
+	 * 根据物料id获取订单明细
+	 *
+	 * @param ids ;
+	 * @return List<OrderDetail>
+	 */
+	@Override
+	public List<OrderDetail> findOrderDetailByProductids(List<Long> ids) {
+		if (CollectionUtils.isEmpty(ids)) {
+			return new ArrayList<>();
+		} else {
+			List<OrderDetail> orderDetails = orderDetailDao.getOrderDetailByProductids(ids);
+			return orderDetails;
+		}
+	}
 }