package com.hotent.uc.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import com.hotent.base.util.StringUtil;
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.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.uc.dao.TenantTypeDao;
import com.hotent.uc.manager.TenantManageManager;
import com.hotent.uc.manager.TenantTypeManager;
import com.hotent.uc.model.TenantType;

/**
 * 
 * <pre> 
 * 描述：租户类型管理 处理实现类
 * 构建组：x7
 * 作者:zhangxw
 * 邮箱:zhangxw@jee-soft.cn
 * 日期:2020-04-17 10:52:37
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@Service("tenantTypeManager")
public class TenantTypeManagerImpl extends BaseManagerImpl<TenantTypeDao, TenantType> implements TenantTypeManager{

	@Resource
	TenantManageManager tenantManageManager;
	
	@Override
	public List<TenantType> getByStatus(String status,List<String> authIds) {
		return baseMapper.getByStatus(status,BeanUtils.isNotEmpty(authIds)?authIds:null);
	}
	
	@Override
	public TenantType getByCode(String code) {
		return baseMapper.getByCode(code);
	}

	@Override
	public TenantType getDefault() {
		return baseMapper.getDefault();
	}

	@Override
	@Transactional
	public CommonResult<String> setDefault(String code) {
		TenantType type = baseMapper.getByCode(code);
		if(BeanUtils.isNotEmpty(type)){
			baseMapper.setNotDefault();
			type.setIsDefault("1");
			super.update(type);
			return new CommonResult<String>(true,"设置成功！");
		}else{
			return new CommonResult<String>(false,"租户类型编码【"+code+"】不存在！");
		}
	}

	@Override
	public CommonResult<String> saveTenantType(TenantType tenantType) throws Exception {
		String msg = "添加租户类型管理成功";
		if(StringUtil.isEmpty(tenantType.getId())){
			TenantType type = this.getByCode(tenantType.getCode());
			if(BeanUtils.isNotEmpty(type)){
				return new CommonResult<String>(false,"新增失败！租户编码【"+tenantType.getCode()+"】已存在，请输入其他编码。");
			}else{
				tenantType.setStatus("enable");
				this.create(tenantType);
			}
		}else{
			TenantType type = this.get(tenantType.getId());
			if(BeanUtils.isNotEmpty(type) && !type.getCode().equals(tenantType.getCode())){
				return new CommonResult<String>(false,"更新失败！不能变更租户编码。");
			}else{
				this.update(tenantType);
				msg = "更新租户类型管理成功";
			}
		}
		return new CommonResult<String>(msg);
	}

	@Override
	public CommonResult<String> removes(String... ids) throws Exception {
		for (String id : ids) {
			TenantType type = this.get(id);
			if(BeanUtils.isNotEmpty(type) && TenantType.DEFAULT.equals(type.getIsDefault())){
				return new CommonResult<String>(false,"被删除租户类型【"+type.getName()+"】为默认租户类型，不能被删除！");
			}
			List<TenantManage> list = tenantManageManager.getByTypeId(id);
			if(BeanUtils.isNotEmpty(list)){
				return new CommonResult<String>(false,"被删除租户类型【"+type.getName()+"】下已存在租户，不能被删除！");
			}
		}
		this.removeByIds(ids);
		return new CommonResult<String>(true, "删除成功");
	}
}
