View Javadoc
1   package com.foxinmy.weixin4j.qy.api;
2   
3   import java.util.List;
4   
5   import com.alibaba.fastjson.JSON;
6   import com.alibaba.fastjson.JSONObject;
7   import com.foxinmy.weixin4j.exception.WeixinException;
8   import com.foxinmy.weixin4j.http.weixin.ApiResult;
9   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10  import com.foxinmy.weixin4j.model.Token;
11  import com.foxinmy.weixin4j.qy.model.Party;
12  import com.foxinmy.weixin4j.token.TokenManager;
13  
14  /**
15   * 部门API
16   *
17   * @className PartyApi
18   * @author jinyu(foxinmy@gmail.com)
19   * @date 2014年11月18日
20   * @since JDK 1.6
21   * @see com.foxinmy.weixin4j.qy.model.Party
22   * @see <a href="https://work.weixin.qq.com/api/doc#10076">管理部门说明</a>
23   */
24  public class PartyApi extends QyApi {
25  	private final TokenManager tokenManager;
26  
27  	public PartyApi(TokenManager tokenManager) {
28  		this.tokenManager = tokenManager;
29  	}
30  
31  	/**
32  	 * 创建部门(根部门的parentid为1)
33  	 *
34  	 * @param party
35  	 *            部门对象
36  	 * @see com.foxinmy.weixin4j.qy.model.Party
37  	 * @see <a href="https://work.weixin.qq.com/api/doc#10076">创建部门说明</a>
38  	 * @return 部门ID
39  	 * @throws WeixinException
40  	 */
41  	public int createParty(Party party) throws WeixinException {
42  		String department_create_uri = getRequestUri("department_create_uri");
43  		JSONObject obj = (JSONObject) JSON.toJSON(party);
44  		if (party.getParentId() < 1) {
45  			obj.remove("parentid");
46  		}
47  		if (party.getId() < 1) {
48  			obj.remove("id");
49  		}
50  		Token token = tokenManager.getCache();
51  		WeixinResponse response = weixinExecutor.post(
52  				String.format(department_create_uri, token.getAccessToken()),
53  				obj.toJSONString());
54  		return response.getAsJson().getIntValue("id");
55  	}
56  
57  	/**
58  	 * 更新部门(如果非必须的字段未指定 则不更新该字段之前的设置值)
59  	 *
60  	 * @param party
61  	 *            部门对象
62  	 * @see com.foxinmy.weixin4j.qy.model.Party
63  	 * @see <a href="https://work.weixin.qq.com/api/doc#10077">更新部门说明</a>
64  	 * @return 处理结果
65  	 * @throws WeixinException
66  	 */
67  	public ApiResult updateParty(Party party) throws WeixinException {
68  		if (party.getId() < 1) {
69  			throw new WeixinException("department id must gt 1");
70  		}
71  		String department_update_uri = getRequestUri("department_update_uri");
72  		JSONObject obj = (JSONObject) JSON.toJSON(party);
73  		if (party.getParentId() < 1) {
74  			obj.remove("parentid");
75  		}
76  		if (party.getOrder() < 0) {
77  			obj.remove("order");
78  		}
79  		Token token = tokenManager.getCache();
80  		WeixinResponse response = weixinExecutor.post(
81  				String.format(department_update_uri, token.getAccessToken()),
82  				obj.toJSONString());
83  		return response.getAsResult();
84  	}
85  
86  	/**
87  	 * 查询部门列表(以部门的order字段从小到大排列)
88  	 *
89  	 * @param partId
90  	 *            部门ID。获取指定部门ID下的子部门 传入0表示获取全部子部门
91  	 * @see com.foxinmy.weixin4j.qy.model.Party
92  	 * @see <a href="https://work.weixin.qq.com/api/doc#10093">获取部门列表</a>
93  	 * @return 部门列表
94  	 * @throws WeixinException
95  	 */
96  	public List<Party> listParty(int partId) throws WeixinException {
97  		String department_list_uri = getRequestUri("department_list_uri");
98  		if (partId > 0) {
99  			department_list_uri += String.format("&id=%d", partId);
100 		}
101 		Token token = tokenManager.getCache();
102 		WeixinResponse response = weixinExecutor.get(String.format(
103 				department_list_uri, token.getAccessToken()));
104 		return JSON.parseArray(response.getAsJson().getString("department"),
105 				Party.class);
106 	}
107 
108 	/**
109 	 * 删除部门(不能删除根部门;不能删除含有子部门、成员的部门)
110 	 *
111 	 * @param partId
112 	 *            部门ID
113 	 * @see <a href="https://work.weixin.qq.com/api/doc#10079">删除部门说明</a>
114 	 * @return 处理结果
115 	 * @throws WeixinException
116 	 */
117 	public ApiResult deleteParty(int partId) throws WeixinException {
118 		String department_delete_uri = getRequestUri("department_delete_uri");
119 		Token token = tokenManager.getCache();
120 		WeixinResponse response = weixinExecutor.get(String.format(
121 				department_delete_uri, token.getAccessToken(), partId));
122 		return response.getAsResult();
123 	}
124 }