package com.cool.store.dao.ad;

import cn.hutool.core.collection.CollStreamUtil;
import com.cool.store.entity.ad.AdDistrictDO;
import com.cool.store.mapper.ad.AdDistrictMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行政区划DAO
 * </p>
 *
 * @author wangff
 * @since 2026/1/8
 */
@Repository
@RequiredArgsConstructor
public class AdDistrictDAO {
    private final AdDistrictMapper adDistrictMapper;

    /**
     * 根据区划编码查询名称
     */
    public Map<String, String> getNameByCodes(List<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return Collections.emptyMap();
        }
        Example example = new Example(AdDistrictDO.class);
        example.createCriteria().andIn("code", codes);
        List<AdDistrictDO> list = adDistrictMapper.selectByExample(example);
        return CollStreamUtil.toMap(list, AdDistrictDO::getCode, AdDistrictDO::getName);
    }

    /**
     * 列表查询
     * @param pid 父级id
     * @return 行政区划列表
     */
    public List<AdDistrictDO> getList(Long pid) {
        return adDistrictMapper.select(AdDistrictDO.builder().pid(pid).build());
    }
}
