package com.ruoyi.erp.service.impl;

import com.ruoyi.erp.mapper.RegionMapper;
import com.ruoyi.erp.domain.Region;
import com.ruoyi.erp.service.IRegionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl implements IRegionService {

    @Resource
    private RegionMapper regionMapper;

    @Override
    public List<?> listProvinces() {
        List<Region> list = regionMapper.selectProvinces();
        return list.stream()
            .map(r -> new Option(r.getProvId(), r.getProvName()))
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public List<?> listCities(Integer provId) {
        List<Region> list = regionMapper.selectCitiesByProv(provId);
        return list.stream()
            .map(r -> new Option(r.getCityId(), r.getCityName()))
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public List<?> listAreas(Integer provId, Integer cityId) {
        List<Region> list = regionMapper.selectAreasByCity(provId, cityId);
        return list.stream()
            .map(r -> new Option(r.getAreaId(), r.getAreaName()))
            .distinct()
            .collect(Collectors.toList());
    }

    public static class Option {
        public final Integer value;
        public final String label;
        public Option(Integer value, String label) { this.value = value; this.label = label; }
    }
}


