package com.lantone.security.facade.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantone.common.constant.AuthConstant;
import com.lantone.security.dto.GetDictionaryInfoByTypeDTO;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.enums.StatusEnum;
import com.lantone.common.service.RedisService;
import com.lantone.common.util.ListUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.security.entity.DictionaryInfo;
import com.lantone.security.service.impl.DictionaryInfoServiceImpl;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 表名：sys_dictionary_info 业务类
 * </p>
 */
@Component
public class DictionaryInfoFacade extends DictionaryInfoServiceImpl {

    @Autowired
    private RedisService redisService;

    public Map<String, Map<String, Map<String, String>>> initDicMap() {
        QueryWrapper<DictionaryInfo> dictionaryInfoQueryWrapper = new QueryWrapper<DictionaryInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("status", StatusEnum.Enable.getKey())
                .orderByDesc("order_no");
        Map<String, Map<String, Map<String, String>>> dicMap = list(dictionaryInfoQueryWrapper)
                .stream()
                .collect(
                        Collectors.groupingBy(
                                DictionaryInfo::getReturnType,
                                Collectors.groupingBy(
                                        DictionaryInfo::getGroupType,
                                        Collectors.toMap(
                                                DictionaryInfo::getName,
                                                DictionaryInfo::getVal
                                        )
                                )
                        )
                );
        redisService.del(AuthConstant.DIC_MAP_KEY);
        redisService.set(AuthConstant.DIC_MAP_KEY, dicMap);
        return dicMap;
    }

    /**
     * @param returnType 返回类型
     * @param groupTypes 分组集合
     * @Description获取指定groupTyeps的字典集
     * @Return java.util.Map<java.lang.String, java.util.Map < java.lang.String, java.lang.String>>
     */
    public Map<String, Map<String, String>> getDicMap(String returnType, List<String> groupTypes) {
        Map<String, Map<String, String>> outDicMap = new HashMap<>();
        Map<String, Map<String, Map<String, String>>> dicMap = redisService.get(AuthConstant.DIC_MAP_KEY);
        Map<String, Map<String, String>> returnDicMap = dicMap.get(returnType);
        if (MapUtils.isNotEmpty(returnDicMap) && ListUtil.isNotEmpty(groupTypes)) {
            groupTypes.stream().forEach(groupType -> {
                outDicMap.put(groupType, returnDicMap.get(groupType));
            });
        }
        return outDicMap;
    }

    /**
     * @param returnType 返回类型
     * @param groupType  分组集合
     * @Description获取指定groupTyeps的字典集
     * @Return java.util.Map<java.lang.String, java.util.Map < java.lang.String, java.lang.String>>
     */
    public Map<String, String> getOneDicMap(String returnType, String groupType) {
        Map<String, Map<String, Map<String, String>>> dicMap = redisService.get(AuthConstant.DIC_MAP_KEY);
        Map<String, Map<String, String>> returnDicMap = dicMap.get(returnType);
        if (MapUtils.isNotEmpty(returnDicMap) && StringUtil.isNotBlank(groupType)) {
            return returnDicMap.get(groupType);
        }
        return null;
    }


    /**
     * @param returnType 返回类型
     * @param groupTypes 分组集合
     * @Description获取指定groupTyeps的字典集(返回结果集为对象形式）
     * @Return java.util.Map<java.lang.String, java.util.List < com.lantone.security.dto.GetDictionaryInfoByTypeDTO>>
     */
    public Map<String, List<GetDictionaryInfoByTypeDTO>> getDicMapObj(String returnType, List<String> groupTypes) {
        Map<String, List<GetDictionaryInfoByTypeDTO>> outDicMap = new HashMap<>();
        Map<String, Map<String, String>> dicMap = getDicMap(returnType, groupTypes);
        dicMap.keySet().stream().forEach(groupType -> {
            List<GetDictionaryInfoByTypeDTO> tempObjs = new ArrayList<>();
            dicMap.get(groupType).keySet().stream().forEach(name -> {
                GetDictionaryInfoByTypeDTO tempObj = new GetDictionaryInfoByTypeDTO();
                tempObj.setName(name);
                tempObj.setVal(dicMap.get(groupType).get(name));
                tempObjs.add(tempObj);
            });
            outDicMap.put(groupType, tempObjs);
        });
        return outDicMap;
    }
}