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


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.common.tenant.TenantContextHolder;
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.EmployeeDao;
import com.keao.edu.user.dao.SysUserDao;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.enums.YesOrNoEnum;
import com.keao.edu.user.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Integer, Employee> implements EmployeeService {
	
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private ImFeignService imFeignService;
	@Autowired
	private SysUserDao sysUserDao;

	@Override
	public BaseDAO<Integer, Employee> getDAO() {
		return employeeDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(Employee employee) {
		String tenantId = TenantContextHolder.getTenantId().toString();
		SysUser sysUser = employee.getSysUser();
		employee.setTenantId(tenantId);
		SysUser user = sysUserDao.queryByPhone(sysUser.getPhone());
		if(user != null && user.getId() != null){
			Employee employee1 = employeeDao.get(user.getId());
			if(employee1 == null){
				if(StringUtils.isEmpty(user.getPassword())){
					sysUser.setPassword(new BCryptPasswordEncoder().encode("123456"));
				}
				employee.setUserId(user.getId());
				employeeDao.insert(employee);
				//新增用户角色
				sysUserDao.batchAddEmployeeRole(user.getId(),sysUser.getRoles());
				user.setUserType(user.getUserType() + ",SYSTEM");
				sysUserDao.update(user);
			}else if(employee1.getDelFlag()){
				employee1.setDelFlag(false);
				employeeDao.update(employee1);
			}else{
				throw new BizException("用户已存在");
			}
			return;
		}
		user = new SysUser();
		user.setTenantId(tenantId);
		user.setPassword(new BCryptPasswordEncoder().encode("123456"));
		user.setUserType("SYSTEM");
		sysUserDao.insert(user);

		employee.setUserId(user.getId());
		employeeDao.insert(employee);
		//新增用户角色
		sysUserDao.batchAddEmployeeRole(user.getId(),sysUser.getRoles());
		ImResult imResult = imFeignService.register(new ImUserModel(user.getId().toString(), user.getRealName(),null));
		user.setImToken(imResult.getToken());
		sysUserDao.update(user);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateEmployee(Employee employee) {
		SysUser sysUser = employee.getSysUser();
		if(null == employeeDao.get(sysUser.getId())){
			throw new BizException("员工信息不存在");
		}
		employee.setUserId(sysUser.getId());
		if(StringUtils.isNotEmpty(sysUser.getPhone())){
			SysUser queryByPhone = sysUserDao.queryByPhone(sysUser.getPhone());
			if(queryByPhone != null && !sysUser.getId().equals(queryByPhone.getId())){
				throw new BizException("手机号已存在");
			}
		}
		employeeDao.update(employee);
		//删除当前用户角色
		sysUserDao.delEmployeeRole(sysUser.getId());
		//新增用户角色
		sysUserDao.batchAddEmployeeRole(sysUser.getId(),sysUser.getRoles());
		sysUserDao.update(sysUser);
		imFeignService.update(new ImUserModel(sysUser.getId().toString(),sysUser.getRealName(),sysUser.getAvatar()));
	}
}