package org.sky.system.rule.tenant;

import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleComponent;
import org.sky.core.tool.constant.KdConstant;
import org.sky.core.tool.utils.Func;
import org.sky.system.cache.ParamCache;
import org.sky.system.pojo.entity.Tenant;
import org.sky.system.pojo.entity.User;
import org.sky.system.pojo.enums.UserType;
import org.sky.system.rule.context.TenantContext;

import java.util.Date;

import static org.sky.common.constant.TenantConstant.DEFAULT_PASSWORD;
import static org.sky.common.constant.TenantConstant.PASSWORD_KEY;
import static org.sky.system.rule.constant.TenantRuleConstant.TENANT_USER_RULE;

/**
 * 租户用户构建
 *
 */
@LiteRuleComponent(id = TENANT_USER_RULE, name = "租户用户构建")
public class TenantUserRule extends RuleComponent {
	@Override
	public void process() {
		// 获取上下文
		TenantContext contextBean = this.getContextBean(TenantContext.class);
		Tenant tenant = contextBean.getTenant();

		// 新建租户对应的默认管理用户
		User user = new User();
		user.setTenantId(tenant.getTenantId());
//		user.setName("admin");
//		user.setRealName("admin");
		user.setAccount("admin");
		user.setName("admin");
		user.setRealName("admin");
		user.setAccount(tenant.getUscc());
//		user.setAccount("91110108MA01XYZ123");
		// 获取参数配置的密码
		String password = Func.toStr(ParamCache.getValue(PASSWORD_KEY), DEFAULT_PASSWORD);
		user.setPassword(password);
		user.setBirthday(new Date());
		user.setSex(1);
		user.setUserType(UserType.WEB.getCategory());
		user.setIsDeleted(KdConstant.DB_NOT_DELETED);

		// 设置上下文
		contextBean.setUser(user);
	}
}
