/**
 * 版权所有 ©2020 威海莱欧科技有限公司
 * 工程名：base02
 * 文件名：AuthorityService.java
 * 创建日期：2020年12月23日 下午3:46:10
 * 作者：xmf
 */
package com.lion.service;

import java.util.List;
import java.util.Map;

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

import com.lion.dao.AuthorityDao;
import com.lion.dao.LoginDao;
import com.lion.entity.LoginUserInfo;
import com.lion.util.Util;

import jodd.util.StringUtil;

/**
 * 类描述
 * @author xmf
 * @since 2020年12月23日
 */
@Service
public class AuthorityService {
	@Autowired
	private AuthorityDao authorityDao;
	@Autowired
	private LoginDao loginDao;
	public Map<String, Object> getAuthority() {
		List<Map<String, Object>> list = authorityDao.getAuthority();
		return Util.responseMap("true", list);
	}
	
	public Map<String, Object> getAuthorityTreeList(LoginUserInfo s) {
		List<Map<String, Object>> list = loginDao.getResourcesByRoleId(s.getRoleid());
		for (Map<String, Object> map : list) {
			String text = map.get("text").toString();
			String bz = map.get("bz").toString();
			if (StringUtil.isNotEmpty(bz)) {
				text = text + "(" + bz + ")";
				map.put("text", text);
			}
		}
		return Util.responseMap("true", ResourceService.returnRecourceTreeList(list));
	}
	
	public Map<String, Object> getAuth( int id) {
		List<Map<String, Object>> list = authorityDao.getAuth(id);
		return Util.responseMap("true", list);
	}
	
	public String DeleteAndAdd( String roleid, String resourceid) {
		int result = 0;
		String[] aa = resourceid.split(",");
		authorityDao.deleteRole_resource(roleid);// 根据当前的roleid删除roleid权限
		for (int i = 0; i < aa.length; i++) {
			String resourceids = aa[i];
			result = authorityDao.addRole_resource(roleid, resourceids);// 添加权限
		}
		if (result > 0) {
			return Util.responseString("true", "操作成功");
		} else {
			return Util.responseString("false", "操作失败");
		}
	}

}
