package cn.ezhizao.project.tenant.service.impl;

import cn.ezhizao.project.setting.domain.BizSource;
import cn.ezhizao.project.setting.service.IBizSourceService;
import cn.ezhizao.project.system.domain.SysDept;
import cn.ezhizao.project.system.domain.SysRole;
import cn.ezhizao.project.system.domain.SysUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ezhizao.framework.aspectj.lang.annotation.DataSource;
import cn.ezhizao.framework.aspectj.lang.enums.DataSourceType;
import cn.ezhizao.project.tenant.domain.Tenant;
import cn.ezhizao.project.tenant.mapper.TenantMapper;
import cn.ezhizao.project.tenant.service.ITenantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author zhushan
 */
@Service
@DataSource(value=DataSourceType.MASTER)
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant>  implements ITenantService {

    @Resource
    private TenantMapper tenantMapper;
    @Resource
    private IBizSourceService sourceService;

    @Override
    public List<Tenant> getList(Tenant tenant) {
        return tenantMapper.getList(tenant);
    }

    @Override
    public void setTenantInfo(Tenant tenant) {

    }

    /**
     * 判断是否初始化管理员
     * @param tenantId 租户id
     * @return 判断结果
     */
    @Override
    public boolean checkAdmin(Long tenantId) {
        return tenantMapper.checkAdminUser(tenantId);
    }

    @Override
    public boolean checkAdminRole(Long tenantId) {
        return tenantMapper.checkAdminRole(tenantId);
    }

    /**
     * 获取租户管理员
     * @param tenantId 租户外键
     * @return 管理员信息
     */
    @Override
    public SysUser getAdminUser(Long tenantId) {
        SysUser admin = tenantMapper.getInitAdminUser(tenantId);
        if (admin == null) {
            admin = tenantMapper.getAdminUser(tenantId);
        }
        return admin;
    }

    /**
     * 获取初始化角色信息
     * @param tenantId 租户外键
     * @return 角色信息
     */
    @Override
    public List<SysRole> getInitRoles(Long tenantId) {
        return tenantMapper.getInitRoles(tenantId);
    }

    /**
     * 判断是否初始化部门
     * @param tenantId
     * @return
     */
    @Override
    public boolean checkInitDept(Long tenantId) {
        return tenantMapper.checkInitDept(tenantId);
    }

    /**
     * 判断角色是否存在角色包含所有的菜单页
     * @param tenantId
     * @param roleId
     * @return 返回判断结果
     */
    @Override
    public boolean checkAdminMenu(Long tenantId, Long roleId) {
        // 获取该租户的所有菜单
        List<Long> menuIds = tenantMapper.getMenuIds(tenantId);
        // 获取该角色的所有菜单
        List<Long> roleMenuIds = tenantMapper.checkAdminMenu(roleId);
        return menuIds.stream().allMatch(v -> roleMenuIds.contains(v));
    }

    /**
     * 判断是否已初始化 客户来源类型
     * @param tenantId 租户外键
     * @return 返回结果
     */
    @Override
    public boolean checkInitSource(Long tenantId) {
        return tenantMapper.checkInitSource(tenantId);
    }

    @Override
    public List<Long> getTenantMenuIds(Long tenantId) {
        return tenantMapper.getMenuIds(tenantId);
    }

    @Override
    public boolean initSource(Long tenantId) {
        List<BizSource> sources = tenantMapper.getInsertSource(tenantId);
        sources.forEach(l -> l.setTenantId(tenantId));
        return sourceService.saveBatch(sources);
    }

    @Override
    public List<SysRole> initRolesData() {
        return tenantMapper.initRolesData();
    }

    @Override
    public SysDept getInitDept(Long tenantId) {
        return tenantMapper.getInitDept(tenantId);
    }
}
