package com.search.supersearch.service.mysql.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.search.supersearch.domain.IamDataCategory;
import com.search.supersearch.domain.IamSearchCategoryidIndexname;
import com.search.supersearch.mapper.mysql.IamSearchCategoryidIndexnameMapper;
import com.search.supersearch.service.mysql.IamDataCategoryService;
import com.search.supersearch.service.mysql.IamSearchCategoryidIndexnameService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 */
@Service
public class IamSearchCategoryidIndexnameServiceImpl extends ServiceImpl<IamSearchCategoryidIndexnameMapper, IamSearchCategoryidIndexname>
        implements IamSearchCategoryidIndexnameService {
    @Resource
    IamDataCategoryService iamDataCategoryService;

    @Override
    public HashMap<String, List<String>> getDataMap() {
        HashMap<String, List<String>> map = Maps.newHashMap();


        for (IamSearchCategoryidIndexname iamSearchCategoryidIndexname : this.list()) {
            String categoryId = iamSearchCategoryidIndexname.getCategoryId();
            List<String> strings = map.get(categoryId);
            if (strings == null || strings.size() == 0) {
                strings = new ArrayList<>();
                strings.add(iamSearchCategoryidIndexname.getIndexName());
                map.put(categoryId, strings);
            } else {
                strings.add(iamSearchCategoryidIndexname.getIndexName());
            }

        }

        return map;
    }

    @Override
    public List<String> getIndexByCategoryId(String categoryId) {
        HashMap<String, List<String>> dataMap = getDataMap();
        List<String> strings = dataMap.get(categoryId);
        return getIamDataCategories(strings, categoryId, dataMap);
    }

    private List<String> getIamDataCategories(List<String> strings, String categoryId, HashMap<String, List<String>> dataMap) {
        if (strings == null) {
            strings = new ArrayList<>();
        }
        if (dataMap.get(categoryId) != null) {
            strings.addAll(dataMap.get(categoryId));
        }
        List<IamDataCategory> list = iamDataCategoryService.lambdaQuery().eq(IamDataCategory::getParentCategoryId, categoryId).list();
        if (list != null || list.size() > 0) {
            for (IamDataCategory iamDataCategory : list) {
                getIamDataCategories(strings, iamDataCategory.getCategoryId(), dataMap);
            }
        }
        return strings;
    }


}




