package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.CourseScheduleStatisticsDao;
import com.ym.mec.biz.dal.entity.CourseScheduleStatistics;
import com.ym.mec.biz.service.CourseScheduleStatisticsService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseScheduleStatisticsServiceImpl extends BaseServiceImpl<Long, CourseScheduleStatistics>  implements CourseScheduleStatisticsService {
	
	@Autowired
	private CourseScheduleStatisticsDao courseScheduleStatisticsDao;

	@Override
	public BaseDAO<Long, CourseScheduleStatistics> getDAO() {
		return courseScheduleStatisticsDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void courseScheduleStatistics() {
		List<Long> delCourseId = new ArrayList<>();
		List<CourseScheduleStatistics> updateCourseScheduleStatisticsList = courseScheduleStatisticsDao.queryUpdateCourseScheduleStatistics();
		if(CollectionUtils.isNotEmpty(updateCourseScheduleStatisticsList)){
			courseScheduleStatisticsDao.batchUpdate(updateCourseScheduleStatisticsList);
			delCourseId.addAll(updateCourseScheduleStatisticsList.stream().map(e -> e.getCourseScheduleId()).collect(Collectors.toList()));
		}
//		List<CourseScheduleStatistics> insertCourseScheduleStatisticsList = courseScheduleStatisticsDao.queryInsertCourseScheduleStatistics();
//		if(CollectionUtils.isNotEmpty(insertCourseScheduleStatisticsList)){
//			courseScheduleStatisticsDao.batchInsert(insertCourseScheduleStatisticsList);
//			delCourseId.addAll(insertCourseScheduleStatisticsList.stream().map(e -> e.getCourseScheduleId()).collect(Collectors.toList()));
//		}
		delCourseId.removeAll(Collections.singleton(null));
		if(CollectionUtils.isNotEmpty(delCourseId)){
			courseScheduleStatisticsDao.delWaitByCourseId(delCourseId);
		}
	}
}