package org.linlinjava.jz.admin.service;

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

import org.linlinjava.jz.admin.vo.Ticket_Consult_Type_Vo;
import org.linlinjava.jz.db.domain.JzDmTicketConsultType;
import org.linlinjava.jz.db.service.JzDmTicketConsultTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

@Service
public class AdminDmTicketConsultTypeService {

	@Autowired
	JzDmTicketConsultTypeService jzDmTicketConsultTypeService;

	/**
	 * 最后的叶子节点
	 * @return
	 */
	public List<Ticket_Consult_Type_Vo> getLastChild() {
		List<Ticket_Consult_Type_Vo> consultTypeTree = this.getConsultTypeTree();

		List<Ticket_Consult_Type_Vo> tempLast = new ArrayList<Ticket_Consult_Type_Vo>();
		this.recursionData(consultTypeTree, tempLast);
		return tempLast;

	}

	/**
	 * 获取最后一层
	 * @param list
	 * @param tempLast
	 */
	public void recursionData(List<Ticket_Consult_Type_Vo> list, List<Ticket_Consult_Type_Vo> tempLast) {
		for (Ticket_Consult_Type_Vo tc : list) {
			if (null != tc.getChildren() && tc.getChildren().size() > 0) {
				this.recursionData(tc.getChildren(), tempLast);
			} else {
				tempLast.add(tc);
			}
		}
	}

	public List<Ticket_Consult_Type_Vo> getConsultTypeTree() {
		List<JzDmTicketConsultType> types = jzDmTicketConsultTypeService.queryByLevel((short) 1);
		// 返回值
		List<Ticket_Consult_Type_Vo> consult_type_vos = new ArrayList<Ticket_Consult_Type_Vo>();

		for (JzDmTicketConsultType type : types) {
			Ticket_Consult_Type_Vo vo = new Ticket_Consult_Type_Vo();
			vo.setId(type.getId());
			vo.setName(type.getName());
			vo.setRemark(type.getRemark());
			vo.setLevel(1);
			vo.setDeleted(type.getDeleted());
			vo.setType(type.getType());
			consult_type_vos.add(vo);

			// 第二级
			List<Ticket_Consult_Type_Vo> childre2 = Lists.newArrayList();
			vo.setChildren(childre2);

			List<JzDmTicketConsultType> consults2 = jzDmTicketConsultTypeService.queryByPid(type.getId());
			for (JzDmTicketConsultType consult_sub : consults2) {
				Ticket_Consult_Type_Vo vo1 = new Ticket_Consult_Type_Vo();
				vo1.setId(consult_sub.getId());
				vo1.setName(consult_sub.getName());
				vo1.setRemark(consult_sub.getRemark());
				vo1.setLevel(consult_sub.getLevel());
				vo1.setDeleted(consult_sub.getDeleted());
				vo1.setType(consult_sub.getType());
				childre2.add(vo1);

				// 第三级
				List<Ticket_Consult_Type_Vo> childre3 = Lists.newArrayList();
				List<JzDmTicketConsultType> consults3 = jzDmTicketConsultTypeService.queryByPid(consult_sub.getId());
				for (JzDmTicketConsultType consult3_sub : consults3) {
					Ticket_Consult_Type_Vo vo2 = new Ticket_Consult_Type_Vo();
					vo2.setId(consult3_sub.getId());
					vo2.setName(consult3_sub.getName());
					vo2.setRemark(consult3_sub.getRemark());
					vo2.setLevel(consult3_sub.getLevel());
					vo2.setDeleted(consult3_sub.getDeleted());
					vo2.setType(consult3_sub.getType());
					childre3.add(vo2);
				}
				vo1.setChildren(childre3);
			}

		}
		
		return consult_type_vos;
	}

}
