package com.hfkj.service.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.SecDictionaryMapper;
import com.hfkj.entity.SecDictionary;
import com.hfkj.entity.SecDictionaryExample;
import com.hfkj.service.SecDictionaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: SecDictionaryServiceImpl
 * @author: HuRui
 * @date: 2024/4/23
 **/
@Service("secDictionaryService")
public class SecDictionaryServiceImpl implements SecDictionaryService {

    @Resource
    private SecDictionaryMapper secDictionaryMapper;
    @Resource
    private RedisUtil redisUtil;
    // 缓存KEY
    private final String cacheKey = "SEC_DICTIONARY";

    @Override
    public List<SecDictionary> getDictionary() {
        Object cache = redisUtil.get(cacheKey);
        if (cache != null) {
            return (List<SecDictionary>) cache;
        }
        SecDictionaryExample example = new SecDictionaryExample();
        example.setOrderByClause("sort_id");
        List<SecDictionary> list = secDictionaryMapper.selectByExample(example);
        redisUtil.set(cacheKey, list);
        return list;
    }

    @Override
    public List<SecDictionary> getDictionary(String codeType) {
        return getDictionary().stream().filter(o -> o.getCodeType().equals(codeType)).collect(Collectors.toList());
    }

    @Override
    public List<SecDictionary> getDictionaryByOl(String codeType) {
        SecDictionaryExample example = new SecDictionaryExample();
        example.createCriteria().andCodeTypeEqualTo(codeType);
        example.or().andCodeTypeEqualTo(codeType);
        return secDictionaryMapper.selectByExample(example);
    }

    @Override
    public SecDictionary getDictionary(String codeType, String codeValue) {
        List<SecDictionary> list = getDictionary().stream()
                .filter(o -> o.getCodeType().equals(codeType) && o.getCodeValue().equals(codeValue))
                .collect(Collectors.toList());
        if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

}
