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.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;

/**
 * @Author qnc99
 * @Date 2020/12/2 0002
 */
@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");
        }
    }
}
