package com.nokia.tsl_data.dao;

import com.nokia.tsl_data.entity.SysDataDictionary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public interface SysDataDictionaryRepository extends JpaRepository<SysDataDictionary, Long> {

    List<SysDataDictionary> findByTypeOrderByOrd(String type);

    SysDataDictionary findByRealNameAndType(String realName, String type);
    SysDataDictionary findByNickNameAndType(String nickName, String type);
    SysDataDictionary findByNickCodeAndType(String nickCode, String type);

    default List<String> findAllCityName() {
        List<String> result = new ArrayList<>();
        List<SysDataDictionary> dictionaries = findByTypeOrderByOrd("work_flow_city");
        for (SysDataDictionary dictionary : dictionaries) {
            result.add(dictionary.getRealName());
        }
        return result;
    }

    default Set<String> findAllServTypeNames() {
        Set<String> result = new HashSet<>();
        List<SysDataDictionary> dictionaries = findByTypeOrderByOrd("serv_type_name");
        for (SysDataDictionary dictionary : dictionaries) {
            result.add(dictionary.getRealName());
        }
        return result;
    }

    default Map<String, String> findHighQualityListCityMap() {
        Map<String, String> result = new HashMap<>();
        List<SysDataDictionary> cities = findByTypeOrderByOrd("high_quality_list_city");
        for (SysDataDictionary city : cities) {
            result.put(city.getNickCode(), city.getRealName());
        }
        return result;
    }

    default Map<String, String> findHighQualityListRegionMap() {
        Map<String, String> result = new HashMap<>();
        List<SysDataDictionary> regions = findByTypeOrderByOrd("high_quality_list_region");
        for (SysDataDictionary region : regions) {
            result.put(region.getNickCode(), region.getRealName());
        }
        return result;
    }

    /**
     * 获取全部区县的map key是区县的realName value是地市/区县组成的列表
     */
    default Map<String, List<Object>> findAllRegion() {
        List<SysDataDictionary> regions = findByTypeOrderByOrd("high_quality_list_region");
        Map<String, List<Object>> result = new HashMap<>();
        for (SysDataDictionary region : regions) {
            List<Object> objects = new ArrayList<>();
            objects.add(region.getParent().getRealName());
            objects.add(region.getRealName());
            result.put(region.getRealName(), objects);
        }
        return result;
    }
}
