Browse Source

批量上架的速度优化

yujia 8 years ago
parent
commit
a7ac39b5cc

+ 40 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/constant/GoodStatusEnum.java

@@ -0,0 +1,40 @@
+package com.uas.platform.b2c.prod.commodity.constant;
+
+/**
+ * goods 的一些状态
+ *
+ * Created by yujia on 2018/1/20.
+ */
+public enum GoodStatusEnum {
+
+    discard("612-614", "废弃"),
+
+    IN_USE("601-602-613", "正在使用");
+
+    private String status;
+
+    private String message;
+
+    GoodStatusEnum(String status, String message) {
+        this.status = status;
+        this.message = message;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public GoodStatusEnum setStatus(String status) {
+        this.status = status;
+        return this;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public GoodStatusEnum setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+}

+ 10 - 3
src/main/java/com/uas/platform/b2c/prod/commodity/dao/GoodsDao.java

@@ -570,11 +570,18 @@ public interface GoodsDao extends JpaSpecificationExecutor<Goods>, JpaRepository
      * @param tag
      * @return
      */
-    @Query(value = "select g from Goods g where" +
-            " g.productid =:productid and g.tag " +
-            "=:tag")
+    @Query(value = "select g from Goods g where g.productid =:productid and g.tag =:tag")
     List<Goods> findGoodsByProductidAndTag(@Param("productid") Long productid, @Param("tag") String tag);
 
+    /**
+     * 根据productid 获取已经存在标签
+     *
+     * @param productid
+     * @return
+     */
+    @Query(value = "select g.tag from Goods g where g.productid =:productid")
+    List<String> findTagByProductid(@Param("productid") Long productid);
+
     /**
      *
      * @param length 长度

+ 1 - 1
src/main/java/com/uas/platform/b2c/prod/commodity/dao/GoodsHistoryDao.java

@@ -48,7 +48,7 @@ public interface GoodsHistoryDao extends JpaSpecificationExecutor<GoodsHistory>,
      * @param batchCode the batch code 批次号
      * @return the goods history
      */
-    @Query(value = "select g from GoodsHistory g where g.batchCode = :batchCode and g.id = (select max(c.id) from GoodsHistory c where c.batchCode = :batchCode)")
+    @Query(value = "select * from trade$goods_history where go_batchcode = :batchCode order by log_operate_date desc LIMIT 0, 1", nativeQuery = true)
 	GoodsHistory findNewByBatchCode(@Param("batchCode") String batchCode);
 
     /**

+ 10 - 2
src/main/java/com/uas/platform/b2c/prod/commodity/dao/ProductDao.java

@@ -123,7 +123,7 @@ public interface ProductDao extends JpaSpecificationExecutor<Product>, JpaReposi
      * @param sourceapp 来源
      * @return
      */
-    @Query(value = "select p from Product p where p.enUU = :enuu and p.sourceApp = :sourceapp and (p.batchCount = 0 or p.batchCount is null) and (p.erpReserve is not null and p.erpReserve <> 0) and p.b2cEnabled = 1")
+    @Query(value = "select p from Product p where p.enUU = :enuu and p.sourceApp = :sourceapp and (p.batchCount = 0 or p.batchCount is null) and (p.erpReserve is not null and p.erpReserve <> 0)")
     List<Product> findByEnUUAndSourceAppNotPutOn(@Param("enuu") Long enuu, @Param("sourceapp") String sourceapp);
 
     /**
@@ -133,7 +133,7 @@ public interface ProductDao extends JpaSpecificationExecutor<Product>, JpaReposi
      * @param sourceapp 来源
      * @return
      */
-    @Query(value = "select count(1) from Product p where p.enUU = :enuu and p.sourceApp = :sourceapp and (p.batchCount = 0 or p.batchCount is null) and p.b2cEnabled = 1")
+    @Query(value = "select count(1) from Product p where p.enUU = :enuu and p.sourceApp = :sourceapp and (p.batchCount = 0 or p.batchCount is null)")
     Integer findCountByEnUUAndSourceAppNotPutOn(@Param("enuu") Long enuu, @Param("sourceapp") String sourceapp);
 
     /**
@@ -157,4 +157,12 @@ public interface ProductDao extends JpaSpecificationExecutor<Product>, JpaReposi
     @Query(value = "select p from Product p where p.enUU = :enuu and p.brand = :brand and p.cmpCode = :code and p.standard = :standard")
     List<Product> findMatchStandard(@Param("enuu") Long enuu, @Param("brand") String brand, @Param("code") String code, @Param("standard") Integer standard);
 
+    /**
+     * 根据数组查询 products 数组
+     * @param ids
+     * @return
+     */
+    @Query(value = "select p from Product p where p.id in (:ids) order by p.id desc")
+    List<Product> findProductInIds(@Param("ids") List<Long> ids);
+
 }

+ 14 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/dao/ProductDetailDao.java

@@ -3,6 +3,10 @@ package com.uas.platform.b2c.prod.commodity.dao;
 import com.uas.platform.b2c.prod.commodity.model.ProductDetail;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
 
 /**
  * Created by wangyc on 2017/10/28.
@@ -17,4 +21,14 @@ public interface ProductDetailDao extends JpaRepository<ProductDetail, Long>, Jp
      * @return
      */
     public ProductDetail findByProductId(Long productId);
+
+
+    /**
+     * 根据productids  找 ProductDetail
+     *
+     * @param pids productids
+     * @return
+     */
+    @Query(value = "select d from ProductDetail d where d.productId in (:pids) order by d.productId desc")
+    List<ProductDetail>  findByProductIds(@Param("pids") List<Long> pids);
 }

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

@@ -293,6 +293,16 @@ public interface GoodsService {
      */
     void publishByBatch(List<Goods> goodses, Set<String> uuids, List<Goods> list);
 
+
+    /**
+     * 批量保存ERP库存信息
+     *
+     * @param goodses the goodses goodes信息
+     * @param uuids   the uuids 器件uuids
+     * @param list    the list
+     */
+    void publishERPProductByBatch(List<Goods> goodses, Set<String> uuids, List<Goods> list);
+
     /**
      * 更新库存的信息,加上事务
      *

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

@@ -655,6 +655,51 @@ public class GoodsServiceImpl implements GoodsService {
         }
     }
 
+
+    /**
+     * 设置上架商品基本属性
+     *
+     * @param goods
+     */
+    private void batchSetGoodsDefaultFromErp(Goods goods, Goods baseGoods) {
+        if (StringUtils.isEmpty(goods.getBatchCode())) {
+            String batchCode = EncodingRulesConstant.BATCH.replace("_TIMESTAP_NUMBER",
+                    createNumberService.getTimeNumber("product$goods", 8));
+            goods.setBatchCode(batchCode);
+        }
+        goods.setCreatedDate(new Date());
+        goods.setUpdateDate(goods.getCreatedDate());
+        goods.setReserve(goods);
+        goods.setPerQty();
+        goods.setEnUU(baseGoods.getEnUU());
+        goods.setEnterpriseName(baseGoods.getEnterpriseName());
+        goods.setPublisherName(baseGoods.getPublisherName());
+        goods.setPublisherUU(baseGoods.getPublisherUU());
+        goods.setPublishPhone(baseGoods.getPublishPhone());
+        if (NumberUtil.compare(goods.getReserve(), goods.getMinBuyQty()) == -1) {
+            goods.setStatus(Status.UNAVAILABLE.value());
+        } else {
+            goods.setStatus(Status.AVAILABLE.value());
+        }
+        goods.setB2cMaxDelivery(baseGoods.getB2cMaxDelivery());
+        goods.setB2cMinDelivery(baseGoods.getB2cMinDelivery());
+        // 设置默认税率
+        goods.setTaxRate();
+
+        if (goods.getReturnInWeek() == null) {
+            // 若无设置,默认不支持7天无理由退货
+            goods.setReturnInWeek((short) 0);
+        }
+        // 计算未税价格
+        goods.setWithOutTaxRMBPrice();
+        goods.setWithOutTaxUSDPrice();
+        // 计算本批最小价格
+        goods.setMinPriceRMB(GoodsUtil.getMinPriceRMB(goods.getPrices()));
+        goods.setMaxPriceRMB(GoodsUtil.getMaxPriceRMB(goods.getPrices()));
+        goods.setMinPriceUSD(GoodsUtil.getMinPriceUSD(goods.getPrices()));
+        goods.setMaxPriceUSD(GoodsUtil.getMaxPriceUSD(goods.getPrices()));
+    }
+
     @Override
     @Transactional
     public Goods publish(Goods goods, String deviceInfo) {
@@ -741,6 +786,39 @@ public class GoodsServiceImpl implements GoodsService {
         System.out.println("goods保存到数据库" + (timeNow.getTime() - time.getTime()));
     }
 
+
+    /**
+     * 批量保存ERP库存信息, 区别与商城自有的批量上架
+     *
+     * @param goodses the goodses goodes信息
+     * @param uuids   the uuids 器件uuids
+     * @param list    the list
+     */
+    @Override
+    public void publishERPProductByBatch(List<Goods> goodses, Set<String> uuids, List<Goods> list) {
+        Date time = new Date();
+        List<GoodsHistory> goodsHistorys = new ArrayList<GoodsHistory>();
+        Goods goods1 = goodses.get(0);
+        setGoodsDefault(goods1);
+        for (Goods goods : goodses) {
+            batchSetGoodsDefaultFromErp(goods, goods1);
+            ResultMap resultMap = checkGoods(goods);
+            if (resultMap.getCode() != CodeType.OK.code()) {
+                continue;
+            }
+            GoodsHistory g = goodsHistoryService.converTGoodsHist(goods, GoodsHistory.OperateType.Publish.getPhrase());
+            goodsHistorys.add(g);
+            list.add(goods);
+            if (goods.getUuid() != null) {
+                uuids.add(goods.getUuid());
+            }
+        }
+        goodsDao.save(goodses);
+        goodsHistoryDao.save(goodsHistorys);
+        Date timeNow = new Date();
+        System.out.println("goods保存到数据库" + (timeNow.getTime() - time.getTime()));
+    }
+
     /**
      * 验证分段的数据是否正确。
      *

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

@@ -62,6 +62,7 @@ import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.StatementCallback;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
@@ -161,6 +162,9 @@ public class ProductServiceImpl implements ProductService {
     @Autowired
     private StoreInService storeInService;
 
+    @Autowired
+    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+
     @Autowired
     private UASBatchPutOnPropertyService uasBatchPutOnPropertyService;
 
@@ -1226,18 +1230,27 @@ public class ProductServiceImpl implements ProductService {
 //            List<Goods> goodses = productDetailPutOn(sourceAppNotPutOn, storeName, storeid, currency, property);
 //            goodsList.addAll(goodses);
 //        }
-
-        List<Product> sourceAppNotPutOn = productDao.findByEnUUAndSourceAppNotPutOn(uu, StringConstant.ERP);
-        if (sourceAppNotPutOn.size() == 0) {
+        String sql = "select pr_id from products where pr_batchcount = 0 and pr_sourceapp = 'ERP' and pr_reserve <> 0 and pr_reserve is not null and pr_b2cenabled = 1 and pr_enuu = (:enuu) and pr_id not in (select DISTINCT go_productid from trade$goods where  go_productid is not null);";
+        Map<String, Long> map = new HashMap<>();
+        map.put("enuu", uu);
+        List<Long> longList = namedParameterJdbcTemplate.queryForList(sql, map, Long.class);
+         if (longList.size() == 0) {
             return new ResultMap(CodeType.OK.code(), "没有可上架的信息", null);
         }
+        List<Product> sourceAppNotPutOn = productDao.findProductInIds(longList);
+        List<ProductDetail> productDetails = productDetailDao.findByProductIds(longList);
+        for (Product product : sourceAppNotPutOn) {
+            for (ProductDetail detail : productDetails) {
+                if (product.getId().longValue() == detail.getProductId().longValue()) {
+                    product.setProductDetail(detail);
+                    break;
+                }
+            }
+        }
         List<Goods> goodses = productDetailPutOn(sourceAppNotPutOn, storeName, storeid, currency, property);
         goodsList.addAll(goodses);
         List<Goods> result = new ArrayList<Goods>();
-        goodsService.publishByBatch(goodsList, uuids, result);
-        for (String uuid : uuids) {
-            goodsService.updateComponentTradeInfos(uuid);
-        }
+        goodsService.publishERPProductByBatch(goodsList, uuids, result);
         List<Product> list = new ArrayList<>();
         for (Goods goods : result) {
             for (Product product : sourceAppNotPutOn) {
@@ -1248,6 +1261,9 @@ public class ProductServiceImpl implements ProductService {
             }
         }
         productDao.save(list);
+        for (String uuid : uuids) {
+            goodsService.updateComponentTradeInfos(uuid);
+        }
         String message = "";
         if (result.size() != 0) {
             message = "成功上架" + result.size() + "个";
@@ -1262,11 +1278,20 @@ public class ProductServiceImpl implements ProductService {
     }
 
 
+    /**
+     * 产品信息转  库存信息
+     *
+     * @param products 上架的产品
+     * @param storeName 店铺名称
+     * @param storeid 店铺id
+     * @param currency 币别信息
+     * @param property 批量上架的配置信息
+     * @return
+     */
     private List<Goods> productDetailPutOn(List<Product> products, String storeName, String storeid, String currency, UASBatchPutOnProperty property) {
         List<Goods> goodses = new ArrayList<>();
         for (Product product : products) {
-            ProductDetail productDetail = productDetailDao.findByProductId(product.getId());
-            ResultMap resultMap = productsConvertGoods(product, productDetail, storeName, storeid, currency, property);
+            ResultMap resultMap = productsConvertGoods(product, storeName, storeid, currency, property);
             if (resultMap.getCode() == CodeType.OK.code()) {
                 goodses.add((Goods) resultMap.getData());
             }
@@ -1278,10 +1303,10 @@ public class ProductServiceImpl implements ProductService {
      * 物料转 goods 信息
      *
      * @param product
-     * @param detail
      * @return
      */
-    private ResultMap productsConvertGoods(Product product, ProductDetail detail, String storeName, String storeid, String currency, UASBatchPutOnProperty property) {
+    private ResultMap productsConvertGoods(Product product, String storeName, String storeid, String currency, UASBatchPutOnProperty property) {
+        ProductDetail detail = product.getProductDetail();
         Goods g = new Goods();
         g.setProductid(product.getId());
         g.setUnit(product.getUnit());
@@ -1300,45 +1325,47 @@ public class ProductServiceImpl implements ProductService {
         g.setMaxDelivery(property.getMaxDelivery());
         g.setMinDelivery(property.getMinDelivery());
         g.setProdNum(product.getProdNum());
-        g.setTag(StringConstant.INIT_TAG);
+        List<String> tags = goodsDao.findTagByProductid(product.getId());
+        Integer i = 1;
+        String tag = StringConstant.SELF_TAG;
+        while(tags.contains(tag + i)) {
+            i++;
+        }
+        g.setTag(tag + i);
         g.setBreakUp(Boolean.FALSE);
         g.setReserve(product.getErpReserve());
+        String packaging = (detail == null || detail.getPackaging() == null) ? product.getPackaging(): detail.getPackaging();
+        packaging =  (packaging == null) ? StringConstant.DEFAULTPACKAGING  : packaging;
+        g.setPackaging(packaging);
 
-        g.setPackaging((detail == null || detail.getPackaging() == null) ? product.getPackaging(): detail.getPackaging());
-        if (g.getPackaging() == null) {
-            g.setPackaging(StringConstant.DEFAULTPACKAGING);
-        }
-
-
-        g.setProduceDate((detail == null || detail.getProduceDate() == null) ? product.getProduceDate() : detail.getProduceDate());
-        if (g.getProduceDate() == null) {
-            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
-            String format = formatter.format(new Date());
+        String produceDate = (detail == null || detail.getProduceDate() == null) ? product.getProduceDate() : detail.getProduceDate();
+        if (produceDate == null) {
+            String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
             g.setProduceDate(format);
+        } else {
+            g.setProduceDate(produceDate);
         }
 
-
-        g.setMinPackQty((detail == null || detail.getMinPackQty() == null) ? product.getMinPackQty() : detail.getMinPackQty());
-        g.setMinBuyQty(detail != null ? detail.getMinBuyQty() : null);
-        int minPackQty = NumberUtil.compare(g.getMinPackQty(), DoubleConstant.zero);
-        int minBuyQty = NumberUtil.compare(g.getMinBuyQty(), DoubleConstant.zero);
-        if (minPackQty == 0 && minBuyQty == 0) {
+        Double minPackQty = (detail == null || detail.getMinPackQty() == null) ? product.getMinPackQty() : detail.getMinPackQty();
+        Double minBuyQty = detail != null ? detail.getMinBuyQty() : null;
+        int minPackQtyCom = NumberUtil.compare(minPackQty, DoubleConstant.zero);
+        int minBuyQtyCom = NumberUtil.compare(minBuyQty, DoubleConstant.zero);
+        if (minPackQtyCom == 0 && minBuyQtyCom == 0) {
             return new ResultMap(CodeType.NOT_COMPLETE_INFO, "上架必填信息缺失");
         } else {
-            if (minPackQty == 0 || minBuyQty == 0) {
-                Double d = minBuyQty == 0 ? g.getMinPackQty() : g.getMinBuyQty();
-                g.setMinBuyQty(d);
-                g.setMinPackQty(d);
-            }
-            double remainder = g.getMinBuyQty() % g.getMinPackQty();
-            if (NumberUtil.compare(remainder, DoubleConstant.zero) != 0) {
-                g.setMinBuyQty(NumberUtil.sub(g.getMinBuyQty(), remainder));
+            if (minPackQtyCom == 0 || minBuyQtyCom == 0) {
+                minPackQty = minBuyQtyCom == 0 ? minPackQty : minBuyQty;
+                minBuyQty = minPackQty;
             }
-            int k = NumberUtil.compare(g.getMinBuyQty(), g.getMinPackQty());
+            double remainder = minBuyQty % minPackQty;
+            minBuyQty = NumberUtil.sub(minBuyQty, remainder);
+            int k = NumberUtil.compare(minBuyQty, minPackQty);
             if (k < 0) {
-                g.setMinBuyQty(g.getMinPackQty());
+                minBuyQty = minPackQty;
             }
         }
+        g.setMinPackQty(minPackQty);
+        g.setMinBuyQty(minBuyQty);
         double price = NumberUtil.mul(detail.getPrice(), property.getFluctuateRate());
         price = NumberUtil.fractionNumCeil(price, IntegerConstant.PRICE_FRACTION);
         List<GoodsQtyPrice> list = new ArrayList<>();

+ 21 - 5
src/main/webapp/resources/js/vendor/controllers/forstore/vendor_materialCtrl.js

@@ -19,8 +19,6 @@ define([ 'app/app', 'jquery-uploadify' ], function(app) {
 
 		$scope.$$nonProduct.enterBatchPutOnPropertySaveButton = false;
 
-		$scope.uasBatchPutOnProperty = {editFluctuateRate : 1, editMaxDelivery : 1, editMinDelivery : 1};
-
 		$scope.salePrice = 0;
 
 		$scope.setSalePrice = function (price) {
@@ -208,6 +206,24 @@ define([ 'app/app', 'jquery-uploadify' ], function(app) {
 			}
 		});
 
+        /**
+         * 获取批量上架的配置信息
+         */
+        UASBatchPutOnPropertyServices.get(null, function (data) {
+            if (data) {
+                $scope.uasBatchPutOnProperty = data;
+                $scope.uasBatchPutOnProperty.editFluctuateRate = $scope.uasBatchPutOnProperty.fluctuateRate * 100;
+                $scope.uasBatchPutOnProperty.editMaxDelivery = $scope.uasBatchPutOnProperty.maxDelivery;
+                $scope.uasBatchPutOnProperty.editMinDelivery = $scope.uasBatchPutOnProperty.minDelivery;
+            } else {
+                $scope.uasBatchPutOnProperty.editFluctuateRate = 1;
+                $scope.uasBatchPutOnProperty.editMaxDelivery = 1;
+                $scope.uasBatchPutOnProperty.editMinDelivery = 1;
+            }
+        }, function (response) {
+            console.log(response);
+        });
+
 		/**
 		 * 切换标准/非标准
 		 * @param isStandard
@@ -2019,9 +2035,9 @@ define([ 'app/app', 'jquery-uploadify' ], function(app) {
 		 * 批量上架信息
 		 */
 		$scope.batchPutOn = function () {
-			var isFluctuateRateChange = $scope.uasBatchPutOnProperty.editFluctuateRate == $scope.uasBatchPutOnProperty.fluctuateRate * 100;
-			var isMinDeliveryChange = $scope.uasBatchPutOnProperty.editMinDelivery == $scope.uasBatchPutOnProperty.minDelivery;
-			var isMaxDeliveryChange = $scope.uasBatchPutOnProperty.editMaxDelivery == $scope.uasBatchPutOnProperty.maxDelivery;
+			var isFluctuateRateChange = $scope.uasBatchPutOnProperty.editFluctuateRate != $scope.uasBatchPutOnProperty.fluctuateRate * 100;
+			var isMinDeliveryChange = $scope.uasBatchPutOnProperty.editMinDelivery != $scope.uasBatchPutOnProperty.minDelivery;
+			var isMaxDeliveryChange = $scope.uasBatchPutOnProperty.editMaxDelivery != $scope.uasBatchPutOnProperty.maxDelivery;
 			if (isFluctuateRateChange || isMinDeliveryChange || isMaxDeliveryChange) {
 				toaster.pop('warning', '提示', '批量上架配置信息被修改,请保存批量上架配置信息之后再上架');
                 return ;