package com.fs.express.cache.impl;

import com.fs.express.FsStoreDeliversService;
import com.fs.express.cache.FsStoreDeliversCacheService;
import com.fs.store.domain.FsStoreDelivers;
import com.fs.store.domain.FsUser;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class FsStoreDeliversCacheServiceImpl implements FsStoreDeliversCacheService {
    @Autowired
    private FsStoreDeliversService fsStoreDeliversService;
    private static final Cache<Long, Integer> EXPRESS_STATUS_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();

    @Override
    public Integer findExpressStatusByOrderId(Long orderId) {
        EXPRESS_STATUS_CACHE.get(orderId, key -> {
            List<FsStoreDelivers> byOrderId = fsStoreDeliversService.findByOrderId(orderId);
            if(CollectionUtils.isNotEmpty(byOrderId)){
                FsStoreDelivers delivers = byOrderId.get(0);
                return delivers.getStatus();
            }
            return 0;
        });

        return 0;
    }
}
