package com.zcloud.service.system.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Department;
import com.zcloud.entity.system.Dictionaries;
import com.zcloud.mapper.datasource.system.DictionariesMapper;
import com.zcloud.service.system.DictionariesService;

/**
 * 说明:按钮权限服务接口实现类
 * 作者:luoxiaobao
 * 官网:www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class DictionariesServiceImpl implements DictionariesService {

	@Autowired
	private DictionariesMapper dictionariesMapper;

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listAllDict(String parentId) throws Exception {
		List<Dictionaries> dictList = this.listSubDictByParentId(parentId);
		for(Dictionaries dict : dictList){
			dict.setTreeurl("dictionaries_list.html?DICTIONARIES_ID="+dict.getDICTIONARIES_ID());
			dict.setSubDict(this.listAllDict(dict.getDICTIONARIES_ID()));
			dict.setTarget("treeFrame");
		}
		return dictList;
	}

	/**
	 * 通过ID获取其子级列表
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listSubDictByParentId(String parentId) throws Exception {
		return dictionariesMapper.listSubDictByParentId(parentId);
	}

	/**
	 * 通过ID获取其子级列表
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listSubDictByParentIdAndCourseware(PageData pd) throws Exception {
		return dictionariesMapper.listSubDictByParentIdAndCourseware(pd);
	}

	@Override
	public List<PageData> getTreeById(String id) throws Exception {
		List<Dictionaries> _list = this.listSubDictByParentId(id); //用传过来的ID获取此ID下的子列表数据
		return _list.stream().map(n -> {
			PageData entity = new PageData();
			entity.put("DICTIONARIES_ID", n.getDICTIONARIES_ID());
			entity.put("BIANMA", n.getBIANMA());
			entity.put("NAME", n.getNAME());
			return entity;
		}).collect(Collectors.toList());
	}

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)用于代码生成器引用数据字典
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listAllDictToCreateCode(String parentId) throws Exception {
		List<Dictionaries> dictList = this.listSubDictByParentId(parentId);
		for(Dictionaries dict : dictList){
			dict.setTreeurl("setDID('"+dict.getDICTIONARIES_ID()+"');");
			dict.setSubDict(this.listAllDictToCreateCode(dict.getDICTIONARIES_ID()));
			dict.setTarget("treeFrame");
		}
		return dictList;
	}
	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)下拉ztree用
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<PageData> listAllDictToSelect(String parentId,List<PageData> zdicPdList) throws Exception {
		List<PageData>[] arrayDep = this.listAllbyPd(parentId,zdicPdList);
		List<PageData> dicPdList = arrayDep[1];
		for(PageData pd : dicPdList){
			this.listAllDictToSelect(pd.getString("id"),arrayDep[0]);
		}
		return arrayDep[0];
	}
	/**下拉ztree用
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData>[] listAllbyPd(String parentId,List<PageData> zdicPdList) throws Exception {
		List<Dictionaries> dicList = this.listSubDictByParentId(parentId);
		List<PageData> dicPdList = new ArrayList<PageData>();
		for(Dictionaries dic : dicList){
			PageData pd = new PageData();
			pd.put("id", dic.getDICTIONARIES_ID());
			pd.put("parentId", dic.getPARENT_ID());
			pd.put("name", dic.getNAME());
			pd.put("icon", "../../../assets/images/user.gif");
			dicPdList.add(pd);
			zdicPdList.add(pd);
		}
		List<PageData>[] arrayDep = new List[2];
		arrayDep[0] = zdicPdList;
		arrayDep[1] = dicPdList;
		return arrayDep;
	}
	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return dictionariesMapper.datalistPage(page);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return dictionariesMapper.findById(pd);
	}
	/**通过名称获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByName(PageData pd)throws Exception{
		return dictionariesMapper.findByName(pd);
	}

	/**通过编码获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByBianma(PageData pd)throws Exception{
		return dictionariesMapper.findByBianma(pd);
	}

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		dictionariesMapper.save(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		dictionariesMapper.edit(pd);
	}

	/**排查表检查是否被占用
	 * @param pd
	 * @throws Exception
	 */
	public PageData findFromTbs(PageData pd)throws Exception{
		return dictionariesMapper.findFromTbs(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		dictionariesMapper.delete(pd);
	}

	/**
	 * 通过IDS获取列表
	 * @param dictionariesIds
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listSubDictByIds(String[] dictionariesIds) throws Exception {
		return dictionariesMapper.listSubDictByIdS(dictionariesIds);
	}

	@Override
	public List<Dictionaries> listSecondLevelDictByParentId(String parentId) throws Exception {
		return dictionariesMapper.listSecondLevelDictByParentId(parentId);
	}
	public List<PageData> listSubDictByParentIdAndSunCount(String parentId) throws Exception {
		return dictionariesMapper.listSubDictByParentIdAndSunCount(parentId);
	}
	/**
	 * 从下而上查询树(无分支)
	 * @param dic
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getTreeFromBelow(List<PageData> nextList, PageData dic) throws Exception{
		List<PageData> dicList = dictionariesMapper.listBySameParent(dic.get("PARENT_ID").toString());
		if(nextList != null && dicList != null && nextList.size() > 0 && dicList.size() > 0) {
			for(PageData d : dicList) {
				if(dic.getString("DICTIONARIES_ID").equals(d.getString("id"))) {
					d.put("children", nextList);
				}
			}
		}
		PageData parDic = new PageData();
		parDic.put("DICTIONARIES_ID", dic.get("PARENT_ID"));
		parDic.put("NOROOT", "1");
		parDic = dictionariesMapper.findById(parDic);
		if(parDic != null && parDic.get("DICTIONARIES_ID") != null && !"0".equals(parDic.get("PARENT_ID").toString())) {
			dicList = getTreeFromBelow(dicList,parDic);
		}
		return dicList;
	}
	public List<PageData> getIdsByRecuByParentId(String parentId) throws Exception {
		return dictionariesMapper.getIdsByRecuByParentId(parentId);
	}

	/**
	 * 更具parentID获取数据字典信息,并且转换能map
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public Map<String,Dictionaries> getMapByParId (String parentId) throws Exception{
		List<Dictionaries> dictList = this.listSubDictByParentId(parentId);
		Map<String,Dictionaries> dicMap = new HashMap<>();
		for (Dictionaries dictionaries :dictList){
			dicMap.put(dictionaries.getNAME(),dictionaries);
		}
		return dicMap;
	}

	public List<PageData> getIdsByParentIds(List<String> parentIds)  throws Exception {
		return dictionariesMapper.getIdsByParentIds(parentIds);
	}

	@Override
	public List<PageData> findByCondition(PageData condition) throws Exception {
		return dictionariesMapper.findByCondition(condition);
	}

}