package action;

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

import util.MyJson;
import util.MyRight;
import biz.CommonBiz;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import entity.OaRight;
import entity.OaRole;
import entity.OaUser;

public class RightAction {
	
	private static final String TOMENU = "tomenu";
	private static final String TOROLE = "torole";
	private CommonBiz commonBiz;
	private List<MyRight> myRights;
	private OaRole role;
	private String rightIds;

	public String getRightIds() {
		return rightIds;
	}

	public void setRightIds(String rightIds) {
		this.rightIds = rightIds;
	}

	public OaRole getRole() {
		return role;
	}

	public void setRole(OaRole role) {
		this.role = role;
	}

	public List<MyRight> getMyRights() {
		return myRights;
	}

	public void setMyRights(List<MyRight> myRights) {
		this.myRights = myRights;
	}

	public CommonBiz getCommonBiz() {
		return commonBiz;
	}

	public void setCommonBiz(CommonBiz commonBiz) {
		this.commonBiz = commonBiz;
	}
	
	/**
	 * 获取当前用户的权限
	 * @return
	 */
	public String getRightList(){
		OaUser user=(OaUser) ActionContext.getContext().getSession().get("user");
		Set<OaRight> rights=user.getOaRole().getOaRights();
		myRights=new ArrayList<MyRight>();
		for (OaRight right : rights) {
			if (right.getPid()==0) {
				MyRight myRight=new MyRight();
				myRight.setOaRight(right);
				for (OaRight right2 : rights) {
					if (right2.getPid().toString().equals(right.getRightId())) {
						myRight.getRightSubs().add(right2);
					}		
				}
				myRights.add(myRight);
			}
		}
		return TOMENU;
	}
	
	/**
	 * 跳转到角色管理页面
	 * @return
	 */
	public String toRole(){
		return TOROLE;
	}
	
	/**
	 * 查询所有角色
	 * @return
	 */
	public String findRoleList(){
		List<OaRole> roleList=commonBiz.getOaRoleDAO().findAll();
		String msg = "{totalCount:" + roleList.size() + ",result:";
		msg += MyJson.toJsonStr(roleList, "oaRights");
		msg += "}";
		ActionContext.getContext().put("msg", msg);
		return Action.SUCCESS;
	}

	/**
	 * 添加角色
	 * @return
	 */
	public String addRole(){
		commonBiz.getOaRoleDAO().save(role);
		ActionContext.getContext().put("msg", "{success:true,msg:'添加成功'}");
		return Action.SUCCESS;
	}
	
	/**
	 * 修改角色
	 * @return
	 */
	public String updateRole(){
		OaRole oaRole=commonBiz.getOaRoleDAO().findById(role.getRoleId());
		oaRole.setRoleName(role.getRoleName());
		commonBiz.getOaRoleDAO().merge(oaRole);
		ActionContext.getContext().put("msg", "{success:true,msg:'修改成功'}");
		return Action.SUCCESS;
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	public String deleteRole(){
		commonBiz.getOaRoleDAO().delete(role);
		ActionContext.getContext().put("msg", "{success:true,msg:'删除成功'}");
		return Action.SUCCESS;
	}
	
	/**
	 * 查询角色已有权限和可分配权限
	 * @return
	 */
	public String findRightsByRoleId(){
		//全部权限
		List<OaRight> rightList=commonBiz.getOaRightDAO().findAll();
		//获取当前角色
		role=commonBiz.getOaRoleDAO().findById(role.getRoleId());
		//已有的权限
		Set<OaRight> hasRights=role.getOaRights();
		//可分配的权限
		List<OaRight> notRights=new ArrayList<OaRight>();
		for (OaRight right : rightList) {
			boolean flag=true;
			for (OaRight r : hasRights) {
				if (right.getRightId().equals(r.getRightId())) {
					flag=false;
				}
			}
			if (flag) {
				notRights.add(right);
			}
		}
		String msg = "{hasRights:";
		msg += MyJson.toJsonStr(hasRights, new String[]{"oaRoles","url"});
		msg += ",notRights:"+MyJson.toJsonStr(notRights, new String[]{"oaRoles","url"});
		msg += "}";
		ActionContext.getContext().put("msg", msg);
		return Action.SUCCESS;
	}
	
	/**
	 * 执行权限更改
	 * @return
	 */
	public String doUpdateRight(){
		//获取当前角色
		role=commonBiz.getOaRoleDAO().findById(role.getRoleId());
		String[] ids=rightIds.split(",");
		//清空原有权限
		role.getOaRights().removeAll(role.getOaRights());
		if (rightIds!=null&&!rightIds.equals("")) {
			for (String id : ids) {
				OaRight right=commonBiz.getOaRightDAO().findById(id);
				role.getOaRights().add(right);
				right.getOaRoles().add(role);
			}
		}
		commonBiz.getOaRoleDAO().merge(role);
		ActionContext.getContext().put("msg", "{success:true,msg:'分配成功'}");
		return Action.SUCCESS;
	}
	
}
最近下载更多
lvdong2023  LV10 2023年5月18日
微信网友_5963854197018624  LV7 2023年4月6日
泓鼎168  LV20 2023年3月30日
cx123123  LV7 2022年5月17日
habiya  LV3 2022年3月15日
ewan007  LV30 2022年3月10日
微信网友_5845420553359360  LV4 2022年2月27日
wanglinddad  LV55 2021年11月6日
freeblow  LV2 2021年5月20日
and123456  LV11 2021年5月11日
最近浏览更多
1111112222  LV6 1月8日
lvyga1  LV2 2024年12月20日
WBelong  LV8 2024年12月5日
angaochong  LV5 2024年10月29日
lqzixi  LV4 2024年10月9日
cyd yyds  LV2 2024年8月5日
校园网  LV9 2024年8月4日
qwe6002  LV10 2024年5月24日
dzx199212  LV1 2024年5月22日
sunlea  LV20 2024年5月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友