jiangping
2023-12-05 6e873adc94aaeb4f918cf4fd45f7bdf936f45db4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package doumeemes.core.utils.dingding;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.google.gson.JsonObject;
import com.taobao.api.ApiException;
import doumeemes.core.constants.ResponseStatus;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.dingding.bean.CompanyUserDTO;
import doumeemes.dao.business.model.Company;
import doumeemes.dao.business.model.CompanyUser;
import doumeemes.dao.business.model.Department;
import doumeemes.dao.ext.vo.CompanyUserExtListVO;
import doumeemes.service.ext.CompanyExtService;
import io.swagger.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 钉钉业务数据同步类
 *
 * @Author : Rk
 * @create 2023/6/16 14:02
 */
public class DingDingSyncUtil {
 
    //文档地址:
    //https://open.dingtalk.com/document/orgapp/queries-the-complete-information-of-a-department-user
 
 
    /**
     * 获取钉钉 用户信息 根据用户ID  userid(钉钉方)
     */
    public static CompanyUser  getDingDingUserInfo(String userId,String accessToken){
        try {
            CompanyUser companyUser = new CompanyUser();
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(userId);
            req.setLanguage("zh_CN");
            OapiV2UserGetResponse rsp = client.execute(req, accessToken);
            if(rsp.getErrcode()!=Constants.ZERO){
                throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常");
            }
            JSONObject jsonObject = JSONObject.parseObject(rsp.getBody()).getJSONObject("result");
            companyUser.setDeleted(Constants.ZERO);
            companyUser.setCreateTime(new Date());
            companyUser.setName(jsonObject.getString("name"));
            companyUser.setPhone(jsonObject.getString("mobile"));
            companyUser.setPhoneAuthStatus(Integer.toString(Constants.ZERO));
            companyUser.setCode(null);
            companyUser.setPosition(jsonObject.getString("title"));
            JSONArray deptIdArray = jsonObject.getJSONArray("dept_id_list");
            if(!Objects.isNull(deptIdArray)&&deptIdArray.size()>Constants.ZERO){
                companyUser.setDepartmentId((Integer)deptIdArray.get(Constants.ZERO));
            }
            companyUser.setStatus(Constants.ZERO);
            companyUser.setDdUnionid(jsonObject.getString("unionid"));
            companyUser.setDdUserid(jsonObject.getString("userid"));
            System.out.println(jsonObject);
            return companyUser;
        } catch (ApiException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常");
        }
    }
 
 
    /**
     * 获取钉钉 部门信息
     */
    public static Department getDingDingDepartInfo(Company company,Long ddDeptId,String accessToken){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
            OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
            req.setDeptId(ddDeptId);
            req.setLanguage("zh_CN");
            OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
            //处理返回信息
            if(rsp.getErrcode()!=Constants.ZERO){
                throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常");
            }
            JSONObject jsonObject = JSONObject.parseObject(rsp.getBody()).getJSONObject("result");
            Department department = new Department();
            department.setCreateTime(new Date());
            department.setDeleted(Constants.ZERO);
            department.setType(Constants.DEPART_TYPE.depart);
            department.setDdDeptId(jsonObject.getLong("dept_id"));
            department.setName(jsonObject.getString("name"));
            department.setParentId(jsonObject.getInteger("parent_id"));
            department.setStatus(Constants.ZERO);
            department.setCompanyId(company.getId());
            return department;
        } catch (ApiException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常");
        }
    }
 
    /**
     * 获取钉钉 所有部门信息
     */
    public static List<Department> getDingDingDepartListInfo(CompanyUserExtListVO companyUser, Long departId){
        try {
            List<Department> departmentList = new ArrayList<>();
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
            OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
            //如果 departId 为空 则同步最高级数据
            if(Objects.isNull(departId)){
                req.setDeptId(departId);
            }
            req.setLanguage("zh_CN");
            //TODO 获取access_token信息
            OapiV2DepartmentListsubResponse rsp = client.execute(req, "access_token");
            //TODO 处理返回信息
            if(rsp.getErrcode().equals(Constants.ZERO)){
                JSONArray jsonArray = JSONArray.parseArray(rsp.getBody());
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Department department = new Department();
                    department.setCreateTime(new Date());
                    department.setDeleted(Constants.ZERO);
                    department.setDdDeptId(jsonObject.getLong("dept_id"));
                    department.setName(jsonObject.getString("name"));
                    department.setParentId(jsonObject.getInteger("parent_id"));
                    department.setCompanyId(companyUser.getId());
                    departmentList.add(department);
                }
            }
            System.out.println(rsp.getBody());
            return departmentList;
        } catch (ApiException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常");
        }
    }
 
 
 
    /**
     * 获取部门信息下 所有用户ID
     * @param companyId
     * @param departId
     */
    public static void getDingDingDepartUserIdListInfo(Integer companyId ,Long departId){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
            OapiUserListidRequest req = new OapiUserListidRequest();
            req.setDeptId(departId);
            //TODO 获取access_token信息
            OapiUserListidResponse rsp = client.execute(req, "access_token");
            //TODO 处理返回信息
            if(rsp.getErrcode().equals(Constants.ZERO)){
                JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
 
            }
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 获取部门下 用户信息
     * @param department  部门信息
     * @param cursor  起始序号
     */
    public static CompanyUserDTO getDingDingDepartUserListInfo(Company company,Department department,Long cursor){
        try {
            CompanyUserDTO companyUserDTO = new CompanyUserDTO();
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
            OapiV2UserListRequest req = new OapiV2UserListRequest();
            req.setDeptId(Long.valueOf(department.getId()));
            //分页查询的游标,最开始传0,后续传返回参数中的next_cursor值
            req.setCursor(cursor);
            //分页大小
            req.setSize(10L);
            req.setContainAccessLimit(false);
            req.setLanguage("zh_CN");
            //TODO 获取access_token信息
            OapiV2UserListResponse rsp = client.execute(req, "");
            //TODO 处理返回信息
            if(rsp.getErrcode().equals(Constants.ZERO)){
                List<CompanyUser> companyUserList = new ArrayList<>();
                JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
                Boolean hasMore = jsonObject.getBoolean("has_more");
                companyUserDTO.setHasMore(hasMore);
                Long nextCursor = jsonObject.getLong("next_cursor");
                companyUserDTO.setNextCursor(nextCursor);
                JSONArray jsonArray = jsonObject.getJSONArray("list");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObjectChild = jsonArray.getJSONObject(i);
                    CompanyUser companyUser = new CompanyUser();
                    companyUser.setDeleted(Constants.ZERO);
                    companyUser.setCreateTime(new Date());
                    companyUser.setName(jsonObjectChild.getString("name"));
                    companyUser.setPhone(jsonObjectChild.getString("mobile"));
                    companyUser.setPosition(jsonObjectChild.getString("title"));
                    companyUser.setPhoneAuthStatus(Integer.toString(Constants.ZERO));
                    //TODO 员工二维码
                    companyUser.setCode(null);
                    companyUser.setDepartmentId(department.getId());
                    companyUser.setStatus(Constants.ZERO);
                    companyUser.setDdUnionid(jsonObjectChild.getString("unionid"));
                    companyUser.setDdUserid(jsonObjectChild.getString("userid"));
                    companyUserList.add(companyUser);
                }
                companyUserDTO.setCompanyUserList(companyUserList);
            }
            System.out.println(rsp.getBody());
            return companyUserDTO;
        } catch (ApiException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseStatus.ERR_STATUS.getCode(),"同步异常");
        }
    }
 
 
 
 
 
 
 
 
 
}