package com.vctgo.file.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.vctgo.common.core.constant.SecurityConstants;
import com.vctgo.common.redis.service.RedisService;
import com.vctgo.file.service.IFileCommonService;
import com.vctgo.system.api.domain.SysUserTempalte;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class FileCommonServiceImpl implements IFileCommonService {

    @Autowired
    private RedisService redisService;

    @Override
    public List<SysUserTempalte> selectSysUserTemplateList(Long tenantId) {
        List<String> multiCacheValue = redisService.getMultiCacheValue(Arrays.asList(SecurityConstants.DETAILS_TENANT + "9999", SecurityConstants.DETAILS_TENANT + tenantId));
        List<SysUserTempalte> sysUserTempalteList = new ArrayList<>();
        multiCacheValue.forEach(cache -> sysUserTempalteList.addAll(JSONArray.parseArray(cache, SysUserTempalte.class)));
        return sysUserTempalteList;
    }

    @Override
    public List<SysUserTempalte> selectSysUserTemplateListByTenantId(Long tenantId) {
        List<String> multiCacheValue = redisService.getMultiCacheValue(Collections.singletonList(SecurityConstants.DETAILS_TENANT + tenantId));
        List<SysUserTempalte> sysUserTempalteList = new ArrayList<>();
        multiCacheValue.forEach(cache -> sysUserTempalteList.addAll(JSONArray.parseArray(cache, SysUserTempalte.class)));
        return sysUserTempalteList;
    }

    @Override
    public List<SysUserTempalte> selectAllSysUserTemplateList() {
        Collection<String> keys = redisService.keys(SecurityConstants.DETAILS_TENANT+"*");
        List<SysUserTempalte> sysUserTempalteList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(keys)) {
            List<String> multiCacheValue = redisService.getMultiCacheValue(new ArrayList<>(keys));
            multiCacheValue.forEach(cache -> sysUserTempalteList.addAll(JSONArray.parseArray(cache, SysUserTempalte.class)));
        }
        return sysUserTempalteList;
    }
}
