package com.jw.app.group.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.DigestException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.poi.util.SystemOutLogger;

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.Encipher;
import com.gaowj.business.util.Java_tool;
import com.gaowj.business.util.PropUtil;
import com.gaowj.business.util.RequestUtils;
import com.gaowj.business.util.SessionUtil;
import com.gaowj.business.vfs.FileUtil;
import com.gaowj.service.client.remote.util.AuthenticateUtil;
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(StringUtils.isEmpty(entity.get("CODE").toString())){
			sysBusiness.delete_group_c_m(map);
		}else{
			String [] str = entity.get("CODE").toString().split(",");
			List<Map<String, Object>> listuser = groupBusiness.listGroupModule(map);
	        for(int i = 0 ; i < str.length ; i++){
	        	if(str[i].startsWith("4_")){
	        		String strid = str[i].substring(2);
	        		System.out.println("需要权限的用户："+ strid);
	        		if(listuser.size() == 0){
	        			map.put("M_ID", strid);
	    				sysBusiness.insert_group_c_m(map);
	        		}else{
	        			for(Map<String, Object> mapuser : listuser){
	            			if(strid.equals(mapuser.get("M_ID"))){
	            				listuser.remove(mapuser);
	            			}else{
	            				map.put("M_ID", strid);
	            				sysBusiness.insert_group_c_m(map);
	            			}
	            			break;
	            		}
	        		}	
	        	}
	        }
	        for(Map<String, Object> deluser : listuser){
	        	sysBusiness.delete_group_c_m(deluser);
	        }
		}
        System.out.println("更新权限结束......");
        jsonObject.putAll(Status.getStatus(Status.UPDATE_OK));
		return "json";
	}
}
