package net.wicp.tams.duckula.ops.pages.cas;

import org.apache.tapestry5.ioc.annotations.Inject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import net.wicp.tams.app.duckula.controller.bean.models.SysRoleAssign;
import net.wicp.tams.app.duckula.controller.bean.models.SysRoleRes;
import net.wicp.tams.app.duckula.controller.bean.models.SysTenant;
import net.wicp.tams.app.duckula.controller.bean.models.SysUser;
import net.wicp.tams.app.duckula.controller.dao.SqlMapper;
import net.wicp.tams.app.duckula.controller.dao.SysRoleAssignMapper;
import net.wicp.tams.app.duckula.controller.dao.SysRoleResMapper;
import net.wicp.tams.app.duckula.controller.dao.SysTenantMapper;
import net.wicp.tams.app.duckula.controller.dao.SysUserMapper;
import net.wicp.tams.common.apiext.UUIDGenerator;
import net.wicp.tams.common.constant.dic.YesOrNo;
import net.wicp.tams.duckula.ops.WebTools;
import net.wicp.tams.duckula.ops.pages.ParentPageBean;

public class TenantManager extends ParentPageBean<SysTenant> {

	@Inject
	private SysTenantMapper sysTenantMapper;
	@Inject
	private SysUserMapper sysUserMapper;
	@Inject
	private SysRoleAssignMapper sysRoleAssignMapper;
	@Inject
	private SysRoleResMapper sysRoleResMapper;
	@Inject
	private SqlMapper sqlMapper;

	private long[] roleResInit = new long[] { 1, 3, 1404404548439179266l, 1404404552566374402l, 1405720906263252993l, 4,
			1404404558501314561l, 1404404560069984257l, 1404404561642848257l, 1404404563022774273l,
			1404404564536918018l, 5, 1404404555280089089l, 1404404556332859393l, 1404404557448544258l };

	private long roleidInit = 1405760902957436931l;// 租户管理员角色

	@Override
	public void doSave(SysTenant t, boolean isInsert) {
		if (isInsert) {
			Long uniqueLong = UUIDGenerator.getUniqueLong();// 安全考虑，手动生成租户的uuid，不是自增长，
			t.setId(uniqueLong);
			this.sysTenantMapper.insert(t);
			// 新增一个默认用户TODO 不要拦截
			WebTools.skipSQLInterceptor(true);// 单次有效
			SysUser newuser = new SysUser();
			newuser.setUserName(t.getCode());
			newuser.setStatus(YesOrNo.yes.name());
			newuser.setPassword("$2a$10$2nMUZ5HG/P61GiB.XB7j2e.s2HCXoo6Nt3uUFsRvYfoYZynz5V84q");// 初始密码"123456"
			newuser.setTenantId(t.getId());
			this.sysUserMapper.insert(newuser);
			// 给这个用户分派管理员角色
			SysRoleAssign sysRoleAssign = new SysRoleAssign();
			sysRoleAssign.setAssignType("STAFF");
			sysRoleAssign.setRoleId(roleidInit);
			sysRoleAssign.setAssignValue(newuser.getId());
			sysRoleAssign.setTenantId(t.getId());
			this.sysRoleAssignMapper.insert(sysRoleAssign);
			// 添加资源与角色关系（因为不同租户会有不同的资源分派）
			for (long resId : roleResInit) {
				SysRoleRes sysRoleRes = new SysRoleRes();
				sysRoleRes.setAuthCode(1l);
				sysRoleRes.setResId(resId);
				sysRoleRes.setRoleId(roleidInit);
				sysRoleRes.setTenantId(t.getId());
				sysRoleResMapper.insert(sysRoleRes);
			}
		} else {
			this.sysTenantMapper.updateByPrimaryKeySelective(t);
		}
	}

	@Override
	public void doDel(String id) {
		this.sysTenantMapper.deleteById(id);
	}

	@Override
	public BaseMapper<SysTenant> getBaseMapper() {
		return this.sysTenantMapper;
	}

	@Override
	public void packageQuery(SysTenant t, QueryWrapper<SysTenant> queryWrapper) {

	}

}