package com.hzjt.service;

import com.google.gson.Gson;
import com.hzjt.domain.Storageserver;
import com.hzjt.mapper.StorageServerMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CacheLoadService {
    private static final Logger log = LoggerFactory.getLogger(CacheLoadService.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${spring.redis.cachekey.ftplist}")
    private String ftplistkey;

    @Autowired
    private StorageServerMapper storageServerMapper;

    public boolean loadFtpCache() {
        try {
            Storageserver server = new Storageserver();
            server.setServerstatus(0);//�����õ�
            server.setServertype("ftp");
            List<Storageserver> storageServers = storageServerMapper.queryStorageServerAll(server);
            if (!storageServers.isEmpty() && storageServers.size() > 0) {
                stringRedisTemplate.opsForValue().set(ftplistkey, new Gson().toJson(storageServers),60*10, TimeUnit.SECONDS);
            } else {
                System.out.println("storageServers.isEmpty");
            }
            return true;
        } catch (Exception e) {
            System.out.println(e.toString());
            log.error(e.getMessage());
            return false;
        }
    }

}
