zoujiajian 2 年 前
コミット
af313cf96c

+ 100 - 21
netflix-web/src/main/java/com/cyksj/web/controller/manage/goods/CmsGoodsDonSkuController.java

@@ -1,22 +1,26 @@
 package com.cyksj.web.controller.manage.goods;
 
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.annotation.Log;
 import com.cyksj.common.exception.BusinessRuntimeException;
+import com.cyksj.common.util.Jsons;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.BusinessType;
 import com.cyksj.enums.GatewayResponse;
+import com.cyksj.model.entity.GoodsDiscountPlusPurchaseRelation;
 import com.cyksj.model.entity.GoodsDon;
 import com.cyksj.model.entity.GoodsDonSku;
-import com.cyksj.model.entity.GoodsDonSpec;
 import com.cyksj.model.manage.request.ReqGoodsSkuInsert;
 import com.cyksj.model.request.CombinationSkuReq;
-import com.cyksj.model.views.GoodsDonSkuView;
-import com.cyksj.redis.RedisService;
 import com.cyksj.service.mange.CmsGoodsDonService;
 import com.cyksj.service.mange.CmsGoodsDonSkuService;
 import com.cyksj.service.mange.CmsGoodsDonSpecService;
+import com.cyksj.service.mange.RefreshCacheService;
 import com.ejlchina.searcher.BeanSearcher;
+import com.ejlchina.searcher.SearchResult;
+import com.ejlchina.searcher.util.MapBuilder;
 import com.ejlchina.searcher.util.MapUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -25,6 +29,7 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.math.BigDecimal;
 import java.util.List;
 import java.util.Set;
 
@@ -49,7 +54,7 @@ public class CmsGoodsDonSkuController {
 
     private final HttpServletRequest request;
 
-    private final RedisService redisService;
+    private final RefreshCacheService refreshCacheService;
 
 
     /**
@@ -59,7 +64,7 @@ public class CmsGoodsDonSkuController {
     @Log(module = "平台管理/新增商品sku&spec", businessType = BusinessType.POST, isSaveRequestData = true)
     public Result<String> skuInsert(@Validated @RequestBody ReqGoodsSkuInsert insert) throws Exception{
         goodsDonSpecService.insertOrUpdateSkuSpec(insert, false);
-
+        refreshCacheService.refreshYhHomePageCache();
         return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
@@ -68,13 +73,8 @@ public class CmsGoodsDonSkuController {
      */
     @PutMapping("/update/spec")
     public Result<String> updateGoodsSpec(@Validated @RequestBody ReqGoodsSkuInsert insert) throws Exception {
-        Long id = insert.getId();
-        Assert.notNull(id, "规格属性id不存在");
-        GoodsDonSpec spec = goodsDonSpecService.getById(id);
-        if (spec == null || !spec.getGoodsId().equals(insert.getGoodsId())) {
-            throw BusinessRuntimeException.getInstance("规格不存在");
-        }
         goodsDonSpecService.insertOrUpdateSkuSpec(insert, true);
+        refreshCacheService.refreshYhHomePageCache();
         return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
@@ -93,9 +93,8 @@ public class CmsGoodsDonSkuController {
         goodsDonSpecService.skuAppend(insert, goods);
         //给平台更新额外属性
         goodsDonService.updateGoodsSkuPrice(goods.getId(), goods);
-        Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "*");
-        redisService.del(keys.toArray(String[]::new));
-       return GatewayResponse.SUCCESS.newBuilder().toResult();
+        refreshCacheService.refreshYhHomePageCache();
+        return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
     /**
@@ -104,7 +103,10 @@ public class CmsGoodsDonSkuController {
     @Transactional(rollbackFor = Throwable.class)
     @PutMapping(value = "/update")
     @Log(module = "平台管理/编辑规格", businessType = BusinessType.PUT, isSaveRequestData = true)
-    public Result<String> skuUpdate(@RequestBody GoodsDonSku sku){
+    public Result<String> skuUpdate(@RequestBody GoodsDonSku sku) throws Exception {
+        Long skuId = sku.getId();
+        GoodsDonSku dbSku = goodsDonSkuService.getById(skuId);
+        Assert.notNull(dbSku, "规格不存在");
         Long goodsId = sku.getGoodsId();
         GoodsDon goods = goodsDonService.getById(goodsId);
         if (goods.getType() == 1) {
@@ -119,13 +121,42 @@ public class CmsGoodsDonSkuController {
         if (sku.getDays() == null) {
             sku.setDays(null);
         }
+        if (sku.getMjRelaxNum() == null) {
+            sku.setMjRelaxNum(null);
+        }
+        String specificGoodsIds = sku.getSpecificGoodsIds();
+        if (StrUtil.isEmpty(specificGoodsIds)) {
+            sku.setSpecificPrice(BigDecimal.ZERO);
+        } else {
+            Set<Long> specificGids = Jsons.parseSet(specificGoodsIds, Long.class);
+            if (specificGids.isEmpty()) {
+                sku.setSpecificPrice(BigDecimal.ZERO);
+            } else {
+                Integer count = goodsDonService.count(Wrappers.lambdaQuery(GoodsDon.class)
+                        .in(GoodsDon::getId, specificGids)
+                        .eq(GoodsDon::getStatus, true)
+                        .eq(GoodsDon::getDeleted, true));
+                if (count != specificGids.size()) {
+                    throw BusinessRuntimeException.getInstance("存在被删除或已下架的平台");
+                }
+                BigDecimal specificPrice = sku.getSpecificPrice();
+                if (specificPrice == null || specificPrice.compareTo(BigDecimal.ZERO) <= 0) {
+                    throw BusinessRuntimeException.getInstance("请输入正确的特定价格");
+                }
+            }
+        }
         //优惠加购
         goodsDonSkuService.handleDiscountPurchaseGoods(sku, sku.getDpSku());
+
+        String benefitsList = sku.getBenefitsList();
+        if (StrUtil.isNotBlank(benefitsList)) {
+            Set<GoodsDonSku> benefitsSkuIds = Jsons.parseSet(benefitsList, GoodsDonSku.class);
+            sku.setBenefitsList(Jsons.toJson(benefitsSkuIds));
+        }
         goodsDonSkuService.saveOrUpdate(sku);
         //给平台更新额外属性
         goodsDonService.updateGoodsSkuPrice(sku.getGoodsId(), null);
-        Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "*");
-        redisService.del(keys.toArray(String[]::new));
+        refreshCacheService.refreshYhHomePageCache();
         return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
@@ -142,8 +173,7 @@ public class CmsGoodsDonSkuController {
         goodsDonSkuService.handleDiscountPurchaseGoods(sku, null);
         goodsDonSkuService.removeById(skuId);
         goodsDonService.updateGoodsSkuPrice(sku.getGoodsId(), null);
-        Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "*");
-        redisService.del(keys.toArray(String[]::new));
+        refreshCacheService.refreshYhHomePageCache();
         return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
@@ -161,8 +191,57 @@ public class CmsGoodsDonSkuController {
      * 获取平台规格列表
      */
     @GetMapping("/get/list")
-    public Result<List<GoodsDonSkuView>> getGoodsSkuList() {
-        List<GoodsDonSkuView> skuViewList = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
+    public Result<SearchResult<GoodsDonSku>> getGoodsSkuList(@RequestParam(defaultValue = "0") Boolean filterPack) {
+        MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
+        if (filterPack) {
+            String condition = " spec_json not like '%套餐%'";
+            builder.put("condition", condition);
+        }
+        SearchResult<GoodsDonSku> skuViewList = beanSearcher.search(GoodsDonSku.class, builder.build());
+        skuViewList.getDataList().forEach(sku->{
+            if (sku.getIsDp()) {
+                sku.setDpSku(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelation.class, MapUtils.builder()
+                        .field(GoodsDiscountPlusPurchaseRelation::getSkuId, sku.getId())
+                        .build()));
+            }
+        });
         return GatewayResponse.SUCCESS.newBuilder().toResult(skuViewList);
     }
+
+    /**
+     * 批量上架
+     */
+    @PutMapping("/batch/update/status/{goodsId}")
+    public Result<String> batchUpdateStatus(@PathVariable Long goodsId, @RequestBody List<Long> skuIds) {
+        GoodsDon goodsDon = goodsDonService.getById(goodsId);
+        if (goodsDon == null || !goodsDon.getDeleted()) {
+            throw BusinessRuntimeException.getInstance("商品不存在");
+        }
+        boolean update = goodsDonSkuService.update(null, Wrappers.lambdaUpdate(GoodsDonSku.class)
+                .set(GoodsDonSku::getStatus, true)
+                .eq(GoodsDonSku::getGoodsId, goodsId)
+                .in(GoodsDonSku::getId, skuIds)
+                .eq(GoodsDonSku::getStatus, false));
+        if (update) {
+            goodsDonService.updateGoodsSkuPrice(goodsId, null);
+            refreshCacheService.refreshYhHomePageCache();
+        }
+        return GatewayResponse.SUCCESS.newBuilder().toResult();
+    }
+
+
+    /**
+     * 规格排序
+     */
+    @PutMapping("/sorted")
+    public Result<String> sorted(@RequestBody GoodsDonSku sku) {
+        Long id = sku.getId();
+        Integer sorted = sku.getSorted();
+        Assert.notNull(id, "请选择指定的规格");
+        goodsDonSkuService.update(null, Wrappers.lambdaUpdate(GoodsDonSku.class)
+                .eq(GoodsDonSku::getId, id)
+                .set(GoodsDonSku::getSorted, sorted));
+        refreshCacheService.refreshYhHomePageCache();
+        return GatewayResponse.SUCCESS.newBuilder().toResult();
+    }
 }