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

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

import com.ssj.bean.sys.imlibuser.TbClassStudent;
import com.ssj.service.sys.homework.service.HomeworkPictureService;
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.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.imlibuser.TbClassTeacher;
import com.ssj.dao.sys.imlibuser.dao.TbClassQueryDao;
import com.ssj.dao.sys.imlibuser.dao.TbClassTeacherDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.imlibuser.service.TbClassTeacherService;
import org.springframework.util.CollectionUtils;

@Service(version = "1.0")
@Transactional
public class TbClassTeacherServiceImpl extends BaseServiceImpl<TbClassTeacher, String> implements TbClassTeacherService {

	@Autowired
	private TbClassTeacherDao dao;
	
	@Autowired
	private TbClassQueryDao queryDao;

	@Autowired
	private HomeworkPictureService homeworkPictureService;
	
	@Override
	public PagingAndSortingRepository<TbClassTeacher, String> getDao() {
		return dao;
	}

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

	@Override
	public TbClassTeacher getTbLibImTeacherByClassId(String classId,String teacherId) {
		return dao.getTbLibImTeacherByClassId(classId, teacherId);
	}

	@Override
	public TbClassTeacher getDefaultTbLibImTeacherByClassId(String classId, String libId, String teacherId) {
		TbClassTeacher tbClassTeacher = dao.getTbLibImTeacherByClassIdIgoreStatus(classId, teacherId);
		if(tbClassTeacher==null) {
			tbClassTeacher=new TbClassTeacher();
			tbClassTeacher.setClassId(classId);
			tbClassTeacher.setLibId(libId);
			tbClassTeacher.setStatus(1);
			tbClassTeacher.setCreateTime(new Date());
			tbClassTeacher.setTeacherId(teacherId);
		} else {
			tbClassTeacher.setStatus(1);
		}
		this.save(tbClassTeacher);
		//老师分到该班级，检索下该班有没有符合条件的未分配的作业，分到该老师名下
		homeworkPictureService.allotLibHomework(libId, teacherId, classId);
		return tbClassTeacher;
	}

	@Override
	public List<TbClassTeacher> getTbLibImTeacherBylibId(String libId, String teacherId) {
		return dao.getTbLibImTeacherBylibId(libId, teacherId);
	}

	@Override
	public void saveTbClassTeacherStatusByLibId(String libId, String teacherId, int status) {
		List<TbClassTeacher> tbClassTeachers = this.getTbLibImTeacherBylibId(libId, teacherId);
		if (tbClassTeachers != null && tbClassTeachers.size() > 0){
			for (TbClassTeacher tbClassTeacher : tbClassTeachers){
				tbClassTeacher.setStatus(status);
			}
			this.save(tbClassTeachers);
		}
	}

	@Override
	public int getTbClassTeacherByClassId(String classId,String libId) {
		return dao.getClassTeacherListByClassId(classId,libId);
	}


	@Override
	public void deleteClassTeacherByClassIdAndLibId(String libId,String classId) {
		 dao.deleteClassTeacherByClassIdAndLibId(libId,classId);
	}
}
