Ver Fonte

促销活动更新索引

zhijiezhao há 3 anos atrás
pai
commit
ae6f6948e6

+ 2 - 0
src/main/java/com/caimei/modules/product/dao/CmPromotionDao.java

@@ -95,4 +95,6 @@ public interface CmPromotionDao extends CrudDao<CmPromotion> {
     CmPromotion findPromotionsByProductId(@Param("productId") Integer productId);
 
     Double getPresentPriceById(@Param("productId") Integer productId);
+    List<Integer> findProductIdByPromotion(String promotionsId);
+    List<Integer> findProductIdsByPromotion(String promotionsId);
 }

+ 49 - 27
src/main/java/com/caimei/modules/product/web/CmPromotionController.java

@@ -2,7 +2,9 @@ package com.caimei.modules.product.web;
 
 import com.caimei.modules.bulkpurchase.entity.PurchaseProduct;
 import com.caimei.modules.cibe.entity.Shop;
+import com.caimei.modules.opensearch.CoreServiceUitls;
 import com.caimei.modules.opensearch.GenerateUtils;
+import com.caimei.modules.product.dao.CmPromotionDao;
 import com.caimei.modules.product.entity.CmPromotion;
 import com.caimei.modules.product.entity.Product;
 import com.caimei.modules.product.service.CmPromotionService;
@@ -45,6 +47,11 @@ public class CmPromotionController extends BaseController {
     private RedisService redisService;
     @Resource
     private GenerateUtils generateUtils;
+    @Autowired
+    private CoreServiceUitls coreServiceUitls;
+    @Autowired
+    private CmPromotionDao cmPromotionDao;
+
     @ModelAttribute
     public CmPromotion get(@RequestParam(required = false) String id) {
         CmPromotion entity = null;
@@ -55,7 +62,7 @@ public class CmPromotionController extends BaseController {
                 if ("0".equals(entity.getDelFlag1())) {
                     if ("1".equals(entity.getStatus())) {
                         entity.setDelFlag1("2");
-                    }else {
+                    } else {
                         Date now = new Date();
                         if (null != entity.getBeginTime() && null != entity.getEndTime()) {
                             boolean effectiveDate = DateUtils.isEffectiveDate(now, entity.getBeginTime(), entity.getEndTime());
@@ -66,11 +73,11 @@ public class CmPromotionController extends BaseController {
                             } else {
                                 entity.setDelFlag1("3");
                             }
-                        }else {
+                        } else {
                             entity.setDelFlag1("0");
                         }
                     }
-                }else {
+                } else {
                     //关闭或删除
                     entity.setDelFlag1("0");
                 }
@@ -78,7 +85,7 @@ public class CmPromotionController extends BaseController {
                     entity.setTouchPrice1(entity.getTouchPrice());
                 } else if ("2".equals(entity.getMode())) {
                     entity.setTouchPrice2(entity.getTouchPrice());
-                }else if ("3".equals(entity.getMode())) {
+                } else if ("3".equals(entity.getMode())) {
                     entity.setTouchPrice3(entity.getTouchPrice());
                 }
             }
@@ -136,7 +143,7 @@ public class CmPromotionController extends BaseController {
                             //未开始
                             cmPromotion.setDelFlag1("1");
                         }
-                    }else {
+                    } else {
                         cmPromotion.setDelFlag1("0");
                     }
                 }
@@ -154,7 +161,7 @@ public class CmPromotionController extends BaseController {
                             promotionProducts.add(product);
                         }
                     }
-                }else {
+                } else {
                     Product product = productService.get(cmPromotion.getProductIds());
                     promotionProducts.add(product);
                 }
@@ -168,7 +175,7 @@ public class CmPromotionController extends BaseController {
                             giftProducts.add(product);
                         }
                     }
-                }else {
+                } else {
                     Product product = productService.get(cmPromotion.getGiftIds());
                     giftProducts.add(product);
                 }
@@ -182,7 +189,7 @@ public class CmPromotionController extends BaseController {
                             promotionShops.add(shop);
                         }
                     }
-                }else {
+                } else {
                     Shop shop = cmPromotionService.findShop(cmPromotion.getShopIds());
                     promotionShops.add(shop);
                 }
@@ -195,14 +202,14 @@ public class CmPromotionController extends BaseController {
                 //非店铺促销下添加促销商品
                 if (!"3".equals(cmPromotion.getType())) {
                     //单品促销时,若添加了新商品或者删除了商品,则不需要从数据库中查询促销商品
-                    if (!"0".equals(cmPromotion.getSearchDbFlag())){
+                    if (!"0".equals(cmPromotion.getSearchDbFlag())) {
                         if (StringUtil.isNotBlank(delProductIds)) {
                             List<Product> promotionProduct = cmPromotionService.findPromotionProduct(cmPromotion);
                             List<String> delProductIdList = new ArrayList<>();
                             if (delProductIds.contains(",")) {
                                 String[] split = delProductIds.split(",");
                                 delProductIdList = Arrays.asList(split);
-                            }else {
+                            } else {
                                 delProductIdList.add(delProductIds);
                             }
                             ListIterator<Product> productListIterator = promotionProduct.listIterator();
@@ -219,19 +226,19 @@ public class CmPromotionController extends BaseController {
                     } else if (StringUtils.isBlank(cmPromotion.getDelProductIds())) {
                         //将原有的促销商品id加入delProductIds,使得添加促销商品时能够找到
                         List<Product> promotionProduct = cmPromotionService.findPromotionProduct(cmPromotion);
-                        cmPromotion.setDelProductIds(promotionProduct.size()>0?promotionProduct.get(0).getProductID().toString():"");
+                        cmPromotion.setDelProductIds(promotionProduct.size() > 0 ? promotionProduct.get(0).getProductID().toString() : "");
                     }
                     //店铺促销
-                }else if ("3".equals(cmPromotion.getType())){
+                } else if ("3".equals(cmPromotion.getType())) {
                     //店铺促销时,若添加了新店铺或者删除了店铺,则不需要从数据库中查询促销店铺
-                    if (!"0".equals(cmPromotion.getSearchDbFlag())){
+                    if (!"0".equals(cmPromotion.getSearchDbFlag())) {
                         if (StringUtil.isNotBlank(delShopIds)) {
                             List<Shop> promotionShop = cmPromotionService.findPromotionShops(cmPromotion);
                             List<String> delShopIdList = new ArrayList<>();
                             if (delShopIds.contains(",")) {
                                 String[] split = delShopIds.split(",");
                                 delShopIdList = Arrays.asList(split);
-                            }else {
+                            } else {
                                 delShopIdList.add(delShopIds);
                             }
                             ListIterator<Shop> shopListIterator = promotionShop.listIterator();
@@ -248,7 +255,7 @@ public class CmPromotionController extends BaseController {
                     } else if (StringUtils.isBlank(cmPromotion.getDelShopIds())) {
                         //将原有的促销店铺id加入delProductIds,使得添加促销店铺时能够找到
                         List<Shop> promotionShop = cmPromotionService.findPromotionShops(cmPromotion);
-                        cmPromotion.setDelShopIds(promotionShop.size()>0?promotionShop.get(0).getShopID().toString():"");
+                        cmPromotion.setDelShopIds(promotionShop.size() > 0 ? promotionShop.get(0).getShopID().toString() : "");
                     }
 
                 }
@@ -259,7 +266,7 @@ public class CmPromotionController extends BaseController {
                         if (delGiftIds.contains(",")) {
                             String[] split = delGiftIds.split(",");
                             delGiftIdList = Arrays.asList(split);
-                        }else {
+                        } else {
                             delGiftIdList.add(delGiftIds);
                         }
                         ListIterator<Product> giftListIterator = giftProduct.listIterator();
@@ -300,7 +307,7 @@ public class CmPromotionController extends BaseController {
                 return "modules/product-new/singlePromotionForm";
             } else if ("2".equals(cmPromotion.getType())) {
                 return "modules/product-new/multiPromotionForm";
-            }else {
+            } else {
                 return "modules/product-new/shopPromotionForm";
             }
         } else {
@@ -315,7 +322,7 @@ public class CmPromotionController extends BaseController {
     @RequiresPermissions("product:product:view")
     @RequestMapping(value = "updateStatus")
     @ResponseBody
-    public Map<String, Object> updateStatus( String id,String flag, RedirectAttributes redirectAttributes) {
+    public Map<String, Object> updateStatus(String id, String flag, RedirectAttributes redirectAttributes) {
         Map<String, Object> map = new HashMap<>();
         if (StringUtils.isNotBlank(id)) {
             CmPromotion cmPromotion = cmPromotionService.get(id);
@@ -329,7 +336,7 @@ public class CmPromotionController extends BaseController {
                 //关闭活动
                 map.put("msg", "关闭成功");
                 cmPromotion.setDelFlag1("1");
-            } else if("open".equals(flag)){
+            } else if ("open".equals(flag)) {
                 //开启活动
                 map.put("msg", "开启成功");
                 if ("1".equals(cmPromotion.getStatus())) {
@@ -337,19 +344,20 @@ public class CmPromotionController extends BaseController {
                 } else {
                     if (cmPromotion.getBeginTime() != null && cmPromotion.getEndTime() != null) {
                         cmPromotion.setDelFlag1("0");
-                    }else {
+                    } else {
                         map.put("success", false);
                         map.put("msg", "促销活动开始时间和结束时间不能为空,请编辑该活动");
                         return map;
                     }
                 }
-            }else if ("delete".equals(flag)){
+            } else if ("delete".equals(flag)) {
                 //删除活动
                 map.put("msg", "删除成功");
                 cmPromotion.setDelFlag1("2");
             }
             cmPromotion.setUpdateTime(new Date());
             cmPromotionService.update(cmPromotion);
+            updateIndex(cmPromotion);
             map.put("success", true);
         } else {
             map.put("success", false);
@@ -358,6 +366,19 @@ public class CmPromotionController extends BaseController {
         cleanRedisCache();
         return map;
     }
+    //更新促销索引
+    private void updateIndex(CmPromotion cmPromotion) {
+        List<Integer> productIdsByPromotion = new ArrayList<>();
+        List<Integer> productIdByPromotion = new ArrayList<>();
+        if (cmPromotion.getType().equals("3")) {
+            productIdsByPromotion = cmPromotionDao.findProductIdsByPromotion(cmPromotion.getId());
+        } else {
+            productIdByPromotion = cmPromotionDao.findProductIdByPromotion(cmPromotion.getId());
+        }
+        // 更新索引
+        productIdsByPromotion.forEach(p->coreServiceUitls.updateProductIndex(p));
+        productIdByPromotion.forEach(p->coreServiceUitls.updateProductIndex(p));
+    }
 
     /**
      * 添加商品/赠品数据
@@ -372,7 +393,7 @@ public class CmPromotionController extends BaseController {
         model.addAttribute("delProductIds", product.getDelProductIds());
         if ("1".equals(product.getPromotionType())) {
             return "modules/product-new/promotionSelectSingleProduct";
-        }else {
+        } else {
             return "modules/product-new/promotionSelectProducts";
         }
     }
@@ -399,14 +420,14 @@ public class CmPromotionController extends BaseController {
         if ("1".equals(cmPromotion.getMode())) {
             cmPromotion.setTouchPrice(cmPromotion.getTouchPrice1());
             cmPromotion.setDiscount("1");
-        }else if ("2".equals(cmPromotion.getMode())) {
+        } else if ("2".equals(cmPromotion.getMode())) {
             cmPromotion.setTouchPrice(cmPromotion.getTouchPrice2());
-        }else if ("3".equals(cmPromotion.getMode())) {
+        } else if ("3".equals(cmPromotion.getMode())) {
             cmPromotion.setTouchPrice(cmPromotion.getTouchPrice3());
         }
         if ("close".equals(cmPromotion.getDelFlag1())) {
             cmPromotion.setDelFlag1("1");
-        }else if (cmPromotion.getDelFlag1() == null || "undefined".equals(cmPromotion.getDelFlag1())){
+        } else if (cmPromotion.getDelFlag1() == null || "undefined".equals(cmPromotion.getDelFlag1())) {
             if ("1".equals(cmPromotion.getStatus())) {
                 //永久开启
                 cmPromotion.setDelFlag1("0");
@@ -414,7 +435,7 @@ public class CmPromotionController extends BaseController {
                 //区间有效判断起止时间是否为空
                 if (null != cmPromotion.getBeginTime() && null != cmPromotion.getEndTime()) {
                     cmPromotion.setDelFlag1("0");
-                }else {
+                } else {
                     cmPromotion.setDelFlag1("1");
                 }
             }
@@ -425,9 +446,10 @@ public class CmPromotionController extends BaseController {
             return form(cmPromotion, model);
         }
         cmPromotionService.save(cmPromotion);
+        updateIndex(cmPromotion);
         addMessage(redirectAttributes, "保存促销列表成功");
         cleanRedisCache();
-        return "redirect:" + Global.getAdminPath() + "/product/cmPromotions/?type="+cmPromotion.getType();
+        return "redirect:" + Global.getAdminPath() + "/product/cmPromotions/?type=" + cmPromotion.getType();
     }
 
     /**

+ 10 - 0
src/main/resources/mappings/modules/product/CmPromotionMapper.xml

@@ -416,4 +416,14 @@
         from product
         where productID = #{productId}
     </select>
+    <select id="findProductIdByPromotion" resultType="java.lang.Integer">
+        select productId
+        from cm_promotions_product
+        where promotionsId = #{promotionsId}
+    </select>
+    <select id="findProductIdsByPromotion" resultType="java.lang.Integer">
+        select productID
+        from product
+        where shopID in (select supplierId from cm_promotions_product where promotionsId = #{promotionsId})
+    </select>
 </mapper>