/** 
 * @Title: Groups.java 
 * @Package com.zhiwei.weixin 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author hero 
 * @date 2016年1月27日 下午1:58:33 
 * @version V1.0 
 */ 
package com.zhiwei.weixin;

import java.util.HashMap;
import java.util.List;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhiwei.weixin.accesstoken.CreatAccessToken;
import com.zhiwei.weixin.http.HttpRequest;
import com.zhiwei.weixin.pojo.Group;
import com.zhiweidata.bjfzxWebapp.util.WechatCodeUtil;

/** 
 * @ClassName: Groups 
 * @Description: TODO(分组管理接口) 
 * @author hero 
 * @date 2016年1月27日 下午1:58:33  
 */
public class Groups {
	
	// 调用接口获取access_token  
    private static String accessToken ;  
	//记录日志
	private static Logger log = LoggerFactory.getLogger(Groups.class);
	
	//创建分组链接
	private static String create_group_url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN";

	//查询所有分组
	private static String getall_group_url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN";
	
	//查询用户所在分组
	private static String getid_url = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN";
	
	//修改分组名称
	private static String updateGroupName_url = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN";
	
	//移动用户分组
	private static String move_userGroup_url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN";
	
	//批量移动用户分组
	private static String move_userGroups_url = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=ACCESS_TOKEN";
	
	//删除分组
	private static String delete_groups_url = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=ACCESS_TOKEN";
	
	public Groups()
	{
		accessToken = WechatCodeUtil.getToken();
//		accessToken = new CreatAccessToken().getAccessToken();
	}

	
	/**
	 * 
	 * @Title: createGroup 
	 * @Description: TODO(创建分组) 
	 * @param @param groupName
	 * @param @return 设定文件 
	 * @return Group 返回类型
	 */
	public Group createGroup(String groupName)
	{
		Group group = new Group();
		String requestUrl = create_group_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		JSONObject jsonGroup = new JSONObject();
		jsonGroup.put("group", new JSONObject().put("name", groupName));
		// 调用接口创建菜单
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", jsonGroup.toString());
		//解析数据
		if (null != jsonObject) {
			try {
				if(null != jsonObject.get("group"))
				{
					JSONObject json = (JSONObject) jsonObject.get("group");
					group.setId(json.getInt("id"));
					group.setGroupName(json.getString("name"));
					group.setCount(0);
				}
			} catch (Exception e) {
				log.error("创建分组数据时出错：", e.getMessage());
				return null;
			}
			
		}
		return group;
	}
	
	
	/**
	 * 
	 * @Title: findAllGroup 
	 * @Description: TODO(查询所有分组) 
	 * @param @return 设定文件 
	 * @return List<Group> 返回类型
	 */
	public HashMap<String,Group> findAllGroup()
	{
		HashMap<String,Group> groupMap = new HashMap<String,Group>();
		String requestUrl = getall_group_url.replace("ACCESS_TOKEN", accessToken);
		// 调用接口创建菜单
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "GET", null);
		if(null != jsonObject)
		{
			try {
				JSONArray josnArray = jsonObject.getJSONArray("groups");
				for(int i = 0;i<josnArray.size();i++)
				{
					JSONObject json = josnArray.getJSONObject(i);
					Group group = new Group();
					group.setId(json.getInt("id"));
					group.setGroupName(json.getString("name"));
					group.setCount(json.getInt("count"));
					groupMap.put(json.getString("name"), group);
				}
			} catch (Exception e) {
				log.error("解析所有分组数据时出错：", e.getMessage());
				return null;
			}
		}
		return groupMap;
	}
	
	
	/**
	 * 
	 * @Title: showGroupIdByOpenid 
	 * @Description: TODO(根据用户uid查询用户所在分组) 
	 * @param @param openid
	 * @param @return 设定文件 
	 * @return int 返回类型
	 */
	public int showGroupIdByOpenid(String openid)
	{
		int groupId = -1;
		String requestUrl = getid_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		JSONObject dataJson = new JSONObject();
		dataJson.put("openid", openid);
		// 调用接口创建菜单
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", dataJson.toString());
		if(null != jsonObject)
		{
			try {
				groupId = jsonObject.getInt("groupid");
			} catch (Exception e) {
				log.error("根据用户openid查询分组时出错：", e.getMessage());
				return -1;
			}
		}
		return groupId;
	}
	
	
	/**
	 * 
	 * @Title: updateGroupName 
	 * @Description: TODO(修改分组名称) 
	 * @param @param groupId
	 * @param @param groupName
	 * @param @return 设定文件 
	 * @return int 返回类型
	 */
	public int updateGroupName(int groupId,String groupName)
	{
		int errcode = 0;
		String requestUrl = updateGroupName_url.replace("ACCESS_TOKEN", accessToken);
		JSONObject dataJson = new JSONObject();
		JSONObject groupJson = new JSONObject();
		groupJson.put("id", groupId);
		groupJson.put("name", groupName);
		dataJson.put("group", groupJson);
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", dataJson.toString());
		if(null != jsonObject)
		{
			errcode = jsonObject.getInt("errcode");
		}
		return errcode;
	}
	
	
	/**
	 * 
	 * @Title: updateGroupByOpenid 
	 * @Description: TODO(根据用户openid移动用户分组) 
	 * @param @param openid
	 * @param @param to_groupid
	 * @param @return 设定文件 
	 * @return int 返回类型
	 */
	public int updateGroupByOpenid(String openid, String to_groupid)
	{
		int errcode = 0;
		String requestUrl = move_userGroup_url.replace("ACCESS_TOKEN", accessToken);
		JSONObject dataJson = new JSONObject();
		dataJson.put("openid", openid);
		dataJson.put("to_groupid", to_groupid);
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", dataJson.toString());
		if(null != jsonObject)
		{
			errcode = jsonObject.getInt("errcode");
		}
		return errcode;
	}
	
	/**
	 * @deprecated:批量移动用户分组
	 * @param User
	 * 			微信用户信息
	 * @return int
	 * **/
	public int moveUserGroup(List<String> openids,String to_groupid)
	{
		int result = 0;
		String requestUrl = move_userGroups_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		JSONObject jsonUser = new JSONObject();
		jsonUser.put("openid_list", openids);
		jsonUser.put("to_groupid", to_groupid);
		System.out.println(jsonUser);
		// 调用接口创建菜单
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", jsonUser.toString());
		//解析数据
		if (null != jsonObject) 
		{
			if (0 != jsonObject.getInt("errcode")) 
			{
				result = jsonObject.getInt("errcode");
			}
		}
		return result;
	}
	
	/**
	 * 
	 * @Title: deleteGroup 
	 * @Description: TODO(删除用户分组) 
	 * @param @param groupId
	 * @param @return 设定文件 
	 * @return int 返回类型
	 */
	public int deleteGroup(String groupId)
	{
		int result = 0;
		String requestUrl = delete_groups_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		JSONObject dataJSON = new JSONObject();
		JSONObject groupJSON = new JSONObject();
		groupJSON.put("id", groupId);
		dataJSON.put("group", groupJSON);
		// 调用接口创建菜单
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "POST", dataJSON.toString());
		//解析数据
		if (null != jsonObject) 
		{
			result = jsonObject.getInt("errcode");
		}
		return result;
	}
}
