package com.zcloud.service.hiddenDangerCheckStandard.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.hiddenDangerCheckStandard.StandardDictionaryMapper;
import com.zcloud.service.hiddenDangerCheckStandard.StandardDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 说明：清单管理
 * 作者：luoxiaobao
 * 时间：2020-12-30
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class StandardDictionaryServiceImpl implements StandardDictionaryService {

	@Autowired
	private StandardDictionaryMapper dictionaryMapper;

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> list(PageData pd)throws Exception{
		return dictionaryMapper.list(pd);
	}
	/**
	 * 新增
	 *
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd) throws Exception {
		dictionaryMapper.save(pd);
	}

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

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

	/**
	 * 列表
	 *
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page) throws Exception {
		return dictionaryMapper.datalistPage(page);
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listTree(PageData pd)throws Exception {
		List<PageData> list = new ArrayList<>();
		List<PageData> listAll = dictionaryMapper.listAll(pd);
		Map<String, PageData> map = new HashMap<>();
		for (PageData r : listAll) {
			map.put(r.getString("DICTIONARY_ID"), r);
		}
		Iterator<Map.Entry<String, PageData>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, PageData> entry = it.next();
			PageData root = entry.getValue();
			if ("0".equals(root.getString("PARENT_ID"))) {
				root.put("PARENT_NAME", "（无）");
				list.add(entry.getValue());
				it.remove();//使用迭代器的remove()方法删除元素
			}
		}
		list.sort((x, y) -> Integer.compare(Integer.parseInt(x.get("DICTIONARY_SORT").toString()), Integer.parseInt(y.get("DICTIONARY_SORT").toString())));
		TreeAppendChildren(map, list);

		return list;
	}

	private void TreeAppendChildren(Map<String, PageData> map, List<PageData> list) {
		if (!map.isEmpty()) {
			for (PageData parent : list) {
				List<PageData> children = new ArrayList<PageData>();
				Iterator<Map.Entry<String, PageData>> it = map.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<String, PageData> entry = it.next();
					PageData child = entry.getValue();
					if (parent.getString("DICTIONARY_ID").equals(child.getString("PARENT_ID"))) {
						children.add(child);
						it.remove();//使用迭代器的remove()方法删除元素
					}
				}
				if (children.size() > 0) {
					children.sort((x, y) -> Integer.compare(Integer.parseInt(x.get("DICTIONARY_SORT").toString()), Integer.parseInt(y.get("DICTIONARY_SORT").toString())));
					parent.put("children", children);
					TreeAppendChildren(map, children);
				}
			}
		}
	}

	/**
	 * 列表(全部)
	 *
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd) throws Exception {
		return dictionaryMapper.listAll(pd);
	}

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

	/**
	 * 批量删除
	 *
	 * @param pd
	 * @throws Exception
	 */
	public void deleteAll(PageData pd) throws Exception {
		dictionaryMapper.deleteAll(pd);
	}

	@Override
	public Integer getCount(PageData pd) throws Exception {
		return dictionaryMapper.getCount(pd);
	}
	@Override
	public List<PageData> listSelect(PageData pd)throws Exception{
		return dictionaryMapper.listSelect(pd);
	}

}


