|
@@ -1,181 +1,152 @@
|
|
package cn.com.ctop.okr.service.impl;
|
|
package cn.com.ctop.okr.service.impl;
|
|
|
|
|
|
import cn.com.ctop.okr.dto.UserInfoDto;
|
|
import cn.com.ctop.okr.dto.UserInfoDto;
|
|
-import cn.com.ctop.okr.entity.CorpDepartInfo;
|
|
|
|
-import cn.com.ctop.okr.entity.CorpUserDepartMap;
|
|
|
|
-import cn.com.ctop.okr.entity.CorpUserInfo;
|
|
|
|
-import cn.com.ctop.okr.feishu.provider.DepartmentProvider;
|
|
|
|
-import cn.com.ctop.okr.mapper.CorpDepartInfoMapper;
|
|
|
|
|
|
+import cn.com.ctop.okr.entity.*;
|
|
import cn.com.ctop.okr.mapper.CorpUserInfoMapper;
|
|
import cn.com.ctop.okr.mapper.CorpUserInfoMapper;
|
|
-import cn.com.ctop.okr.service.CorpUserInfoService;
|
|
|
|
-import cn.com.ctop.okr.utils.Check;
|
|
|
|
-import cn.com.ctop.okr.utils.DateUtils;
|
|
|
|
|
|
+import cn.com.ctop.okr.mapper.QYWxDepartInfoMapper;
|
|
|
|
+import cn.com.ctop.okr.mapper.QYWxUserInfoMapper;
|
|
|
|
+import cn.com.ctop.okr.qywx.provider.AuthProvider;
|
|
|
|
+import cn.com.ctop.okr.qywx.provider.DepartUserProvider;
|
|
|
|
+import cn.com.ctop.okr.service.QYWxService;
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import jdk.nashorn.internal.runtime.UserAccessorProperty;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@Slf4j
|
|
@Slf4j
|
|
-public class QYWxServiceImpl extends ServiceImpl<CorpUserInfoMapper, CorpUserInfo> implements CorpUserInfoService {
|
|
|
|
|
|
+public class QYWxServiceImpl extends ServiceImpl<CorpUserInfoMapper, CorpUserInfo> implements QYWxService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private CorpDepartInfoMapper corpDepartInfoMapper;
|
|
|
|
|
|
+ private QYWxDepartInfoMapper wxDepartInfoMapper;
|
|
@Autowired
|
|
@Autowired
|
|
- private CorpUserInfoMapper corpUserInfoMapper;
|
|
|
|
|
|
+ private QYWxUserInfoMapper wxUserInfoMapper;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public List<CorpDepartInfo> queryUserDeparts(String openId){
|
|
|
|
- return corpDepartInfoMapper.queryUserDeparts(openId);
|
|
|
|
|
|
+ public void getWxDepartAndUserInfo() throws Exception {
|
|
|
|
+ //String token = queryWxToken(); //获取token
|
|
|
|
+ String token = "dtGm-D6u-nO3wGM8GAlh6OWI1BzzZQBZQPHnA24S8NHpMrECPp8KNjY4EAZcKaJoSTZudQsVO3Ox7yyeJmO9ZOa02n4N4FVxIX-CQNLSNsgoafC75jeecDPPBhhQX7TesqFWoumcWVso2yNycQ_Kal1S1jvS0SBY1ggwsWe6OZhH_k0XElPEop6D50Ai6NFX1eOoTFMvUsSZecIiZRRFiQ";
|
|
|
|
+ System.out.println(token);
|
|
|
|
+ queryUserDeparts(token); //更新部门列表
|
|
|
|
+ queryUserInfo(token); //更新员工信息
|
|
|
|
+ updateUserOpenId(token); //转换员工userid到openId
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取飞书公司部门和员工信息
|
|
|
|
|
|
+ * 获取token
|
|
|
|
+ * @return
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- @Override
|
|
|
|
- public void loadFeishuDepartAndUserInfo() throws Exception {
|
|
|
|
- log.info("获取飞书公司部门列表开始");
|
|
|
|
- //同步飞书部门列表
|
|
|
|
- loadFeishuDepartInfo("0", null);
|
|
|
|
- log.info("获取飞书公司部门列表结束");
|
|
|
|
- log.info("获取飞书公司员工信息开始");
|
|
|
|
- loadFeishuUserInfoByDepartId();
|
|
|
|
- log.info("获取飞书公司员工信息结束");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void loadFeishuDepartInfo(String department_id, String page_token) throws Exception {
|
|
|
|
- JSONObject resultJson = DepartmentProvider.getDepartSimpleList(department_id,null);
|
|
|
|
- Integer code = resultJson.getInteger("code");
|
|
|
|
- if(code != 0){
|
|
|
|
- //log.error("部门列表返回不成功,返回为:{}",resultJson.getString("msg"));
|
|
|
|
- throw new Exception("部门列表返回不成功,返回为:" + resultJson);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
- if(Check.isNull(dataJson)){
|
|
|
|
- throw new Exception("部门列表返回不成功,返回为:" + resultJson);
|
|
|
|
- }
|
|
|
|
- JSONArray departmentInfos = dataJson.getJSONArray("department_infos");
|
|
|
|
- int size = departmentInfos.size();
|
|
|
|
-
|
|
|
|
- for(int i=0; i<size; i++){
|
|
|
|
- JSONObject departmentInfo = departmentInfos.getJSONObject(i);
|
|
|
|
- CorpDepartInfo departInfo = new CorpDepartInfo();
|
|
|
|
- departInfo.setDepartmentId(departmentInfo.getString("id"));
|
|
|
|
- departInfo.setDepartmentName(departmentInfo.getString("name"));
|
|
|
|
- departInfo.setOpenDepartmentId(departmentInfo.getString("parent_open_department_id"));
|
|
|
|
- departInfo.setParentId(departmentInfo.getString("parent_id"));
|
|
|
|
- departInfo.setParentOpenDepartmentId(departmentInfo.getString("parent_open_department_id"));
|
|
|
|
-
|
|
|
|
- corpDepartInfoMapper.replaceInfoDepartInfo(departInfo);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Boolean hasMore = dataJson.getBoolean("has_more");
|
|
|
|
- if(hasMore){
|
|
|
|
- String pageToken = dataJson.getString("page_token");
|
|
|
|
- loadFeishuDepartInfo(department_id, pageToken);
|
|
|
|
- }
|
|
|
|
|
|
+ public String queryWxToken() throws Exception {
|
|
|
|
+ return AuthProvider.getQYWXAccessToken();
|
|
}
|
|
}
|
|
|
|
|
|
- private void loadFeishuUserInfoByDepartId(){
|
|
|
|
- List<String> departIdList = corpDepartInfoMapper.getCorpDepartIdList();
|
|
|
|
- for (String departId:departIdList){
|
|
|
|
- try {
|
|
|
|
- loadFeishuUserInfo(departId,null);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error(e.getMessage());
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取企业微信公司所有部门
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public void queryUserDeparts(String token) throws Exception{
|
|
|
|
+ JSONObject resultJson = DepartUserProvider.getCorpDepartList(null,token);
|
|
|
|
+ Integer errcode = resultJson.getInteger("errcode");
|
|
|
|
+ if(errcode == 0){
|
|
|
|
+ JSONArray departmentArray = resultJson.getJSONArray("department");
|
|
|
|
+
|
|
|
|
+ if(departmentArray == null){
|
|
|
|
+ throw new Exception("获取部门失败,返回值:" + JSON.toJSONString(resultJson));
|
|
|
|
+ }
|
|
|
|
+ int size = departmentArray.size();
|
|
|
|
+ for(int i=0; i < size; i++){
|
|
|
|
+ JSONObject json = departmentArray.getJSONObject(i);
|
|
|
|
+ QYWxDepartInfo departInfo = new QYWxDepartInfo();
|
|
|
|
+ departInfo.setId(json.getLong("id"));
|
|
|
|
+ departInfo.setDepartName(json.getString("name"));
|
|
|
|
+ departInfo.setDepartOrder(json.getLong("order"));
|
|
|
|
+ departInfo.setParentid(json.getLong("parentid"));
|
|
|
|
+ wxDepartInfoMapper.insertWxDepartInfo(departInfo);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void loadFeishuUserInfo(String department_id, String page_token) throws Exception {
|
|
|
|
- JSONObject resultJson = DepartmentProvider.getDepartmentUserDetailList(department_id, page_token);
|
|
|
|
-
|
|
|
|
- Integer code = resultJson.getInteger("code");
|
|
|
|
- if(code != 0){
|
|
|
|
- throw new Exception("飞书部门员工明细返回不成功,返回为:" + resultJson);
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取企业微信员工信息
|
|
|
|
+ * @param token
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public void queryUserInfo(String token) throws Exception {
|
|
|
|
+ JSONObject resultJson = DepartUserProvider.getUserDetailInfoByDepartId(1,token);
|
|
|
|
+ Integer errcode = resultJson.getInteger("errcode");
|
|
|
|
|
|
- JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
- if (Check.isNull(dataJson)){
|
|
|
|
- throw new Exception("飞书部门员工明细返回不成功,返回为:" + resultJson);
|
|
|
|
- }
|
|
|
|
|
|
+ if(errcode == 0){
|
|
|
|
+ JSONArray userArray = resultJson.getJSONArray("userlist");
|
|
|
|
|
|
- JSONArray userInfoArray = dataJson.getJSONArray("user_infos");
|
|
|
|
- int size = userInfoArray.size();
|
|
|
|
- for(int i=0; i<size; i++){
|
|
|
|
- JSONObject userJson = userInfoArray.getJSONObject(i);
|
|
|
|
- //数据入库
|
|
|
|
- CorpUserInfo user = new CorpUserInfo();
|
|
|
|
- String openId = userJson.getString("open_id");
|
|
|
|
-
|
|
|
|
- user.setAvatar72(userJson.getString("avatar_72"));
|
|
|
|
- user.setAvatar240(userJson.getString("avatar_240"));
|
|
|
|
- user.setAvatar640(userJson.getString("avatar_640"));
|
|
|
|
- user.setAvatarUrl(userJson.getString("avatar_url"));
|
|
|
|
- user.setCity(userJson.getString("city"));
|
|
|
|
- user.setCountry(userJson.getString("country"));
|
|
|
|
- user.setEmail(userJson.getString("email"));
|
|
|
|
- user.setEmployeeId(userJson.getString("employee_id"));
|
|
|
|
- user.setEmployeeNo(userJson.getString("employee_no"));
|
|
|
|
- user.setEmployeeType(userJson.getInteger("employee_type"));
|
|
|
|
- user.setEnName(userJson.getString("en_name"));
|
|
|
|
- user.setGender(userJson.getInteger("gender"));
|
|
|
|
- user.setIsTenantManager(userJson.getBoolean("is_tenant_manager")==true?1:0);
|
|
|
|
- user.setLeaderEmployeeId(userJson.getString("leader_employee_id"));
|
|
|
|
- user.setLeaderOpenId(userJson.getString("leader_open_id"));
|
|
|
|
- user.setLeaderUnionId(userJson.getString("leader_union_id"));
|
|
|
|
- user.setMobile(userJson.getString("mobile"));
|
|
|
|
- user.setName(userJson.getString("name"));
|
|
|
|
- user.setNamePy(userJson.getString("name_py"));
|
|
|
|
- user.setOpenId(openId);
|
|
|
|
- user.setUnionId(userJson.getString("union_id"));
|
|
|
|
- user.setStatus(userJson.getInteger("status"));
|
|
|
|
- user.setUserUpdateTime(DateUtils.timeStampToDate(userJson.getLong("update_time") * 1000));
|
|
|
|
- user.setWorkStation(userJson.getString("work_station"));
|
|
|
|
-
|
|
|
|
- corpUserInfoMapper.replaceInfoUserInfo(user);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- //员工部门关联表入库
|
|
|
|
- JSONArray departListArray = userJson.getJSONArray("departments");
|
|
|
|
- corpDepartInfoMapper.deleteUserDepartMapByOpenId(openId);
|
|
|
|
- List<CorpUserDepartMap> userDepartMapList = new ArrayList<>();
|
|
|
|
- int departListArraySize = departListArray.size();
|
|
|
|
- for(int j=0; j<departListArraySize; j++){
|
|
|
|
- CorpUserDepartMap map = new CorpUserDepartMap();
|
|
|
|
- String departmentId = departListArray.getString(j);
|
|
|
|
- map.setDepartmentId(departmentId);
|
|
|
|
- map.setOpenId(openId);
|
|
|
|
- userDepartMapList.add(map);
|
|
|
|
|
|
+ if(userArray == null){
|
|
|
|
+ throw new Exception("获取员工明细失败,返回值:" + JSON.toJSONString(resultJson));
|
|
}
|
|
}
|
|
- corpDepartInfoMapper.insertUserDepartMapList(userDepartMapList);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- Boolean hasMore = resultJson.getJSONObject("data").getBoolean("has_more");
|
|
|
|
- if(hasMore){
|
|
|
|
- String pageToken = resultJson.getJSONObject("data").getString("page_token");
|
|
|
|
- loadFeishuUserInfo(department_id,pageToken);
|
|
|
|
|
|
+ Integer size = userArray.size();
|
|
|
|
+ for(int j=0; j<size; j++){
|
|
|
|
+ JSONObject json = userArray.getJSONObject(j);
|
|
|
|
+ QYWxUserInfo userInfo = new QYWxUserInfo();
|
|
|
|
+ String userid = json.getString("userid");
|
|
|
|
+
|
|
|
|
+ userInfo.setMobile(json.getString("mobile"));
|
|
|
|
+ userInfo.setAvatar(json.getString("avatar"));
|
|
|
|
+ userInfo.setMainDepartment(json.getLong("main_department"));
|
|
|
|
+ userInfo.setUserid(userid);
|
|
|
|
+ userInfo.setThumbAvatar(json.getString("thumb_avatar"));
|
|
|
|
+ userInfo.setName(json.getString("name"));
|
|
|
|
+ userInfo.setQrCode(json.getString("qr_code"));
|
|
|
|
+ userInfo.setEmail(json.getString("email"));
|
|
|
|
+ userInfo.setStatus(json.getInteger("status"));
|
|
|
|
+
|
|
|
|
+ wxUserInfoMapper.replaceIntoWxUserInfo(userInfo);
|
|
|
|
+
|
|
|
|
+ JSONArray departArray = json.getJSONArray("department");
|
|
|
|
+ Integer departSize = departArray.size();
|
|
|
|
+
|
|
|
|
+ wxUserInfoMapper.deleteUserDepartMapByUserid(userid);
|
|
|
|
+ for(int k=0; k<departSize ; k++){
|
|
|
|
+ QYWxUserDepartMap map = new QYWxUserDepartMap();
|
|
|
|
+ Long departId = departArray.getLong(k);
|
|
|
|
+ map.setDepartId(departId);
|
|
|
|
+ map.setUserid(userid);
|
|
|
|
+ wxUserInfoMapper.insertUserDeopartMap(map);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * userid转换为openId
|
|
|
|
+ * @param token
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public void updateUserOpenId(String token) throws Exception {
|
|
|
|
+ List<String> userList = wxUserInfoMapper.getUseridList();
|
|
|
|
+ for(String userid:userList){
|
|
|
|
+ JSONObject resultJson = DepartUserProvider.convertUserIdToOpenId(userid, token);
|
|
|
|
+ Integer errcode = resultJson.getInteger("errcode");
|
|
|
|
+ if(errcode == 0){
|
|
|
|
+ String openId = resultJson.getString("openid");
|
|
|
|
+ wxUserInfoMapper.updateUserInfoByUserid(userid, openId);
|
|
|
|
+ wxUserInfoMapper.updateUserDepartMapOpenIdByUserid(userid, openId);
|
|
|
|
+ }else{
|
|
|
|
+ throw new Exception("userid转换为openId出错啦,resultJson为" + resultJson);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
|
- public CorpUserInfo getUserInfoByOpenId(String openId){
|
|
|
|
- return corpUserInfoMapper.getUserInfoByOpenId(openId);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- //根据名字模糊查询飞书用户
|
|
|
|
|
|
+ //根据名字模糊查询企业微信用户
|
|
@Override
|
|
@Override
|
|
public List<UserInfoDto> getUserListByName(String name){
|
|
public List<UserInfoDto> getUserListByName(String name){
|
|
- return corpUserInfoMapper.getUserListByName(name);
|
|
|
|
|
|
+ return wxUserInfoMapper.getUserListByName(name);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|