package pwc.taxtech.atms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.dao.CacheMapper;
import pwc.taxtech.atms.dto.CacheDto;
import pwc.taxtech.atms.entity.Cache;
import pwc.taxtech.atms.entity.CacheExample;

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

import static pwc.taxtech.atms.common.CommonUtils.copyProperties;

@Service
public class CacheServiceImpl {

    @Autowired
    private CacheMapper cacheMapper;

    public List<CacheDto> getAllCache() {
        List<Cache> list = cacheMapper.selectByExample(null);
        return list.stream().map(this::convertToCacheDto).collect(Collectors.toList());
    }

    public CacheDto getCacheByKey(String cacheKey) {
        CacheExample example = new CacheExample();
        example.createCriteria().andCacheKeyEqualTo(cacheKey);
        List<Cache> list = cacheMapper.selectByExample(example);
        return list.stream().findFirst().map(this::convertToCacheDto).orElse(null);
    }

    private CacheDto convertToCacheDto(Cache from) {
        CacheDto to = copyProperties(from, new CacheDto());
        to.setCacheTime(from.getLastModifyTime());
        return to;
    }

}