package com.xintong.visualinspection.service.impl;

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

import com.xintong.visualinspection.bean.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.dao.cluster.DepartmentDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.DepartmentService;

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

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

	@Autowired
	private DepartmentDao departmentDao;

	@Override
	public List<Organ> getOrgans(Organ organ,User u) {

		List<Organ> organs = departmentDao.getOrgan(organ);
		List<Organ> olist = new ArrayList<>();
		Map<Integer, List<Organ>> map = new HashMap<>();
		// 找出所有父
		for (Organ o : organs) {
			if (o.getParentid() != null && o.getParentid()!=0) {
				List<Organ> organList = map.get(o.getParentid());
				if (organList == null) {
					organList = new ArrayList<>();
				}
				organList.add(o);
				map.put(o.getParentid(), organList);
			}
		}
		// 找出子
		for (Integer oid : map.keySet()) {
			for (Organ m2 : organs) {
				if (m2.getId() == oid) {
					m2.setChilds(map.get(oid));
					if (m2.getParentid() == null || m2.getParentid()==0) {
						olist.add(m2);
					}
				}
			}
		}

		return getOragnsByPermison(olist,u);
	}

	@Override
	public void insert(Organ organ) {
		departmentDao.insert(organ);

	}

	@Override
	public void update(Organ organ) {
		departmentDao.update(organ);
	}

	@Override
	public void delete(Integer id) {
		departmentDao.delete(id);
	}

	@Override
	public List<FeeStation> getAllFS() {
		// TODO Auto-generated method stub
		return departmentDao.getAllFS();
	}

    @Override
    public List<FeeStation> getAllSA() {
        // TODO Auto-generated method stub
        return departmentDao.getAllSA();
    }

	@Override
	public List<Organ> getAll() {

		return departmentDao.getAll();
	}

	@Override
	public FeeStation getFSByDeptId(Integer id) {
		return departmentDao.getFsBydeptId(id);
	}
	
	private List<Organ> getOragnsByPermison(List<Organ> oragns,User user){
		if(user == null ) return oragns;
		boolean hasrole = false;
		for(Role r:user.getRoles()){
			if(r.getName().equals("ROLE_ADMIN") || r.getName().equals("ROLE_JICHA_ADMIN")){//管理员和稽查管理员可查看所有用户（by tiger）
				hasrole = true;
				break;
			}
		}
		if(hasrole){
			return oragns;
		}
		
		//找出自己
		Organ temp = null;
		for(Organ o :oragns){
			temp = findOrgan(o,user.getOrganid());
			if(temp!=null) break;	
		}

		if(temp!=null){
			oragns = new ArrayList<>();
			oragns.add(temp);
		} 
	    	
	    return oragns;
	}
	
	@Override
	public List<Organ> getOrgans(Organ organ) {
		return departmentDao.getOrgan(organ);
	}

	private Organ findOrgan(Organ o,Integer organid)
	{
		if(o.getId().intValue() == organid.intValue()){
			return o;
		}else{
			if(o.getChilds()!=null && o.getChilds().size()>0){
				for(Organ s:o.getChilds()){
					Organ oo = findOrgan(s, organid);
					if(oo!=null) return oo;
				}
				return null;
			}else{
				return null;
			}
		}
	}

	@Override
	public List<FeeStation> getAllRM() {
		// TODO Auto-generated method stub
		return departmentDao.getAllRM();
	}

	@Override
	public List<WreckerGroup> getAllWG() {
		// TODO Auto-generated method stub
		return departmentDao.getAllWG();
	}

}
