Browse Source

修改助力道具缓存

silent 4 years ago
parent
commit
5e11813637

+ 11 - 0
src/main/java/org/springblade/gateway/goods_gateway/constant/HelpGoodsConstant.java

@@ -0,0 +1,11 @@
+package org.springblade.gateway.goods_gateway.constant;
+
+/**
+ * @Author: Silent
+ * @Description
+ * @Date: Created in 9:54 2021/11/25
+ * @Modified By:
+ */
+public interface HelpGoodsConstant {
+	String HELP_GOODS_CACHE = "SING:HELP_GOODS:CACHE";
+}

+ 22 - 7
src/main/java/org/springblade/gateway/goods_gateway/controller/AppHelpGoodsController.java

@@ -9,17 +9,18 @@ import lombok.extern.slf4j.Slf4j;
 import org.springblade.core.log.annotation.ApiLog;
 import org.springblade.core.tool.api.R;
 import org.springblade.gateway.goods_gateway.service.AppHelpGoodsService;
+import org.springblade.gateway.goods_gateway.util.HelpGoodsUtil;
 import org.springblade.payment.cmcc.request.CmccDectOrderRequest;
 import org.springblade.payment.cmcc.request.CmccPlaceOrderRequest;
 import org.springblade.payment.cmcc.util.CmccUtil;
+import org.springblade.sing.goods.service.IHelpGoodsService;
+import org.springblade.sing.goods.vo.HelpGoodsVO;
+import org.springblade.sing.goods.wrapper.HelpGoodsWrapper;
 import org.springblade.sing.point.entity.CmccPointRecord;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.math.BigDecimal;
+import java.util.List;
 
 /**
  * @Author: Silent
@@ -33,8 +34,9 @@ import java.math.BigDecimal;
 @Api(value = "助力商场", tags = "助力商场接口")
 @Slf4j
 public class AppHelpGoodsController {
-	@Autowired
-	private AppHelpGoodsService appHelpGoodsService;
+	private final AppHelpGoodsService appHelpGoodsService;
+	private final IHelpGoodsService helpGoodsService;
+
 
 	/**
 	 * 兑换道具
@@ -120,4 +122,17 @@ public class AppHelpGoodsController {
 			return R.fail(e.getMessage());
 		}
 	}
+
+	/**
+	 * 分页 助力商城
+	 */
+	@GetMapping("/getHelpGoodsList")
+	@ApiOperationSupport(order = 2)
+	@ApiOperation(value = "获取道具助力列表", notes = "传入helpGoods")
+	public R<List<HelpGoodsVO>> getHelpGoodsList(@ApiParam(value = "活动ID",required = true) @RequestParam Long activeId,
+									  @ApiParam(value = "用户ID",required = true) @RequestParam Long userId,
+									  @ApiParam(value = "作品ID",required = true) @RequestParam Long productId) {
+		return R.data(HelpGoodsWrapper.build(userId,productId)
+			.listVO(HelpGoodsUtil.getHelpGoodsList(activeId)));
+	}
 }

+ 70 - 0
src/main/java/org/springblade/gateway/goods_gateway/util/HelpGoodsUtil.java

@@ -0,0 +1,70 @@
+package org.springblade.gateway.goods_gateway.util;
+
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springblade.common.utils.SpringContextHolder;
+import org.springblade.core.redis.cache.BladeRedis;
+import org.springblade.gateway.goods_gateway.constant.HelpGoodsConstant;
+import org.springblade.sing.goods.entity.HelpGoods;
+import org.springblade.sing.goods.service.IHelpGoodsService;
+
+import javax.validation.constraints.NotNull;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @Author: Silent
+ * @Description 助力道具工具
+ * @Date: Created in 10:02 2021/11/25
+ * @Modified By:
+ */
+public class HelpGoodsUtil {
+	private static final IHelpGoodsService helpGoodsService = SpringContextHolder.getBean(IHelpGoodsService.class);
+	private static final BladeRedis bladeRedis = SpringContextHolder.getBean(BladeRedis.class);
+
+	/**
+	 * 获取助力道具列表
+	 * @param activeId
+	 * @return
+	 */
+	public static List<HelpGoods> getHelpGoodsList(@NotNull Long activeId){
+		List<HelpGoods> list = bladeRedis.hGet(HelpGoodsConstant.HELP_GOODS_CACHE, activeId);
+		if(ObjectUtils.isEmpty(list)){
+			list = helpGoodsService.list(Wrappers.<HelpGoods>lambdaQuery()
+				.eq(HelpGoods::getActiveId, activeId));
+			//设置缓存
+			setHelpGoodsListCache(activeId,list);
+		}
+		return list;
+	}
+
+	/**
+	 * 设置助力道具缓存
+	 * @param activeId
+	 * @param helpGoodsList
+	 */
+	public static void setHelpGoodsListCache(@NotNull Long activeId,@NotNull List<HelpGoods> helpGoodsList){
+		if(ObjectUtils.isNotEmpty(helpGoodsList) && ObjectUtils.isNotEmpty(activeId) && helpGoodsList.size()!=0){
+			bladeRedis.hSet(HelpGoodsConstant.HELP_GOODS_CACHE,activeId,helpGoodsList);
+		}
+	}
+
+	/**
+	 * 删除助力道具缓存
+	 * @param activeId
+	 * @return
+	 */
+	public static Boolean removeHelpGoodsListCache(@NotNull Long activeId){
+		return bladeRedis.del(activeId.toString());
+	}
+
+	/**
+	 * 删除集合
+	 * @param activeIds
+	 * @return
+	 */
+	public static Long removeHelpGoodsListCache(Collection<Long> activeIds){
+		return bladeRedis.del(activeIds.stream().map(Object::toString).collect(Collectors.toList()));
+	}
+}

+ 17 - 9
src/main/java/org/springblade/sing/goods/controller/HelpGoodsController.java

@@ -16,25 +16,27 @@
  */
 package org.springblade.sing.goods.controller;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
-import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import lombok.AllArgsConstructor;
-import javax.validation.Valid;
-
+import org.springblade.core.boot.ctrl.BladeController;
 import org.springblade.core.mp.support.Condition;
 import org.springblade.core.mp.support.Query;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.Func;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.bind.annotation.RequestParam;
-import com.baomidou.mybatisplus.core.metadata.IPage;
+import org.springblade.gateway.goods_gateway.util.HelpGoodsUtil;
 import org.springblade.sing.goods.entity.HelpGoods;
+import org.springblade.sing.goods.service.IHelpGoodsService;
 import org.springblade.sing.goods.vo.HelpGoodsVO;
 import org.springblade.sing.goods.wrapper.HelpGoodsWrapper;
-import org.springblade.sing.goods.service.IHelpGoodsService;
-import org.springblade.core.boot.ctrl.BladeController;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 助力商城 控制器
@@ -91,6 +93,7 @@ public class HelpGoodsController extends BladeController {
 	@ApiOperationSupport(order = 4)
 	@ApiOperation(value = "新增", notes = "传入helpGoods")
 	public R save(@Valid @RequestBody HelpGoods helpGoods) {
+		HelpGoodsUtil.removeHelpGoodsListCache(helpGoods.getActiveId());
 		return R.status(helpGoodsService.save(helpGoods));
 	}
 
@@ -101,6 +104,7 @@ public class HelpGoodsController extends BladeController {
 	@ApiOperationSupport(order = 5)
 	@ApiOperation(value = "修改", notes = "传入helpGoods")
 	public R update(@Valid @RequestBody HelpGoods helpGoods) {
+		HelpGoodsUtil.removeHelpGoodsListCache(helpGoods.getActiveId());
 		return R.status(helpGoodsService.updateById(helpGoods));
 	}
 
@@ -111,6 +115,7 @@ public class HelpGoodsController extends BladeController {
 	@ApiOperationSupport(order = 6)
 	@ApiOperation(value = "新增或修改", notes = "传入helpGoods")
 	public R submit(@Valid @RequestBody HelpGoods helpGoods) {
+		HelpGoodsUtil.removeHelpGoodsListCache(helpGoods.getActiveId());
 		return R.status(helpGoodsService.saveOrUpdate(helpGoods));
 	}
 
@@ -122,7 +127,10 @@ public class HelpGoodsController extends BladeController {
 	@ApiOperationSupport(order = 7)
 	@ApiOperation(value = "逻辑删除", notes = "传入ids")
 	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
-		return R.status(helpGoodsService.deleteLogic(Func.toLongList(ids)));
+		List<Long> longs = Func.toLongList(ids);
+		List<HelpGoods> helpGoods = helpGoodsService.listByIds(longs);
+		HelpGoodsUtil.removeHelpGoodsListCache(helpGoods.stream().map(HelpGoods::getActiveId).collect(Collectors.toSet()));
+		return R.status(helpGoodsService.deleteLogic(longs));
 	}