package com.fs.hisStore.service.cache.impl;

import com.fs.course.domain.FsUserCourse;
import com.fs.course.service.IFsUserCourseService;
import com.fs.hisStore.service.cache.IFsUserCourseCacheService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
@Slf4j
public class FsUserCourseCacheScrmServiceImpl implements IFsUserCourseCacheService {
    private final IFsUserCourseService fsUserCourseService;
    private static final Cache<Long, FsUserCourse> FS_USER_COURSE_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();
    private static final Cache<Long, String> COURSE_ID_AND_NAME = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();
    @Override
    public FsUserCourse selectFsUserCourseByCourseId(Long courseId) {
        return FS_USER_COURSE_CACHE.get(courseId,e-> fsUserCourseService.selectFsUserCourseByCourseId(courseId));
    }

    @Override
    public String selectCourseNameByCourseId(Long courseId) {
        if(courseId == null) {
            return "-";
        }
        return COURSE_ID_AND_NAME.get(courseId, e-> {
            FsUserCourse course = fsUserCourseService.selectFsUserCourseByCourseId(courseId);
            if(course == null) {
                return "-";
            }
            return course.getCourseName();
        });
    }
}
