package com.fjhx.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.ExRegion;
import com.fjhx.mapper.ExRegionMapper;
import com.fjhx.service.ExRegionService;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-10-17
 */
@Service
public class ExRegionServiceImpl extends ServiceImpl<ExRegionMapper, ExRegion> implements ExRegionService {


    @Override
    public Map<String, String> getChineseNameById(Collection<String> regionSet) {

        List<ExRegion> list = lambdaQuery()
                .select(ExRegion::getId, ExRegion::getName)
                .in(ExRegion::getId, regionSet)
                .list();

        if (list.size() == 0) {
            return new HashMap<>();
        }

        return list.stream().collect(Collectors.toMap(
                ExRegion::getId,
                ExRegion::getName,
                (v1, v2) -> v1
        ));
    }

    @Override
    public List<ExRegion> getList(ExRegion region) {

        return lambdaQuery()
                .select(ExRegion::getId, ExRegion::getParentId, ExRegion::getType, ExRegion::getChineseName, ExRegion::getName)
                .eq(ObjectUtil.isNotEmpty(region.getType()), ExRegion::getType, region.getType())
                .eq(ObjectUtil.isNotEmpty(region.getParentId()), ExRegion::getParentId, region.getParentId())
                .list();

    }
}
