Pārlūkot izejas kodu

feat:乐团改造

Joburgess 4 gadi atpakaļ
vecāks
revīzija
97660eed58

+ 25 - 2
mec-biz/src/main/java/com/ym/mec/biz/event/listener/CourseEventListener.java

@@ -2,6 +2,7 @@ package com.ym.mec.biz.event.listener;
 
 import com.ym.mec.biz.event.CourseStatusChangeEvent;
 import com.ym.mec.biz.service.CourseScheduleStudentPaymentService;
+import com.ym.mec.common.redis.service.RedisCache;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -9,6 +10,8 @@ import org.springframework.context.event.EventListener;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * @Author qnc99
  * @Date 2020/12/2 0002
@@ -18,13 +21,33 @@ public class CourseEventListener {
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
+    private final static String KEY = "updateCourseActualPrice";
+
     @Autowired
     private CourseScheduleStudentPaymentService courseScheduleStudentPaymentService;
+    @Autowired
+    private RedisCache<String, Object> redisCache;
 
     @Async
     @EventListener
     public void courseStatusChangeMonitor(CourseStatusChangeEvent courseStatusChangeEvent){
-        logger.info("课程状态变更:{}", courseStatusChangeEvent.getCourseIds());
-        courseScheduleStudentPaymentService.updateCourseActualPrice();
+        if(redisCache.getRedisTemplate().opsForValue().size(KEY)>0){
+            logger.info("课程状态变更(未执行,任务执行中):{}", courseStatusChangeEvent.getCourseIds());
+            return;
+        }
+
+        Boolean success = redisCache.getRedisTemplate().opsForValue().setIfAbsent(KEY, Thread.currentThread().getId(), 1, TimeUnit.HOURS);
+        if(!success){
+            logger.info("课程状态变更(未执行,任务执行中):{}", courseStatusChangeEvent.getCourseIds());
+            return;
+        }
+
+        logger.info("课程状态变更(执行开始):{}", courseStatusChangeEvent.getCourseIds());
+
+        try{
+            courseScheduleStudentPaymentService.updateCourseActualPrice();
+        } finally {
+            redisCache.delete(KEY);
+        }
     }
 }

+ 22 - 38
mec-biz/src/main/java/com/ym/mec/biz/service/impl/CourseScheduleStudentPaymentServiceImpl.java

@@ -74,8 +74,6 @@ public class CourseScheduleStudentPaymentServiceImpl extends BaseServiceImpl<Lon
 	private MusicGroupStudentClassAdjustDao musicGroupStudentClassAdjustDao;
 	@Autowired
 	private SysConfigDao sysConfigDao;
-	@Autowired
-	private RedisCache<String, Object> redisCache;
 
 	@Override
 	public BaseDAO<Long, CourseScheduleStudentPayment> getDAO() {
@@ -676,47 +674,33 @@ public class CourseScheduleStudentPaymentServiceImpl extends BaseServiceImpl<Lon
 	@Override
 	@Transactional(rollbackFor = Exception.class)
 	public void updateCourseActualPrice() {
-
-		String key = "updateCourseActualPrice";
-
-		if(redisCache.getRedisTemplate().opsForValue().size(key)>0){
+		List<CourseScheduleStudentPaymentDto> courseScheduleStudentPayments = courseScheduleStudentPaymentDao.findNeedUpdateActualPriceStudentCourses();
+		if(CollectionUtils.isEmpty(courseScheduleStudentPayments)){
 			return;
 		}
-
-		redisCache.getRedisTemplate().opsForValue().setIfAbsent(key, Thread.currentThread().getId(), 1, TimeUnit.HOURS);
-
-		try {
-			List<CourseScheduleStudentPaymentDto> courseScheduleStudentPayments = courseScheduleStudentPaymentDao.findNeedUpdateActualPriceStudentCourses();
-			if(CollectionUtils.isEmpty(courseScheduleStudentPayments)){
-				redisCache.delete(key);
-				return;
-			}
-			List<CourseScheduleStudentPayment> needUpdates = new ArrayList<>();
-			Map<CourseStatusEnum, List<CourseScheduleStudentPaymentDto>> statusCoursesMap = courseScheduleStudentPayments.stream().collect(Collectors.groupingBy(CourseScheduleStudentPaymentDto::getCourseStatus));
-			if(statusCoursesMap.containsKey(CourseStatusEnum.NOT_START)){
-				List<CourseScheduleStudentPaymentDto> notStartCourses = statusCoursesMap.get(CourseStatusEnum.NOT_START);
-				for (CourseScheduleStudentPaymentDto notStartCourse : notStartCourses) {
-					studentRegistrationService.updateUserSurplusCourseFee(notStartCourse.getUserId(), notStartCourse.getMusicGroupId(), notStartCourse.getActualPrice(), StringUtils.join("课程状态变更到未开始,退还缴费:", notStartCourse.getCourseScheduleId()), null);
-					notStartCourse.setActualPrice(BigDecimal.ZERO);
-					needUpdates.add(notStartCourse);
-				}
+		List<CourseScheduleStudentPayment> needUpdates = new ArrayList<>();
+		Map<CourseStatusEnum, List<CourseScheduleStudentPaymentDto>> statusCoursesMap = courseScheduleStudentPayments.stream().collect(Collectors.groupingBy(CourseScheduleStudentPaymentDto::getCourseStatus));
+		if(statusCoursesMap.containsKey(CourseStatusEnum.NOT_START)){
+			List<CourseScheduleStudentPaymentDto> notStartCourses = statusCoursesMap.get(CourseStatusEnum.NOT_START);
+			for (CourseScheduleStudentPaymentDto notStartCourse : notStartCourses) {
+				studentRegistrationService.updateUserSurplusCourseFee(notStartCourse.getUserId(), notStartCourse.getMusicGroupId(), notStartCourse.getActualPrice(), StringUtils.join("课程状态变更到未开始,退还缴费:", notStartCourse.getCourseScheduleId()), null);
+				notStartCourse.setActualPrice(BigDecimal.ZERO);
+				needUpdates.add(notStartCourse);
 			}
+		}
 
-			if(statusCoursesMap.containsKey(CourseStatusEnum.OVER)){
-				List<CourseScheduleStudentPaymentDto> notStartCourses = statusCoursesMap.get(CourseStatusEnum.OVER);
-				notStartCourses.sort(Comparator.comparing(CourseScheduleStudentPaymentDto::getCourseStartTime));
-				for (CourseScheduleStudentPaymentDto notStartCourse : notStartCourses) {
-					BigDecimal actualPrice = Objects.isNull(notStartCourse.getActualPrice())?BigDecimal.ZERO:notStartCourse.getActualPrice();
-					BigDecimal deductAmount = studentRegistrationService.updateUserSurplusCourseFee(notStartCourse.getUserId(), notStartCourse.getMusicGroupId(), (notStartCourse.getExpectPrice().subtract(actualPrice)).negate(), StringUtils.join("课程结束,扣除课程费用:", notStartCourse.getCourseScheduleId()), null);
-					notStartCourse.setActualPrice(actualPrice.add(deductAmount.abs()));
-					needUpdates.add(notStartCourse);
-				}
-			}
-			if(!CollectionUtils.isEmpty(needUpdates)){
-				courseScheduleStudentPaymentDao.batchUpdate(needUpdates);
+		if(statusCoursesMap.containsKey(CourseStatusEnum.OVER)){
+			List<CourseScheduleStudentPaymentDto> notStartCourses = statusCoursesMap.get(CourseStatusEnum.OVER);
+			notStartCourses.sort(Comparator.comparing(CourseScheduleStudentPaymentDto::getCourseStartTime));
+			for (CourseScheduleStudentPaymentDto notStartCourse : notStartCourses) {
+				BigDecimal actualPrice = Objects.isNull(notStartCourse.getActualPrice())?BigDecimal.ZERO:notStartCourse.getActualPrice();
+				BigDecimal deductAmount = studentRegistrationService.updateUserSurplusCourseFee(notStartCourse.getUserId(), notStartCourse.getMusicGroupId(), (notStartCourse.getExpectPrice().subtract(actualPrice)).negate(), StringUtils.join("课程结束,扣除课程费用:", notStartCourse.getCourseScheduleId()), null);
+				notStartCourse.setActualPrice(actualPrice.add(deductAmount.abs()));
+				needUpdates.add(notStartCourse);
 			}
-		} finally {
-			redisCache.delete(key);
+		}
+		if(!CollectionUtils.isEmpty(needUpdates)){
+			courseScheduleStudentPaymentDao.batchUpdate(needUpdates);
 		}
 	}
 }

+ 6 - 1
mec-web/src/main/java/com/ym/mec/web/controller/TaskController.java

@@ -1,5 +1,6 @@
 package com.ym.mec.web.controller;
 
+import com.ym.mec.biz.event.source.CourseEventSource;
 import com.ym.mec.biz.service.*;
 import com.ym.mec.common.controller.BaseController;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -81,6 +82,9 @@ public class TaskController extends BaseController {
 	@Autowired
 	private CourseScheduleStudentPaymentService courseScheduleStudentPaymentService;
 
+	@Autowired
+	private CourseEventSource courseEventSource;
+
 	@GetMapping(value = "/autoAffirmReceiveTask")
 	// 自动确认收货
 	public void affirmReceive(){
@@ -294,6 +298,7 @@ public class TaskController extends BaseController {
 
 	@GetMapping("/updateCourseActualPrice")
 	public void updateCourseActualPrice(){
-		courseScheduleStudentPaymentService.updateCourseActualPrice();
+//		courseScheduleStudentPaymentService.updateCourseActualPrice();
+		courseEventSource.courseStatusChange(null);
 	}
 }