Explorar o código

烟台凯越订阅人事档案变动事件 回调计算更新员工假期余额

wzy hai 1 ano
pai
achega
d6b4c3daee

+ 129 - 4
mjava-kaiyue/src/main/java/com/malk/kaiyue/controller/KYController.java

@@ -1,19 +1,144 @@
 package com.malk.kaiyue.controller;
 
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.malk.controller.DDCallbackController;
+import com.malk.kaiyue.service.KYService;
 import com.malk.server.common.McR;
+import com.malk.server.dingtalk.DDConf;
+import com.malk.server.dingtalk.crypto.DingCallbackCrypto;
+import com.malk.service.dingtalk.DDClient_Event;
+import lombok.SneakyThrows;
+import lombok.Synchronized;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
 
 @Slf4j
 @RestController
 @RequestMapping
-public class KYController {
+public class KYController extends DDCallbackController {
+    @Autowired
+    private KYService kyService;
+
+    @Autowired
+    private DDConf ddConf;
+
     @PostMapping("test")
     McR test() {
 
         log.info("11111");
         return McR.success();
     }
+
+    //获取在职员工userId列表
+    @GetMapping("/getEmployeeUserId")
+    McR getEmployeeUserId() {
+
+        Map result = kyService.getEmployeeUserId();
+
+        return McR.success(result);
+    }
+
+    //获取员工花名册信息
+    @PostMapping("/getEmployeeRosterInfo")
+    McR getEmployeeRosterInfo(@RequestBody Map<String, Object> map) {
+        List<Map> result = kyService.getEmployeeRosterInfo(map);
+
+        return McR.success(result);
+    }
+
+    //计算并设置员工年假数
+    @PostMapping("/getEmployeeAnnualLeaveNum")
+    McR getEmployeeAnnualLeaveNum(@RequestBody Map<String, Object> map) {
+        return kyService.getEmployeeAnnualLeaveNum(map);
+    }
+
+
+    //保存十分钟内已处理的回调事件
+    private Map<String, Long> eventList = new HashMap<>();
+
+    private Lock lock = new ReentrantLock();
+
+    //钉钉事件回调
+
+    @Synchronized
+    @SneakyThrows
+    public Map<String, String> invokeCallback(@RequestParam(value = "signature", required = false) String signature,
+                                              @RequestParam(value = "timestamp", required = false) String timestamp,
+                                              @RequestParam(value = "nonce", required = false) String nonce,
+                                              @RequestBody(required = false) JSONObject json) {
+            DingCallbackCrypto callbackCrypto = new DingCallbackCrypto(ddConf.getToken(), ddConf.getAesKey(), ddConf.getAppKey());
+            // 处理回调消息,得到回调事件decryptMsg...
+            final String decryptMsg = callbackCrypto.getDecryptMsg(signature, timestamp, nonce, json.getString("encrypt"));
+            JSONObject eventJson = JSON.parseObject(decryptMsg);
+            Map success = callbackCrypto.getEncryptedMap(DDConf.CALLBACK_RESPONSE, System.currentTimeMillis(), DingCallbackCrypto.Utils.getRandomStr(8));
+
+            // 检查回调事件是否已经处理过,如果是,则忽略该回调
+            if (isCallbackProcessed(decryptMsg)) {
+                log.info("----- [DD]该回调事件已处理过 忽略该回调 -----");
+                return success;
+            }
+
+            // 业务处理代码...
+            String eventType = eventJson.getString("EventType");
+            if (DDConf.CALLBACK_CHECK.equals(eventType)) {
+                log.info("----- [DD]验证注册 -----");
+                return success;
+            }
+            // [回调任务执行逻辑: 异步] 钉钉超时3s未返回会被记录为失败, 可通过失败接口获取记录
+            if (Arrays.asList(DDConf.HRM_USER_RECORD_CHANGE).contains(eventType)) {
+                log.info("[DD]人事档案变动回调, eventType:{}, eventJson:{}",eventType, eventJson);
+                //获取员工userId
+                String userId = "";
+                if (Objects.nonNull(eventJson.get("staffId"))){
+                    //人事档案返回的userId
+                    userId = eventJson.get("staffId").toString();
+                }else if (Objects.nonNull(eventJson.get("userid"))){
+                    //通讯录事件返回的userId
+                    userId = eventJson.get("userid").toString();
+                }else {
+                    log.error("[DD]人事档案变动回调, 未获取到userId");
+                    return success;
+                }
+
+                log.info("员工userId:"+userId);
+                Map<String, Object> map = new HashMap();
+                map.put("userid_list", userId);
+                //更新员工年假余额
+                log.info("----- [DD]更新员工年假余额 -----");
+                kyService.getEmployeeAnnualLeaveNum(map);
+
+                // 将回调事件和当前时间戳添加到已处理集合中
+                long currentTime = System.currentTimeMillis();
+                eventList.put(decryptMsg, currentTime);
+
+                return success;
+            }
+            log.info("----- [DD]已注册, 未处理的其它回调 -----, eventType:{}, eventJson:{}",eventType, eventJson);
+            return success;
+    }
+
+    /**
+     * 检查该回调事件在十分钟内是否处理过,应对钉钉瞬间重复回调
+     *
+     * @param decryptMsg 回调事件
+     * @return 是否处理过
+     */
+    private boolean isCallbackProcessed(String decryptMsg) {
+        // 清理超过十分钟的回调事件
+        long currentTime = System.currentTimeMillis();
+        long expirationTime = currentTime - TimeUnit.MINUTES.toMillis(10);
+        eventList.entrySet().removeIf(entry -> entry.getValue() < expirationTime);
+
+        return eventList.containsKey(decryptMsg);
+    }
+
 }

+ 31 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/KYService.java

@@ -0,0 +1,31 @@
+package com.malk.kaiyue.service;
+
+import com.malk.server.common.McR;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.Map;
+
+
+public interface KYService {
+
+    /**
+     * 获取员工花名册信息
+     * @param map
+     * @return
+     */
+    List<Map> getEmployeeRosterInfo(Map<String, Object> map);
+
+    /**
+     * 获取在职员工userId列表
+     * @return
+     */
+    Map getEmployeeUserId();
+
+    /**
+     * 计算并设置获取员工年假数
+     * @param map
+     * @return
+     */
+    McR getEmployeeAnnualLeaveNum(Map<String, Object> map);
+}

+ 222 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/impl/KYServiceImpl.java

@@ -0,0 +1,222 @@
+package com.malk.kaiyue.service.impl;
+
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
+import com.malk.kaiyue.service.KYService;
+import com.malk.server.common.McR;
+import com.malk.server.dingtalk.DDConf;
+import com.malk.server.dingtalk.DDR;
+import com.malk.service.dingtalk.DDClient;
+import com.malk.utils.UtilHttp;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+public class KYServiceImpl implements KYService {
+    @Autowired
+    private DDClient ddClient;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Override
+    public List<Map> getEmployeeRosterInfo(Map<String, Object> map) {
+        //获取accessToken
+        String access_token = ddClient.getAccessToken();
+        //获取员工花名册字段信息
+        if (Objects.nonNull(map)){
+            DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list", null, DDConf.initTokenParams(access_token), map, DDR.class);
+            return (List<Map>)ddr.getResult();
+        }
+
+        return null;
+    }
+
+    @Override
+    public Map getEmployeeUserId() {
+        //获取accessToken
+        String access_token = ddClient.getAccessToken();
+
+        Map<String,Object> map = new HashMap<>();
+        //在职员工状态筛选,可以查询多个状态。不同状态之间使用英文逗号分隔。
+        //2:试用期  3:正式  5:待离职  -1:无状态
+        map.put("status_list","3");
+        //分页游标,从0开始。根据返回结果里的next_cursor是否为空来判断是否还有下一页,且再次调用时offset设置成next_cursor的值。
+        map.put("offset",0);
+        //分页大小,最大50。
+        map.put("size",50);
+
+        //获取员工userId集合
+        DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob", null, DDConf.initTokenParams(access_token), map, DDR.class);
+
+        return (Map)ddr.getResult();
+    }
+
+
+    public McR getEmployeeAnnualLeaveNum(Map<String, Object> map) {
+        //获取accessToken
+        String access_token = ddClient.getAccessToken();
+        //获取agentId
+        String agentId = ddConf.getAgentId().toString();
+        //查询接口body添加参数
+        //field_filter_list:要查询字段(花名册字段信息参考:https://open.dingtalk.com/document/orgapp/roster-custom-field-business-code)
+        //agentid:企业内部应用AgentId
+        map.put("field_filter_list","sys00-name,sys01-positionLevel,sys00-confirmJoinTime,sys02-joinWorkingTime,sys05-contractRenewCount");
+        map.put("agentid",agentId);
+
+        List<String> result = new ArrayList<>();
+        //获取员工花名册字段信息
+        if (Objects.nonNull(map)){
+            DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list", null, DDConf.initTokenParams(access_token), map, DDR.class);
+            List<Map> employeeData = (List<Map>) ddr.getResult();
+
+            //遍历员工信息
+            for (Map data : employeeData) {
+                String userId = data.get("userid").toString();
+                //首次参加工作日期(计算工龄)
+                String joinWorkingTime = "";
+                //入职日期
+                String confirmJoinTime = "";
+                //职级
+                String positionLevel = "";
+                //姓名
+                String name = "";
+                //合同续签次数
+                int contractRenewCount = 0;
+                List<Map> fieldDataList = (List<Map>) data.get("field_data_list");
+                for (Map fieldData : fieldDataList) {
+                    String fieldCode = fieldData.get("field_code").toString();
+                    List<Map> fieldValueList = (List<Map>) fieldData.get("field_value_list");
+                    if (Objects.nonNull(fieldValueList.get(0).get("value"))){
+                        String value = fieldValueList.get(0).get("value").toString();
+                        switch (fieldCode){
+                            case "sys02-joinWorkingTime": joinWorkingTime = value;break;
+                            case "sys00-confirmJoinTime": confirmJoinTime = value;break;
+                            case "sys01-positionLevel": positionLevel = value;break;
+                            case "sys00-name": name = value;break;
+                            case "sys05-contractRenewCount": contractRenewCount = Integer.valueOf(value);break;
+                            default:break;
+                        }
+                    }else {
+                        log.info("更新员工年假余额:参数缺啦!");
+                        return McR.errorParam("参数缺啦!");
+                    }
+
+                }
+                //工龄(年) 计算规则:首次工作时间至当年一月一日 数值向下取整
+                int workAge =(int) (DateUtil.betweenDay(DateUtil.parse(joinWorkingTime), DateUtil.beginOfYear(new Date()), true) / 365);
+                System.out.println(workAge + "年");
+                //法定年假
+                int legalAnnualLeave = 0;
+                //福利年假
+                int welfareAnnualLeave = 0;
+                //根据职级、工龄和合同续签数计算年假基数
+                if (positionLevel.equals("5")){
+                    if (workAge < 10){
+                        legalAnnualLeave = 5;
+                        welfareAnnualLeave = 15;
+                    }else if (workAge >= 10 && workAge < 20){
+                        legalAnnualLeave = 10;
+                        welfareAnnualLeave = 10;
+                    }else if (workAge >= 20){
+                        legalAnnualLeave = 15;
+                        welfareAnnualLeave = 5;
+                    }
+                }else if (positionLevel.equals("4") || positionLevel.equals("3")){
+                    if (workAge < 10){
+                        legalAnnualLeave = 5;
+                        welfareAnnualLeave = 7 + 2 * contractRenewCount;
+                    }else if (workAge >= 10 && workAge < 20){
+                        legalAnnualLeave = 10;
+                        welfareAnnualLeave = 2 + 2 * contractRenewCount;
+                    }else if (workAge >= 20){
+                        legalAnnualLeave = 15;
+                        welfareAnnualLeave = 1;
+                    }
+                }else if (positionLevel.equals("2")){
+                    if (workAge < 10){
+                        legalAnnualLeave = 5;
+                        welfareAnnualLeave = 5 + 2 * contractRenewCount;
+                    }else if (workAge >= 10 && workAge < 20){
+                        legalAnnualLeave = 10;
+                        welfareAnnualLeave = 0 + 2 * contractRenewCount;
+                    }else if (workAge >= 20){
+                        legalAnnualLeave = 15;
+                        welfareAnnualLeave = 0;
+                    }
+                }else if (positionLevel.equals("1")){
+                    if (workAge < 10){
+                        legalAnnualLeave = 5;
+                        welfareAnnualLeave = 3  + 2 * contractRenewCount;
+                    }else if (workAge >= 10 && workAge < 20){
+                        legalAnnualLeave = 10;
+                        welfareAnnualLeave = 0  + 2 * contractRenewCount;
+                    }else if (workAge >= 20){
+                        legalAnnualLeave = 15;
+                        welfareAnnualLeave = 0;
+                    }
+                }else {
+                    legalAnnualLeave = 0;
+                    welfareAnnualLeave = 0;
+                }
+                //开始日期为当年1月1日
+                DateTime beginDate = DateUtil.beginOfYear(new Date());
+                //有效日期至当年12月31日
+                DateTime endDate = DateUtil.offsetDay(DateUtil.endOfYear(new Date()),-1);
+
+                result.add("姓名:"+name+",职级:"+positionLevel+",工龄:"+workAge+"年,合同续签数"+contractRenewCount+",法定年假:" + legalAnnualLeave + "天,福利年假:" + welfareAnnualLeave + "天"+",截止日期:"+endDate);
+                //年假基数
+                double yearLeave = legalAnnualLeave + welfareAnnualLeave;
+                double yearLeaveDecimalPart = 0;
+                //判断员工是否当年新入职
+                if (DateUtil.year(DateUtil.parse(confirmJoinTime)) == DateUtil.year(new Date())){
+                    int workDay = (int) DateUtil.betweenDay(DateUtil.parse(confirmJoinTime),endDate,true);
+                    yearLeave = workDay * yearLeave / 365.0;
+                    yearLeaveDecimalPart = yearLeave - (int) yearLeave;
+                    if (yearLeaveDecimalPart < 0.25){
+                        yearLeave = (int) yearLeave;
+                    }else if (yearLeaveDecimalPart < 0.75){
+                        yearLeave = (int) yearLeave + 0.5;
+                    }else if (yearLeaveDecimalPart < 1){
+                        yearLeave = (int) yearLeave + 1;
+                    }
+                }
+                //更新假期余额接口的body
+                Map<String,Object> updateBody = new HashMap<>();
+                Map<String,Object> leave_quotas = new HashMap<>();
+                //额度有效期开始时间,毫秒级时间戳
+                leave_quotas.put("start_time",beginDate.getTime());
+                //额度有效期结束时间,毫秒级时间戳。
+                leave_quotas.put("end_time",endDate.getTime());
+                //操作原因
+                leave_quotas.put("reason","接口测试修改");
+                //以天计算的额度总数 假期类型按天计算时,该值不为空且按百分之一天折算。 例如:1000=10天。
+                leave_quotas.put("quota_num_per_day",(int) (yearLeave * 100) );
+                //以小时计算的额度总数 假期类型按小时,计算该值不为空且按百分之一小时折算。例如:1000=10小时。
+                leave_quotas.put("quota_num_per_hour",0);
+                //额度所对应的周期,格式必须是"yyyy",例如"2021"
+                leave_quotas.put("quota_cycle",DateUtil.year(new Date())+"");
+                //自定义添加的假期类型:年假开发测试的leave_code
+                leave_quotas.put("leave_code","28abb7bf-e1b6-4387-9e2a-e1b2ae983e7a");
+                //要更新的员工的userId
+                leave_quotas.put("userid",userId);
+
+                updateBody.put("leave_quotas",leave_quotas);
+                //当前企业内拥有OA审批应用权限的管理员的userId(飞超)
+                updateBody.put("op_userid","02421908021891243060");
+
+                //更新假期余额
+                UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/update", null, DDConf.initTokenParams(access_token), updateBody, DDR.class);
+            }
+        }
+        log.info(result.stream().collect(Collectors.joining(",")));
+        return McR.success(result);
+    }
+}

+ 16 - 6
mjava-kaiyue/src/main/resources/application-dev.yml

@@ -49,14 +49,24 @@ logging:
 
 # dingtalk
 dingtalk:
-  agentId: 2673435445
-  appKey: dingozv6fzkpqkiupd3d
-  appSecret: bO4AA6ujXj8xgLBJI5pR7ns0vRsHCn8Ng9fTf9WF95HTOlCW0oybYpHsuxXuBPiO
-  corpId: dingcc1b1ffad0d5ca1d
-  aesKey:
-  token:
+  agentId: 2999477924
+  appKey: dingzorik72leqm5qgpj
+  appSecret: csxfrSOZy02aXvc4IkqM9dFqz7cEDgUogvJaBIq_rtIbvjZLDKiVkHdVgKeNfoVQ
+  corpId: ding43bb7be8e7bdc63224f2f5cc6abecb85
+  aesKey: 7txhFmSyWIXIrEvwlNfcuMfOQe19K6hqCdIaXMHLO2K
+  token: 24VR2Bnu
   operator: ""   # OA管理员账号 [0开头需要转一下字符串]
 
+  #poc
+  #agentId: 2995824312
+  #appKey: ding3ap1jk1tg44tz3s2
+  #appSecret: PaWTDG-FiX-RW5fnV9r8CzEmR-9QlJpubC88txhprL_Z_iREO62B-iRW6w7gkA_K
+  #corpId: ding321c72787fffc78b35c2f4657eb6378f
+  #aesKey: LSIc7r5uHAP0dd6v23J3LWRmjECMNzbkIcxAwdx63RE
+  #token: yqXHMHaK4oHYvjyQshU4zFqgrHFq7PcBxVSqGo1BAQk0
+  #operator: ""   # OA管理员账号 [0开头需要转一下字符串]
+
+
 # aliwork
 aliwork:
   appType: APP_ZL5NE83JE84UUACZDD03

+ 15 - 0
mjava/src/main/java/com/malk/server/dingtalk/DDConf.java

@@ -60,6 +60,21 @@ public class DDConf {
      */
     public static final String BPMS_INSTANCE_CHANGE = "bpms_instance_change";
 
+    /**
+     * 通讯录用户增加
+     */
+    public static final String USER_ADD_ORG = "user_add_org";
+
+    /**
+     * 通讯录用户更改
+     */
+    public static final String USER_DODIFY_ORG = "user_modify_org";
+
+    /**
+     * 人事档案变动
+     */
+    public static final String HRM_USER_RECORD_CHANGE = "hrm_user_record_change";
+
     /**
      * token授权参数: 旧版本
      */