package com.saye.hospitalgd.service.system.impl;

import com.saye.hospitalgd.commons.entity.DepartEntity;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.mapper.system.DepartMapper;
import com.saye.hospitalgd.model.Departs;
import com.saye.hospitalgd.service.system.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class DepartServiceImpl implements DepartService {
	@Autowired
	private DepartMapper departMapper;
	
	/**
	 * @description 查询部门树
	 * @author thuang       
	 * @created 2019年11月12日 下午3:53:20
	 */
	@Override
	public List<HashMap<Object, Object>> findDepartTreeNodeList(String parentId) {
		List<HashMap<Object, Object>> list = departMapper.selectDepartTreeListByParentId(parentId);

		List<HashMap<Object,Object>> resourceInfo=new ArrayList<>();

		HashMap<String,HashMap<Object,Object>> idMap=new HashMap<>();
		HashMap<String,List<HashMap<Object,Object>>> pidMap=new HashMap<>();

		//循环处理将个事件放入map，根据pid分组放入另外一个map用来准备生成树
		for (int i=0;i<list.size();i++){
			HashMap<Object, Object> map = list.get(i);
			String id = StringDUtil.changeNullToEmpty(map.get("DEPART_ID"));
			String pid = StringDUtil.changeNullToEmpty(map.get("PARENT_ID"));
			map.put("name",map.get("DEPART_NAME"));
			map.put("id",map.get("DEPART_ID"));
			map.put("title",map.get("DEPART_NAME"));
			idMap.put(id,map);
			if (pid.equals(parentId)) {
				resourceInfo.add(map);
				continue;
			}

			List<HashMap<Object, Object>> pidList = pidMap.get(pid);
			if (pidList == null){
				pidList=new ArrayList<>();
				pidMap.put(pid,pidList);
			}

			pidList.add(map);
		}
		//循环生成树
		for (String key : pidMap.keySet()){
			List<HashMap<Object, Object>> list1 = pidMap.get(key);
			HashMap<Object, Object> dataMap = idMap.get(key);
			if (dataMap !=null){
				dataMap.put("children",list1);
			}
		}
		return resourceInfo;
	}
	
	@Override
	@Transactional
	public int addDepart(HashMap<String, String> map)throws Exception {
		// TODO Auto-generated method stub
		return departMapper.addDepart(map);
	}


	@Override
	@Transactional
	public int modifyDepart(HashMap<String, String> map) throws Exception{
		// TODO Auto-generated method stub
		return departMapper.modifyDepart(map);
	}


	@Override
	@Transactional
	public int removeDepart(HashMap<String, String> map) throws Exception{
		// TODO Auto-generated method stub
		return departMapper.removeDepart(map);
	}


	//查询所有的部门信息
	@Override
	public List<DepartEntity> selectDepartTreeList() {
		// 首先查询说有的部门信息
		List<Departs> list=	departMapper.selectDepartTreeList();
		return tree(list,"-1");
	}
	

	//递归查询的方法
	public List<DepartEntity> tree(List<Departs> list,String parentId){
		List<DepartEntity> ls=new ArrayList<DepartEntity>();
		for(Departs d:list){
			String departsId=d.getDepartId();
			String pid=d.getParentId();
			if(parentId.equals(pid)){
				List<DepartEntity> c_node=tree(list,departsId);
				DepartEntity de=new DepartEntity();
				de.setChildren(c_node);
				de.setId(departsId);
				de.setTitle(d.getDepartName());
				de.setSpread(true);
				ls.add(de);
			}
		}
		return ls;
	}

	@Override
	public List<HashMap<Object, Object>> findDepartByParentId(HashMap<String, Object> map) {
		
		return this.departMapper.findDepartByParentId(map);
	}

	/**
	 * @description: 查询所有部门数据
	 * @author thuang
	 * @date 2021/5/19 10:11
	 * @version 1.0
	 */
	@Override
	public List<HashMap<Object,Object>> selectAllDeparts() {

		return this.departMapper.selectAllDeparts();
	}
}
