package com.ym.mec.biz.event.listener;

import com.ym.mec.biz.event.CourseStatusChangeEvent;
import com.ym.mec.biz.service.CourseScheduleStudentPaymentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author qnc99
 * @Date 2020/12/2 0002
 */
@Component
public class CourseEventListener {

    @Autowired
    private CourseScheduleStudentPaymentService courseScheduleStudentPaymentService;

    @EventListener
    public void courseStatusChangeMonitor(CourseStatusChangeEvent courseStatusChangeEvent){
        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);
        }
    }
}
