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.Organ;
import com.xintong.visualinspection.bean.Permission;
import com.xintong.visualinspection.dao.master.PermissionDao;
import com.xintong.visualinspection.dao.master.RoleDao;
import com.xintong.visualinspection.pojo.RolePermision;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.PermissionService;

import lombok.Data;

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

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

    @Autowired
    private PermissionDao permissionDao;
    
	@Override
	public List<Permission> getPermissions(Permission permission) {
		
		List<Permission> permisions = permissionDao.getPermission(permission);
		List<Permission> olist = new ArrayList<>();
		Map<Integer, List<Permission>> map = new HashMap<>();
		// 找出所有父
		for (Permission o : permisions) {
			if (o.getPid() != null && o.getPid()!=0) {
				List<Permission> organList = map.get(o.getPid());
				if (organList == null) {
					organList = new ArrayList<>();
				}
				organList.add(o);
				map.put(o.getPid(), organList);
			}
		}
		// 找出子
		for (Integer oid : map.keySet()) {
			for (Permission m2 : permisions) {
				if (m2.getId().equals( oid)) {
					m2.setChilds(map.get(oid));
					if (m2.getPid() == null || m2.getPid()==0) {
						olist.add(m2);
					}
				}
			}
		}

		return olist;
	}

	@Override
	public void insert(Permission permission) {
		permissionDao.insert(permission);
	}

	@Override
	public void update(Permission permission) {
		permissionDao.update(permission);
	}

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

	@Override
	public List<Permission> getByRoleId(int roleId) {
		return permissionDao.getByRoleId(roleId);
	}

	@Override
	public void addrolepermision(RolePermision rolePermision) {
		if(rolePermision.getPermission_type() == null){
			rolePermision.setPermission_type(1);
		}
		permissionDao.addrolepermision(rolePermision);
	}

	@Override
	public void deleterolepermision(RolePermision rolePermision) {
		permissionDao.deleterolepermision(rolePermision);
	}

	@Override
	public void updaterolepermision(RolePermision rolePermision) {
		permissionDao.updaterolepermision(rolePermision);
	}

  
}
