package com.gci.boot.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.common.dict.excel.DictParser;
import com.gci.boot.constant.CacheConstant;
import com.gci.boot.modules.system.entity.SysDict;
import com.gci.boot.modules.system.mapper.SysDictMapper;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.vo.DictModel;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenzhiwei
 * @since 2023-08-25
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService, DictParser {

    @Override
    public List<DictModel> queryDictItemsByCode(String code) {
        return baseMapper.queryDictItemsByCode(code);
    }

    @Override
    public List<DictModel> queryTableDictItemsByCodeAndFilter(String table, String text, String code, String filterSql) {
        return baseMapper.queryTableDictItemsByCode(table, text, code, filterSql);
    }
    @Override
    public List<DictModel> queryTableDictItemsByCode(String table, String text, String code) {
        return baseMapper.queryTableDictItemsByCode(table, text, code, null);
    }
    
    @Override
    public String queryTableDictTextByKey(String table, String text, String code, String key) {
    	return queryTableDictTextByKey( table,  text,  code,  key,false);
    }
    
    /**
     * 通过查询指定table的 text code 获取字典值text
     * dictTableCache采用redis缓存有效期10分钟
     *	hasDelFlag 是否要判断删除字段
     * @param table
     * @param text
     * @param code
     * @param key
     * @return
     */
    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_TABLE_CACHE, key = "#table+':'+#code+':'+#key+':'+#hasDelFlag")
    public String queryTableDictTextByKey(String table, String text, String code, String key,boolean hasDelFlag) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        List<DictModel> dictModels = new ArrayList<>();
        if(hasDelFlag) {
        	//判断表是否有delFlag字段
        	String delFlag = queryDictDelFlag(table);
        	hasDelFlag = StringUtils.isNotBlank(delFlag);
        }
        if(hasDelFlag) {
        	dictModels = baseMapper.queryNoDelTableDictByValue(table, text, code, key);
        }else {
        	dictModels = baseMapper.queryTableDictByValue(table, text, code, key);
        }
        
        if (dictModels.size() == 0) {
            return null;
        }
        return dictModels.get(0).getText();
    }

    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_CACHE, key = "#code+':'+#key")
    public String queryDictTextByKey(String code, String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        List<DictModel> dictModels = baseMapper.queryDictByValue(code, key);
        if (dictModels.size() == 0) {
            return null;
        }
        return dictModels.get(0).getText();
    }

    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_CACHE_TEXTKEY, key = "#code+':'+#key")
    public String queryDictValueByText(String code, String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        List<DictModel> dictModels = baseMapper.queryDictByText(code, key);
        if (dictModels.size() == 0) {
            return null;
        }
        return dictModels.get(0).getValue();
    }

    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_TABLE_CACHE_TEXTKEY, key = "#table+':'+#text+':'+#key")
    public String queryTableDictValueByText(String table, String text, String code, String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        List<DictModel> dictModels = baseMapper.queryTableDictByText(table, text, code, key);
        if (dictModels.size() == 0) {
            return null;
        }
        return dictModels.get(0).getValue();
    }
    
    @Cacheable(value = CacheConstant.SYS_DICT_DELFLAG_CACHE, key = "#table")
    public String queryDictDelFlag(String table) {
    	return baseMapper.queryDictDelFlag(table);
    }

}
