소스 검색

绍兴凯悦v1.0

wzy 7 달 전
부모
커밋
9352bc0c81

+ 155 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/controller/KYSXController.java

@@ -0,0 +1,155 @@
+package com.malk.kaiyue.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.malk.controller.DDCallbackController;
+import com.malk.kaiyue.service.KYSXService;
+import com.malk.server.common.McR;
+import com.malk.server.dingtalk.DDConf;
+import com.malk.server.dingtalk.crypto.DingCallbackCrypto;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+@RestController
+@RequestMapping("/sx")
+public class KYSXController extends DDCallbackController {
+    @Autowired
+    private KYSXService kysxService;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_sx.token}")
+    private String token;
+
+    @Value("${dingtalk_sx.aesKey}")
+    private String aesKey;
+
+    @Value("${dingtalk_sx.appKey}")
+    private String appKey;
+
+    @GetMapping("/test")
+    public McR test() {
+        log.info("11111111");
+        return McR.success();
+    }
+
+    //获取在职员工userId列表
+    @GetMapping("/getEmployeeUserId")
+    McR getEmployeeUserId() {
+
+        List<String> result = kysxService.getEmployeeUserId();
+
+        return McR.success(result);
+    }
+
+    //获取员工花名册信息
+    @PostMapping("/getEmployeeRosterInfo")
+    McR getEmployeeRosterInfo(@RequestBody Map<String, Object> map) {
+        List<Map> result = kysxService.getEmployeeRosterInfo(map);
+
+        return McR.success(result);
+    }
+
+    //每年1月1日 01:00定时更新员工年假数
+    @Scheduled(cron = "0 0 1 1 1 ? ")
+    @GetMapping("/cronUpdateEmployeeAnnualLeaveNum")
+    McR cronUpdateEmployeeAnnualLeaveNum(){
+        System.out.println("定时更新员工年假数开始执行"+new Date());
+        return kysxService.updateEmployeeAnnualLeaveNum();
+    }
+
+
+    @PostMapping("/getUserLeaveInfo")
+    McR getUserLeaveInfo(@RequestBody Map map) {
+        String userId = map.get("userId").toString();
+        Map result = kysxService.getUserLeaveInfo(userId);
+        return McR.success(result);
+    }
+
+    //保存10s内已处理的回调事件
+    private Map<String, Long> eventList = new HashMap<>();
+
+    //钉钉事件回调
+    @SneakyThrows
+    @RequestMapping(value = "/callback", method = RequestMethod.POST)
+    public synchronized 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(token, aesKey, appKey);
+        // 处理回调消息,得到回调事件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]更新员工年假余额 -----");
+            kysxService.getEmployeeAnnualLeaveNum(map);
+
+            // 将回调事件和当前时间戳添加到已处理集合中
+            long currentTime = System.currentTimeMillis();
+            eventList.put(decryptMsg, currentTime);
+
+            return success;
+        }
+        log.info("----- [DD]已注册, 未处理的其它回调 -----, eventType:{}, eventJson:{}",eventType, eventJson);
+        return success;
+    }
+
+    /**
+     * 检查该回调事件在10s内是否处理过,应对钉钉瞬间重复回调
+     *
+     * @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);
+    }
+
+
+}

+ 38 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/KYSXService.java

@@ -0,0 +1,38 @@
+package com.malk.kaiyue.service;
+
+import com.malk.server.common.McR;
+import org.springframework.scheduling.annotation.Async;
+
+import java.util.List;
+import java.util.Map;
+
+public interface KYSXService {
+    /**
+     * 获取员工花名册信息
+     * @param map
+     * @return
+     */
+    List<Map> getEmployeeRosterInfo(Map<String, Object> map);
+
+    /**
+     * 获取在职员工userId列表
+     * @return
+     */
+    List<String> getEmployeeUserId();
+
+    /**
+     * 计算并设置获取员工年假数
+     * @param map
+     * @return
+     */
+    @Async
+    Map getEmployeeAnnualLeaveNum(Map<String, Object> map);
+
+    /**
+     * 每年1月1日 01:00定时更新员工年假数
+     * @return
+     */
+    McR updateEmployeeAnnualLeaveNum();
+
+    Map getUserLeaveInfo(String userId);
+}

+ 1 - 1
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/impl/KYNTServiceImpl.java

@@ -163,7 +163,7 @@ public class KYNTServiceImpl implements KYNTService {
                 }
                 //若没有升职日期 则默认当天是升职日期
                 if ("".equals(promotionTime)){
-                    promotionTime = DateUtil.today();
+                    promotionTime = confirmJoinTime;
                 }
                 //若没有现合同开始日期
                 if ("".equals(owContractStartTime)){

+ 536 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/impl/KYSXServiceImpl.java

@@ -0,0 +1,536 @@
+package com.malk.kaiyue.service.impl;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import com.malk.kaiyue.service.KYNTService;
+import com.malk.kaiyue.service.KYSXService;
+import com.malk.server.common.McR;
+import com.malk.server.dingtalk.DDConf;
+import com.malk.server.dingtalk.DDR;
+import com.malk.server.dingtalk.DDR_New;
+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.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+public class KYSXServiceImpl implements KYSXService {
+    @Autowired
+    private DDClient ddClient;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_sx.appKey}")
+    private String appKey;
+
+    @Value("${dingtalk_sx.appSecret}")
+    private String appSecret;
+
+    @Value("${dingtalk_sx.agentId}")
+    private String agentId;
+
+    @Value("${dingtalk_sx.operator}")
+    private String opUserId;
+
+    //绍兴凯悦-年假(测)
+//    private static final String LEAVE_CODE = "1eecac0a-18b3-4596-b696-ba60d71c8306";
+
+    //绍兴凯悦-年假
+    private static final String LEAVE_CODE = "597d0415-b863-4b16-bc3a-3a5e9d9d2b77";
+
+    //绍兴凯悦-调休假
+    private static final String COMPENSATORY_LEAVE_CODE = "4429b500-6492-403e-891b-6aeca4590bef";
+    @Override
+    public List<Map> getEmployeeRosterInfo(Map<String, Object> map) {
+        //获取accessToken
+        String access_token = ddClient.getAccessToken(appKey,appSecret);
+        //获取员工花名册字段信息
+        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 List<String> getEmployeeUserId() {
+        Map<String,Object> map = new HashMap<>();
+        //在职员工状态筛选,可以查询多个状态。不同状态之间使用英文逗号分隔。
+        //2:试用期  3:正式  5:待离职  -1:无状态
+        map.put("status_list","2,3");
+        //分页游标,从0开始。根据返回结果里的next_cursor是否为空来判断是否还有下一页,且再次调用时offset设置成next_cursor的值。
+        map.put("offset",0);
+        //分页大小,最大50。
+        map.put("size",50);
+
+        //获取员工userId集合
+        List<String> userIdList = new ArrayList<>();
+        getUserIdList(map,userIdList);
+
+        return userIdList;
+    }
+
+    public List<String> getUserIdList(Map map,List<String> userIdList){
+        //获取accessToken
+        String access_token = ddClient.getAccessToken(appKey,appSecret);
+        //调用钉钉接口获取在职员工userId集合
+        DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob", null, DDConf.initTokenParams(access_token), map, DDR.class);
+        Map result = (Map) ddr.getResult();
+        //将返回结果里的data_list合并到userIdList
+        userIdList.addAll((List<String>) result.get("data_list"));
+        //判断是否还有下一页
+        if (Objects.nonNull(result.get("next_cursor"))){
+            map.put("offset",result.get("next_cursor"));
+            //递归将集合合并到userIdList
+            getUserIdList(map,userIdList);
+        }
+
+        return userIdList;
+    }
+
+    //保存5s内已处理的更新假期余额事件
+    private Map<String, Long> bodyList = new HashMap<>();
+
+    @Async
+    public Map getEmployeeAnnualLeaveNum(Map<String, Object> map) {
+        //获取accessToken
+        String access_token = ddClient.getAccessToken(appKey,appSecret);
+
+        //查询接口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,sys01-employeeType,9bd53d78-3008-4927-aef8-152a1b44f29b,434d3cd0-3b02-4250-9e7d-7748c31efa84,sys00-confirmJoinTime,sys02-joinWorkingTime,sys05-nowContractStartTime,sys05-contractRenewCount");
+        map.put("agentid",agentId);
+
+        List<Map> errorList = new ArrayList<>();
+        List<Map> successList = 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();
+                try {
+                    //首次参加工作日期(计算工龄)
+                    String joinWorkingTime = "";
+                    //入职日期
+                    String confirmJoinTime = "";
+                    //职级
+                    String positionLevel = "";
+                    //原职级
+                    String oldPositionLevel = "";
+                    //升职日期
+                    String promotionTime = "";
+                    //姓名
+                    String name = "";
+                    //现合同开始日期
+                    String contractStartTime = "";
+                    //合同续签次数
+                    int contractRenewCount = 0;
+                    //员工类型
+                    String employeeType = "";
+
+                    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");
+
+                        String value = getString(fieldValueList.get(0).get("value"));
+                        String label = getString(fieldValueList.get(0).get("label"));
+                        switch (fieldCode){
+                            case "sys02-joinWorkingTime": joinWorkingTime = value;break;
+                            case "sys00-confirmJoinTime": confirmJoinTime = value;break;
+                            case "sys01-positionLevel": positionLevel = value;break;
+                            case "9bd53d78-3008-4927-aef8-152a1b44f29b": oldPositionLevel = label;break;
+                            case "434d3cd0-3b02-4250-9e7d-7748c31efa84": promotionTime = value;break;
+                            case "sys00-name": name = value;break;
+                            case "sys05-nowContractStartTime": contractStartTime = value;break;
+                            case "sys05-contractRenewCount": contractRenewCount = value.equals("") ? 0 : Integer.valueOf(value);break;
+                            case "sys01-employeeType": employeeType = label;break;
+                            default:break;
+                        }
+                    }
+
+                    if (employeeType.equals("劳务外包") || employeeType.equals("实习") || employeeType.equals("兼职")){
+                        Map successMap = new HashMap();
+                        successMap.put(userId,"姓名:"+name+",劳务外包、实习、兼职员工不发放年假");
+                        successList.add(successMap);
+
+                        continue;
+                    }
+
+
+                    //若没有原职级 则默认原职级是现职级
+                    if ("".equals(oldPositionLevel)){
+                        oldPositionLevel = positionLevel;
+                    }
+                    //若没有升职日期 则默认当天是升职日期
+                    if ("".equals(promotionTime)){
+                        promotionTime = confirmJoinTime;
+                    }
+                    //若没有现合同开始日期 则默认现合同开始日期是入职日期
+                    if ("".equals(contractStartTime)){
+                        contractStartTime = confirmJoinTime;
+                    }
+                    //若没有首次参加工作日期 则默认入职日期是首次参加工作日期
+                    if ("".equals(joinWorkingTime)){
+                        joinWorkingTime = confirmJoinTime;
+                    }
+
+                    if ("".equals(confirmJoinTime) || "".equals(positionLevel) || "".equals(name)){
+                        Map errorMap = new HashMap();
+                        errorMap.put(userId,"参数缺失!");
+                        errorList.add(errorMap);
+//                        log.info("更新员工userid:{} 参数缺失!", userId);
+                        continue;
+                    }
+
+                    //旧合同续签次数等于合同续签数-1 最小为0
+                    int oldContractRenewCount = Math.max(0, contractRenewCount - 1);
+
+                    //假期有效开始日期为当年1月1日
+                    DateTime beginDate = DateUtil.beginOfYear(new Date());
+                    //假期有效截至日期为当年12月31日
+                    DateTime endDate = DateUtil.endOfYear(new Date());
+                    //当年天数
+                    int yearDays = DateUtil.dayOfYear(endDate);
+
+                    //工龄(年) 计算规则:首次工作时间至当年一月一日 数值向下取整
+                    int workAge =(int) (DateUtil.betweenYear(DateUtil.parse(joinWorkingTime), beginDate, true));
+                    if (DateUtil.dayOfYear(DateUtil.parse(joinWorkingTime)) != 1 && workAge > 0){
+                        workAge --;
+                    }
+
+                    System.out.println("截至今年1月1日,工龄为:"+workAge + "年");
+                    //年假数
+                    double yearLeave = getLeaveNum(confirmJoinTime,beginDate,endDate,promotionTime,contractStartTime,oldPositionLevel,positionLevel,workAge,oldContractRenewCount,contractRenewCount,yearDays);
+
+                    //年假小数
+                    double 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;
+                    }
+
+                    //查询出用户消费年假记录 当返回leaveRecords中calType为null或不返回该字段则为请假消耗 将计算出的年假数减去请假消耗的数量
+                    Map body = new HashMap();
+                    body.put("opUserId",opUserId);
+                    body.put("leaveCode",LEAVE_CODE);
+                    body.put("userIds",new String[]{userId});
+                    body.put("pageNumber",0);
+                    body.put("pageSize",50);
+                    DDR_New useDdr = (DDR_New) UtilHttp.doPost("https://api.dingtalk.com/v1.0/attendance/vacations/records/query", DDConf.initTokenHeader(access_token), null, body, DDR_New.class);
+                    Map useResult = (Map) useDdr.getResult();
+                    List<Map> useList = (List<Map>) useResult.get("leaveRecords");
+                    Double useLeaveNum = 0d;
+                    if (Objects.nonNull(useList) && !useList.isEmpty()){
+                        for (Map use : useList) {
+                            //判断是否为今年请假
+                            DateTime gmtCreate = DateUtil.date((long) use.get("gmtCreate"));
+                            if (DateUtil.year(gmtCreate) == DateUtil.year(new Date())){
+                                //判断是否为正常请假而不是接口测试或期初假期发放
+                                if (Objects.nonNull(use.get("leaveReason"))){
+                                    if (!"接口年假发放".equals(use.get("leaveReason").toString()) && !"期初假期发放".equals(use.get("leaveReason").toString())){
+
+                                        //若是请假消耗或管理员手动减少
+                                        if (use.containsKey("calType") && Objects.nonNull(use.get("calType"))){
+                                            if ("delete".equals(use.get("calType").toString())){
+                                                useLeaveNum += (int) use.get("recordNumPerDay");
+                                            }
+                                        }
+                                        /*if (!use.containsKey("calType") || Objects.isNull(use.get("calType")) || "delete".equals(use.get("calType").toString())){
+                                            useLeaveNum += (int) use.get("recordNumPerDay") / 100;
+                                        }*/
+                                        //注:若是管理员手动增加 则假期余额会多出一个高级假期记录增加的假期天数  最终会在设置的假期余额的基础上加上这些天数
+                                        //故此处手动新增的假期余额不做处理
+                                    }
+                                }
+                            }
+                        }
+                    }
+
+                    //实际年假数
+                    double realYearLeave = (yearLeave * 100 - useLeaveNum) < 0 ? 0 : (yearLeave * 100 - useLeaveNum);
+
+                    //更新假期余额接口的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) realYearLeave );
+                    //以小时计算的额度总数 假期类型按小时,计算该值不为空且按百分之一小时折算。例如: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",LEAVE_CODE);
+                    //要更新的员工的userId
+                    leave_quotas.put("userid",userId);
+
+                    updateBody.put("leave_quotas",leave_quotas);
+                    //当前企业内拥有OA审批应用权限的管理员的userId
+                    updateBody.put("op_userid",opUserId);
+
+                    String bodyStr = yearLeave + userId;
+
+                    // 检查更新事件是否已经处理过,如果是,则忽略该更新
+                    if (isUpdateLeave(bodyStr)) {
+                        log.info("更新事件已处理,忽略该回调...");
+                        return null;
+                    }
+
+                    // 将更新和当前时间戳添加到已处理集合中
+                    long currentTime = System.currentTimeMillis();
+                    bodyList.put(bodyStr, currentTime);
+
+                    //更新假期余额
+                    UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/update", null, DDConf.initTokenParams(access_token), updateBody, DDR.class);
+
+                    Map successMap = new HashMap();
+                    successMap.put(userId,"姓名:"+name+",职级:" + positionLevel+",原职级:" + oldPositionLevel+",入职日期:" + confirmJoinTime + ",升职日期:" + promotionTime + ",年假数:"+ realYearLeave + ",开始日期:" + beginDate+",截止日期:" + endDate);
+                    successList.add(successMap);
+                }catch (Exception e){
+                    log.info("更新员工年假失败:{}",e);
+                    Map errorMap = new HashMap();
+                    errorMap.put(userId,e.getMessage());
+                    errorList.add(errorMap);
+                }
+            }
+        }
+        log.info("更新失败列表:{}",errorList);
+        log.info("更新成功列表:{}",successList);
+        Map result = new HashMap();
+        result.put("errorList",errorList);
+        result.put("successList",successList);
+        return result;
+    }
+
+    private String getString(Object label) {
+        return Objects.isNull(label) ? "" : label.toString();
+    }
+
+    private double getLeaveNum(String confirmJoinTime,Date beginDate,Date endDate,String promotionTime,String owContractStartTime,String oldPositionLevel,String positionLevel,int workAge,int oldContractRenewCount,int contractRenewCount,int yearDays) {
+        double yearLeave = 0.0;
+
+        long day1 = 0;
+        long day2 = 0;
+        long day3 = 0;
+
+        //判断员工是否当年新入职
+        if (DateUtil.year(DateUtil.parse(confirmJoinTime)) == DateUtil.year(new Date())){
+            beginDate = DateUtil.parse(confirmJoinTime);
+        }
+
+        //判断员工是否当年升职
+        if (DateUtil.year(DateUtil.parse(promotionTime)) == DateUtil.year(new Date())){
+            //判断现合同开始日期是否当年
+            if (DateUtil.year(DateUtil.parse(owContractStartTime)) == DateUtil.year(new Date())){
+                //判断现合同开始日期和升职日期先后顺序
+                if (DateUtil.parse(owContractStartTime).before(DateUtil.parse(promotionTime))){
+                    day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(owContractStartTime),true);
+                    day2 = DateUtil.betweenDay(DateUtil.parse(owContractStartTime),DateUtil.parse(promotionTime),true);
+                    day3 = DateUtil.betweenDay(DateUtil.parse(promotionTime),endDate,true);
+                    yearLeave = (double) (day1 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,oldContractRenewCount) + day2 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,contractRenewCount) + day3 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
+                }else {
+                    day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(promotionTime),true);
+                    day2 = DateUtil.betweenDay(DateUtil.parse(promotionTime),DateUtil.parse(owContractStartTime),true);
+                    day3 = DateUtil.betweenDay(DateUtil.parse(owContractStartTime),endDate,true);
+                    yearLeave = (double) (day1 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,oldContractRenewCount) + day2 * getAnnualLeaveBaseNum(positionLevel,workAge,oldContractRenewCount) + day3 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
+                }
+            }else {
+                day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(promotionTime),true);
+                day2 = DateUtil.betweenDay(DateUtil.parse(promotionTime),endDate,true);
+                yearLeave = (double) (day1 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,contractRenewCount) + day2 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
+            }
+        }else {
+            //判断现合同开始日期是否当年
+            if (DateUtil.year(DateUtil.parse(owContractStartTime)) == DateUtil.year(new Date())){
+                day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(owContractStartTime),true);
+                day2 = DateUtil.betweenDay(DateUtil.parse(owContractStartTime),endDate,true);
+                yearLeave = (double) (day1 * getAnnualLeaveBaseNum(positionLevel,workAge,oldContractRenewCount) + day2 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
+            }else {
+                yearLeave = getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount);
+            }
+        }
+        return yearLeave;
+    }
+
+    private int     getAnnualLeaveBaseNum(String positionLevel, int workAge, int contractRenewCount) {
+        //法定年假
+        int legalAnnualLeave = 0;
+        //福利年假
+        int welfareAnnualLeave = 0;
+        //根据职级、工龄和合同续签数计算年假基数
+        if (positionLevel.equals("总经理")){
+            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("部门总监") || positionLevel.equals("经理级")){
+            if (workAge < 10){
+                legalAnnualLeave = 5;
+                welfareAnnualLeave = 7 + 2 * (Math.min(contractRenewCount, 2));
+            }else if (workAge >= 10 && workAge < 20){
+                legalAnnualLeave = 10;
+                welfareAnnualLeave = 2 + 2 * (Math.min(contractRenewCount, 2));
+            }else if (workAge >= 20){
+                legalAnnualLeave = 15;
+                welfareAnnualLeave = 1;
+            }
+        }else if (positionLevel.equals("主管")){
+            if (workAge < 10){
+                legalAnnualLeave = 5;
+                welfareAnnualLeave = 5 + 2 * (Math.min(contractRenewCount, 2));
+            }else if (workAge >= 10 && workAge < 20){
+                legalAnnualLeave = 10;
+                welfareAnnualLeave = 0 + 2 * (Math.min(contractRenewCount, 2));
+            }else if (workAge >= 20){
+                legalAnnualLeave = 15;
+                welfareAnnualLeave = 0;
+            }
+        }else if (positionLevel.equals("员工")){
+            if (workAge < 10){
+                legalAnnualLeave = 5;
+                welfareAnnualLeave = 3  + 2 * (Math.min(contractRenewCount, 2));
+            }else if (workAge >= 10 && workAge < 20){
+                legalAnnualLeave = 10;
+                welfareAnnualLeave = 0  + 2 * (Math.min(contractRenewCount, 1));
+            }else if (workAge >= 20){
+                legalAnnualLeave = 15;
+                welfareAnnualLeave = 0;
+            }
+        }else {
+            legalAnnualLeave = 0;
+            welfareAnnualLeave = 0;
+        }
+        int annualLeave = legalAnnualLeave + welfareAnnualLeave;
+        return annualLeave;
+    }
+
+    @Override
+    public McR updateEmployeeAnnualLeaveNum() {
+        //获取员工userId集合
+        List<String> userIdList = getEmployeeUserId();
+
+        //遍历集合给所有员工更新年假余额
+        List<Map> result = new ArrayList<>();
+        if (Objects.nonNull(userIdList) && !userIdList.isEmpty()){
+            Map map = new HashMap();
+            //将userIdList拆分成50长度的多个数组
+            List<List<String>> userIdListList = CollectionUtil.splitList(userIdList,50);
+            for (List<String> userIdList1 : userIdListList) {
+                map.put("userid_list",String.join(",",userIdList1));
+                result.add(getEmployeeAnnualLeaveNum(map));
+            }
+            log.info("result:{}",result);
+        }
+
+        return McR.success();
+    }
+
+    @Override
+    public Map getUserLeaveInfo(String userId) {
+        long currentTime = System.currentTimeMillis();
+
+        Map leaveMap = new HashMap();
+
+        //获取员工调休假余额
+        List<Map> leaveQuotasList = new ArrayList<>();
+        getLeaveNum(COMPENSATORY_LEAVE_CODE,userId,0,50,leaveQuotasList);
+
+        //查询员工调休假余额
+        BigDecimal compensatoryLeaveNum = new BigDecimal(0.00);
+        if (Objects.nonNull(leaveQuotasList)){
+            for (Map leaveQuotas : leaveQuotasList) {
+                if ((long) leaveQuotas.get("start_time") <= currentTime && currentTime <= (long) leaveQuotas.get("end_time")){
+                    if (Objects.isNull(leaveQuotas.get("quota_num_per_day")) && Objects.nonNull(leaveQuotas.get("quota_num_per_hour"))){
+                        compensatoryLeaveNum = compensatoryLeaveNum.add(new BigDecimal(String.valueOf(leaveQuotas.get("quota_num_per_hour"))).divide(new BigDecimal(100)));
+                    }
+                    if (Objects.nonNull(leaveQuotas.get("used_num_per_hour"))){
+                        compensatoryLeaveNum = compensatoryLeaveNum.subtract(new BigDecimal(String.valueOf(leaveQuotas.get("used_num_per_hour"))).divide(new BigDecimal(100)));
+                    }
+                }
+            }
+        }
+
+        if (compensatoryLeaveNum.compareTo(new BigDecimal(80)) >= 0){
+            leaveMap.put("可预支调休",80);
+            leaveMap.put("实际加班小时数",compensatoryLeaveNum.subtract(new BigDecimal(80)));
+            leaveMap.put("已预支调休",0);
+        }else {
+            leaveMap.put("可预支调休",compensatoryLeaveNum);
+            leaveMap.put("实际加班小时数",0);
+            leaveMap.put("已预支调休",new BigDecimal(80).subtract(compensatoryLeaveNum));
+        }
+
+        return leaveMap;
+    }
+
+    private List<Map> getLeaveNum(String leave_code,String userId,int offset,int size,List<Map> leaveQuotasList) {
+        String access_token = ddClient.getAccessToken(appKey,appSecret);
+        Map body = new HashMap();
+        body.put("leave_code",leave_code);
+        body.put("op_userid",opUserId);
+        body.put("userids",userId);
+        body.put("offset",offset);
+        body.put("size",size);
+
+        DDR_New ddrNew = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/list", null, DDConf.initTokenParams(access_token), body, DDR_New.class);
+        Map result = (Map) ddrNew.getResult();
+
+        if (Objects.nonNull(result.get("leave_quotas"))){
+            leaveQuotasList.addAll((List<Map>) result.get("leave_quotas"));
+        }
+
+        if ((boolean) result.get("has_more")){
+            getLeaveNum(leave_code,userId,offset+size,size,leaveQuotasList);
+        }
+        return leaveQuotasList;
+    }
+
+    /**
+     * 检查该更新事件在5s内是否处理过,应对钉钉瞬间重复回调
+     *
+     * @param msg 回调事件
+     * @return 是否处理过
+     */
+    private boolean isUpdateLeave(String msg) {
+        // 清理超过10s的回调事件
+        long currentTime = System.currentTimeMillis();
+        long expirationTime = currentTime - TimeUnit.SECONDS.toMillis(5);
+        bodyList.entrySet().removeIf(entry -> entry.getValue() < expirationTime);
+
+        return bodyList.containsKey(msg);
+    }
+
+}

+ 11 - 1
mjava-kaiyue/src/main/resources/application-dev.yml

@@ -1,6 +1,6 @@
 # 环境配置
 server:
-  port: 9020
+  port: 9021
   servlet:
     context-path: /api/kaiyue
 
@@ -113,3 +113,13 @@ dingtalk_hq:
   aesKey: fI8d5nU9EdAFRMv7MJDMJFrhpkPLrFUVYjpmPEhIfPD
   token: cdclTjeXBL3MGhlxu9cD1VSQUTeCulxFLCYqkJuC4s
   operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-sx
+dingtalk_sx:
+  agentId: 3287531742
+  appKey: dingjq5ojnotlqqvyk8t
+  appSecret: hQDKS9t5cbd4GtEgJ7zmcjDjC8yaLxU7OZsr-_PlxAonR1pCmTUaPu4RFg5AJIvX
+  corpId: ding70f9bcd3bf9753c6acaaa37764f94726
+  aesKey: tX2Wv0HX4W0ox6OhNRIiI6vtMXYrfJvBqa75TjvIBNV
+  token: 8GYLFFJoPQLI4k8mSAXcwInOXagyIu7gowjzwcyhLXMdtMbEpvptT
+  operator: "02145933103920308091"   # OA管理员账号 [0开头需要转一下字符串]

+ 11 - 11
mjava-kaiyue/src/main/resources/application-prod.yml

@@ -49,19 +49,9 @@ mybatis-plus:
   #扫描mapper文件
   mapper-locations: classpath:mapper/*.xml
 
-# filepath
-file:
-  path:
-    file: /Users/malk/server/_Tool/var/mjava/tmp/file/
-    image: /Users/malk/server/_Tool/var/mjava/tmp/image/
-    tmp: /Users/malk/server/_Tool/var/mjava/tmp/
-  source:
-    fonts: /Users/malk/server/_Tool/fonts/simsun.ttc
 logging:
-  file:
-    path: /Users/malk/server/_Tool/var/mjava/log
   level:
-    com.malk.*: debug
+    com.malk.*: info
 
 
 #高级假期
@@ -112,3 +102,13 @@ dingtalk_hq:
   aesKey: fI8d5nU9EdAFRMv7MJDMJFrhpkPLrFUVYjpmPEhIfPD
   token: cdclTjeXBL3MGhlxu9cD1VSQUTeCulxFLCYqkJuC4s
   operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-sx
+dingtalk_sx:
+  agentId: 3287531742
+  appKey: dingjq5ojnotlqqvyk8t
+  appSecret: hQDKS9t5cbd4GtEgJ7zmcjDjC8yaLxU7OZsr-_PlxAonR1pCmTUaPu4RFg5AJIvX
+  corpId: ding70f9bcd3bf9753c6acaaa37764f94726
+  aesKey: tX2Wv0HX4W0ox6OhNRIiI6vtMXYrfJvBqa75TjvIBNV
+  token: 8GYLFFJoPQLI4k8mSAXcwInOXagyIu7gowjzwcyhLXMdtMbEpvptT
+  operator: "02145933103920308091"   # OA管理员账号 [0开头需要转一下字符串]

+ 61 - 0
mjava-kaiyue/src/main/resources/logback-spring.xml

@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration debug="false" scan="false" scanPeriod="60 seconds">
+    <springProperty scope="context" name="LOG_HOME" source="logging.path" defaultValue="/home/server/log/"/>
+    <property name="FileNamePattern" value="${LOG_HOME}%d{yyyyMM}/%d{dd}"/>
+
+    <!-- 定义控制台输出 -->
+    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
+        <layout class="ch.qos.logback.classic.PatternLayout">
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] - %-5level - %logger{50} - %msg%n</pattern>
+        </layout>
+    </appender>
+
+    <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <!-- 指定日志文件的名称 -->
+        <!--<file>${FileNamePattern}/info.log</file>-->
+
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <fileNamePattern>${FileNamePattern}/info-%i.log</fileNamePattern>
+            <MaxHistory>30</MaxHistory>
+            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+                <MaxFileSize>30MB</MaxFileSize>
+            </timeBasedFileNamingAndTriggeringPolicy>
+        </rollingPolicy>
+
+        <layout class="ch.qos.logback.classic.PatternLayout">
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern>
+        </layout>
+    </appender>
+
+    <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
+        <discriminator>
+            <Key>processid</Key>
+            <DefaultValue>sys</DefaultValue>
+        </discriminator>
+        <sift>
+            <appender name="FILE-${processid}"
+                      class="ch.qos.logback.core.rolling.RollingFileAppender">
+                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+                    <FileNamePattern>
+                        ${FileNamePattern}/${processid}.log
+                    </FileNamePattern>
+                </rollingPolicy>
+                <layout class="ch.qos.logback.classic.PatternLayout">
+                    <Pattern>
+                        %d{yyyyMMdd:HH:mm:ss.SSS} [%thread] %-5level %msg%n
+                    </Pattern>
+                </layout>
+            </appender>
+        </sift>
+    </appender>
+
+
+    <!-- 日志输出级别 -->
+    <logger name="org.springframework" level="debug"  additivity="false"/>
+    <logger name="com.malk.connecter" level="debug"/>
+    <root level="INFO">
+        <appender-ref ref="stdout"/>
+        <appender-ref ref="appLogAppender"/>
+        <appender-ref ref="SIFT"/>
+    </root>
+</configuration>