package com.vctgo.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.constant.CacheConstants;
import com.vctgo.common.mybatisplus.aop.TemporaryIgnoreTables;
import com.vctgo.common.redis.service.RedisService;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysTenant;
import com.vctgo.system.api.model.LoginUser;
import com.vctgo.system.domain.SysConfigPermission;
import com.vctgo.system.mapper.SysConfigPermissionMapper;
import com.vctgo.system.service.ISysConfigPermissionService;
import com.vctgo.system.service.ISysTenantService;
import com.vctgo.system.util.ConstantUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import org.springframework.transaction.annotation.Transactional;

/**
 * 权限架构--系统基础配置Service业务层处理
 *
 * @author caesar
 * @date 2023-10-08
 */
@Slf4j
@Service
public class SysConfigPermissionServiceImpl implements ISysConfigPermissionService {

    @Autowired
    private SysConfigPermissionMapper sysConfigPermissionMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ISysTenantService sysTenantService;

    /**
     * 查询权限架构--系统基础配置
     *
     * @param id 权限架构--系统基础配置主键
     * @return 权限架构--系统基础配置
     */
    @Override
    public SysConfigPermission selectSysConfigPermissionById(Long id) {
        return sysConfigPermissionMapper.selectById(id);
    }

    /**
     * 查询权限架构--系统基础配置列表-分页
     *
     * @param sysConfigPermission 权限架构--系统基础配置
     * @return 权限架构--系统基础配置
     */
    @Override
    public IPage<SysConfigPermission> selectSysConfigPermissionPage(SysConfigPermission sysConfigPermission) {
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L));
        return sysConfigPermissionMapper.selectSysConfigPermissionList(mpPage, sysConfigPermission);
    }

    /**
     * 查询权限架构--系统基础配置列表
     *
     * @param sysConfigPermission 权限架构--系统基础配置
     * @return 权限架构--系统基础配置
     */
    @Override
    public List<SysConfigPermission> selectSysConfigPermissionList(SysConfigPermission sysConfigPermission) {
        return sysConfigPermissionMapper.selectSysConfigPermissionList(sysConfigPermission);
    }

    /**
     * 新增权限架构--系统基础配置
     *
     * @param sysConfigPermission 权限架构--系统基础配置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysConfigPermission(SysConfigPermission sysConfigPermission) {
        return sysConfigPermissionMapper.insert(sysConfigPermission);
    }

    /**
     * 修改权限架构--系统基础配置
     *
     * @param sysConfigPermission 权限架构--系统基础配置
     * @return 结果
     */
    @TemporaryIgnoreTables
    @Transactional
    @Override
    public int updateSysConfigPermission(SysConfigPermission sysConfigPermission) {
        sysConfigPermission.setUpdateById(SecurityUtils.getUserId());
        sysConfigPermission.setUpdateTime(new Date());
        int result = sysConfigPermissionMapper.updateById(sysConfigPermission);

        // 通过租户类型获取对应的租户id列表
        SysTenant sysTenant = new SysTenant();
        sysTenant.setDelFlag("0");
        sysTenant.setTenantType(Integer.valueOf(sysConfigPermission.getTenantType()));
        List<SysTenant> sysTenantList = sysTenantService.selectSysTenantList(sysTenant);
        if (!sysTenantList.isEmpty()) {
            List<Long> tenantIdList = sysTenantList.stream().map(SysTenant::getId).collect(Collectors.toList());
            SecurityUtils.setTemporaryIgnoreTables(Arrays.asList("st", "su", "sys_service", "ss", "sys_re_service_tenant", "srst", "sys_tenant", "sys_user"));
            // 让当前租户下所有人员都退出登录
            List<String> keyList = new ArrayList<>();
            Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
            for (String key : keys) {
                LoginUser onlineUser = redisService.getCacheObject(key);
                if (Objects.nonNull(onlineUser)
                        && Objects.nonNull(onlineUser.getSysUser())
                        && Objects.nonNull(onlineUser.getSysUserTempalte().getTenantId())
                        && tenantIdList.contains(onlineUser.getSysUserTempalte().getTenantId())) {
                    keyList.add(key);
                }
            }
            redisService.deleteObject(keyList);
        }
        return result;
    }

    /**
     * 逻辑删除权限架构--系统基础配置
     *
     * @param ids 需要删除的权限架构--系统基础配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int logicalDeleteSysConfigPermissionByIds(Long[] ids) {
        SysConfigPermission sysConfigPermission = new SysConfigPermission();
        sysConfigPermission.setUpdateById(SecurityUtils.getUserId());
        sysConfigPermission.setUpdateBy(SecurityUtils.getUsername());
        sysConfigPermission.setUpdateTime(new Date());
        sysConfigPermission.setIsDeleted(ConstantUtils.IS_DELETED_1);
        return sysConfigPermissionMapper.logicalDeleteSysConfigPermissionByIds(ids, sysConfigPermission);
    }

    /**
     * 批量删除权限架构--系统基础配置
     *
     * @param ids 需要删除的权限架构--系统基础配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysConfigPermissionByIds(Long[] ids) {
        return sysConfigPermissionMapper.deleteSysConfigPermissionByIds(ids);
    }

    /**
     * 删除权限架构--系统基础配置信息
     *
     * @param id 权限架构--系统基础配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysConfigPermissionById(Long id) {
        return sysConfigPermissionMapper.deleteById(id);
    }
}
