package com.xintong.visualinspection.service.impl;

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

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.CheckItem;
import com.xintong.visualinspection.dao.master.CheckItemDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.CheckItemService;

/**
 * 文件名：UserServiceImpl
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@Service
public class CheckItemServiceImpl extends BaseService implements CheckItemService {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CheckItemServiceImpl.class);


    @Autowired
    private CheckItemDao checkItemDao;


	@Override
	public List<CheckItem> getAll() {
		List<CheckItem> list = checkItemDao.getAll();
		return getCheckItems(list) ;
	}

	
	public List<CheckItem> getCheckItems(List<CheckItem> obj) {
		List<CheckItem> checkItems = obj;
		List<CheckItem> olist = new ArrayList<>();
		Map<Long, List<CheckItem>> map = new HashMap<>();
		// 找出所有父
		for (CheckItem o : checkItems) {
			if (o.getParent_id() != null && o.getParent_id()!=0) {
				List<CheckItem> checkItemList = map.get(o.getParent_id());
				if (checkItemList == null) {
					checkItemList = new ArrayList<>();
				}
				checkItemList.add(o);
				map.put(o.getParent_id(), checkItemList);
			}else{
				if(!map.containsKey(o.getId())){
					map.put(o.getId(), new ArrayList<CheckItem>());
				}
			}
		}
		// 找出子
		for (Long oid : map.keySet()) {
			for (CheckItem m2 : checkItems) {
				if (m2.getId().longValue() == oid.longValue()) {
					m2.setSubCheckItem(map.get(oid));
					if (m2.getParent_id() == null || m2.getParent_id()==0L) {
						olist.add(m2);
					}
				}
			}
		}
		return olist;
	}

	@Override
	public CheckItem getById(Long id) {
		// TODO Auto-generated method stub
		return checkItemDao.getOne(id);
	}


	@Override
	public List<CheckItem> getByParentId(Long id) {
		// TODO Auto-generated method stub
		return checkItemDao.getByParentId(id);
	}


	@Override
	public void insert(CheckItem checkItem) {
		// TODO Auto-generated method stub
		checkItemDao.insert(checkItem);
	}


	@Override
	public void update(CheckItem checkItem) {
		// TODO Auto-generated method stub
		checkItemDao.update(checkItem);
	}


	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		checkItemDao.delete(id);
	}


	@Override
	public List<CheckItem> getByRuleId(Long id) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<CheckItem> getCheckItemByRuleType(Map<String,String> param) {
		// TODO Auto-generated method stub
		return checkItemDao.getCheckItemByRuleType(param);
	}
}
