package com.ylcm.sys.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ylcm.sys.domain.Permission;
import com.ylcm.sys.mapper.PermissionMapper;
import com.ylcm.sys.service.PermissionService;


@SuppressWarnings({ "unchecked", "rawtypes" })
@Service
public class PremissionServceImpl implements PermissionService {

	@Autowired
	private PermissionMapper permissionMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#addPermission(com.yaozhitech
	 * .dspoms.domain.Permission)
	 */
	@Override
	public void addPermission(Permission permission) {

		/**
		 * 1、调用新增权限接口
		 */
		permissionMapper.addPermission(permission);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#updPermission(com.yaozhitech
	 * .dspoms.domain.Permission)
	 */
	@Override
	public void updPermission(Permission permission) {

		/**
		 * 1、调用更新权限接口
		 */
		permissionMapper.updPermission(permission);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaozhitech.dspoms.service.PermissionService#delPermission(int)
	 */
	@Override
	public void delPermission(int permissionId) {

		/**
		 * 1、调用删除权限信息
		 */
		permissionMapper.delPermission(permissionId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaozhitech.dspoms.service.PermissionService#getMenuList()
	 */
	@Override
	public List<Permission> getMenuList() {

		/**
		 * 1、调用接口（查询父级权限列表）
		 */
		return permissionMapper.getMenuList();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaozhitech.dspoms.service.PermissionService#getAllMenus()
	 */
	@Override
	public List<Permission> getAllMenus() {

		/**
		 * 1、调用接口（获取父级菜单权限信息(含子菜单)）
		 */
		return permissionMapper.getAllMenus();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#getPermissionById(int)
	 */
	@Override
	public Permission getPermissionById(int permissionId) {

		/**
		 * 1、调用接口（根据权限标识获取权限信息）
		 */
		return permissionMapper.getPermissionById(permissionId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#getPermissionsByRoleId
	 * (int)
	 */
	@Override
	public List<Permission> getMenusByRoleId(int roleId) {

		/**
		 * 1、调用接口（根据角色获取父级菜单权限信息 (含子菜单)）
		 */
		return permissionMapper.getMenusByRoleId(roleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#getPermissionsByRoleId
	 * (int)
	 */
	@Override
	public List<Permission> getPermissionsByRoleId(int roleId) {

		/**
		 * 1、调用接口（根据角色获取所有权限信息）
		 */
		return permissionMapper.getPermissionsByRoleId(roleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#getRowCount(java.lang
	 * .String, java.lang.String, java.lang.String, int, int)
	 */
	@Override
	public int getRowCount(String name, String code, String type,
			int pageIndex, int pageSize) {

		/**
		 * 1、组装查询条件map
		 */
		Map map = new HashMap();
		map.put("name", name);
		map.put("code", code);
		map.put("type", type);
		map.put("pageIndex", (pageIndex - 1) * pageSize);
		map.put("pageSize", pageSize);

		/**
		 * 2、调用接口（根据条件查询权限总数）
		 */
		return permissionMapper.getRowCount(map);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#getPermissionList(java
	 * .lang.String, java.lang.String, java.lang.String, int, int)
	 */
	@Override
	public List<Permission> getPermissionList(String name, String code,
			String type, int pageIndex, int pageSize) {

		/**
		 * 1、组装查询条件map
		 */
		Map map = new HashMap();
		map.put("name", name);
		map.put("code", code);
		map.put("type", type);
		map.put("pageIndex", (pageIndex - 1) * pageSize);
		map.put("pageSize", pageSize);

		/**
		 * 2、调用接口（根据条件查询权限列表）
		 */
		return permissionMapper.getPermissionList(map);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaozhitech.dspoms.service.PermissionService#getRolePermById(int)
	 */
	@Override
	public Permission getRolePermById(int permissionId) {

		/**
		 * 1、调用接口（查找当前权限是否关联角色）
		 */
		return permissionMapper.getRolePermById(permissionId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#delPermissionsByIds(java
	 * .lang.String[])
	 */
	@Override
	public void delPermissionsByIds(String[] ids) {

		/**
		 * 1、调用接口（删除权限信息(只删除未关联角色的权限)）
		 */
		permissionMapper.delPermissionsByIds(ids);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#getCheckPermission(java
	 * .util.Map)
	 */
	@Override
	public int getCheckPermission(Map map) {

		/**
		 * 1、调用接口（唯一性判断）
		 */
		return permissionMapper.getCheckPermission(map);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#delRolePermissionsById
	 * (int)
	 */
	@Override
	public void delRolePermissionsById(int roleId) {

		/**
		 * 1、删除角色权限关联信息(根据角色id)
		 */
		permissionMapper.delRolePermissionsById(roleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaozhitech.dspoms.service.PermissionService#addRolePermissions(java
	 * .lang.String, int)
	 */
	@Override
	public void addRolePermissions(String permissionIds, int roleId) {
		String[] ids = permissionIds.split(",");
		for (int i = 0; i < ids.length; i++) {

			/**
			 * 1、组装查询条件map
			 */
			Map map = new HashMap();
			map.put("roleId", roleId);
			map.put("permissionId", Integer.valueOf(ids[i]));
			Timestamp timestamp = new Timestamp(new Date().getTime());
			map.put("crtTime", timestamp);
			map.put("updTime", timestamp);

			/**
			 * 2、添加角色权限关联信息
			 */
			permissionMapper.addRolePermission(map);
		}
	}

	@Override
	public List<Permission> getAllFunctions(List<Permission> permissionList) {
	    if(CollectionUtils.isEmpty(permissionList)) {
	    	return null;
	    }
	    List<Permission> funcPer = new ArrayList<Permission>();
	    for(Permission p : permissionList) {
	    	if(CollectionUtils.isEmpty(p.getSubPerms())) {
	    		continue;
	    	}
    		p.getSubPerms().stream().forEach(x ->{
    			//子菜单对应功能权限
    			List<Permission> funcList = permissionMapper.getPermissionsByPid(x.getPermissionId());
    			if(CollectionUtils.isNotEmpty(funcList)) {
    				x.setSubPerms(funcList);
    				funcPer.add(x);
    			}
    		});
	    }
		return funcPer;
	}
}
