浏览代码

添加刷新活动作品缓存接口

silent 4 年之前
父节点
当前提交
777d402603

+ 16 - 16
src/main/java/org/springblade/gateway/active_gateway/controller/AppActiveProductController.java

@@ -4,29 +4,26 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.ObjectUtils;
 import org.springblade.common.utils.IPUtils;
 import org.springblade.core.boot.ctrl.BladeController;
 import org.springblade.core.log.annotation.ApiLog;
 import org.springblade.core.tool.api.R;
 import org.springblade.gateway.active_gateway.service.AppActiveProductService;
-import org.springblade.gateway.active_gateway.util.ActiveProductUtil;
 import org.springblade.sing.active.entity.ActiveHelpRecord;
-import org.springblade.sing.active.entity.ActiveProductRecord;
 import org.springblade.sing.active.entity.ActiveRecord;
 import org.springblade.sing.active.service.IActiveProductRecordService;
 import org.springblade.sing.active.service.IActiveRecordService;
+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 javax.annotation.PostConstruct;
 import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
 
 /**
  * @Author: Silent
@@ -54,16 +51,7 @@ public class AppActiveProductController extends BladeController {
 		//查询所有的活动列表
 		for (ActiveRecord activeRecord : activeRecordService.list(
 			Wrappers.<ActiveRecord>lambdaQuery().select(ActiveRecord::getId))) {
-			//查询该活动的所有作品
-			List<ActiveProductRecord> activeProductRecordList = activeProductRecordService
-				.list(Wrappers.<ActiveProductRecord>lambdaQuery()
-					.select(ActiveProductRecord::getId, ActiveProductRecord::getVoteCount)
-					.eq(ActiveProductRecord::getActiveId, activeRecord.getId())
-					.eq(ActiveProductRecord::getStatus, 1));
-			if (ObjectUtils.isNotEmpty(activeProductRecordList) && activeProductRecordList.size() != 0) {
-				Map<Object, Double> map = activeProductRecordList.stream().collect(Collectors.toMap(ele -> ele.getId(), ele -> Double.valueOf(ele.getVoteCount())));
-				ActiveProductUtil.addActiveCache(activeRecord.getId().toString(), map);
-			}
+			activeService.refreshActiveProductCache(activeRecord.getId());
 		}
 	}
 
@@ -85,4 +73,16 @@ public class AppActiveProductController extends BladeController {
 			return R.fail(e.getMessage());
 		}
 	}
+
+	/**
+	 * 作品缓存刷新
+	 */
+	@ApiLog("作品助力")
+	@PostMapping("/refreshActiveProductCache")
+	@ApiOperationSupport(order = 2)
+	@ApiOperation(value = "作品缓存刷新", notes = "传入activeId")
+	public R<String> refreshActiveProductCache(@ApiParam("活动ID") @RequestParam Long activeId) {
+		activeService.refreshActiveProductCache(activeId);
+		return R.success("刷新成功");
+	}
 }

+ 2 - 0
src/main/java/org/springblade/gateway/active_gateway/service/AppActiveProductService.java

@@ -21,4 +21,6 @@ public interface AppActiveProductService {
 	void userPuFaPointUpdate(ActiveHelpRecord activeHelpRecord, ActiveSettingDto activeSetting);
 
 	void userHelpCountCheck(ActiveHelpRecord activeHelpRecord, ActiveProductRecord activeProductRecord, LoginUser loginUser, HelpGoods helpGoods);
+
+    void refreshActiveProductCache(Long id);
 }

+ 29 - 4
src/main/java/org/springblade/gateway/active_gateway/service/impl/AppActiveProductServiceImpl.java

@@ -37,7 +37,9 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @Author: Silent
@@ -104,7 +106,7 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 		}
 
 		//判断所使用的道具是否是该活动的
-		if(!activeRecord.getId().equals(helpGoods.getActiveId())){
+		if (!activeRecord.getId().equals(helpGoods.getActiveId())) {
 			throw new AppActiveProductException("所使用的道具非该活动下的");
 		}
 
@@ -133,6 +135,7 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 
 	/**
 	 * 更新活动作品的票数和热力值
+	 *
 	 * @param activeHelpRecord
 	 * @param activeProductRecord
 	 * @param activeSetting
@@ -146,7 +149,7 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 		if (ObjectUtils.isNotEmpty(activeSetting.getVoteAndHeatRate()) && !activeSetting.getVoteAndHeatRate().equals(BigDecimal.ZERO)) {
 			//查询参赛用户
 			UserHeat userHeat = userHeatService.getOne(Wrappers.<UserHeat>lambdaQuery().eq(UserHeat::getPhone, activeProductRecord.getPhone()));
-			if(ObjectUtils.isEmpty(userHeat)){
+			if (ObjectUtils.isEmpty(userHeat)) {
 				userHeat = new UserHeat();
 				userHeat.setPhone(activeProductRecord.getPhone());
 				userHeat.setHeatValue(BigDecimal.ZERO);
@@ -181,11 +184,12 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 
 	/**
 	 * 更新用户普法积分
+	 *
 	 * @param activeHelpRecord
 	 * @param activeSetting
 	 */
 	@Override
-	public void userPuFaPointUpdate(ActiveHelpRecord activeHelpRecord,ActiveSettingDto activeSetting) {
+	public void userPuFaPointUpdate(ActiveHelpRecord activeHelpRecord, ActiveSettingDto activeSetting) {
 		//判断是否普法积分
 		if (ObjectUtils.isNotEmpty(activeSetting.getVoteAndPointRate()) && !activeSetting.getVoteAndPointRate().equals(BigDecimal.ZERO)) {
 			//修改普法积分(票数*积分比例+原有积分)
@@ -203,7 +207,7 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 
 			//修改积分
 			UserPufaPoint userPufaPoint = userPufaPointService.getOne(Wrappers.<UserPufaPoint>lambdaQuery().eq(UserPufaPoint::getPhone, activeHelpRecord.getPhone()));
-			if(ObjectUtils.isEmpty(userPufaPoint)){
+			if (ObjectUtils.isEmpty(userPufaPoint)) {
 				userPufaPoint = new UserPufaPoint();
 				userPufaPoint.setPhone(activeHelpRecord.getPhone());
 				userPufaPoint.setUserId(activeHelpRecord.getUserId());
@@ -218,6 +222,7 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 
 	/**
 	 * 判断用户使用该道具对该活动助力次数是否到达上限
+	 *
 	 * @param activeHelpRecord
 	 * @param activeProductRecord
 	 * @param loginUser
@@ -244,4 +249,24 @@ public class AppActiveProductServiceImpl implements AppActiveProductService {
 			}
 		}
 	}
+
+	/**
+	 * 刷新活动作品缓存
+	 *
+	 * @param id 活动ID
+	 */
+	@Transactional(rollbackFor = Exception.class)
+	@Override
+	public void refreshActiveProductCache(Long id) {
+		//查询该活动的所有作品
+		List<ActiveProductRecord> activeProductRecordList = activeProductRecordService
+			.list(Wrappers.<ActiveProductRecord>lambdaQuery()
+				.select(ActiveProductRecord::getId, ActiveProductRecord::getVoteCount)
+				.eq(ActiveProductRecord::getActiveId, id)
+				.eq(ActiveProductRecord::getStatus, 1));
+		if (ObjectUtils.isNotEmpty(activeProductRecordList) && activeProductRecordList.size() != 0) {
+			Map<Object, Double> map = activeProductRecordList.stream().collect(Collectors.toMap(ele -> ele.getId(), ele -> Double.valueOf(ele.getVoteCount())));
+			ActiveProductUtil.addActiveCache(id.toString(), map);
+		}
+	}
 }