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

import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.ClassGroupTypeEnum;
import com.ym.mec.biz.dal.enums.TeachTypeEnum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.StudentVipGroupQueryInfo;
import com.ym.mec.biz.dal.page.VipGroupAttendanceQueryInfo;
import com.ym.mec.biz.service.CourseScheduleService;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import com.ym.mec.util.collection.MapUtil;
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.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VipGroupServiceImpl extends BaseServiceImpl<Long, VipGroup> implements VipGroupService {
	
	@Autowired
	private VipGroupDao vipGroupDao;
	@Autowired
	private ClassGroupDao classGroupDao;
	@Autowired
	private VipGroupClassGroupMapperDao vipGroupClassGroupMapperDao;
	@Autowired
	private ClassGroupTeacherMapperDao classGroupTeacherMapperDao;
	@Autowired
	private CourseScheduleService courseScheduleService;

	@Override
	public BaseDAO<Long, VipGroup> getDAO() {
		return vipGroupDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void createVipGroup(VipGroupApplyDto vipGroup) {
		VipGroupApplyBaseInfoDto vipGroupApplyBaseInfoDto=vipGroup.getVipGroupApplyBaseInfo();
		vipGroupDao.insert(vipGroupApplyBaseInfoDto);
		ClassGroup classGroup=new ClassGroup();
		classGroup.setName(vipGroupApplyBaseInfoDto.getName());
		classGroup.setExpectStudentNum(vipGroupApplyBaseInfoDto.getExpectStudentNum());
		classGroup.setType(ClassGroupTypeEnum.VIP);
		classGroup.setDelFlag(YesOrNoEnum.NO);
		classGroupDao.insert(classGroup);
		VipGroupClassGroupMapper vipGroupClassGroupMapper=new VipGroupClassGroupMapper();
		vipGroupClassGroupMapper.setVipGroupId(vipGroupApplyBaseInfoDto.getId());
		vipGroupClassGroupMapper.setClassGroupId(classGroup.getId());
		vipGroupClassGroupMapperDao.insert(vipGroupClassGroupMapper);
		ClassGroupTeacherMapper classGroupTeacherMapper=new ClassGroupTeacherMapper();
		classGroupTeacherMapper.setTeacherRole(TeachTypeEnum.TEACHING);
		classGroupTeacherMapper.setUserId(Integer.parseInt(vipGroupApplyBaseInfoDto.getTeacherId().toString()));
		classGroupTeacherMapper.setSalary(vipGroupApplyBaseInfoDto.getSalary());
		classGroupTeacherMapperDao.insert(classGroupTeacherMapper);
		vipGroup.getCourseSchedules().forEach(courseSchedule -> {
			courseSchedule.setClassGroupId(classGroup.getId());
		});
		courseScheduleService.batchAddCourseSchedule(vipGroup.getCourseSchedules());
	}

	@Override
	public PageInfo findStudentVipGroupList(StudentVipGroupQueryInfo queryInfo) {
		PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List dataList = null;
		int count = vipGroupDao.countVipGorups(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = vipGroupDao.findVipGroups(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public VipGroupManageDetailDto getVipGroupDetail(Long vipGroupId) {
		VipGroupManageDetailDto vipGroupBaseInfo = vipGroupDao.getVipGroupBaseInfo(vipGroupId);
		return vipGroupBaseInfo;
	}

	@Override
	public PageInfo findVipGroupAttendances(VipGroupAttendanceQueryInfo queryInfo) {
		PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List dataList = null;
		int count = vipGroupDao.countVipGroupAttendance(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = vipGroupDao.findVipGroupAttendances(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public StudentVipGroupDetailDto getVipGroupShowDetail(Long vipGroupId) {
		return vipGroupDao.getVipGroupDetail(vipGroupId);
	}
}