package com.jw.app.group.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_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";
	}
}
