package com.keao.edu.user.service.impl;


import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.exception.BizException;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.im.api.client.ImFeignService;
import com.keao.edu.im.api.entity.ImResult;
import com.keao.edu.im.api.entity.ImUserModel;
import com.keao.edu.user.dao.SysUserDao;
import com.keao.edu.user.dao.TeacherDao;
import com.keao.edu.user.entity.Teacher;
import com.keao.edu.user.enums.YesOrNoEnum;
import com.keao.edu.user.service.TeacherService;
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;

@Service
public class TeacherServiceImpl extends BaseServiceImpl<Integer, Teacher> implements TeacherService {

	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private ImFeignService imFeignService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

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

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int add(Teacher teacher) {
		SysUser queryUserInfo = sysUserFeignService.queryUserInfo();
		SysUser sysUser = teacher.getSysUser();
		SysUser user = sysUserDao.queryByPhone(sysUser.getPhone());
		Date date = new Date();
		String tenantId = queryUserInfo.getTenantId();
		teacher.setTenantId(tenantId);
		sysUser.setTenantId(tenantId);
		if(user != null && user.getId() != null){
			Integer id = user.getId();
			sysUser.setId(id);
			Teacher teacher1 = teacherDao.get(id);
			if(teacher1 == null || teacher1.getUserId() == null){
				teacher.setUserId(id);
				teacherDao.insert(teacher);
				sysUser.setUserType(user.getUserType() + ",TEACHER");
				sysUser.setUpdateTime(date);
				sysUserDao.update(sysUser);
			}else if (teacher1.getDelFlag() == YesOrNoEnum.YES){
				teacher.setDelFlag(YesOrNoEnum.NO);
				teacherDao.update(teacher);
			}else {
				throw new BizException("用户已存在");
			}
			return id;
		}
		sysUser.setUserType("TEACHER");
		sysUser.setUsername(sysUser.getRealName());
		sysUserDao.insert(sysUser);
		Integer userId = sysUser.getId();
		teacher.setUserId(userId);
		teacherDao.insert(teacher);
		ImResult imResult = imFeignService.register(new ImUserModel(userId.toString(), sysUser.getUsername(), sysUser.getAvatar()));
		if(imResult != null){
			sysUser.setImToken(imResult.getToken());
			sysUserDao.update(sysUser);
		}
		return userId;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateTeacher(Teacher teacher) {
		SysUser sysUser = teacher.getSysUser();
		sysUser.setId(teacher.getUserId());
		if(null == teacherDao.get(sysUser.getId())){
			throw new BizException("教师信息不存在");
		}
		if(StringUtils.isNotEmpty(sysUser.getPhone())){
			SysUser queryUserByMobile = sysUserDao.queryByPhone(sysUser.getPhone());
			if(queryUserByMobile != null && !sysUser.getId().equals(queryUserByMobile.getId())){
				throw new BizException("手机号已存在");
			}
		}
		Date date = new Date();
		teacher.setUpdateTime(date);
		teacherDao.update(teacher);
		sysUserDao.update(sysUser);
		imFeignService.update(new ImUserModel(sysUser.getId().toString(),sysUser.getRealName(),sysUser.getAvatar()));
	}
}