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

import com.ssj.bean.sys.imlibuser.TbClass;
import com.ssj.bean.sys.imlibuser.TbClassTeacher;
import com.ssj.bean.sys.imlibuser.TbClassTeacherTemp;
import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.bean.weixin.libmy.domain.TbLibManagerAudit;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.dao.sys.imlibuser.dao.TbClassQueryDao;
import com.ssj.dao.sys.imlibuser.dao.TbClassTeacherTempDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.Constant;
import com.ssj.framework.core.util.PasswordUtil;
import com.ssj.framework.idworker.IdWorker;
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.sys.imlibuser.service.TbClassTeacherTempService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.library.service.ILibJoinService;
import com.ssj.service.weixin.library.service.LibManagerAuditService;
import com.ssj.service.weixin.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
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 TbClassTeacherTempServiceImpl extends BaseServiceImpl<TbClassTeacherTemp, String> implements TbClassTeacherTempService {

	@Autowired
	private TbClassTeacherTempDao dao;
	
	@Autowired
	private TbClassQueryDao queryDao;
	
	@Autowired
	private TbClassStudentService tbLibImStudentService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
 	private ILibJoinService joinService;    
	
    @Autowired
    private IBookManagerService managerService;
    
    @Autowired
    private TbClassTeacherService tbClassTeacherService;
    
	@Autowired
	private TbClassService tbClassService;
	
    @Autowired
    private LibManagerAuditService auditService;
    
    
	@Override
	public PagingAndSortingRepository<TbClassTeacherTemp, String> getDao() {
		return dao;
	}
	

	@Override
	public Page<Map<String, Object>> findTeacherErrorByPage(
			Map<String, Object> searchParam, Pageable pageable) {
		return queryDao.findTeacherErrorByPage(searchParam, pageable);
	}
	
	
	private int containSubject(String subject) {
		int contain=0;
		List<String> subjectList=new ArrayList<String>();
		subjectList.add("语文");
		subjectList.add("数学");
		subjectList.add("英语");
		for (String s : subjectList) {
			if(subject.contains(s)) {
				contain=contain+1;
			}
		}
		return contain;
	}
	
	@Override
	public int insertTbLibImTeacher(TbLibJoin libJoin ,TbClassTeacherTemp tbLibImTeacherTemp) {
		if(libJoin!=null){
			tbLibImTeacherTemp.setLibId(libJoin.getId());
			tbLibImTeacherTemp.setLeagueName(libJoin.getLeagueName());
			tbLibImTeacherTemp.setCreateTime(new Date());
			tbLibImTeacherTemp.setStatus(1);
			
			try {
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getSubject())) {
					String subject=tbLibImTeacherTemp.getSubject();
					if(StringUtils.isNotEmpty(subject.replaceFirst("语文", "").replaceFirst("数学", "").replaceFirst("英语", "").replaceAll(",", ""))) {
						tbLibImTeacherTemp.setStatus(0);
						tbLibImTeacherTemp.setErrorReason("填写科目不正确，科目只能包含语文,数学,英语与英文逗号分隔");
						this.save(tbLibImTeacherTemp);
						return 0;
					}
					int contain=containSubject(subject);
					if(contain<=0) {
						tbLibImTeacherTemp.setStatus(0);
						tbLibImTeacherTemp.setErrorReason("填写科目不正确，科目只能包含语文,数学,英语");
						this.save(tbLibImTeacherTemp);
						return 0;
					}else {
						if(contain>=2 && !subject.contains(",")) {
							tbLibImTeacherTemp.setStatus(0);
							tbLibImTeacherTemp.setErrorReason("多个科目间隔使用英语逗号分隔','");
							this.save(tbLibImTeacherTemp);
							return 0;
						}
					}
				}
				
				User user=userService.findByMobile(tbLibImTeacherTemp.getTeacherPhone());
				if(user==null){//无老师user，生成老师user
					user=new User();
					user.setId(IdWorker.generateId());
					user.setUserName(tbLibImTeacherTemp.getTeacherName());
					user.setMobile(tbLibImTeacherTemp.getTeacherPhone());
					user.setCreateTime(new Date());
					user.setPassword("123456");
					user.setPwdSalt(PasswordUtil.generateSalt());
					user.setPassword(PasswordUtil.initPassword(user.getPassword(), user.getPwdSalt()));
					user=userService.save(user);
				}
				
				//如果之前有这个老师的，就统一使用用旧老师的名称，如果没有就使用新的名称。
				TbLibManager tbLibManager=managerService.findAllByUserIdAndTypeAndState(user.getId(),Constant.TEACHER_TYPE,1);
				if(tbLibManager==null){//有老师user，生成type=2的老师
					tbLibManager=new TbLibManager();
					tbLibManager.settPhoto(user.getPhoto());
					tbLibManager.setUserId(user.getId());
					tbLibManager.setType(Constant.TEACHER_TYPE);
					tbLibManager.setCreateTime(new Date());
					tbLibManager.setName(tbLibImTeacherTemp.getTeacherName());
					tbLibManager.setTelephone(tbLibImTeacherTemp.getTeacherPhone());
					tbLibManager.setCourseName(tbLibImTeacherTemp.getSubject());
					if(StringUtils.isEmpty(tbLibImTeacherTemp.getSubject())) {
						tbLibManager.setCourseName("语文,数学,英语");
					}
					tbLibManager.setGrade(tbLibImTeacherTemp.getGrade());
					tbLibManager.setSchool(tbLibImTeacherTemp.getSchool());
					tbLibManager.setOrgName(tbLibImTeacherTemp.getSchool());
					tbLibManager.setLibId(tbLibImTeacherTemp.getLibId());
					if(tbLibImTeacherTemp.getIsCorrect()!=null && tbLibImTeacherTemp.getIsCorrect()==1) {
						tbLibManager.setIsCorrect(1);
					}else {
						tbLibManager.setIsCorrect(0);
					}
					tbLibManager.setState(1);
					tbLibManager=managerService.save(tbLibManager);
				}
				
				
				TbLibManager tbLibManager1=managerService.findAllByUserIdAndTypeAndState(tbLibImTeacherTemp.getLibId(),user.getId(), Constant.LIB_TEACHER_TYPE,1);
				if(tbLibManager1==null){//有老师user，生成type=15的老师
					tbLibManager1=new TbLibManager();
					tbLibManager1.settPhoto(user.getPhoto());
					tbLibManager1.setUserId(user.getId());
					tbLibManager1.setType(Constant.LIB_TEACHER_TYPE);
					tbLibManager1.setCreateTime(new Date());
					tbLibManager1.setName(tbLibImTeacherTemp.getTeacherName());
					tbLibManager1.setTelephone(tbLibImTeacherTemp.getTeacherPhone());
					tbLibManager1.setCourseName(tbLibImTeacherTemp.getSubject());
					if(StringUtils.isEmpty(tbLibImTeacherTemp.getSubject())) {
						tbLibManager1.setCourseName("语文,数学,英语");
					}
					tbLibManager1.setGrade(tbLibImTeacherTemp.getGrade());
					tbLibManager1.setSchool(tbLibImTeacherTemp.getSchool());
					tbLibManager1.setOrgName(tbLibImTeacherTemp.getSchool());
					tbLibManager1.setLibId(tbLibImTeacherTemp.getLibId());
					if(tbLibImTeacherTemp.getIsCorrect()!=null && tbLibImTeacherTemp.getIsCorrect()==1) {
						tbLibManager1.setIsCorrect(1);
					}else {
						tbLibManager1.setIsCorrect(0);
					}
					tbLibManager1.setState(1);
					managerService.save(tbLibManager1);
				}
				
				TbLibManager tbLibManager2=managerService.findAllByUserIdAndTypeAndState(tbLibImTeacherTemp.getLibId(),user.getId(), Constant.ORG_TEACHER_TYPE,1);
				if(tbLibManager2==null){//有老师user，生成type=10的老师
					tbLibManager2=new TbLibManager();
					tbLibManager2.settPhoto(user.getPhoto());
					tbLibManager2.setUserId(user.getId());
					tbLibManager2.setType(Constant.ORG_TEACHER_TYPE);
					tbLibManager2.setCreateTime(new Date());
					tbLibManager2.setName(tbLibImTeacherTemp.getTeacherName());
					tbLibManager2.setTelephone(tbLibImTeacherTemp.getTeacherPhone());
					tbLibManager2.setCourseName(tbLibImTeacherTemp.getSubject());
					if(StringUtils.isEmpty(tbLibImTeacherTemp.getSubject())) {
						tbLibManager2.setCourseName("语文,数学,英语");
					}
					tbLibManager2.setGrade(tbLibImTeacherTemp.getGrade());
					tbLibManager2.setSchool(tbLibImTeacherTemp.getSchool());
					tbLibManager2.setOrgName(tbLibImTeacherTemp.getSchool());
					tbLibManager2.setLibId(tbLibImTeacherTemp.getLibId());
					if(tbLibImTeacherTemp.getIsCorrect()!=null && tbLibImTeacherTemp.getIsCorrect()==1) {
						tbLibManager2.setIsCorrect(1);
					}else {
						tbLibManager2.setIsCorrect(0);
					}
					tbLibManager2.setState(1);
					managerService.save(tbLibManager2);
				}
				
				//导入了自批改老师，平台老师老师设置为无效
				List<TbLibManager> platformTbLibManagers=managerService.findByUserIdAndTypeInAndState(user.getId(),new int[]{8, 11, 12, 13, 14},1);
				if(platformTbLibManagers!=null && platformTbLibManagers.size()>0) {
					for (TbLibManager platformTbLibManager : platformTbLibManagers) {
						platformTbLibManager.setState(2);
					}
					managerService.save(platformTbLibManagers);	
				}
				
				//没有班级信息的，不关联班级信息
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getClassName())) {
					TbClass tbClass=tbClassService.getTbClassGradeByLibId(tbLibImTeacherTemp.getLibId(),tbLibImTeacherTemp.getGrade(), tbLibImTeacherTemp.getClassName());
					if(tbClass==null) {
						tbClass=new TbClass();
						tbClass.setLibId(tbLibImTeacherTemp.getLibId());
						tbClass.setClassName(tbLibImTeacherTemp.getClassName());
						tbClass.setGrade(tbLibImTeacherTemp.getGrade());
						tbClass.setCreateTime(new Date());
						tbClass.setStatus(1);
						tbClass=tbClassService.save(tbClass);
					}
					
					TbClassTeacher tbClassTeacher=tbClassTeacherService.getTbLibImTeacherByClassIdAndType(tbClass.getId(),user.getId(),1);
					if(tbClassTeacher==null) {
						tbClassTeacher=new TbClassTeacher();
						tbClassTeacher.setClassId(tbClass.getId());
						tbClassTeacher.setLibId(libJoin.getId());
						tbClassTeacher.setType(1);
						tbClassTeacher.setStatus(1);
						tbClassTeacher.setCreateTime(new Date());
						tbClassTeacher.setTeacherId(user.getId());
						tbClassTeacherService.save(tbClassTeacher);
					}else {
						tbLibImTeacherTemp.setTeacherId(user.getId());
						tbLibImTeacherTemp.setStatus(0);
						tbLibImTeacherTemp.setErrorReason("与平台已有老师信息冲突");
						this.save(tbLibImTeacherTemp);
						return 0;
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
//				tbLibImTeacherTemp.setStatus(0);
//				tbLibImTeacherTemp.setErrorReason("异常："+e.getMessage());
			}finally{
			}
			return 1;
		}
		return 0;
	}

	@Override
	public int insertBatchTbLibImTeacher(List<TbClassTeacherTemp> tbLibImTeacherTemps) {
		int successNum=0;
		for (TbClassTeacherTemp tbLibImTeacherTemp : tbLibImTeacherTemps) {
			try {
				TbLibJoin tbLibJoin=null;
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getLibId())){
					tbLibJoin=joinService.getById(tbLibImTeacherTemp.getLibId());
				}else{
					tbLibJoin=tbLibImStudentService.getTbLibJoinByLeagueName(tbLibImTeacherTemp.getLeagueName());
				}
				//无机构信息
				if(tbLibJoin!=null){
					successNum=successNum+this.insertTbLibImTeacher(tbLibJoin, tbLibImTeacherTemp);
				}else{
					successNum=-1;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return successNum;
	}


	@Override
	public TbClassTeacherTemp getTbLibImTeacherTempByLibId(String libId, String teacherId) {
		return dao.getTbLibImTeacherTempByLibId(libId, teacherId);
	}


	@Override
	public int insertBatchTbLibImPrincipal(List<TbClassTeacherTemp> tbLibImTeacherTemps) {
		int successNum=0;
		for (TbClassTeacherTemp tbLibImTeacherTemp : tbLibImTeacherTemps) {
			try {
				TbLibJoin tbLibJoin=null;
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getLibId())){
					tbLibJoin=joinService.getById(tbLibImTeacherTemp.getLibId());
				}else{
					tbLibJoin=tbLibImStudentService.getTbLibJoinByLeagueName(tbLibImTeacherTemp.getLeagueName());
				}
				//无机构信息
				if(tbLibJoin!=null){
					successNum=successNum+this.insertTbLibImPrincipal(tbLibJoin, tbLibImTeacherTemp);
				}else{
					successNum=-1;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return successNum;
	}
	
	
	@Override
	public int insertTbLibImPrincipal(TbLibJoin libJoin ,TbClassTeacherTemp tbLibImTeacherTemp) {
		if(libJoin!=null){
			tbLibImTeacherTemp.setLibId(libJoin.getId());
			tbLibImTeacherTemp.setLeagueName(libJoin.getLeagueName());
			tbLibImTeacherTemp.setCreateTime(new Date());
			tbLibImTeacherTemp.setStatus(1);
			
			try {
				User user=userService.findByMobile(tbLibImTeacherTemp.getTeacherPhone());
				if(user==null){//无老师user，生成老师user
					user=new User();
					user.setId(IdWorker.generateId());
					user.setUserName(tbLibImTeacherTemp.getTeacherName());
					user.setMobile(tbLibImTeacherTemp.getTeacherPhone());
					user.setCreateTime(new Date());
					user.setPassword("123456");
					user.setPwdSalt(PasswordUtil.generateSalt());
					user.setPassword(PasswordUtil.initPassword(user.getPassword(), user.getPwdSalt()));
					user=userService.save(user);
				}
				//如果之前有这个老师的，就统一使用用旧老师的名称，如果没有就使用新的名称。
				TbLibManager tbLibManager=managerService.findAllByUserIdAndTypeAndState(user.getId(),Constant.CURATOR,1);
				if(tbLibManager==null){//有老师user，生成type=2的老师
					tbLibManager=new TbLibManager();
					tbLibManager.setUserId(user.getId());
					tbLibManager.setType(Constant.CURATOR);
					tbLibManager.setCreateTime(new Date());
					tbLibManager.setName(tbLibImTeacherTemp.getTeacherName());
					tbLibManager.setTelephone(tbLibImTeacherTemp.getTeacherPhone());
					tbLibManager.setLibId(tbLibImTeacherTemp.getLibId());
					if(tbLibImTeacherTemp.getIsCorrect()!=null && tbLibImTeacherTemp.getIsCorrect()==1) {
						tbLibManager.setIsCorrect(1);
					}else {
						tbLibManager.setIsCorrect(0);
					}
					tbLibManager.setState(1);
					tbLibManager=managerService.save(tbLibManager);
				}else {
					tbLibImTeacherTemp.setTeacherId(user.getId());
					tbLibImTeacherTemp.setStatus(0);
					tbLibImTeacherTemp.setErrorReason("与平台已有老师信息冲突");
					this.save(tbLibImTeacherTemp);
					return 0;
				}
				
				TbLibManagerAudit audit = new  TbLibManagerAudit();
		        audit.setStatus(1);
		        audit.setTelephone(tbLibManager.getTelephone());
		        audit.setLibManagerId(tbLibManager.getId());
		        audit.setLibId(tbLibManager.getLibId());
		        audit.setType(tbLibManager.getType());
		        audit.setCreateTime(new Date());
		        audit.setLastUpdateTime(new Date());
		        audit.setUserId(tbLibManager.getUserId());
		        audit.setName(tbLibManager.getName());
		        auditService.save(audit);
		        
		        TbLibJoin tbLibJoin=joinService.getById(tbLibManager.getLibId());
		        if(tbLibJoin!=null && tbLibJoin.getLibtype()!=5) {
		        	tbLibJoin.setLibtype(5);
		        	joinService.save(tbLibJoin);
		        }
		        
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
			}
			return 1;
		}
		return 0;
	}
	
	


	@Override
	public int insertBatchTbLibImLeader(List<TbClassTeacherTemp> tbLibImTeacherTemps) {
		int successNum=0;
		for (TbClassTeacherTemp tbLibImTeacherTemp : tbLibImTeacherTemps) {
			try {
				TbLibJoin tbLibJoin=null;
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getLibId())){
					tbLibJoin=joinService.getById(tbLibImTeacherTemp.getLibId());
				}else{
					tbLibJoin=tbLibImStudentService.getTbLibJoinByLeagueName(tbLibImTeacherTemp.getLeagueName());
				}
				//无机构信息
				if(tbLibJoin!=null){
					successNum=successNum+this.insertTbLibImLeader(tbLibJoin, tbLibImTeacherTemp);
				}else{
					successNum=-1;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return successNum;
	}
	
	
	@Override
	public int insertTbLibImLeader(TbLibJoin libJoin, TbClassTeacherTemp tbLibImTeacherTemp) {
		if(libJoin!=null){
			tbLibImTeacherTemp.setLibId(libJoin.getId());
			tbLibImTeacherTemp.setLeagueName(libJoin.getLeagueName());
			tbLibImTeacherTemp.setCreateTime(new Date());
			tbLibImTeacherTemp.setStatus(1);
			
			try {
				
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getSubject())) {
					String subject=tbLibImTeacherTemp.getSubject();
					if(StringUtils.isNotEmpty(subject.replaceFirst("语文", "").replaceFirst("数学", "").replaceFirst("英语", "").replaceAll(",", ""))) {
						tbLibImTeacherTemp.setStatus(0);
						tbLibImTeacherTemp.setErrorReason("填写科目不正确，科目只能包含语文,数学,英语与英文逗号分隔");
						this.save(tbLibImTeacherTemp);
						return 0;
					}
					int contain=containSubject(subject);
					if(contain<=0) {
						tbLibImTeacherTemp.setStatus(0);
						tbLibImTeacherTemp.setErrorReason("填写科目不正确，科目只能包含语文,数学,英语");
						this.save(tbLibImTeacherTemp);
						return 0;
					}else {
						if(contain>=2 && !subject.contains(",")) {
							tbLibImTeacherTemp.setStatus(0);
							tbLibImTeacherTemp.setErrorReason("多个科目间隔使用英语逗号分隔','");
							this.save(tbLibImTeacherTemp);
							return 0;
						}
					}
				}
				
				User user=userService.findByMobile(tbLibImTeacherTemp.getTeacherPhone());
				if(user==null){//无老师user，生成老师user
					user=new User();
					user.setId(IdWorker.generateId());
					user.setUserName(tbLibImTeacherTemp.getTeacherName());
					user.setMobile(tbLibImTeacherTemp.getTeacherPhone());
					user.setCreateTime(new Date());
					user.setPassword("123456");
					user.setPwdSalt(PasswordUtil.generateSalt());
					user.setPassword(PasswordUtil.initPassword(user.getPassword(), user.getPwdSalt()));
					user=userService.save(user);
				}
				//如果之前有这个老师的，就统一使用用旧老师的名称，如果没有就使用新的名称。
				TbLibManager tbLibManager=managerService.findAllByUserIdAndTypeAndState(user.getId(),Constant.LEADER,1);
				if(tbLibManager==null){//有老师user，生成type=2的老师
					tbLibManager=new TbLibManager();
					tbLibManager.setUserId(user.getId());
					tbLibManager.setType(Constant.LEADER);
					tbLibManager.setGrade(tbLibImTeacherTemp.getGrade());
					tbLibManager.setCourseName(tbLibImTeacherTemp.getSubject());
					tbLibManager.setCreateTime(new Date());
					tbLibManager.setName(tbLibImTeacherTemp.getTeacherName());
					tbLibManager.setTelephone(tbLibImTeacherTemp.getTeacherPhone());
					tbLibManager.setLibId(tbLibImTeacherTemp.getLibId());
					if(tbLibImTeacherTemp.getIsCorrect()!=null && tbLibImTeacherTemp.getIsCorrect()==1) {
						tbLibManager.setIsCorrect(1);
					}else {
						tbLibManager.setIsCorrect(0);
					}
					tbLibManager.setState(1);
					tbLibManager=managerService.save(tbLibManager);
				}else {
					tbLibImTeacherTemp.setTeacherId(user.getId());
					tbLibImTeacherTemp.setStatus(0);
					tbLibImTeacherTemp.setErrorReason("与平台已有老师信息冲突");
					this.save(tbLibImTeacherTemp);
					return 0;
				}
				
				TbLibManagerAudit audit= new  TbLibManagerAudit();
		        audit.setStatus(1);
		        audit.setTelephone(tbLibManager.getTelephone());
		        audit.setLibManagerId(tbLibManager.getId());
		        audit.setLibId(tbLibManager.getLibId());
		        audit.setType(tbLibManager.getType());
		        audit.setCreateTime(new Date());
		        audit.setLastUpdateTime(new Date());
		        audit.setUserId(tbLibManager.getUserId());
		        audit.setName(tbLibManager.getName());
		        auditService.save(audit);
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
			}
			return 1;
		}
		return 0;
	}
	


	@Override
	public int insertBatchTbLibImHeadmaster(List<TbClassTeacherTemp> tbLibImTeacherTemps) {
		int successNum=0;
		for (TbClassTeacherTemp tbLibImTeacherTemp : tbLibImTeacherTemps) {
			try {
				TbLibJoin tbLibJoin=null;
				if(StringUtils.isNotEmpty(tbLibImTeacherTemp.getLibId())){
					tbLibJoin=joinService.getById(tbLibImTeacherTemp.getLibId());
				}else{
					tbLibJoin=tbLibImStudentService.getTbLibJoinByLeagueName(tbLibImTeacherTemp.getLeagueName());
				}
				//无机构信息
				if(tbLibJoin!=null){
					successNum=successNum+this.insertTbLibImHeadmaster(tbLibJoin, tbLibImTeacherTemp);
				}else{
					successNum=-1;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return successNum;
	}


	@Override
	public int insertTbLibImHeadmaster(TbLibJoin libJoin, TbClassTeacherTemp tbLibImTeacherTemp) {
		if(libJoin!=null){
			tbLibImTeacherTemp.setLibId(libJoin.getId());
			tbLibImTeacherTemp.setLeagueName(libJoin.getLeagueName());
			tbLibImTeacherTemp.setCreateTime(new Date());
			tbLibImTeacherTemp.setStatus(1);
			
			try {
				User user=userService.findByMobile(tbLibImTeacherTemp.getTeacherPhone());
				if(user==null){//无老师user，生成老师user
					user=new User();
					user.setId(IdWorker.generateId());
					user.setUserName(tbLibImTeacherTemp.getTeacherName());
					user.setMobile(tbLibImTeacherTemp.getTeacherPhone());
					user.setCreateTime(new Date());
					user.setPassword("123456");
					user.setPwdSalt(PasswordUtil.generateSalt());
					user.setPassword(PasswordUtil.initPassword(user.getPassword(), user.getPwdSalt()));
					user=userService.save(user);
				}
				tbLibImTeacherTemp.setTeacherId(user.getId());
				
				//如果之前有这个老师的，就统一使用用旧老师的名称，如果没有就使用新的名称。
				TbClass tbClass=tbClassService.getTbClassGradeByLibId(tbLibImTeacherTemp.getLibId(),tbLibImTeacherTemp.getGrade(), tbLibImTeacherTemp.getClassName());
				if(tbClass==null) {
					tbClass=new TbClass();
					tbClass.setLibId(tbLibImTeacherTemp.getLibId());
					tbClass.setClassName(tbLibImTeacherTemp.getSubject());
					tbClass.setGrade(tbLibImTeacherTemp.getGrade());
					tbClass.setCreateTime(new Date());
					tbClass.setStatus(1);
					tbClass=tbClassService.save(tbClass);
				}
				TbClassTeacher tbClassType=tbClassTeacherService.getTbLibImTeacherByClassType(tbClass.getId());
				if(tbClassType==null) {
					TbClassTeacher tbClassTeacher=tbClassTeacherService.getTbLibImTeacherByClassIdAndType(tbClass.getId(),user.getId(),2);
					if(tbClassTeacher==null) {
						 tbClassTeacher=new TbClassTeacher();
						 tbClassTeacher.setClassId(tbClass.getId());
						 tbClassTeacher.setCreateTime(new Date());
						 tbClassTeacher.setLibId(tbLibImTeacherTemp.getLibId());
						 tbClassTeacher.setType(2);
						 tbClassTeacher.setStatus(1);
						 tbClassTeacher.setTeacherId(user.getId());
						 tbClassTeacherService.save(tbClassTeacher);
					}else {
						tbLibImTeacherTemp.setTeacherId(user.getId());
						tbLibImTeacherTemp.setStatus(0);
						tbLibImTeacherTemp.setErrorReason("已存在班级老师身份");
						this.save(tbLibImTeacherTemp);
						return 0;
					}
				}else {
					tbLibImTeacherTemp.setTeacherId(user.getId());
					tbLibImTeacherTemp.setStatus(0);
					tbLibImTeacherTemp.setErrorReason("已存在班主任级信息");
					this.save(tbLibImTeacherTemp);
					return 0;
				}
			
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
			}
			return 1;
		}
		return 0;
	}

}
