package com.usky.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.system.RemoteDeptService;
import com.usky.system.domain.SysTenant;
import com.usky.system.domain.SysUserRole;
import com.usky.system.domain.SysUserTenant;
import com.usky.system.mapper.SysUserRoleMapper;
import com.usky.system.mapper.SysUserTenantMapper;
import com.usky.system.service.SysTenantService;
import com.usky.system.service.SysUserTenantService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2024-09-11
 */
@Service
public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantMapper, SysUserTenant> implements SysUserTenantService {

    @Autowired
    private SysTenantService sysTenantService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 企业邀请用户
     *
     * @param sysUserTenant
     */
    @Override
    @Transactional
    public void insertInviteUser(SysUserTenant sysUserTenant) {
        int userCount = baseMapper.checkUserIdUnique(sysUserTenant.getTenantId(), sysUserTenant.getUserId());
        if (userCount > 0) {
            throw new BusinessException("用户已绑定，无法重复绑定！");
        } else {
            // SysUserRole sysUserRole = sysUserRoleMapper.selectOne(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, sysUserTenant.getUserId()).eq(SysUserRole::getTenantId, sysUserTenant.getTenantId()));
            // if (sysUserRole == null) {
            //     SysUserRole sysUserRole1 = new SysUserRole();
            //     sysUserRole1.setUserId(sysUserTenant.getUserId());
            //     sysUserRole1.setTenantId(sysUserTenant.getTenantId());
            //     sysUserRoleMapper.insert(sysUserRole1);
            // }
            this.save(sysUserTenant);
        }
    }

    /**
     * 企业解绑用户
     *
     * @param tenantId 租户ID
     * @param userId 用户ID
     */
    @Override
    @Transactional
    public void deleteUserTenant(Integer tenantId, Long userId) {
        baseMapper.deleteUserTenant(tenantId, userId);
        Long[] userIds = new Long[]{userId};
        sysUserRoleMapper.deleteUserRole(userIds, tenantId);
    }

    /**
     * 根据用户查询企业下拉框
     */
    @Override
    @Transactional
    public List<SysTenant> getTenantByUser(Long userId) {
        LambdaQueryWrapper<SysUserTenant> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysUserTenant::getUserId, userId);
        List<SysUserTenant> userTenants = this.list(queryWrapper);
        List<Integer> tenantIdList = new ArrayList<>();
        List<SysTenant> tenantList = new ArrayList<>();
        LambdaQueryWrapper<SysTenant> queryWrapper1 = Wrappers.lambdaQuery();
        if (CollectionUtils.isNotEmpty(userTenants)) {
            for (int i = 0; i < userTenants.size(); i++) {
                tenantIdList.add(userTenants.get(i).getTenantId());
            }
            queryWrapper1.in(SysTenant::getId, tenantIdList)
                    .eq(SysTenant::getStatus, 0);
            tenantList = sysTenantService.list(queryWrapper1);
        }
        return tenantList;
    }

    @Override
    @Transactional
    public Boolean getIdByUser(Integer tenantId, Long userId) {
        LambdaQueryWrapper<SysUserTenant> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysUserTenant::getUserId, userId)
                .eq(SysUserTenant::getTenantId, tenantId);
        List<SysUserTenant> userTenants = this.list(queryWrapper);
        return userTenants.get(0).getIsDefault();
    }
}
