|
@@ -1,18 +1,28 @@
|
|
|
package cn.com.ctop.okr.service.impl;
|
|
|
|
|
|
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.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.vo.UserDepartMap;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.ibatis.annotations.Param;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class CorpUserInfoServiceImpl extends ServiceImpl<CorpUserInfoMapper, CorpUserInfo> implements CorpUserInfoService {
|
|
|
|
|
|
@Autowired
|
|
@@ -25,13 +35,140 @@ public class CorpUserInfoServiceImpl extends ServiceImpl<CorpUserInfoMapper, Cor
|
|
|
return corpDepartInfoMapper.queryUserDeparts(openId);
|
|
|
}
|
|
|
|
|
|
- public CorpUserInfo saveUserInfo(String openId){
|
|
|
+ /**
|
|
|
+ * 获取飞书公司部门和员工信息
|
|
|
+ * @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);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return null;
|
|
|
+ private void loadFeishuUserInfoByDepartId(){
|
|
|
+ List<String> departIdList = corpDepartInfoMapper.getCorpDepartIdList();
|
|
|
+ for (String departId:departIdList){
|
|
|
+ try {
|
|
|
+ loadFeishuUserInfo(departId,null);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
+ if (Check.isNull(dataJson)){
|
|
|
+ throw new Exception("飞书部门员工明细返回不成功,返回为:" + resultJson);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
public CorpUserInfo getUserInfoByOpenId(String openId){
|
|
|
return corpUserInfoMapper.getUserInfoByOpenId(openId);
|
|
|
}
|