package com.vctgo.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vctgo.common.core.constant.SecurityConstants;
import com.vctgo.common.core.web.domain.TenantEntity;
import com.vctgo.common.redis.service.RedisService;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysPlatformUser;
import com.vctgo.system.api.domain.SysUser;
import com.vctgo.system.api.domain.SysUserTempalte;
import com.vctgo.system.mapper.SysPlatformUserMapper;
import com.vctgo.system.mapper.SysUserMapper;
import com.vctgo.system.service.SystemCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SystemCommonServiceImpl  implements SystemCommonService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private SysPlatformUserMapper sysPlatformUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public void synchronizedUserInfo(SysUserTempalte sysUserTempalte) {
        String tenantId = SecurityUtils.getTenantId().toString().intern();
        synchronized (tenantId) {
            // 将新增用户添加到内存中
            String value = redisService.getCacheObject(SecurityConstants.DETAILS_TENANT + tenantId);
            if (Objects.nonNull(value)) {
                List<SysUserTempalte> sysUserTempalteList = JSONArray.parseArray(value, SysUserTempalte.class);
                if (!CollectionUtils.isEmpty(sysUserTempalteList)) {
                    sysUserTempalteList.removeIf(sysUserTemplateTmp -> sysUserTemplateTmp.getUserId().longValue() == sysUserTempalte.getUserId().longValue());
                }
                sysUserTempalteList.add(sysUserTempalte);
                redisService.setCacheObject(SecurityConstants.DETAILS_TENANT + tenantId, JSONArray.toJSONString(sysUserTempalteList));
            } else {
                redisService.setCacheObject(SecurityConstants.DETAILS_TENANT + tenantId, JSONArray.toJSONString(Collections.singletonList(sysUserTempalte)));
            }
        }
    }

    @Override
    public void loadingSysPlatformUserCache() {
        LambdaQueryWrapper<SysPlatformUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.select(SysPlatformUser::getUserId, SysPlatformUser::getNickName, SysPlatformUser::getUserName, SysPlatformUser::getUserStatus,
                SysPlatformUser::getAccountStatus, SysPlatformUser::getStatus, SysPlatformUser::getDelFlag, TenantEntity::getTenantId);
        log.info("开始同步平台用户数据到redis");
        List<SysPlatformUser> sysUserList = sysPlatformUserMapper.selectList(sysUserWrapper);
        sysUserList.stream().collect(Collectors.groupingBy(SysPlatformUser::getTenantId)).forEach((tenantId, sysUserListTmp) -> {
            redisService.setCacheObject(SecurityConstants.DETAILS_TENANT + tenantId, JSONObject.toJSONString(sysUserListTmp));
        });
        log.info("结束同步平台用户数据到redis");
    }

    @Override
    public void loadingUserCache() {
        SecurityUtils.setTemporaryIgnoreTables(Collections.singletonList("sys_user"));
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.select(SysUser::getUserId, SysUser::getNickName, SysUser::getUserAccount, SysUser::getUserStatus, SysUser::getAccountStatus, SysUserTempalte::getStatus, SysUser::getDelFlag, SysUser::getPhonenumber, TenantEntity::getTenantId);
        log.info("开始同步幼儿园用户数据到redis");
        List<SysUser> sysUserList = sysUserMapper.selectList(sysUserWrapper);
        sysUserList.stream().filter(sysUser -> Objects.nonNull(sysUser.getTenantId()))
                .collect(Collectors.groupingBy(SysUser::getTenantId))
                .forEach((tenantId, sysUserListTmp) -> redisService.setCacheObject(SecurityConstants.DETAILS_TENANT + tenantId, JSONObject.toJSONString(sysUserListTmp)));
        log.info("结束同步幼儿园用户数据到redis");
    }

}
