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

import com.alibaba.fastjson.JSON;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.auth.api.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.dao.OrganizationDao;
import com.ym.mec.biz.dal.dao.SubjectDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.page.TeacherQueryInfo;
import com.ym.mec.biz.service.TeacherService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl extends BaseServiceImpl<Integer, Teacher>  implements TeacherService {
	
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private SubjectDao subjectDao;
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Integer, Teacher> getDAO() {
		return teacherDao;
	}

	@Override
	public Teacher findByCertificateNum(String certificateNum){
		return teacherDao.findByCertificateNum(certificateNum);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(Teacher teacher) throws Exception {
		SysUser user = sysUserFeignService.getUserByMobile(teacher.getPhone());
		if(user != null && user.getId() != null){
			throw new Exception("系统已存在该手机号的老师，请核查");
		}
		//保存用户表信息
		sysUserFeignService.addUser(teacher);
		//注册到融云

		//保存教师表数据
		teacherDao.insert(teacher);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateTea(Teacher teacher) {
		teacher.setUpdateTime(new Date());
		teacherDao.update(teacher);
		teacherDao.updateUser(teacher);
	}

	@Override
	public void deleteTeacher(Long userId) {
		SysUser sysUser=new SysUser();
		sysUser.setId(userId.intValue());
		sysUser.setDelFlag(YesOrNoEnum.YES);
		teacherDao.updateUser(sysUser);
	}

	@Override
	public PageInfo<Teacher> queryPageDetail(TeacherQueryInfo queryInfo) {
		PageInfo<Teacher> pageInfo = queryPage(queryInfo);
		List<Teacher> teachers = pageInfo.getRows();
		if(teachers != null && teachers.size() > 0){
			teachers.forEach(e->{
				if(StringUtils.isNotEmpty(e.getSubjectId())){
					e.setSubjectId(JSON.toJSONString(subjectDao.findBySubIds(e.getSubjectId())));
				}
			});
		}
		return pageInfo;
	}

	@Override
	public Teacher getDetail(Integer id) {
		Teacher teacher = teacherDao.get(id);
		if(teacher != null){
			if(StringUtils.isNotEmpty(teacher.getFlowOrganRange())){
				List<String> organNames = organizationDao.findByOrganIds(teacher.getFlowOrganRange());
				teacher.setFlowOrganRange(JSON.toJSONString(organNames));
			}
			if(StringUtils.isNotEmpty(teacher.getSubjectId())){
				teacher.setSubjectId(JSON.toJSONString(subjectDao.findBySubIds(teacher.getSubjectId())));
			}
		}
		return teacher;
	}

}