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

import com.ym.mec.biz.dal.dao.StudentBasicInfoDao;
import com.ym.mec.biz.dal.dao.StudentStatisticsDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.StudentStatisticsDto;
import com.ym.mec.biz.dal.dto.StudentStatisticsSumDto;
import com.ym.mec.biz.dal.dto.StudentStatisticsSumForDateDto;
import com.ym.mec.biz.dal.entity.StudentStatistics;
import com.ym.mec.biz.dal.page.StudentStatisticsQueryInfo;
import com.ym.mec.biz.service.StudentStatisticsService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StudentStatisticsServiceImpl extends BaseServiceImpl<Integer, StudentStatistics>  implements StudentStatisticsService {
	
	@Autowired
	private StudentStatisticsDao studentStatisticsDao;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private StudentBasicInfoDao studentBasicInfoDao;

	@Override
	public BaseDAO<Integer, StudentStatistics> getDAO() {
		return studentStatisticsDao;
	}

	@Override
	public void updateStudentStatistics() {
		//更新声部班老师
		studentBasicInfoDao.updateSubjectTeacher();
		//更新乐团主管、指导老师
		studentStatisticsDao.updateTeacherAndEdu();
		//更新未排课总数
		studentStatisticsDao.updateNoCourseNum();
		//更新未开始价值总和
		studentStatisticsDao.updateNotStartCourseFee();
		//更新未排课价值
		studentStatisticsDao.updateNoCourseFee();
		//更新第一次课的时间\最近一次课时间
		studentStatisticsDao.updateFirstAndLastCourseTime();
		//更新进行中课程组数量
		studentStatisticsDao.updateNormalGroupNum();
		//更新总课时数、已完成、剩余课时数、最近30天课耗、最近1年课耗
		studentStatisticsDao.updateCourseNum();
	}

	@Override
	public PageInfo<StudentStatisticsDto> queryStatisticsPage(StudentStatisticsQueryInfo queryInfo) {
		PageInfo<StudentStatisticsDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		if(StringUtils.isNotEmpty(queryInfo.getOrganId())){
			List<Integer> organIdList = Arrays.stream(queryInfo.getOrganId().split(",")).map(id -> Integer.valueOf(id)).collect(Collectors.toList());
			params.put("organIdList",organIdList);
		}
		List<StudentStatisticsDto> dataList = null;
		int count = studentStatisticsDao.countStatistics(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentStatisticsDao.queryStatistics(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

    @Override
    public StudentStatisticsSumDto studentSmallClassStatisticsSum(String groupType,String organId) {
		List<Integer> organIdList = new ArrayList<>();
		if(StringUtils.isNotEmpty(organId)){
			organIdList = Arrays.stream(organId.split(",")).map(id -> Integer.valueOf(id)).collect(Collectors.toList());
		}
		return studentStatisticsDao.studentSmallClassStatisticsSum(groupType,organIdList);
    }

    @Override
    public StudentStatisticsSumForDateDto studentSmallClassStatisticsSumForDate(String groupType, String startDate, String endDate,String organId) {
		StudentStatisticsSumForDateDto sumForDateDto = new StudentStatisticsSumForDateDto();
		List<Integer> organIdList = new ArrayList<>();
		if(StringUtils.isNotEmpty(organId)){
			organIdList = Arrays.stream(organId.split(",")).map(id -> Integer.valueOf(id)).collect(Collectors.toList());
		}
		//获取流失人数
		sumForDateDto.setLostNum(studentStatisticsDao.countLostStudentNum(groupType,startDate,endDate,organIdList));
		//获取新增人数
		sumForDateDto.setAddNum(studentStatisticsDao.countAddStudentNum(groupType,startDate,endDate,organIdList));
		//获取续费人数
		sumForDateDto.setRenewNum(studentStatisticsDao.countRenewStudentNum(groupType,endDate,organIdList));
		//获取回访人数
		sumForDateDto.setVisitNum(studentStatisticsDao.countVisitNum(groupType,startDate,endDate,organIdList));
        return sumForDateDto;
    }

	@Override
	public List<Map<Integer, String>> queryTeacherList(String groupType) {
		List<Integer> teacherIds = studentStatisticsDao.queryTeacherIds(groupType);
		if(CollectionUtils.isNotEmpty(teacherIds)){
			return teacherDao.queryNameByIdList(teacherIds);
		}
		return new ArrayList<>();
	}

	@Override
	public List<Map<Integer, String>> queryDirectorList(String groupType) {
		List<Integer> teacherIds = studentStatisticsDao.queryDirectorList(groupType);
		if(CollectionUtils.isNotEmpty(teacherIds)){
			return teacherDao.queryNameByIdList(teacherIds);
		}
		return new ArrayList<>();
	}
}