package com.inkasso.factoring.dict.service.impl;

import com.inkasso.adapter.util.ValidatorUtil;
import com.inkasso.auth.exception.ValidatorException;
import com.inkasso.config.cache.CacheKeyGenerator;
import com.inkasso.config.cache.RedisCache;
import com.inkasso.factoring.common.entity.ValidatorEntity;
import com.inkasso.factoring.dict.dao.DictionaryMapper;
import com.inkasso.factoring.dict.entity.Dictionary;
import com.inkasso.factoring.dict.service.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private RedisCache redisCache;

    @Transactional
    @Override
    public Dictionary add(Dictionary dictionary) {
        ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, dictionary);
        dictionaryMapper.add(dictionary);
        return get(dictionary.getId());
    }


    @Override
    public Dictionary get(Long id) {
        Dictionary dictionary = (Dictionary) redisCache
                .get(CacheKeyGenerator.generateCacheKeyFromId(Dictionary.class, id));
        if (dictionary == null) {
            dictionary = dictionaryMapper.get(id);
            cacheDictionary(dictionary);
        }
        return dictionary;
    }

    @Override
    public Dictionary getByCode(String code) {
        Dictionary dictionary = (Dictionary) redisCache
                .get(CacheKeyGenerator.generateCacheKeyFromString(Dictionary.class, code));
        if (dictionary == null) {
            dictionary = dictionaryMapper.getByCode(code);
            cacheDictionary(dictionary);
        }
        return dictionary;
    }

    @Override
    public Dictionary getByName(String name) {
        Dictionary dictionary = (Dictionary) redisCache
                .get(CacheKeyGenerator.generateCacheKeyFromString(Dictionary.class, name));
        if (dictionary == null) {
            dictionary = dictionaryMapper.getByName(name);
            cacheDictionary(dictionary);
        }
        return dictionary;
    }

    @Override
    public List<Dictionary> findByParentId(Long parentId) {
        return dictionaryMapper.findByParentId(parentId);
    }

    @Override
    public List<Dictionary> findByParentIdIsNull() {
        return dictionaryMapper.findByParentIdIsNull();
    }

    @Override
    public List<Dictionary> findByParentCodeAndCode(String parentCode, String code) {
        if (StringUtils.isEmpty(parentCode) && StringUtils.isEmpty(code)) {
            throw new ValidatorException(300, "查询字典项的参数不能为空");
        }
        return dictionaryMapper.findByParentCodeAndCode(parentCode, code);
    }

    @Override
    public Object findByParentCode(String parentCode) {
        return dictionaryMapper.findByParentCode(parentCode);
    }

    @Override
    public Dictionary getByNameAndParentCode(String name, String parentCode) {
        return dictionaryMapper.getByNameAndParentCode(name, parentCode);
    }

    @Override
    public Dictionary getByCodeAndParentCode(String code, String parentCode) {
        return dictionaryMapper.getByCodeAndParentCode(code, parentCode);
    }

    /**
     * TODO 根据dictionaryId获取缓存中的dictionary信息
     *
     * @param dictionaryId
     * @return
     */
    public Dictionary getRedisByDictionaryId(String dictionaryId) {
        Dictionary dictionary = (Dictionary) redisCache
                .get(CacheKeyGenerator.generateCacheKeyFromString(Dictionary.class, dictionaryId));
        return dictionary;
    }

    /**
     * TODO 根据Code获取缓存中的dictionary信息
     *
     * @param code
     * @return
     */
    public Dictionary getRedisByCode(String code) {
        Dictionary dictionary = (Dictionary) redisCache
                .get(CacheKeyGenerator.generateCacheKeyFromString(Dictionary.class, code));
        return dictionary;
    }

    /**
     * TODO 缓存dictionary
     *
     * @param dictionary
     */
    private void cacheDictionary(Dictionary dictionary) {
        if (dictionary != null) {
            String cacheKey = CacheKeyGenerator.generateCacheKeyFromId(Dictionary.class, dictionary.getId());
            redisCache.set(cacheKey, dictionary);
            String cacheKey2 = CacheKeyGenerator.generateCacheKeyFromString(Dictionary.class, dictionary.getCode());
            redisCache.set(cacheKey2, dictionary);
        }
    }
}
