package com.neusoft.smart.pos.org.service;

import com.neusoft.smart.pos.dto.org.response.CityRegionDictResponse;
import com.neusoft.smart.pos.framework.utils.Utility;
import com.neusoft.smart.pos.org.persistent.domain.CityRegionDictionary;
import com.neusoft.smart.pos.org.repository.CityRegionDictionaryRepository;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

@Service
public class CityRegionDictionaryService {
    @Resource
    public CityRegionDictionaryRepository cityRegionDictionaryRepository;

    public List<CityRegionDictResponse> getCitys(String parentId){    	
    	List<CityRegionDictionary> cityList = new ArrayList<>();
    	if (!StringUtils.isEmpty(parentId) && Utility.isNumeric(parentId)){
    		//传父ID，查询父ID所属的城市列表
    		cityList = cityRegionDictionaryRepository.getCitysById(Integer.parseInt(parentId));
    		
    	}else{
    		//无ID参数，查询所有省
    		cityList = cityRegionDictionaryRepository.getAllProvinces();
    	}
    	List<CityRegionDictResponse> result = new ArrayList<>();
    	cityList.forEach(po -> {
			CityRegionDictResponse response = new CityRegionDictResponse();
			BeanUtils.copyProperties(po, response);
			result.add(response);
		});
		return result;
    }
}
