package com.hotent.uc.manager.impl;

import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.TenantManageManager;
import com.hotent.uc.model.TenantManage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.uc.dao.TenantMailServerDao;
import com.hotent.uc.manager.TenantMailServerManager;
import com.hotent.uc.model.TenantMailServer;

import javax.annotation.Resource;

/**
 * 
 * <pre> 
 * 描述：租户邮件服务器信息 处理实现类
 * 构建组：x7
 * 作者:zhangxw
 * 邮箱:zhangxw@jee-soft.cn
 * 日期:2020-04-17 11:01:30
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@Service("tenantMailServerManager")
public class TenantMailServerManagerImpl extends BaseManagerImpl<TenantMailServerDao, TenantMailServer> implements TenantMailServerManager{
	@Resource
	TenantManageManager tenantManageManager;

	@Override
	public TenantMailServer getByTenantId(String tenantId) {
		return baseMapper.getByTenantId(tenantId);
	}

	@Override
	@Transactional
	public void deleteByTenantId(String tenantId) {
		baseMapper.deleteByTenantId(tenantId);
	}

	@Override
	public CommonResult<String> saveTenantMailServer(TenantMailServer tenantMailServer) throws Exception {
		String msg = "添加租户邮件服务器信息成功";
		if(StringUtil.isEmpty(tenantMailServer.getTenantId())){
			return new CommonResult<String>(false,"租户id【tenantId】必填！");
		}
		TenantMailServer mailServer = this.getByTenantId(tenantMailServer.getTenantId());
		if(StringUtil.isEmpty(tenantMailServer.getId()) && BeanUtils.isEmpty(mailServer)){
			this.create(tenantMailServer);
		}else if(StringUtil.isEmpty(tenantMailServer.getId()) && BeanUtils.isNotEmpty(mailServer)){
			TenantManage tenant = tenantManageManager.get(mailServer.getTenantId());
			return new CommonResult<String>(false,"租户【"+tenant.getName()+"】已存在邮件服务器信息，不需要多次添加！");
		}else{
			this.update(tenantMailServer);
			msg = "更新租户邮件服务器信息成功";
		}
		return new CommonResult<String>(msg);
	}

}
