package com.cy.report.action.manage;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.cy.report.action.BaseAction;
import com.cy.report.action.InitListener;
import com.cy.report.dao.DaoFactory;
import com.cy.report.dao.GameInfoDao;
import com.cy.report.database.MysqlDBManager;
import com.cy.report.pojo.AppInfo;
import com.cy.report.pojo.ChannelInfo;
import com.cy.report.pojo.Group;
import com.cy.report.pojo.GroupAuthority;
import com.cy.report.pojo.User;
import com.cy.report.pojo.UserGroup;
import com.cy.report.pojo.UserOpenKey;
import com.cy.report.service.GroupService;
import com.cy.report.service.ManagerService;
import com.cy.report.service.ServiceFactory;
import com.cy.report.service.UserService;
import com.cy.report.utils.AuthUtil;
import com.cy.report.utils.Constant;
import com.cy.report.utils.RediesUtil;
import com.cy.report.utils.RegularUtil;
import com.cy.report.utils.StringUtil;

public class AccountManageAction extends BaseAction {
	private List<AppInfo> appList = new ArrayList<AppInfo>();
	private List<String> channelList = new ArrayList<String>();
	private List<String> reportList = new ArrayList<String>();
	private List<String> manageresourceList = new ArrayList<String>();
	private List<User> userList = new ArrayList<User>();
	private List<Group> groupList = new ArrayList<Group>();
	private static final String USERDUMPLICATE_KEY = "user_dumplicate";
	private static final String NO_ROLE = "no_role";
	private static final String LOGIN_KEY = "login";
	private static final String ERROR = "error";
//	private int userid = 1;
	private int groupidforList = -1;
	public User user;
	public Group group;
	private String message;
	private String jsonStr;
	private String groupid;
	private UserOpenKey userOpenKey;
	
	private ManagerService ms = ServiceFactory.getManagerService();
	private GroupService gs = ServiceFactory.getGroupService();
	private UserService us = ServiceFactory.getUserService();

	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}
	public List<User> getUserList() {
		return userList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}



	public List<Group> getGroupList() {
		return groupList;
	}



	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}
	
	

	public int getGroupidforList() {
		return groupidforList;
	}

	public void setGroupidforList(int groupidforList) {
		this.groupidforList = groupidforList;
	}
	
	

	public String getGroupid() {
		return groupid;
	}

	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}
	
	


	public String execute() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int userid = user.getId();
					if (ismanager) {
						userid = user.getParentuserid();
					}
					if (request.getParameter("groupid") == null) {
						userList = this.us.getUserListByParentUserId(userid);
						for (User u : userList) {
							List<String> list = getGroupInfoByUserId(u.getId());
							if (StringUtil.isEmpty(list.get(0)) && StringUtil.isEmpty(list.get(1))) {
								if (u.getIsmanager().equals("1")) {
									u.setGroupids("-2");
									u.setGroupnames("-2");
								}
							} else {
								if (u.getIsmanager().equals("1")) {
									u.setGroupids(list.get(0)+",-2");
									u.setGroupnames(list.get(1)+",-2");
								} else {
									u.setGroupids(list.get(0));
									u.setGroupnames(list.get(1));
								}
							}
							
						}
					} else {
						String groupid = request.getParameter("groupid");
						this.setGroupidforList(Integer.valueOf(groupid));
						if (this.groupidforList == -1) {
							userList = this.us.getUserListByParentUserId(userid);
							for (User u : userList) {
								List<String> list = getGroupInfoByUserId(u.getId());
								if (StringUtil.isEmpty(list.get(0)) && StringUtil.isEmpty(list.get(1))) {
									if (u.getIsmanager().equals("1")) {
										u.setGroupids("-2");
										u.setGroupnames("-2");
									}
								} else {
									if (u.getIsmanager().equals("1")) {
										u.setGroupids(list.get(0)+",-2");
										u.setGroupnames(list.get(1)+",-2");
									} else {
										u.setGroupids(list.get(0));
										u.setGroupnames(list.get(1));
									}
								}
							}
							
						} else if (this.groupidforList == -2) {
							userList = this.us.getUserListByParentUserIdAndIsManager(userid);
							for (User u : userList) {
								List<String> list = getGroupInfoByUserId(u.getId());
								if (StringUtil.isEmpty(list.get(0)) && StringUtil.isEmpty(list.get(1))) {
									u.setGroupids("-2");
									u.setGroupnames("-2");
								} else {
									u.setGroupids(list.get(0)+",-2");
									u.setGroupnames(list.get(1)+",-2");
								}
							}
						} else {
							List<UserGroup> ugL = this.ms.getUsersByGroupId(this.groupidforList);
							for (UserGroup ug : ugL) {
								User u = this.us.getUserByUserId(ug.getUserid());
								List<String> list = getGroupInfoByUserId(u.getId());
								u.setGroupids(list.get(0));
								u.setGroupnames(list.get(1));
								
								userList.add(u);
							}
						}
					}
					groupList = this.gs.getGroupListByCreaeteUserId(userid);
					appList = this.ms.getAppByUserId(String.valueOf(userid));
					reportList = this.ms.getReports();
					manageresourceList = this.ms.getManageResource();
					return SUCCESS;
				} else {
					return NO_ROLE;
				}
			} else {
				return LOGIN_KEY;
			}
		} else {
			return LOGIN_KEY;
		}
	}
	
	public String createOriginalUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String email = request.getParameter("email");
		boolean b1 = RegularUtil.regularEmail(email);
		int userid = -1;
		if (!b1) {
			//email格式不正确.
			userid = -3; 
		} else {
			User u = this.us.getUserByEmail(email);
			if (u == null) {
				String pass = request.getParameter("password");
				String userName = request.getParameter("name");
				if (StringUtil.isEmpty(userName)) {
					System.out.println(email.indexOf("@"));
					userName = email.substring(0, email.indexOf("@"));
				}
				boolean b2 = RegularUtil.regularPass(pass);
				boolean b3 = RegularUtil.regularECNPL(userName);
				if (!b2) {
					//pass格式不正确.
					userid = -4;
				} else if (!b3){
					//显示名格式不正确.
					userid = -5;
				} else {
					User user = new User();
					user.setEmail(email);
					user.setPassword(pass);
					user.setUsername(userName);
					user.setParentuserid(0);
					user.setIsoriginaluser("1");
					user.setAccount_level(1);
					userid = this.us.insertOriginalUser(user);
				}
			} else {
				//email已经存在.
				userid = -2;
			}
		}
		jsonStr = "{\"result\":"+userid+"}";
		return SUCCESS;
	}
	
	public String createOriginalUserIndex() {
		return SUCCESS;
	}
	
	public String grantApp2SpecialAccountIndex() {
		return SUCCESS;
	}
	
	public String grantApp2SpecialAccount() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String appkey = request.getParameter("appkey");
		List<AppInfo> appList = ServiceFactory.getAppInfoService().getAppInfoByAppKey(appkey);
		AppInfo app = appList.get(0);
		String os = app.getOs();
		String category = app.getCategory();
		String step = app.getStep();
		String status = app.getStatus();
		
		String userid = String.valueOf(this.us.getUserByEmail(email).getId());
		
		String sql = "insert into appinfo(dev_id,appkey,name,os,category,step,status) values('"+userid+"','"+appkey+"','"+name+"','"+os+"','"+category+"','"+step+"','"+status+"')";
		MysqlDBManager.insert(sql);
		jsonStr = "{\"result\":\"success\"}";
		return SUCCESS;
	}
	
	
	public String addAccount() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User userV = (User) session.getAttribute(Constant.SESSION_KEY);
			if (userV != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(userV);
				boolean ismanager = AuthUtil.isManagerUser(userV);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int account_id = userV.getId();
					if (ismanager) {
						account_id = userV.getParentuserid();
					}
					if (request.getParameter("id") != null) {
						String id = request.getParameter("id");
						user = this.us.getUserByUserId(Integer.valueOf(id));
						List<String> list = getGroupInfoByUserId(user.getId());
						user.setGroupids(list.get(0));
						user.setGroupnames(list.get(1));
					}
					groupList = this.gs.getGroupListByCreaeteUserId(account_id);
					return SUCCESS;
				} else {
					return NO_ROLE;
				}
			} else {
				return LOGIN_KEY;
			}
		} else {
			return LOGIN_KEY;
		}
	}
	public String addAccountGroup() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					appList = this.ms.getAppByUserId(String.valueOf(account_id));
					reportList = this.ms.getReports();
					manageresourceList = this.ms.getManageResource();
					if (request.getParameter("groupid") != null) {
						String id = request.getParameter("groupid");
						if (this.gs.isCreateUserRight(account_id, Integer.valueOf(id))) {
							groupid = id;
							return SUCCESS;
						} else {
							jsonStr = "{\"result\":-2}";
							return SUCCESS;
						}
					}
					return SUCCESS;
				} else {
					return NO_ROLE;
				}
			} else {
				return LOGIN_KEY;
			}
		} else {
			return LOGIN_KEY;
		}
	}
	
	/**
	 * 子账户注册,新建账户,如果账户不是管理员账户,ismanager为0,则将账户与相关账户组关联.
	 * @return
	 */
	public String createUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int parentuserid = user.getId();
					if (ismanager) {
						parentuserid = user.getParentuserid();
					}
//				int parentuserid = userid;
					if (this.user != null) {
						boolean b1 = RegularUtil.regularEmail(this.user.getEmail());
						boolean b2 = true;//RegularUtil.regularPass(this.user.getPassword());
						boolean b3 = true;
						if (!StringUtil.isEmpty(this.user.getUsername())) {
							if (!RegularUtil.regularECNPL(this.user.getUsername())) {
								b3 = false;
							}
						}
						boolean b4 = true;
						if (!StringUtil.isEmpty(this.user.getPosition())) {
							if (!RegularUtil.regularECNPL(this.user.getPosition())) {
								b4 = false;
							}
						}
						if (b1 && b2 && b3 && b4) {
							this.user.setParentuserid(parentuserid);
							this.user.setIsoriginaluser("0");
							int id = us.insertUser(this.user);
							if (this.user.getIsmanager().equals("0")) {
								String group =  this.user.getGroupids();
								grantGroupsToUser(user, id, group, ismanager);
							}
							return SUCCESS;
						} else {
							return ERROR;
						}
						
						
						
					}
				} else {
					return NO_ROLE;
				}
			} else {
				return LOGIN_KEY;
			}
		} else {
			return LOGIN_KEY;
		}
		return null;
	}
	/**
	 * 通过email验证用户是否已经存在,存在返回1,不存在返回0,未登录返回-1,email格式不正确返回-3.
	 * @return
	 */
	public String validUserByEmail() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					String email = request.getParameter("email");
					////System.out.println("email...."+email);
					if (RegularUtil.regularEmail(email)) {
						
						User userr = this.us.getUserByEmail(email);
						if (userr == null) {
							jsonStr = "{\"result\":0}";
						} else {
							jsonStr = "{\"result\":1}";
						}
						////System.out.println(jsonStr);
						return SUCCESS;
					} else {
						jsonStr = "{\"result\":-3}";
						return SUCCESS;
					}
					
					
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	/**
	 * 修改密码
	 */
	public String alterPasswd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		jsonStr = "{\"data\":\"no\"}";
		if( session != null && session.getAttribute(Constant.SESSION_KEY) != null ) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if( user != null ){
				String oldPasswd = request.getParameter("oldpasswd");
				String newPasswd = request.getParameter("newpasswd");
//				if( !RegularUtil.regularPass(newPasswd) ){
//					jsonStr = "{\"data\":\"formaterror\"}";
//					return SUCCESS;
//				}
				int accound_id = user.getId();
				if( this.us.isRightPasswd(oldPasswd,user) ){
					user.setPassword(newPasswd);
					this.us.updateUserPassByUserId(user);
					jsonStr = "{\"data\":\"right\"}";
				}else {
					jsonStr = "{\"data\":\"wrong\"}";
					return SUCCESS;
				}
			}else {
				return LOGIN_KEY;
			}
		}else {
			return LOGIN_KEY;
		}
		
		return SUCCESS;
	}
	/**
	 * 修改账户信息,如果账户不是管理员账户,iamanager为0,则需要删除对应的账户组关联(删除该账户原有的关联,建立新的关联).
	 * 密码为空,则不修改账户密码,密码不为空,将密码加密后修改账户密码.
	 * @return
	 */
	public String alterUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					if (this.us.isParentUserRight(account_id, this.user)) {
						String pass = this.user.getPassword();
						if (!StringUtil.isEmpty(pass)) {
							boolean b2 = true;//RegularUtil.regularPass(this.user.getPassword());
							boolean b3 = true;
							if (!StringUtil.isEmpty(this.user.getUsername())) {
								if (!RegularUtil.regularECNPL(this.user.getUsername())) {
									b3 = false;
								}
							}
							boolean b4 = true;
							if (!StringUtil.isEmpty(this.user.getPosition())) {
								if (!RegularUtil.regularECNPL(this.user.getPosition())) {
									b4 = false;
								}
							}
							if (b2 && b3 && b4) {
								
								int userid = this.us.updateUserPassByUserId(this.user);
								if (this.user.getIsmanager().equals("0")) {
									this.ms.deleteUerGroupByUserId(userid);
									grantGroupsToUser(user, userid, this.user.getGroupids(), ismanager);
								} else {
									this.ms.deleteUerGroupByUserId(userid);
								}
							} else {
								return ERROR;
							}
						} else {
							boolean b3 = true;
							if (!StringUtil.isEmpty(this.user.getUsername())) {
								if (!RegularUtil.regularECNPL(this.user.getUsername())) {
									b3 = false;
								}
							}
							boolean b4 = true;
							if (!StringUtil.isEmpty(this.user.getPosition())) {
								if (!RegularUtil.regularECNPL(this.user.getPosition())) {
									b4 = false;
								}
							}
							if (b3 && b4) {
								
								int userid = this.us.updateUserByUserId(this.user);
								if (this.user.getIsmanager().equals("0")) {
									this.ms.deleteUerGroupByUserId(userid);
									grantGroupsToUser(user, userid, this.user.getGroupids(), ismanager);
								} else {
									this.ms.deleteUerGroupByUserId(userid);
								}
							} else {
								return ERROR;
							}
						}
						return SUCCESS;
					} else {
						return NO_ROLE;
					}
				} else {
					return NO_ROLE;
				}
			} else {
				return LOGIN_KEY;
			}
		} else {
			return LOGIN_KEY;
		}
	}
	
	
	/**
	 * 批量删除账户,如果账户不是管理员账户,ismanager为0,则需要删除对应的所有账户组关联.
	 * @return
	 */
	public String deleteUsers() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
						String useridParam =  request.getParameter("userid");
						if (useridParam.contains(",")) {
							String[] userids = useridParam.split(",");
							for (String id : userids) {
								int userid = Integer.valueOf(id);
								if (this.us.isParentUserRight(user.getId(), userid)) {
									User u = this.us.getUserByUserId(userid);
									if (u.getIsmanager().equals("0")) {
										this.ms.deleteUerGroupByUserId(userid);
									}
									this.us.deleteUserById(userid);
								}
							}
						} else {
							int userid = Integer.valueOf(useridParam);
							if (this.us.isParentUserRight(user.getId(), userid)) {
								User u = this.us.getUserByUserId(userid);
								if (u.getIsmanager().equals("0")) {
									this.ms.deleteUerGroupByUserId(userid);
								}
								this.us.deleteUserById(userid);
							}
						}
						jsonStr = "{\"result\":\"" + SUCCESS + "\"}";
						////System.out.println(jsonStr);
						return SUCCESS;
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	/**
	 * 通过groupname验证账户组是否已经存在,存在返回1,不存在返回0,-1,登陆,名字格式不正确返回-3.
	 * @return
	 */
	public String validGroupByGroupName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					String groupName = request.getParameter("groupname");
					if (RegularUtil.regularECNPL(groupName)) {
						
						Group group = this.gs.getGroupByGroupName(groupName);
						
						if (group == null) {
							jsonStr = "{\"result\":0}";
						} else {
							jsonStr = "{\"result\":1}";
						}
						////System.out.println(jsonStr);
						return SUCCESS;
					} else {
						jsonStr = "{\"result\":-3}";
						return SUCCESS;
					}
					
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	/**
	 * 创建账户组,并关联账户组相关的appkey、channelname、reportname.
	 * authorityJsonStr 格式为[{"appname":"ee","channelnames":"21,32","reportnames":"active,retention","resourcenames":"event,warnmanage"},{"appname":"e2","channelnames":"21,32","reportnames":"active,retention"}]
	 * @return  名字格式不正确返回-3.
	 */
	public String createGroup() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int userid = user.getId();
					if (ismanager) {
						userid = user.getParentuserid();
					}
					String groupName = request.getParameter("groupname");
					if (RegularUtil.regularECNPL(groupName)) {
						String authorityJsonStr = request.getParameter("authorityJsonStr");
						////System.out.println(authorityJsonStr);
						Group group = new Group();
						group.setCreateuserid(userid);
						group.setGroupname(groupName);
						int id = this.gs.insertGroupOnlyName(group);
						if (!StringUtil.isEmpty(authorityJsonStr)) {

							JSONArray json = JSONArray.fromObject(authorityJsonStr);
							StringBuffer sb = new StringBuffer("INSERT INTO group_authority(groupid,appkey,channelname,reportname,authcategory) values");
							StringBuffer sbvalue = new StringBuffer();
							for (int i = 0; i < json.size(); i++) {
								JSONObject object = (JSONObject) json.get(i);
								String appname = object.getString("appkey");
								String channelnames = object.getString("channelnames");
								String reportnames = object.getString("reportnames");
								String resourcenames = object.getString("resourcenames");
								if (channelnames.equals("reyun_allchannel")) {
									channelnames = "reyun_allchannel_-3";
								}
//							String authCategory = String.valueOf(i+1);
								if (!StringUtil.isEmpty(reportnames)) {
									sbvalue.append(grantAppChannelReportToGroup(String.valueOf(id), appname,
											channelnames, reportnames));
								}
								if (!StringUtil.isEmpty(resourcenames)) {
									sbvalue.append(grantAppResourceToGroup(String.valueOf(id), appname, resourcenames));
								}
								
							}
							if (sbvalue.length()>0) {
								sb.append(sbvalue.substring(1));
								System.out.println(sb.toString());
								MysqlDBManager.insert(sb.toString());
							}
						
//							JSONArray json = JSONArray.fromObject(authorityJsonStr);
//							for (int i = 0; i < json.size(); i++) {
//								JSONObject object = (JSONObject) json.get(i);
//								String appname = object.getString("appkey");
//								String channelnames = object.getString("channelnames");
//								String reportnames = object.getString("reportnames");
//								String resourcenames = object.getString("resourcenames");
////							String authCategory = String.valueOf(i+1);
//								if (!StringUtil.isEmpty(reportnames)) {
//									grantAppChannelReportToGroup(String.valueOf(id), appname,
//											channelnames, reportnames);
//								}
//								if (!StringUtil.isEmpty(resourcenames)) {
//									grantAppResourceToGroup(String.valueOf(id), appname, resourcenames);
//								}
//							}
						}
						jsonStr = "{\"groupid\":" + id + "}";
						////System.out.println(jsonStr);
						return SUCCESS;
						
					} else {
						jsonStr = "{\"result\":-3}";
						return SUCCESS;
					}
					
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}

		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	
	/**
	 * 创建账户组,并关联账户组相关的appkey、channelname、reportname.
	 * 
	 * @return  名字格式不正确返回-3.
	 */
	public String createGroupOnlyName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					
					int userid = user.getId();
					if (ismanager) {
						userid = user.getParentuserid();
					}
					String groupName = request.getParameter("groupname");
					if (RegularUtil.regularECNPL(groupName)) {
						
						Group group = this.gs.getGroupByGroupName(groupName);
						
						if (group == null) {
							group = new Group();
							group.setCreateuserid(userid);
							group.setGroupname(groupName);
							int id = this.gs.insertGroupOnlyName(group);
							jsonStr = "{\"groupid\":" + id + "}";
						} else {
							jsonStr = "{\"groupid\":" + -1 + "}";
						}
						
						
						////System.out.println(jsonStr);
						return SUCCESS;
					} else {
						jsonStr = "{\"result\":-3}";
						return SUCCESS;
					}
					
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	
	/**
	 * 修改账户组信息,及其对应的appkey、channelname、reportname关联(删除该账户组原有的关联,建立新的关联).
	 * @return 名字格式不正确返回-3.
	 */
	public String alterGroup() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					int id = Integer.valueOf(request.getParameter("groupid"));
					if (this.gs.isCreateUserRight(account_id, id)) {
						String groupName = request.getParameter("groupname");
						if (RegularUtil.regularECNPL(groupName)) {
							
							String authorityJsonStr = request.getParameter("authorityJsonStr");
							Group group = new Group();
							group.setId(id);
							// group.setCreateuserid(userid);
							group.setGroupname(groupName);
							
							this.gs.updateGroupById(group);
							this.ms.deleteGroupAppChannelReportByGroupId(String.valueOf(id));
							////System.out.println("authorityJsonStr-----------" + authorityJsonStr);
							if (!StringUtil.isEmpty(authorityJsonStr)) {
								JSONArray json = JSONArray.fromObject(authorityJsonStr);
								StringBuffer sb = new StringBuffer("INSERT INTO group_authority(groupid,appkey,channelname,reportname,authcategory) values");
								int length = sb.length();
								StringBuffer sbvalue = new StringBuffer();
								for (int i = 0; i < json.size(); i++) {
									JSONObject object = (JSONObject) json.get(i);
									String appname = object.getString("appkey");
									String channelnames = object.getString("channelnames");
									String reportnames = object.getString("reportnames");
									String resourcenames = object.getString("resourcenames");
									if (channelnames.equals("reyun_allchannel")) {
										channelnames = "reyun_allchannel_-3";
									}
//								String authCategory = String.valueOf(i+1);
									if (!StringUtil.isEmpty(reportnames)) {
										sbvalue.append(grantAppChannelReportToGroup(String.valueOf(id), appname,
												channelnames, reportnames));
									}
									if (!StringUtil.isEmpty(resourcenames)) {
										sbvalue.append(grantAppResourceToGroup(String.valueOf(id), appname, resourcenames));
									}
									
								}
								if (sbvalue.length()>0) {
									sb.append(sbvalue.substring(1));
									System.out.println(sb.toString());
									MysqlDBManager.insert(sb.toString());
								}
							}
							jsonStr = "{\"groupid\":" + id + "}";
							////System.out.println(jsonStr);
							return SUCCESS;
						} else {
							jsonStr = "{\"result\":-3}";
							return SUCCESS;
						}
						
					}
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	/**
	 * 获取账户组所有信息及权限,并组装成JSON格式:{"groupid":14,"groupname":"test","authorityJsonStr":[{"reportnames":"payment,retetion","appname":"xiyou2","appkey":"1004","channelnames":"8"}]}
	 * @return
	 */
	public String getGroupInfoForUpdate() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				String isspecialaccount = (String)session.getAttribute("isspecialaccount");
				if ((isoriginaluser || ismanager) && "0".equals(isspecialaccount)) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					String id = request.getParameter("groupid");
					if (this.gs.isCreateUserRight(account_id, Integer.valueOf(id))) {
						
						String groupName = this.gs.getGroupById(Integer.valueOf(id)).getGroupname();
						String authJsonStr = getAuthByGroupId(id,String.valueOf(account_id));
						
//						String authorityJsonStr = getAuthorityByGroupId(id);
						jsonStr = "{\"groupid\":"+id+",\"groupname\":\""+groupName+"\",\"authorityJsonStr\":"+authJsonStr+"}";
						////System.out.println("jsonStr>>>>"+jsonStr);
						return SUCCESS;
					} else {
						jsonStr = "{\"result\":-2}";
						return SUCCESS;
					}
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	
	/**
	 * 删除账户组,及其对应的appkey、channelname、reportname关联,userid的关联.
	 * @return
	 */
	public String deleteGroup() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				if (isoriginaluser || ismanager) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					String groupidParam = ServletActionContext.getRequest().getParameter(
					"groupid");
					int groupid = Integer.valueOf(groupidParam);
					if (this.gs.isCreateUserRight(account_id, groupid)) {
						this.gs.deleteGroupById(groupid);
						this.ms.deleteGroupAppChannelReportByGroupId(String.valueOf(groupid));
						this.ms.deleteUerGroupByGroupId(groupid);
						jsonStr = "{\"result\":\"" + SUCCESS + "\"}";
						////System.out.println(jsonStr);
						return SUCCESS;
					}
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	
	/**
	 * 将一个或多个账户从某一个账户组中移除,如果该账户组为管理员账户组,id为-1,设置账户们ismanager字段为0即可.
	 * @return
	 */
	public String deleteUsersFromGroup() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				if (isoriginaluser || ismanager) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					String useridParam =  ServletActionContext.getRequest().getParameter("userid");
					String groupidParam =  ServletActionContext.getRequest().getParameter("groupid");
					if (useridParam.contains(",")) {
						String[] userids = useridParam.split(",");
						for (String id : userids) {
							if (groupidParam.equals("-2")) {
								User u = this.us.getUserByUserId(Integer.valueOf(id));
								u.setIsmanager("0");
								this.us.updateIsManagerById(u);
							} else {
								if (this.us.isParentUserRight(account_id, Integer.valueOf(id)) && this.gs.isCreateUserRight(account_id, Integer.valueOf(groupidParam))) {
									this.ms.deleteUserGroupByGroupIdAndUserId(Integer.valueOf(id), Integer.valueOf(groupidParam));
								}
							}
						}
					} else {
						if (groupidParam.equals("-2")) {
							User u = this.us.getUserByUserId(Integer.valueOf(useridParam));
							u.setIsmanager("0");
							this.us.updateIsManagerById(u);
						} else {
							if (this.us.isParentUserRight(account_id, Integer.valueOf(useridParam)) && this.gs.isCreateUserRight(account_id, Integer.valueOf(groupidParam))) {
								this.ms.deleteUserGroupByGroupIdAndUserId(Integer.valueOf(useridParam), Integer.valueOf(groupidParam));
							}
						}
					}
					jsonStr = "{\"result\":\"" + SUCCESS + "\"}";
					return SUCCESS;
					
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	
	/**
	 * 将一个或多个账户与一个或多个账户组关联,如果将用户授权到管理员账户组,更新用户ismanager字段为1即可.
	 * @return
	 */
	public String grantUsersToGroups() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				if (isoriginaluser || ismanager) {
					int account_id = user.getId();
					if (ismanager) {
						account_id = user.getParentuserid();
					}
					
					String useridParam =  ServletActionContext.getRequest().getParameter("userid");
					String groupidParam =  ServletActionContext.getRequest().getParameter("groupid");
					if (useridParam.contains(",")) {
						String[] userids = useridParam.split(",");
						for (String id : userids) {
							if (this.us.isParentUserRight(account_id, Integer.valueOf(id))) {
								grantGroupsToUser(user, Integer.valueOf(id), groupidParam, ismanager);
							}
						}
					} else {
						if (this.us.isParentUserRight(account_id, Integer.valueOf(useridParam))) {
							grantGroupsToUser(user, Integer.valueOf(useridParam), groupidParam, ismanager);
						}
					}
					jsonStr = "{\"result\":\"" + SUCCESS + "\"}";
					return SUCCESS;
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	/**
	 * 选择appkey之后,返回该app对应的channel.
	 * @return
	 */
	public String getChannelByAppKey() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(session != null && session.getAttribute(Constant.SESSION_KEY) != null) {
			User user = (User) session.getAttribute(Constant.SESSION_KEY);
			if (user != null) {
				////System.out.println(user.getEmail());
				boolean isoriginaluser = AuthUtil.isOriginalUser(user);
				boolean ismanager = AuthUtil.isManagerUser(user);
				if (isoriginaluser || ismanager) {
					String appkey =  ServletActionContext.getRequest().getParameter("appkey");
					channelList = RediesUtil.readAppChannel(appkey);
					List<ChannelInfo> channels = ServiceFactory.getChannelManageService().getChannelList(appkey);
					StringBuffer sb = new StringBuffer();
//					for (ChannelInfo info : channels) {
//						String channelkey = info.getChannelkey();
//						if (!channelList.contains(channelkey)) {
//							channelList.add(channelkey);
//						}
//					}
					for (String c : channelList) {
						sb.append(c).append(",");
					}
					String r = sb.toString();
					if (r.contains(",")) {
						r = r.substring(0, r.length()-1);
					}
					jsonStr = "{\"channelnames\":\""+r+"\"}";
					////System.out.println(jsonStr);
					return SUCCESS;
//					this.setChannelList(ms.filterChannelByUserIdAndAppKey(userid, appkey, channelList));
				} else {
					jsonStr = "{\"result\":-2}";
					return SUCCESS;
				}
			}
		}
		jsonStr = "{\"result\":-1}";
		return SUCCESS;
	}
	
	/**
	 * 根据userid获取相关联账户组的id及name.
	 * @param userid
	 * @return
	 */
	private List<String> getGroupInfoByUserId(int userid) {
		List<String> result = new ArrayList<String>();
		List<UserGroup> usergroupList = this.ms.getGroupsByUserId(userid);
		StringBuffer groupsname = new StringBuffer();
		StringBuffer groupsid = new StringBuffer();
		for (UserGroup usergroup : usergroupList) {
			groupsid.append(usergroup.getGroupid()).append(",");
			Group group = this.gs.getGroupById(usergroup.getGroupid());
			groupsname.append(group.getGroupname());
			groupsname.append(",");
		}
		if (groupsname.toString().contains(",") && groupsid.toString().contains(",")) {
			String groupid = groupsid.toString().substring(0, groupsid.toString().length()-1);
			String groupname = groupsname.toString().substring(0, groupsname.toString().length()-1);
			result.add(groupid);
			result.add(groupname);
		} else {
			result.add("");
			result.add("");
		}
		return result;
	}
	
	/**
	 * 将账户与一个或多个账户组关联.
	 * @param userid
	 * @param groups
	 */
	private void grantGroupsToUser(User user, int userid, String groups, boolean ismanager) {
		int account_id = user.getId();
		if (ismanager) {
			account_id = user.getParentuserid();
		}
		if (groups.contains(",")) {
			String[] groupIds = groups.split(",");
			for (String groupId : groupIds) {
				if (groupId.equals("-2")) {
					User u = this.us.getUserByUserId(Integer.valueOf(userid));
					u.setIsmanager("1");
					this.us.updateIsManagerById(u);
				} else {
					if (this.gs.isCreateUserRight(account_id, Integer.valueOf(groupId))) {
						this.ms.grantGroupsToUser(userid, Integer.valueOf(groupId));
					}
				}
			}
		} else {
			if (groups.equals("-2")) {
				User u = this.us.getUserByUserId(Integer.valueOf(userid));
				u.setIsmanager("1");
				this.us.updateIsManagerById(u);
			} else {
				if (this.gs.isCreateUserRight(account_id, Integer.valueOf(groups))) {
					this.ms.grantGroupsToUser(userid, Integer.valueOf(groups));
				}
			}
		}
	}
	/**
	 * 设置报表权限,权限类型设置为1.
	 * 将groupid与某个appkey对应的所有的channelname、reportname关联.
	 * @param groupid
	 * @param appkey
	 * @param channelnames
	 * @param reportnames
	 */
	private String grantAppChannelReportToGroup(String groupid, String appkey,
			String channelnames, String reportnames) {
		StringBuffer sb = new StringBuffer();
		if (channelnames.contains(",")) {
			String[] channels = channelnames.split(",");
			for (String channel : channels) {
				String authCategory = "1";
				if (channel.equals("-1")) {
					authCategory = "2";
				}
				if (reportnames.contains(",")) {
					String[] reports = reportnames.split(",");
					for (String report : reports) {
						sb.append(",").append("('"+groupid+"','"+appkey+"','"+channel+"','"+report+"','"+authCategory+"')");
//						this.ms.grantAppChannelReportToGroup(groupid, appkey, channel, report, authCategory);
					}
				} else {
					sb.append(",").append("('"+groupid+"','"+appkey+"','"+channel+"','"+reportnames+"','"+authCategory+"')");
//					this.ms.grantAppChannelReportToGroup(groupid, appkey, channel, reportnames, authCategory);
				}
			}
		} else {
			String authCategory = "1";
			if (channelnames.equals("-1")) {
				authCategory = "2";
			}
			if (reportnames.contains(",")) {
				String[] reports = reportnames.split(",");
				for (String report : reports) {
					sb.append(",").append("('"+groupid+"','"+appkey+"','"+channelnames+"','"+report+"','"+authCategory+"')");
//					this.ms.grantAppChannelReportToGroup(groupid, appkey, channelnames, report, authCategory);
				}
			} else {
				sb.append(",").append("('"+groupid+"','"+appkey+"','"+channelnames+"','"+reportnames+"','"+authCategory+"')");
//				this.ms.grantAppChannelReportToGroup(groupid, appkey, channelnames, reportnames, authCategory);
			}
		}
//		return sb;
//		if (sb.length()>0)
//		{
//			return sb.substring(1);
//		}
		return sb.toString();
		
	}
	/**
	 * 授权管理权限.channel自动设置为-1,权限类型设置为2.
	 * @param groupid
	 * @param appkey
	 * @param resoutcenames
	 */
	private String grantAppResourceToGroup(String groupid, String appkey, String resoutcenames) {
		String[] resources = resoutcenames.split(",");
		StringBuffer sb = new StringBuffer();
		for (String resource : resources) {
			sb.append(",").append("('"+groupid+"','"+appkey+"','-1','"+resource+"','2')");
//			this.ms.grantAppChannelReportToGroup(groupid, appkey, "-1", resource, "2");
		}
//		if (sb.length()>0)
//		{
//			return sb.substring(1);
//		}
		return sb.toString();
	}
	/**
	 * 获取某个账户组所有的权限,组装JSON数组:[{"reportnames":"payment,retetion","appname":"xiyou2","appkey":"1004","channelnames":"8"}]
	 * @param groupid
	 * @return
	 */
//	private String getAuthorityByGroupId(String groupid) {
//		Map<String, Map<String, String>> authMap = ms.getAuthMapByGroupId(groupid);
//		Set<Entry<String, Map<String, String>>> authSet = authMap.entrySet();
//		List<Authority> list = new ArrayList<Authority>();
//		for (Entry<String, Map<String, String>> authEn : authSet) {
//			String authIndex = authEn.getKey();
//			//System.out.println("getAuthorityByGroupId~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+authIndex);
//			Map<String, String> map = authEn.getValue();
//			Set<Entry<String, String>> set = map.entrySet();
//			for (Entry en : set) {
//				String key = (String) en.getKey();
//				String[] keys =key.split("_");
//				String appkey = keys[0];
//				String appname = keys[1];
//				String c_r = (String) en.getValue();
//				String[] values = c_r.split(",");
//				String chans = "";
//				String repts = "";
//				for (String str : values) {
//					if (str.indexOf("_c") != -1) {
//						chans = str.substring(0, str.indexOf("_c")) + "," + chans;
//					}
//					if (str.indexOf("_r") != -1) {
//						repts = str.substring(0, str.indexOf("_r")) + "," + repts;
//					}
//				}
//				chans = chans.substring(0, chans.length()-1);
//				repts = repts.substring(0, repts.length()-1);
//				Authority au = new Authority(appkey, appname, chans, repts);
//				list.add(au);
//			}
//		}
//		JSONArray array = JSONArray.fromObject(list);
//		return array.toString();
//	}
	/**
	 * [{"resourcenames":"event,customevent,consumePointManage,channelmanage,warnmanage","reportnames":"active,comprehensive,retention,install,payment,reged,convertion,economy,online,frequency,new,customretetion,outflow,terminal,target,liftcycle,incomefactors,outflowcast,incomecast,incomeanalysis,paymentpermeate,paymentuserretention,whaleuser,virtualCur,consumePoint,segment","appname":"Android测试(请勿删除或修改)","appkey":"f7858cef36c76544a1599b6110c815ce","channelnames":"99,103"}]
	 */
	private String getAuthByGroupId(String groupid, String userid) {
		List<GroupAuthority> gaList = ms.getGroupAuthsByGroupId(groupid);
		//<appkey,auth object list>
		Map<String, List<GroupAuthority>> map = new HashMap<String, List<GroupAuthority>>();
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		for (GroupAuthority ga : gaList) {
			List<GroupAuthority> gl = null;
			String appkey = ga.getAppkey();
			if (map.containsKey(appkey)) {
				gl = map.get(appkey);
			} else {
				gl = new ArrayList<GroupAuthority>();
			}
			gl.add(ga);
			map.put(appkey, gl);
		}
		Set<Entry<String, List<GroupAuthority>>> enSet = map.entrySet();
		for (Entry<String, List<GroupAuthority>> en : enSet) {
			String appkey = en.getKey();
			List<AppInfo> appList = ServiceFactory.getAppInfoService().getAppInfoByAppKey(appkey);
//			List<String> cList = ServiceFactory.getQueryService().getList(appkey, "channel");
			List<String> cList = RediesUtil.readAppChannel(appkey);
			List<ChannelInfo> channelinfos = ServiceFactory.getChannelManageService().getChannelList(appkey);
			StringBuffer sb = new StringBuffer();
			for (ChannelInfo info : channelinfos) {
				String channelkey = info.getChannelkey();
				if (!cList.contains(channelkey)) {
					cList.add(channelkey);
				}
			}
			AppInfo app = null;
			for (AppInfo info : appList) {
				if (info.getDev_id().equals(userid)) {
					app = info;
				}
			}
			String appname = app.getName();
			List<GroupAuthority> list = en.getValue();
			StringBuffer channelsb = new StringBuffer();
			StringBuffer reportsb = new StringBuffer();
			StringBuffer resourcesb = new StringBuffer();
			StringBuffer csb = new StringBuffer();
			for (String c : cList) {
				csb.append(c).append(",");
			}
			String cs = csb.toString();
			if (cs.contains(",")) {
				cs = cs.substring(0, cs.length()-1);
			}
			List<String> sl = new ArrayList<String>();
			Map<String, String> innerMap = new HashMap<String, String>();
			for (GroupAuthority ga : list) {
				String category = ga.getAuthcategory();
				if (category.equals("1")) {
					String cn = ga.getChannelname();
					if (cn.equals("reyun_allchannel_-3")) {
						cn = "reyun_allchannel";
					}
					if (channelsb.indexOf(cn+",") == -1) {
						channelsb.append(cn).append(",");
					}
					if (reportsb.indexOf(ga.getReportname()+",") == -1) {
						reportsb.append(ga.getReportname()).append(",");
					}
				} else {
					if (resourcesb.indexOf(ga.getReportname()+",") == -1) {
						resourcesb.append(ga.getReportname()).append(",");
					}
				}
			}
			String channels = channelsb.toString();
			String reports = reportsb.toString();
			String resources = resourcesb.toString();
			if (channels.contains(",")) {
				channels =  channels.substring(0, channels.length()-1);
			}
			if (reports.contains(",")) {
				reports =  reports.substring(0, reports.length()-1);
			}
			if (resources.contains(",")) {
				resources =  resources.substring(0, resources.length()-1);
			}
			innerMap.put("channelnames", channels);
			innerMap.put("reportnames", reports);
			innerMap.put("resourcenames", resources);
			innerMap.put("appkey", appkey);
			innerMap.put("appname", appname);
			innerMap.put("channels", cs);
			result.add(innerMap);
		}
		
		
		JSONArray array = JSONArray.fromObject(result);
		return array.toString();
	}

	public List<AppInfo> getAppList() {
		return appList;
	}

	public void setAppList(List<AppInfo> appList) {
		this.appList = appList;
	}

	public List<String> getChannelList() {
		return channelList;
	}

	public void setChannelList(List<String> channelList) {
		this.channelList = channelList;
	}

	public List<String> getReportList() {
		return reportList;
	}

	public void setReportList(List<String> reportList) {
		this.reportList = reportList;
	}

	public List<String> getManageresourceList() {
		return manageresourceList;
	}

	public void setManageresourceList(List<String> manageresourceList) {
		this.manageresourceList = manageresourceList;
	}
	
	
	
}