package com.jw.app.user.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

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;
import com.gaowj.business.util.SessionUtil;
import com.jw.app.business.GroupBusiness;
import com.jw.app.business.utils.Status;


public class actAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6496396449440750926L;

	private JSONObject jsonObject = new JSONObject();

	private File leftFile;

	private File appFile;

	private String loginuserCode = SessionUtil.getCode();

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public File getLeftFile() {
		return leftFile;
	}

	public void setLeftFile(File leftFile) {
		this.leftFile = leftFile;
	}

	public File getAppFile() {
		return appFile;
	}

	public void setAppFile(File appFile) {
		this.appFile = appFile;
	}
	//添加权限
	public String addGroup() throws BusinessException{
		SystemOpeBusiness sysBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		GroupBusiness groupBusiness = BusinessManager.getBusiness(GroupBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		System.out.println("开始添加权限......");
		Map<String, Object> map = new HashMap<String, Object>();
		String gid = (String) entity.get("G_ID");
		map.put("G_ID", gid);
		if(entity.get("CODE")==null||StringUtils.isEmpty(entity.get("CODE").toString())){
			sysBusiness.delete_group_c_u(map);
		}else{
			String [] str = entity.get("CODE").toString().split(",");
			List<Map<String, Object>> listuser = groupBusiness.listGroupUser(map);
	        for(int i = 0 ; i < str.length ; i++){
	        	if(str[i].startsWith("2_")){
	        		String strid = str[i].substring(2);
	        		System.out.println("需要权限的用户："+ strid);
	        		if(listuser.size() == 0){
	        			map.put("U_ID", strid);
	    				sysBusiness.insert_group_c_u(map);
	        		}else{
	        			for(Map<String, Object> mapuser : listuser){
	            			if(strid.equals(mapuser.get("U_ID"))){
	            				listuser.remove(mapuser);
	            			}else{
	            				map.put("U_ID", strid);
	            				sysBusiness.insert_group_c_u(map);
	            			}
	            			break;
	            		}
	        		}	
	        	}
	        }
	        for(Map<String, Object> deluser : listuser){
	        	sysBusiness.delete_group_c_u(deluser);
	        }
		}
        System.out.println("更新权限结束......");
        jsonObject.putAll(Status.getStatus(Status.UPDATE_OK));
		return "json";
	}
	
	//添加权限
	public String addUserGLuser() throws BusinessException{
		SystemOpeBusiness sysBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request,RequestUtils.UPDATEDATAFIELDPREFIX);
		System.out.println("开始添加权限......");
		Map<String, Object> map = new HashMap<String, Object>();
		String guanli = (String) entity.get("CODE");
		String beiguanli = (String) entity.get("USER");
		String qxlx = (String) entity.get("QXLX");
		if(StringUtils.isNotEmpty(guanli) && StringUtils.isNotEmpty(qxlx)){
			map.put("GUANLI", guanli);
			map.put("QXLX", qxlx);
			sysBusiness.delete_person_p(map);
			if(StringUtils.isNotEmpty(beiguanli)){
				String [] str = beiguanli.split(",");
				for(int i = 0 ; i < str.length ; i++){
		        	if(str[i].startsWith("2_")){
		        		String strid = str[i].substring(2);
		        		map.put("BEIGUANLI", strid);
		        		sysBusiness.insert_person_p(map);
		        	}
				}
			}
		}
        System.out.println("更新权限结束......");
        jsonObject.putAll(Status.getStatus(Status.UPDATE_OK));
		return "json";
	}
}
