package com.xintong.visualinspection.service.impl;

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

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

import com.mysql.jdbc.StringUtils;
import com.xintong.visualinspection.bean.CheckItem;
import com.xintong.visualinspection.bean.CheckRule;
import com.xintong.visualinspection.bean.Organ;
import com.xintong.visualinspection.dao.master.CheckItemDao;
import com.xintong.visualinspection.dao.master.CheckRuleDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.CheckRuleService;
import com.xintong.visualinspection.util.CacheUtil;
import com.xintong.visualinspection.util.CodeUtil;

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

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


    @Autowired
    private CheckRuleDao checkRuleDao;
    
    @Autowired
    private CheckItemDao checkItemDao ;

	@Override
	public List<CheckRule> getAll() {
		// TODO Auto-generated method stub
		return checkRuleDao.getAll();
	}

	@Override
	public CheckRule getById(Long id) {
		CheckRule obj = checkRuleDao.getOne(id);
		// 获取子类的父
		return obj ;
	}
	
	@Override
	public CheckRule getListById(Long id) {
		// 这边要对所有取出来的checkItem数据进行处理
		CheckRule obj = checkRuleDao.getOne(id);
		List<CheckItem> lists = checkItemDao.getAll();
		List<CheckItem> result_list = new ArrayList<>();
		Map<Long,CheckItem> map = new HashMap<>();
		for(CheckItem o : obj.getCheck_items()){
			map.put(o.getId(),o);
			getMyParent(o, lists, map);
		}
		// 把map中value全部拿出来
		for(CheckItem o : map.values()){
			result_list.add(o);
		}
		obj.setCheck_items(getCheckItems(result_list));
		return obj;
	}
	
	private void getMyParent(CheckItem checkItem,List<CheckItem> lists,Map<Long,CheckItem> map ){
		if(checkItem.getParent_id() !=null && checkItem.getParent_id()!=0){
			for(CheckItem obj : lists){
				if(checkItem.getParent_id().longValue() == obj.getId().longValue()){
					// 判断这个有没有父亲了
					if(obj.getParent_id()!=null){
						getMyParent(obj,lists,map);
					}
					if(!map.containsKey(obj.getId())){
						map.put(obj.getId(), obj);
					}
					break;
				}
			}
		}
	}
	
	// 将list 父子分别排序好 (静态模板函数)
	private 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()==0) {
						olist.add(m2);
					}
				}
			}
		}
		return olist;
	}
	
	@Override
	public List<CheckRule> getByName(String name) {
		// TODO Auto-generated method stub
		List<CheckRule> rules =checkRuleDao.getByName(name);
		for(CheckRule r:rules){
			r.setRule_type_name(CodeUtil.getCodeNameByFlagAndValue("rule_type", r.getRule_type()));
		}
		return checkRuleDao.getByName(name);
	}

	@Override
	public void insert(CheckRule checkRule) {
		// TODO Auto-generated method stub
		//如果需要设置rultType，需把表中有相同ruleType的字段置为空，保证ruleType的唯一性
		if(checkRule.getRule_type()!=null){
			checkRuleDao.updateRuleType(checkRule);
		}
		checkRuleDao.insert(checkRule);
//		if(checkRule.getCheck_items()!=null && checkRule.getCheck_items().size()>0) {
//			Long id = checkRuleDao.getLastId();
//			for(CheckItem item:checkRule.getCheck_items()){
//				item.setRule_id(id);
//			}
//			checkRuleDao.insertRuleItem(checkRule.getCheck_items());
//		}
	}

	@Override
	public void update(CheckRule checkRule) {
		//如果需要设置rultType，需把表中有相同ruleType的字段置为空，保证ruleType的唯一性
		if(checkRule.getRule_type()!=null){
			checkRuleDao.updateRuleType(checkRule);
		}
		checkRuleDao.update(checkRule);
//		checkRuleDao.deleteRuleItem(checkRule.getId());
//		if(checkRule.getCheck_items()!=null && checkRule.getCheck_items().size()>0) {
//			Long id = checkRule.getId();
//			for(CheckItem item:checkRule.getCheck_items()){
//				item.setRule_id(id);
//			}
//			checkRuleDao.insertRuleItem(checkRule.getCheck_items());
//		}
	}

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

	@Override
	public List<CheckRule> getAllAndCountItem(CheckRule checkRule) {
		
		List<CheckRule> lists = checkRuleDao.getAllAndCountItem(checkRule);
		for(CheckRule c:lists){
			if(c.getChecked_dept_id()!=null){
				Organ obj = CacheUtil.deptMap.get(c.getChecked_dept_id().intValue());
				if(obj!=null){
					c.setChecked_dept_name(obj.getOrganname());					
				}
				
			}
			c.setRule_type_name(CodeUtil.getCodeNameByFlagAndValue("rule_type", c.getRule_type()));
		}
		return  lists;
	}

	@Override
	public CheckRule getItemListByRuleType(Map<String, String> param) {
		// TODO Auto-generated method stub
		CheckRule rule = checkRuleDao.getByRuleType(param);
		if(rule!=null){
			return getListById(rule.getId());
		}
		return null;
	}
}
