Kaynağa Gözat

更新课程营收优化

zouxuan 3 yıl önce
ebeveyn
işleme
8aabe604da

+ 4 - 5
mec-biz/src/main/java/com/ym/mec/biz/event/CourseStatusChangeEvent.java

@@ -3,7 +3,6 @@ package com.ym.mec.biz.event;
 import org.springframework.context.ApplicationEvent;
 
 import java.util.List;
-import java.util.Set;
 
 /**
  * @Author qnc99
@@ -11,18 +10,18 @@ import java.util.Set;
  */
 public class CourseStatusChangeEvent extends ApplicationEvent {
 
-    private Set<Long> courseIds;
+    private List<Long> courseIds;
 
-    public CourseStatusChangeEvent(Object source, Set<Long> courseIds) {
+    public CourseStatusChangeEvent(Object source, List<Long> courseIds) {
         super(source);
         this.courseIds = courseIds;
     }
 
-    public Set<Long> getCourseIds() {
+    public List<Long> getCourseIds() {
         return courseIds;
     }
 
-    public void setCourseIds(Set<Long> courseIds) {
+    public void setCourseIds(List<Long> courseIds) {
         this.courseIds = courseIds;
     }
 }

+ 15 - 64
mec-biz/src/main/java/com/ym/mec/biz/event/listener/CourseEventListener.java

@@ -1,23 +1,16 @@
 package com.ym.mec.biz.event.listener;
 
-import com.ym.mec.biz.dal.dao.CourseScheduleStudentPaymentDao;
-import com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment;
 import com.ym.mec.biz.event.CourseStatusChangeEvent;
 import com.ym.mec.biz.service.CourseScheduleStudentPaymentService;
-import com.ym.mec.biz.service.StudentServeService;
-import com.ym.mec.common.redis.service.RedisCache;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.commons.collections.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.event.EventListener;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
-import org.springframework.util.CollectionUtils;
 
-import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**
  * @Author qnc99
@@ -26,64 +19,22 @@ import java.util.stream.Collectors;
 @Component
 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;
-    @Autowired
-    private CourseScheduleStudentPaymentDao courseScheduleStudentPaymentDao;
-    @Autowired
-    private StudentServeService studentServeService;
 
-    @Async
     @EventListener
     public void courseStatusChangeMonitor(CourseStatusChangeEvent courseStatusChangeEvent){
-//        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(null, null, null);
-        } finally {
-            redisCache.delete(KEY);
-        }
-    }
-
-    @Async
-    @EventListener
-    public void courseStatusChangeMonitor2(CourseStatusChangeEvent courseStatusChangeEvent){
-        if(redisCache.getRedisTemplate().opsForValue().size("exercisesSituationStatistics2")>0){
-            return;
-        }
-
-        Boolean success = redisCache.getRedisTemplate().opsForValue().setIfAbsent("exercisesSituationStatistics2", Thread.currentThread().getId(), 1, TimeUnit.HOURS);
-        if(!success){
-            return;
-        }
-
-
-        try{
-//            List<CourseScheduleStudentPayment> cssps = courseScheduleStudentPaymentDao.findByCourseScheduleIds(new ArrayList<>(courseStatusChangeEvent.getCourseIds()));
-//            if(CollectionUtils.isEmpty(cssps)){
-//                return;
-//            }
-//            List<Integer> studentIds = cssps.stream().map(CourseScheduleStudentPayment::getUserId).collect(Collectors.toList());
-//            studentServeService.exercisesSituationStatistics2(null,studentIds);
-        } finally {
-            redisCache.delete("exercisesSituationStatistics2");
+        List<Long> courseIds = courseStatusChangeEvent.getCourseIds();
+        if(CollectionUtils.isNotEmpty(courseIds)){
+            ExecutorService executor = Executors.newCachedThreadPool();
+            CompletableFuture.runAsync(()->{
+                try {
+                    //更新课程营收
+                    courseScheduleStudentPaymentService.updateCourseActualPrice(courseIds);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            },executor);
         }
     }
 }

+ 2 - 1
mec-biz/src/main/java/com/ym/mec/biz/event/source/CourseEventSource.java

@@ -5,6 +5,7 @@ import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -23,7 +24,7 @@ public class CourseEventSource {
      * @date 2020/12/2 0002
      * @return void
      */
-    public void courseStatusChange(Set<Long> courseIds){
+    public void courseStatusChange(List<Long> courseIds){
         applicationContext.publishEvent(new CourseStatusChangeEvent(this, courseIds));
     }
 

+ 2 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/CourseScheduleService.java

@@ -365,13 +365,13 @@ public interface CourseScheduleService extends BaseService<Long, CourseSchedule>
 	 * 更新课程至结束状态
 	 * @return
 	 */
-	boolean updateCourseScheduleToOverStatus();
+	List<Long> updateCourseScheduleToOverStatus();
 
 	/**
 	 * 更新课程至进行中状态
 	 * @return
 	 */
-	boolean updateCourseScheduleToUnderway();
+	List<Long> updateCourseScheduleToUnderway();
 
 	/**
 	 * @describe 获取老师指定班级的排课

+ 8 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/CourseScheduleStudentPaymentService.java

@@ -122,4 +122,12 @@ public interface CourseScheduleStudentPaymentService extends BaseService<Long, C
 	 */
 	void updateCourseActualPrice(List<String> groupIds, List<Integer> userIds, GroupType groupType);
 
+	/**
+	 * @describe 更新课程营收
+	 * @author qnc99
+	 * @date 2020/12/2 0002
+	 * @return void
+	 */
+	void updateCourseActualPrice(List<Long> courseIds);
+
 }

+ 7 - 6
mec-biz/src/main/java/com/ym/mec/biz/service/impl/CourseScheduleServiceImpl.java

@@ -3693,7 +3693,7 @@ public class CourseScheduleServiceImpl extends BaseServiceImpl<Long, CourseSched
 		//保存日志调整信息
 		saveModifyLog(modifyLogs,courseScheduleIds);
 
-		courseEventSource.courseStatusChange(new HashSet<>(courseScheduleIds));
+		courseEventSource.courseStatusChange(courseScheduleIds);
 
 		imUserFriendService.refreshCourseImUserFriend(courseScheduleId);
 		// 推送
@@ -4195,7 +4195,7 @@ public class CourseScheduleServiceImpl extends BaseServiceImpl<Long, CourseSched
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean updateCourseScheduleToOverStatus() {
+    public List<Long> updateCourseScheduleToOverStatus() {
 
         Date date = new Date();
         //自动更新课程状态至“已结束”
@@ -4204,6 +4204,7 @@ public class CourseScheduleServiceImpl extends BaseServiceImpl<Long, CourseSched
         List<CourseSchedule> updateList = new ArrayList<CourseSchedule>();
         List<Integer> classGroupIds = new ArrayList<Integer>();
         List<Long> courseScheduleIdList = new ArrayList<Long>();
+        List<Long> courseIds = new ArrayList<Long>();
 
         for (CourseSchedule courseSchedule : list) {
             courseSchedule.setStatus(CourseStatusEnum.OVER);
@@ -4226,7 +4227,7 @@ public class CourseScheduleServiceImpl extends BaseServiceImpl<Long, CourseSched
 			}catch (Exception e){
 				LOGGER.error("deductAmount  >>>>>>>>>",e.getCause());
 			}
-			List<Long> courseIds = updateList.stream().map(CourseSchedule::getId).collect(Collectors.toList());
+			courseIds = updateList.stream().map(CourseSchedule::getId).collect(Collectors.toList());
 			List<CourseSchedule> beMergeCourses = courseScheduleDao.getBeMergeCourseWithMainCourseIds(courseIds);
 			if(!CollectionUtils.isEmpty(beMergeCourses)){
 				beMergeCourses.forEach(c->c.setStatus(CourseStatusEnum.OVER));
@@ -4242,12 +4243,12 @@ public class CourseScheduleServiceImpl extends BaseServiceImpl<Long, CourseSched
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
-		return true;
+		return courseIds;
     }
 
     @Override
 	@Transactional(rollbackFor = Exception.class)
-	public boolean updateCourseScheduleToUnderway() {
+	public List<Long> updateCourseScheduleToUnderway() {
     	Date date = new Date();
     	List<CourseSchedule> list = courseScheduleDao.queryUnderwayWithNoUpdateStatus();
 
@@ -4262,7 +4263,7 @@ public class CourseScheduleServiceImpl extends BaseServiceImpl<Long, CourseSched
         if (updateList.size() > 0) {
             courseScheduleDao.batchUpdate(updateList);
 		}
-		return true;
+		return updateList.stream().map(e->e.getId()).collect(Collectors.toList());
 	}
 
 	@Override

+ 16 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/CourseScheduleStudentPaymentServiceImpl.java

@@ -685,6 +685,22 @@ public class CourseScheduleStudentPaymentServiceImpl extends BaseServiceImpl<Lon
 
 	@Override
 	@Transactional(rollbackFor = Exception.class)
+	public void updateCourseActualPrice(List<Long> courseIds) {
+		List<CourseScheduleStudentPayment> studentPayments = courseScheduleStudentPaymentDao.findByCourseScheduleIds(courseIds);
+		Map<GroupType, List<CourseScheduleStudentPayment>> groupTypeListMap = studentPayments.stream().collect(Collectors.groupingBy(CourseScheduleStudentPayment::getGroupType));
+		for (GroupType groupType : groupTypeListMap.keySet()) {
+			List<CourseScheduleStudentPayment> groupTypePayments = groupTypeListMap.get(groupType);
+			if(CollectionUtils.isEmpty(groupTypePayments)){
+				continue;
+			}
+			List<String> groupIds = groupTypePayments.stream().map(e -> e.getMusicGroupId()).collect(Collectors.toList());
+			List<Integer> userIds = groupTypePayments.stream().map(e -> e.getUserId()).collect(Collectors.toList());
+			this.updateCourseActualPrice(groupIds,userIds,groupType);
+		}
+	}
+
+	@Override
+	@Transactional(rollbackFor = Exception.class)
 	public void updateCourseActualPrice(List<String> groupIds, List<Integer> userIds, GroupType groupType) {
 		studentDao.lockUser(1);
 		List<CourseScheduleStudentPaymentDto> courseScheduleStudentPayments = courseScheduleStudentPaymentDao.findNeedUpdateActualPriceStudentCourses(groupIds, userIds, groupType);

+ 4 - 3
mec-web/src/main/java/com/ym/mec/web/controller/TaskController.java

@@ -230,9 +230,10 @@ public class TaskController extends BaseController {
 	@GetMapping("/updateCourseScheduleToOverStatus")
 	// 更新课程状态至已结束
 	public void updateCourseScheduleToOverStatus() {
-		courseScheduleService.updateCourseScheduleToOverStatus();
-		courseScheduleService.updateCourseScheduleToUnderway();
-		courseEventSource.courseStatusChange(null);
+		List<Long> courseIds = courseScheduleService.updateCourseScheduleToOverStatus();
+		courseEventSource.courseStatusChange(courseIds);
+		courseIds = courseScheduleService.updateCourseScheduleToUnderway();
+		courseEventSource.courseStatusChange(courseIds);
 	}
 
 	@GetMapping("/updateVipGroupToFinishedStatus")