package com.ssj.service.sys.imlibuser.service.impl;

import com.ssj.bean.sys.imlibuser.TbClass;
import com.ssj.bean.sys.imlibuser.TbClassStudent;
import com.ssj.bean.sys.imlibuser.TbClassTeacher;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.dao.sys.imlibuser.dao.TbClassDao;
import com.ssj.dao.sys.imlibuser.dao.TbClassQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.Constant;
import com.ssj.service.sys.imlibuser.service.TbClassService;
import com.ssj.service.sys.imlibuser.service.TbClassStudentService;
import com.ssj.service.sys.imlibuser.service.TbClassTeacherService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.library.service.ILibVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TbClassServiceImpl extends BaseServiceImpl<TbClass, String> implements TbClassService {

	@Autowired
	private TbClassDao dao;
	
	@Autowired
	private TbClassQueryDao libImQueryDao;
	
	@Autowired
	private ILibVipService libVipService;
	
	@Autowired
	private IBookManagerService managerService;
	
	@Autowired
	private TbClassTeacherService tbClassTeacherService;
	
	@Autowired
	private TbClassStudentService tbClassStudentService;
	
	
	@Override
	public PagingAndSortingRepository<TbClass, String> getDao() {
		return dao;
	}


	@Override
	public TbClass getTbClassByLibId(String libId, String className) {
		return dao.getTbClassByLibId(libId, className);
	}
	
	@Override
	public TbClass getTbClassGradeByLibId(String libId,String grade,String className){
		return dao.getTbClassGradeByLibId(libId, grade,className);
	}

	@Override
	public Page<Map<String, Object>> findClassByPage(Map<String, Object> searchParam, Pageable pageable) {
		return libImQueryDao.findClassByPage(searchParam, pageable);
	}

	@Override
	public TbClass getDefaultTbClassByLibId(String libId) {
		TbClass tbClass = dao.getDefaultTbClassByLibId(libId);
		if(tbClass==null) {
			tbClass=new TbClass();
			tbClass.setLibId(libId);
			tbClass.setClassName("一班");
			tbClass.setCreateTime(new Date());
			tbClass.setStatus(2);
			tbClass=this.save(tbClass);
			
			//同步旧数据
			this.syncTbClassServiceByStudent(libId, tbClass);
			this.syncTbClassServiceByTeacher(libId, tbClass);
		}
		return tbClass;
	}

	@Override
	public List<TbClass> getTbClassListByLibId(String libId){
		return dao.findTbClassListByLibId(libId);
	}

	@Override
	public List<TbClass> getTbClassSchoolNameByLibId(String libId){
		return dao.findTbClassSchoolNameByLibId(libId);
	}

	@Override
	public List<TbClass> getTbClassListByClassName(String libId,String className){
		return dao.findTbClassByLibIdAndClassName(libId,className);
	}


	@Override
	public void syncTbClassServiceByStudent(String libId, TbClass tbClass) {
		List<TbClassStudent> tbClassStudents=new ArrayList<TbClassStudent>();
		TbClassStudent tbClassStudent=null;
		List<LibVip> libVips=libVipService.findByLibId(libId);
		if(libVips!=null) {
			for (LibVip libVip : libVips) {
				tbClassStudent=new TbClassStudent();
				tbClassStudent.setClassId(tbClass.getId());
				tbClassStudent.setCreateTime(new Date());
				tbClassStudent.setLibId(libId);
				tbClassStudent.setStatus(1);
				tbClassStudent.setVipId(libVip.getId());
				tbClassStudents.add(tbClassStudent);
			}
			tbClassStudentService.save(tbClassStudents);
		}
	}


	@Override
	public void syncTbClassServiceByTeacher(String libId, TbClass tbClass) {
		List<TbClassTeacher> tbClassTeachers=new ArrayList<TbClassTeacher>();
		TbClassTeacher tbClassTeacher=null;
		List<TbLibManager> tbLibManagers=managerService.findTbLibManagerByLibId(libId,  Constant.ORG_TEACHER_TYPE,1);
		if(tbLibManagers!=null) {
			for (TbLibManager tbLibManager : tbLibManagers) {
				tbClassTeacher=new TbClassTeacher();
				tbClassTeacher.setClassId(tbClass.getId());
				tbClassTeacher.setCreateTime(new Date());
				tbClassTeacher.setLibId(libId);
				tbClassTeacher.setStatus(1);
				tbClassTeacher.setTeacherId(tbLibManager.getUserId());
				tbClassTeachers.add(tbClassTeacher);
			}
			tbClassTeacherService.save(tbClassTeachers);
		}
	}

	@Override
	public TbClass findDefaultClass(String libId) {
		return dao.findTopByLibIdAndStatus(libId, 2);
	}

	@Override
	public void updateByClassIdAndClassName(String classId,String className){
		  dao.updateByClassIdAndClassName(classId,className);
	}
}
