package com.gci.boot.common.dict.excel;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gci.boot.constant.CacheConstant;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.system.service.impl.SysDictServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 24642
 */
@Slf4j
@Component
public class MyExcelDictHandleImpl implements IExcelDictHandler {
    @Autowired
    private SysDictServiceImpl sysDictService;
    private static SysDictServiceImpl sysDictDetailMapper;
    @Autowired
    private CacheManager cacheManager;
    private static CacheManager cacheManagerMapper;

    @PostConstruct
    public void init() {
        sysDictDetailMapper = this.sysDictService;
        cacheManagerMapper = cacheManager;
    }
    /**
     * 导出
     * @param dict 字典key
     * @param o 对象
     * @param propName 属性名
     * @param value 属性值
     * @return 翻译后字典值
     */
    @Override
    public String toName(String dict, Object o, String propName, Object value) {
        if (ObjectUtil.isEmpty(value)) {
            return StrUtil.EMPTY;
        }
        List<String> keys = Arrays.asList(Convert.toStr(value).split(","));
        //支持一下表字典
        if (dict.contains(StrUtil.COMMA)) {
            List<String> split = StrUtil.split(dict, StrUtil.COMMA);
            if (split.size() != 3) {
                throw new GciBootException("字典值错误！");
            } else {
                // table text code
                String collect = keys.stream().map(String::trim).filter(k -> k.length() > 0)
                        .map(k -> getTableDictTextByKey(split, k))
                        .collect(Collectors.joining(","));
                return StrUtil.isNotBlank(collect) && !StrUtil.NULL.equals(collect) ? collect : StrUtil.EMPTY;
            }
        } else {
            String collect = keys.stream().map(String::trim).filter(k -> k.length() > 0)
                    .map(k -> getDictTextByKey(dict, k))
                    .collect(Collectors.joining(","));
            return StrUtil.isNotBlank(collect) && !StrUtil.NULL.equals(collect) ? collect : StrUtil.EMPTY;
        }
    }

    private String getTableDictTextByKey(List<String> split, String k) {
        Cache cache = cacheManagerMapper.getCache(CacheConstant.SYS_DICT_TABLE_CACHE);
        String cacheKey = split.get(0) + ":" + split.get(2) + ":" + k + ":" + false;
        if (ObjectUtil.isNotNull(cache)) {
            Cache.ValueWrapper valueWrapper = cache.get(cacheKey);
            if (ObjectUtil.isNotNull(valueWrapper)) {
                return Convert.toStr(valueWrapper.get());
            }
        }
        String sqlValue = sysDictDetailMapper.queryTableDictTextByKey(split.get(0), split.get(1), split.get(2), k);
        if (ObjectUtil.isNotNull(cache)) {
            if (StrUtil.isNotBlank(sqlValue)) {
                cache.put(cacheKey,sqlValue);
            }
        }
        return sqlValue;
    }

    private String getDictTextByKey(String dict, String k) {
        Cache cache = cacheManagerMapper.getCache(CacheConstant.SYS_DICT_CACHE);
        String cacheKey = dict + ":" + k;
        if (ObjectUtil.isNotNull(cache)) {
            Cache.ValueWrapper valueWrapper = cache.get(cacheKey);
            if (ObjectUtil.isNotNull(valueWrapper)) {
                return Convert.toStr(valueWrapper.get());
            }
        }
        String sqlValue = sysDictDetailMapper.queryDictTextByKey(dict, k);
        if (ObjectUtil.isNotNull(cache)) {
            if (StrUtil.isNotBlank(sqlValue)) {
                cache.put(cacheKey,sqlValue);
            }
        }
        return sqlValue;
    }

    @Override
    public String toValue(String dict, Object o, String s1, Object o1) {
        return null;
    }
}
