Browse Source

OKR业务代码提交

hcst_sunzhen 4 years ago
parent
commit
c35d478e5c

+ 1 - 0
src/main/java/cn/com/ctop/okr/config/ShiroConfig.java

@@ -150,6 +150,7 @@ public class ShiroConfig {
         filterChainDefinitionMap.put("/corp/*", "anon");
         filterChainDefinitionMap.put("/corp/*", "anon");
         filterChainDefinitionMap.put("/okr/*", "anon");
         filterChainDefinitionMap.put("/okr/*", "anon");
         filterChainDefinitionMap.put("/feishu/*", "anon");
         filterChainDefinitionMap.put("/feishu/*", "anon");
+        filterChainDefinitionMap.put("/qywx/*", "anon");
 
 
 
 
         // 添加自己的过滤器并且取名为jwt
         // 添加自己的过滤器并且取名为jwt

+ 74 - 0
src/main/java/cn/com/ctop/okr/controller/QyWxController.java

@@ -0,0 +1,74 @@
+package cn.com.ctop.okr.controller;
+
+import cn.com.ctop.okr.dto.UserInfoDto;
+import cn.com.ctop.okr.service.QYWxService;
+import cn.com.ctop.okr.vo.Result;
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+
+@Controller
+@RequestMapping("/qywx")
+public class QyWxController {
+    private static Logger logger = Logger.getLogger(QyWxController.class);
+
+    @Autowired
+    private QYWxService wxService;
+
+    @ResponseBody
+    @RequestMapping(value = "/departAndUser")
+    public Result departAndUser(HttpServletRequest request,
+                          HttpServletResponse response){
+        logger.info("获取企业微信公司部门和员工信息接口开始");
+        Result result = new Result();
+        result.setSuccess(true);
+
+        try {
+            wxService.getWxDepartAndUserInfo();
+        }catch (Exception e){
+            result.setSuccess(false);
+            result.setMessage("获取企业微信公司部门和员工信息接口出错啦");
+            logger.error(e.getMessage());
+        }
+
+        return result;
+    }
+
+
+    /**
+     * 根据名字模糊查询企业微信用户
+     * @param dto
+     * @param request
+     * @param response
+     * @return
+     */
+    @ResponseBody
+    @RequestMapping(value = "/getUserListByName")
+    public Result<List<UserInfoDto>> getUserListByName(@RequestBody UserInfoDto dto, HttpServletRequest request,
+                                                       HttpServletResponse response){
+        logger.info("/qywx/getUserListByName 方法开始");
+        Result<List<UserInfoDto>> result = new Result();
+        result.setSuccess(true);
+
+        try {
+            List<UserInfoDto> userInfoList = wxService.getUserListByName(dto.getName());
+            result.setResult(userInfoList);
+        }catch (Exception e){
+            logger.error(e.getMessage());
+            result.setSuccess(false);
+            result.setMessage("error");
+        }finally {
+            logger.info("/qywx/getUserListByName 方法结束");
+        }
+
+        return result;
+    }
+
+}

+ 2 - 2
src/main/java/cn/com/ctop/okr/entity/QYWxDepartInfo.java

@@ -10,7 +10,7 @@ import lombok.Data;
 public class QYWxDepartInfo {
 public class QYWxDepartInfo {
     @TableId(value = "id",type = IdType.AUTO)
     @TableId(value = "id",type = IdType.AUTO)
     private Long id;
     private Long id;
-    private String name;
+    private String departName;
     private Long parentid;
     private Long parentid;
-    private Long order;
+    private Long departOrder;
 }
 }

+ 13 - 0
src/main/java/cn/com/ctop/okr/entity/QYWxUserDepartMap.java

@@ -0,0 +1,13 @@
+package cn.com.ctop.okr.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+@Data
+@TableName("wx_user_depart_map")
+public class QYWxUserDepartMap {
+    private Long id;
+    private String userid;
+    private String openId;
+    private Long departId;
+}

+ 4 - 4
src/main/java/cn/com/ctop/okr/entity/QYWxUserInfo.java

@@ -9,13 +9,13 @@ import lombok.Data;
 @TableName("wx_user_info")
 @TableName("wx_user_info")
 public class QYWxUserInfo {
 public class QYWxUserInfo {
     @TableId(value = "userid",type = IdType.INPUT)
     @TableId(value = "userid",type = IdType.INPUT)
-    private Long userid;
-    private String open_id;
+    private String userid;
+    private String openId;
     private String name;
     private String name;
     private String mobile;
     private String mobile;
     private String avatar;
     private String avatar;
-    private String main_department;
-    private String thumb_avatar;
+    private Long mainDepartment;
+    private String thumbAvatar;
     private String qrCode;
     private String qrCode;
     private String email;
     private String email;
     private Integer status;
     private Integer status;

+ 1 - 1
src/main/java/cn/com/ctop/okr/mapper/QYWxDepartInfoMapper.java

@@ -8,7 +8,7 @@ import org.apache.ibatis.annotations.Param;
 
 
 import java.util.List;
 import java.util.List;
 
 
-public interface QYWxDepartInfoMapper extends BaseMapper<CorpUserInfo> {
+public interface QYWxDepartInfoMapper extends BaseMapper<QYWxDepartInfo> {
 
 
     void insertWxDepartInfo(@Param("departInfo")QYWxDepartInfo departInfo);
     void insertWxDepartInfo(@Param("departInfo")QYWxDepartInfo departInfo);
 
 

+ 31 - 0
src/main/java/cn/com/ctop/okr/mapper/QYWxUserInfoMapper.java

@@ -0,0 +1,31 @@
+package cn.com.ctop.okr.mapper;
+
+import cn.com.ctop.okr.dto.UserInfoDto;
+import cn.com.ctop.okr.entity.CorpUserInfo;
+import cn.com.ctop.okr.entity.QYWxUserDepartMap;
+import cn.com.ctop.okr.entity.QYWxUserInfo;
+import cn.hutool.system.UserInfo;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+public interface QYWxUserInfoMapper extends BaseMapper<QYWxUserInfo> {
+
+    //CorpUserInfo getUserInfoByOpenId(@Param("openId") String openId);
+    //
+    void replaceIntoWxUserInfo(@Param("userInfo") QYWxUserInfo userInfo);
+    //
+    void deleteUserDepartMapByUserid(@Param("userid") String userid);
+
+    void insertUserDeopartMap(@Param("map") QYWxUserDepartMap map);
+
+    List<String> getUseridList();
+
+    void updateUserInfoByUserid(@Param("userid")String userid, @Param("openId")String openId);
+
+    void updateUserDepartMapOpenIdByUserid(@Param("userid")String userid, @Param("openId")String openId);
+
+    List<UserInfoDto> getUserListByName(@Param("name")String name);
+
+}

+ 5 - 5
src/main/java/cn/com/ctop/okr/mapper/xml/QYWxDepartInfoMapper.xml

@@ -3,20 +3,20 @@
 <mapper namespace="cn.com.ctop.okr.mapper.QYWxDepartInfoMapper">
 <mapper namespace="cn.com.ctop.okr.mapper.QYWxDepartInfoMapper">
 
 
     <insert id="insertWxDepartInfo">
     <insert id="insertWxDepartInfo">
-        insert into
+        replace into
         wx_depart_info
         wx_depart_info
         (
         (
             id,
             id,
-            name,
+            depart_name,
             parentid,
             parentid,
-            order
+            depart_order
         )
         )
         values
         values
         (
         (
             #{departInfo.id},
             #{departInfo.id},
-            #{departInfo.name},
+            #{departInfo.departName},
             #{departInfo.parentid},
             #{departInfo.parentid},
-            #{departInfo.order},
+            #{departInfo.departOrder}
         )
         )
 
 
     </insert>
     </insert>

+ 96 - 0
src/main/java/cn/com/ctop/okr/mapper/xml/QYWxUserInfoMapper.xml

@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.ctop.okr.mapper.QYWxUserInfoMapper">
+
+    <insert id="replaceIntoWxUserInfo">
+        replace into
+        wx_user_info
+        (
+            userid,
+            open_id,
+            name,
+            mobile,
+            avatar,
+            main_department,
+            thumb_avatar,
+            qr_code,
+            email,
+            status
+        )
+        values
+        (
+            #{userInfo.userid},
+            #{userInfo.openId},
+            #{userInfo.name},
+            #{userInfo.mobile},
+            #{userInfo.avatar},
+            #{userInfo.mainDepartment},
+            #{userInfo.thumbAvatar},
+            #{userInfo.qrCode},
+            #{userInfo.email},
+            #{userInfo.status}
+        )
+    </insert>
+
+    <delete id="deleteUserDepartMapByUserid">
+        delete
+        from
+        wx_user_depart_map
+        where
+        userid = #{userid}
+    </delete>
+
+    <insert id="insertUserDeopartMap">
+        insert
+        into
+        wx_user_depart_map
+            (
+                userid,
+                depart_id
+            )
+        values
+            (
+                #{map.userid},
+                #{map.departId}
+            )
+    </insert>
+
+    <select id="getUseridList" resultType="string">
+        select
+        userid
+        from
+        wx_user_info
+    </select>
+
+    <update id="updateUserInfoByUserid">
+        update
+        wx_user_info
+        set
+        open_id = #{openId}
+        where
+        userid = #{userid}
+    </update>
+
+    <update id="updateUserDepartMapOpenIdByUserid">
+        update
+        wx_user_depart_map
+        set
+        open_id = #{openId}
+        where
+        userid = #{userid}
+    </update>
+
+    <select id="getUserListByName" resultType="cn.com.ctop.okr.dto.UserInfoDto">
+        select
+        open_id as userId,
+        name as name
+        from
+        wx_user_info
+        where
+        <if test="name != null">
+            name like concat('%', #{name}, '%') and
+        </if>
+        1=1
+    </select>
+
+</mapper>

+ 9 - 4
src/main/java/cn/com/ctop/okr/qywx/provider/DepartUserProvider.java

@@ -3,6 +3,7 @@ package cn.com.ctop.okr.qywx.provider;
 import cn.com.ctop.okr.qywx.constant.QYWxConstant;
 import cn.com.ctop.okr.qywx.constant.QYWxConstant;
 import cn.com.ctop.okr.utils.HttpUtils;
 import cn.com.ctop.okr.utils.HttpUtils;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.lang.StringUtils;
 
 
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
@@ -20,6 +21,9 @@ public class DepartUserProvider {
     public static JSONObject getCorpDepartList(String department_id, String token) throws Exception {
     public static JSONObject getCorpDepartList(String department_id, String token) throws Exception {
         TreeMap<String,Object> params = new TreeMap<>();
         TreeMap<String,Object> params = new TreeMap<>();
         params.put("access_token", token);
         params.put("access_token", token);
+        if(StringUtils.isNotBlank(department_id)){
+            params.put("id",department_id);
+        }
 
 
         String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL+QYWxConstant.QYWX_DEPART_LIST,null,params);
         String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL+QYWxConstant.QYWX_DEPART_LIST,null,params);
         JSONObject resultJson = JSONObject.parseObject(resultStr);
         JSONObject resultJson = JSONObject.parseObject(resultStr);
@@ -78,10 +82,11 @@ public class DepartUserProvider {
 
 
     public static void main(String[] args) throws Exception {
     public static void main(String[] args) throws Exception {
         //String token = AuthProvider.getQYWXAccessToken();
         //String token = AuthProvider.getQYWXAccessToken();
-        //System.out.println(token);
-        //getCorpDepartList(null,"2qZIc_qzyZ7ZuTB4cdUAZbVDGfJCoEc6YRPyprp_KgaPMlEgi8sN5jIFCsNMy375QgnycQY8B-shloUEqFv08q9BvMppO-VC5wx89yrHFPugCD9j81-jfwxTf_PLYgLaaRY5EqDE3Yh-Ng4aOERNlOB_EgFcMSGwGja-vtKSgI5DbLrXhlVHfWbicWNdOJTeNfWcEV_WU9EsAivhadMhlQ");
-        //getUserDetailInfoByDepartId(2,"2qZIc_qzyZ7ZuTB4cdUAZbVDGfJCoEc6YRPyprp_KgaPMlEgi8sN5jIFCsNMy375QgnycQY8B-shloUEqFv08q9BvMppO-VC5wx89yrHFPugCD9j81-jfwxTf_PLYgLaaRY5EqDE3Yh-Ng4aOERNlOB_EgFcMSGwGja-vtKSgI5DbLrXhlVHfWbicWNdOJTeNfWcEV_WU9EsAivhadMhlQ");
-        convertUserIdToOpenId(null,"2qZIc_qzyZ7ZuTB4cdUAZbVDGfJCoEc6YRPyprp_KgaPMlEgi8sN5jIFCsNMy375QgnycQY8B-shloUEqFv08q9BvMppO-VC5wx89yrHFPugCD9j81-jfwxTf_PLYgLaaRY5EqDE3Yh-Ng4aOERNlOB_EgFcMSGwGja-vtKSgI5DbLrXhlVHfWbicWNdOJTeNfWcEV_WU9EsAivhadMhlQ");
+        String token = "jLzCVGM285_KOhhlcJzVpPa8zOPtgyRPMm0K3lPKxHQk_6fWadrdYHJBL2cHb10M5aKPFwknwUFGnbFKK7zg53468kCBxusO6PijA4zsxZZmsRFk8giQcKzzttZFJ-cq3nZ7J1vqty63bb9B40T6rQkTZ5zc8Z0esfXyrnCL7-26i_4K5lFcY5LICzM0qfPsFGUgYeEwIfPiBrLAGTw4Tw";
+        System.out.println(token);
+        getCorpDepartList(null,token);
+        //getUserDetailInfoByDepartId(1,token);
+        //convertUserIdToOpenId(null,"2qZIc_qzyZ7ZuTB4cdUAZbVDGfJCoEc6YRPyprp_KgaPMlEgi8sN5jIFCsNMy375QgnycQY8B-shloUEqFv08q9BvMppO-VC5wx89yrHFPugCD9j81-jfwxTf_PLYgLaaRY5EqDE3Yh-Ng4aOERNlOB_EgFcMSGwGja-vtKSgI5DbLrXhlVHfWbicWNdOJTeNfWcEV_WU9EsAivhadMhlQ");
     }
     }
 
 
 
 

+ 3 - 8
src/main/java/cn/com/ctop/okr/service/QYWxService.java

@@ -1,7 +1,6 @@
 package cn.com.ctop.okr.service;
 package cn.com.ctop.okr.service;
 
 
 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.CorpUserInfo;
 import cn.com.ctop.okr.entity.CorpUserInfo;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.service.IService;
 
 
@@ -9,12 +8,8 @@ import java.util.List;
 
 
 public interface QYWxService extends IService<CorpUserInfo> {
 public interface QYWxService extends IService<CorpUserInfo> {
 
 
-    //List<CorpDepartInfo> queryUserDeparts(String openId);
-    //
-    //CorpUserInfo getUserInfoByOpenId(String openId);
-    //
-    //void loadFeishuDepartAndUserInfo() throws Exception;
-    //
-    //List<UserInfoDto> getUserListByName(String name);
+    void getWxDepartAndUserInfo() throws Exception;
+
+    List<UserInfoDto> getUserListByName(String name);
 
 
 }
 }

+ 155 - 5
src/main/java/cn/com/ctop/okr/service/impl/CorpUserInfoServiceImpl.java

@@ -1,35 +1,185 @@
 package cn.com.ctop.okr.service.impl;
 package cn.com.ctop.okr.service.impl;
 
 
+import cn.com.ctop.okr.dto.FeishuUserInfoDto;
+import cn.com.ctop.okr.dto.UserInfoDto;
 import cn.com.ctop.okr.entity.CorpDepartInfo;
 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.entity.CorpUserInfo;
+import cn.com.ctop.okr.feishu.provider.DepartmentProvider;
 import cn.com.ctop.okr.mapper.CorpDepartInfoMapper;
 import cn.com.ctop.okr.mapper.CorpDepartInfoMapper;
 import cn.com.ctop.okr.mapper.CorpUserInfoMapper;
 import cn.com.ctop.okr.mapper.CorpUserInfoMapper;
-import cn.com.ctop.okr.service.QYWxService;
+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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.annotations.Param;
 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 CorpUserInfoServiceImpl extends ServiceImpl<CorpUserInfoMapper, CorpUserInfo> implements QYWxService {
+public class CorpUserInfoServiceImpl extends ServiceImpl<CorpUserInfoMapper, CorpUserInfo> implements CorpUserInfoService {
 
 
     @Autowired
     @Autowired
     private CorpDepartInfoMapper corpDepartInfoMapper;
     private CorpDepartInfoMapper corpDepartInfoMapper;
     @Autowired
     @Autowired
     private CorpUserInfoMapper corpUserInfoMapper;
     private CorpUserInfoMapper corpUserInfoMapper;
 
 
-    //@Override
-    public List<CorpDepartInfo> loadQYWXxDepartInfo(String openId){
+    @Override
+    public List<CorpDepartInfo> queryUserDeparts(String openId){
+        return corpDepartInfoMapper.queryUserDeparts(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();
 
 
-        return corpDepartInfoMapper.queryUserDeparts(openId);
+        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);
+        }
+    }
+
+    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);
+    }
+
+    //根据名字模糊查询飞书用户
+    @Override
+    public List<UserInfoDto> getUserListByName(String name){
+        return corpUserInfoMapper.getUserListByName(name);
+    }
 
 
 
 
 
 

+ 106 - 135
src/main/java/cn/com/ctop/okr/service/impl/QYWxServiceImpl.java

@@ -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);
     }
     }