package com.sunxung.factoring.service.sys.impl;

import com.sunxung.factoring.component.cache.CacheKeyGenerator;
import com.sunxung.factoring.component.cache.RedisCache;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.ValidatorEntity.ValidatorType;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.mapper.sys.DictionaryMapper;
import com.sunxung.factoring.service.sys.DictionaryService;
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
    RedisCache redisCache;

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

    @Transactional
    @Override
    public Dictionary update(Dictionary dictionary) {
        ValidatorUtil.validator(ValidatorType.UPDATE, dictionary);
        dictionaryMapper.update(dictionary);
        cacheDictionary(dictionaryMapper.get(dictionary.getId()));
        return get(dictionary.getId());
    }

    @Transactional
    @Override
    public boolean delete(Long id) {
        Dictionary dictionary = dictionaryMapper.get(id);
        if (dictionary != null) {
            dictionaryMapper.delete(id);
            redisCache.remove(CacheKeyGenerator.generateCacheKeyFromId(Dictionary.class, id));
        }
        return true;
    }

    @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 List<Dictionary> findByParentId(Long parentId) {
        return dictionaryMapper.findByParentId(parentId);
    }

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

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

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

    @Override
    public Dictionary getByNameAndParentCode(String name, String parentCode) {
        return dictionaryMapper.getByNameAndParentCode(name, 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 dictionaryId
     * @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);
        }
    }
}
