package com.gaowj.system.back;

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

import org.apache.commons.lang3.StringUtils;

import com.gaowj.business.SystemOpeBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.RequestUtils;

public class userScopeAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1494875526896488279L;
	private List<Map<String,Object>> scope;//可选取权限类别
	private String u_name;//设置管理人员姓名
	private String u_emid;//设置管理人员emid
	private List<Map<String,Object>> department;//所有部门
	private List<Map<String,Object>> loginuser;//所有人员
	private List<Map<String,Object>> oldPP;//所管理的人员
	private List<String> oldPPStr=new ArrayList<String>();
	
	public List<Map<String,Object>> getScope() {
		if(scope==null){
			scope = new ArrayList<Map<String,Object>>();
			Map<String,Object> ls_m = new HashMap<String,Object>(); 
			ls_m.put("SCOPECODE","YBYW");
			ls_m.put("SCOPENAME","一般业务");
			scope.add(ls_m);
			ls_m = null;
			ls_m = new HashMap<String,Object>();
            ls_m.put("SCOPECODE","SPGL");
            ls_m.put("SCOPENAME","审批管理");
            scope.add(ls_m);
		}
		return scope;
	}
	public void setScope(List<Map<String,Object>> scope) {
		this.scope = scope;
	}
	public List<Map<String, Object>> getLoginuser() {
		return loginuser;
	}
	public void setLoginuser(List<Map<String, Object>> loginuser) {
		this.loginuser = loginuser;
	}
	public List<Map<String, Object>> getOldPP() {
		return oldPP;
	}
	public void setOldPP(List<Map<String, Object>> oldPP) {
		this.oldPP = oldPP;
	}
	public List<Map<String, Object>> getDepartment() {
		return department;
	}
	public void setDepartment(List<Map<String, Object>> department) {
		this.department = department;
	}
	public List<String> getOldPPStr() {
		return oldPPStr;
	}
	public void setOldPPStr(List<String> oldPPStr) {
		this.oldPPStr = oldPPStr;
	}
	public String getU_name() {
		return u_name;
	}
	public void setU_name(String u_name) {
		this.u_name = u_name;
	}
	
	public String getU_emid() {
		return u_emid;
	}
	public void setU_emid(String u_emid) {
		this.u_emid = u_emid;
	}
	
	public String execute() throws BusinessException{
		reflush();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String save() throws BusinessException{
		String touu = RequestUtils.getString(request, "touu");
		String ls_qx = RequestUtils.getString(request, "qxlx");
		String emid = RequestUtils.getString(request, "emid");
		Map<String,Object> u = (Map<String, Object>) request.getSession().getAttribute("userInfo");
		String curr = (String) u.get("EMID");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		if(StringUtils.isNotEmpty(emid)){
			String[] uuids= touu.split(",");
			Map<String,Object> uu = new HashMap<String,Object>();
			uu.put("QXLX", ls_qx);
			uu.put("GUANLI", emid);
			uu.put("SETTING", curr);
			Map<String,Object> uuDel = new HashMap<String,Object>();
			uuDel.put("QXLX", ls_qx);
			uuDel.put("GUANLI", emid);
			if(StringUtils.isNotEmpty(touu)){
				systemOpeBusiness.delete_person_p(uuDel);
				for(int i=0;i<uuids.length;i++){
					uu.put("BEIGUANLI", uuids[i]);
					systemOpeBusiness.insert_person_p(uu);
				}
				
			}else{
				systemOpeBusiness.delete_person_p(uuDel);
			}
			
		}
		reflush();
		return "save";
		
	}
	
	public void reflush() throws BusinessException{
		String ls_myzd_id = RequestUtils.getString(request, RequestUtils.TABLEIDKEY);
		String ls_qx = RequestUtils.getString(request, "qxlx");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String,Object> query = new HashMap<String,Object>();
		if(StringUtils.isNotEmpty(ls_myzd_id) && StringUtils.isNotEmpty(ls_qx)){
			Map<String,Object> queryUser = new HashMap<String,Object>();
			queryUser.put("CODE", ls_myzd_id);
			Map<String,Object> guanli = systemOpeBusiness.listLoginUserInfo(queryUser).get(0);
			u_name = (String) guanli.get("EMNAME");
			u_emid = (String) guanli.get("EMID");
			
			department = systemOpeBusiness.listDepartmentInfo(query);
			loginuser = systemOpeBusiness.listLoginUserInfo(query);
			query.put("QXLX", ls_qx);
			query.put("GUANLI", u_emid);
			oldPP = systemOpeBusiness.listPerson_from_P(query);
			
			//
			for(Map<String,Object> u:oldPP){
              String str = (String)u.get("BEIGUANLI");
              oldPPStr.add(str);
			}
		}
		
	}
}
