package com.sonic.frog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sonic.frog.dao.TimbreDictDao;
import com.sonic.frog.domain.entity.TimbreDict;
import com.sonic.frog.service.TimbreDictService;
import com.sonic.frog.utils.CacheUtils;
import com.sonic.frog.utils.RedisKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 音色字典表服务实现类
 */
@Service
@Slf4j
public class TimbreDictServiceImpl extends ServiceImpl<TimbreDictDao, TimbreDict> implements TimbreDictService {
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @Autowired
    private CacheUtils cacheUtils;

    @Override
    public List<TimbreDict> getAllTimbreDictList() {
        String aiTimbreDictCacheKey = redisKeyUtils.aiTimbreDictCacheKey();
        return cacheUtils.getCacheListAndSet(aiTimbreDictCacheKey, TimbreDict.class, () -> {
            List<TimbreDict> list = list(Wrappers.<TimbreDict>lambdaQuery().eq(TimbreDict::getIsDelete, false));
            return JSON.toJSONString(list);
        }, 1 * 60 * 60);
    }

    @Override
    public TimbreDict getTimbreDictByCode(String code) {
        List<TimbreDict> allTimbreDictList = getAllTimbreDictList();
        return allTimbreDictList.stream().filter(timbreDict -> timbreDict.getCode().equals(code)).findFirst().orElse(null);
    }
}