Newer
Older
huludao / src / main / java / com / newfiber / api / mobile / service / AdministrativeDivisionService.java
package com.newfiber.api.mobile.service;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.newfiber.api.core.base.BaseService;
import com.newfiber.api.pc.dao.EventsManage.AdministrativeDivisionDao;
import com.newfiber.api.pc.model.EventsManage.AdministrativeDivision;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 行政区划表 Service
 * @author xcc
 * @date 2018-12-04 16:22:00
 */
@Service("administrativeDivisionService")
public class AdministrativeDivisionService extends BaseService<AdministrativeDivisionDao, AdministrativeDivision> {
	@Autowired
	private AdministrativeDivisionDao administrativeDivisionDao;

	/** 
	 * @Description: 按照当前用户查询当前用户的行政区划和下级行政区划
	 * @param map
	 * @return: java.util.List<com.newfiber.api.mobile.model.domain.AdministrativeDivision>
	 * @Author: Wmj 
	 * @Date: 2018/12/8 8:58 
	 */ 
	public List queryListByUserNo(Map<String,Object> map){
		//根据当前用户查询所属行政区划
        List<AdministrativeDivision> administrativeDivisionList = administrativeDivisionDao.queryDivisionNoByUserNo(map);

		//按照行政区划查询下级行政区划(递归方式)
        if(CollectionUtils.isNotEmpty(administrativeDivisionList)){
        	AdministrativeDivision administrativeDivision = administrativeDivisionList.get(0);
			List lists = new ArrayList<>();
			Map<String,Object> maps = new HashMap();
			maps.put("divisionNo",administrativeDivision.getDivisionNo());
			maps.put("divisionName",administrativeDivision.getDivisionName());
			List list = recursiveQuery(administrativeDivision.getDivisionNo());
			if(!CollectionUtil.isEmpty(list)){
				maps.put("child",list);
			}
			lists.add(maps);
			return lists;
		}

		return Lists.newArrayList();
	}

	public List recursiveQuery(String divisionNos){
		List<AdministrativeDivision> administrativeDivisionList = administrativeDivisionDao.queryListByDivisionNo(divisionNos);
		List lists = new ArrayList<>();
		if(!CollectionUtil.isEmpty(administrativeDivisionList)){
			for (AdministrativeDivision administrativeDivision : administrativeDivisionList) {
				Map<String,Object> map = new HashMap();
				map.put("divisionNo",administrativeDivision.getDivisionNo());
				map.put("divisionName",administrativeDivision.getDivisionName());
				List list = recursiveQuery(administrativeDivision.getDivisionNo());
				if(!CollectionUtil.isEmpty(list)){
					map.put("child",list);
				}
				lists.add(map);
			}
			return lists;
		}
		return null;
	}

	/**
	 * 获取当前用户下级所有区划
	 * @author chg
	 * @date 2018-12-12
	 * @param sid  当前用户sid
	 * @return
	 */
	public List<AdministrativeDivision> queryDivisionsubordinate(String sid){
		return administrativeDivisionDao.queryDivisionsubordinate(sid);
	}
}