Browse Source

凯悦项目合并

wzy 10 months ago
parent
commit
6406c54bb9

+ 69 - 0
mjava-kaiyue/pom.xml

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.malk</groupId>
+        <artifactId>java-mcli</artifactId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>com.malk.kaiyue</groupId>
+    <artifactId>mjava-kaiyue</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.malk</groupId>
+            <artifactId>mjava</artifactId>
+            <version>0.0.3</version>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.32</version>
+            <scope>runtime</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.5.3.2</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.1.1.RELEASE</version>
+                <configuration>
+                    <includeSystemScope>true</includeSystemScope>
+                    <!-- 如果没有该配置,devtools不会生效: 打包时关闭 -->
+                    <fork>false</fork>
+                    <!-- 避免中文乱码 -->
+                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
+                </configuration>
+                <!-- 允许生成可运行jar -->
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+        <finalName>${project.artifactId}</finalName>
+    </build>
+
+</project>

+ 34 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/Boot.java

@@ -0,0 +1,34 @@
+package com.malk.kaiyue;
+
+import com.querydsl.jpa.impl.JPAQueryFactory;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
+
+import javax.persistence.EntityManager;
+
+/**
+ * corp项目: 扫描公共模块
+ * -
+ * 若是无需数据库模块, 配置无效地址也可启动, 引入mjava不支持直接 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 配置
+ * 需要配置 jpa.hibernate.ddl-auto 为 none. 标识对表没有任何操作. 若不设置为 none, flyway.enabled 配置会无效, 在没有数库连接情况下程序无法启动
+ */
+@EnableJpaAuditing
+@SpringBootApplication(scanBasePackages = {"com.malk"})
+@MapperScan("com.malk.kaiyue.mapper")
+public class Boot {
+
+    public static void main(String... args) {
+        SpringApplication.run(Boot.class, args);
+    }
+
+    /**
+     * 让Spring管理JPAQueryFactory [不使用Qualifier详见mjava-Boot]
+     */
+    @Bean
+    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
+        return new JPAQueryFactory(entityManager);
+    }
+}

+ 154 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/controller/KYCDController.java

@@ -0,0 +1,154 @@
+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.KYCDService;
+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.stereotype.Component;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+@RestController
+@RequestMapping("/cd")
+@Component
+public class KYCDController extends DDCallbackController {
+    @Autowired
+    private KYCDService kycdService;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_cd.token}")
+    private String token;
+
+    @Value("${dingtalk_cd.aesKey}")
+    private String aesKey;
+
+    @Value("${dingtalk_cd.appKey}")
+    private String appKey;
+
+    @PostMapping("/test")
+    McR test(){
+        log.info("11111");
+        return McR.success();
+    }
+
+    //获取在职员工userId列表
+    @GetMapping("/getEmployeeUserId")
+    McR getEmployeeUserId() {
+
+        List<String> userIdList = kycdService.getEmployeeUserId();
+
+        return McR.success(userIdList);
+    }
+
+    //获取员工花名册信息
+    @PostMapping("/getEmployeeRosterInfo")
+    McR getEmployeeRosterInfo(@RequestBody Map<String, Object> map) {
+        List<Map> result = kycdService.getEmployeeRosterInfo(map);
+
+        return McR.success(result);
+    }
+
+    //计算并设置员工年假数
+    @PostMapping("/getEmployeeAnnualLeaveNum")
+    McR getEmployeeAnnualLeaveNum(@RequestBody Map<String, Object> map) {
+        return kycdService.getEmployeeAnnualLeaveNum(map);
+    }
+
+    //获取员工真实假期余额
+    @PostMapping("/getUserLeaveInfo")
+    McR getUserLeaveInfo(@RequestBody Map map) {
+        String userId = map.get("userId").toString();
+        Map result = kycdService.getUserLeaveInfo(userId);
+        return McR.success(result);
+    }
+
+
+    //每年1月1日 00:00定时更新员工旧职级
+    /*@Scheduled(cron = "0 0 0 1 1 ? ")
+    @GetMapping("/cronUpdateEmployeeOldPositionLevel")
+    McR cronUpdateEmployeeOldPositionLevel(){
+        System.out.println("定时更新员工旧职级开始执行"+new Date());
+        return kycdService.updateEmployeeOldPositionLevel();
+    }*/
+
+    //每年1月1日 01:00定时更新员工年假数
+    /*@Scheduled(cron = "0 0 1 1 1 ? ")
+    @GetMapping("/cronUpdateEmployeeAnnualLeaveNum")
+    McR cronUpdateEmployeeAnnualLeaveNum(){
+        System.out.println("定时更新员工年假数开始执行"+new Date());
+        return kycdService.updateEmployeeAnnualLeaveNum();
+    }*/
+
+    //保存十分钟内已处理的回调事件
+    private Map<String, Long> eventList = new HashMap<>();
+
+    @SneakyThrows
+    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));
+
+        // 业务处理代码...
+        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 {
+                log.error("[DD]人事档案变动回调, 未获取到userId");
+                return success;
+            }
+
+            log.info("员工userId:"+userId);
+            Map<String, Object> map = new HashMap();
+            map.put("userid_list", userId);
+            //更新员工年假余额
+            log.info("----- [DD]更新员工年假余额 -----");
+            kycdService.getEmployeeAnnualLeaveNum(map);
+            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);
+    }
+}

+ 161 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/controller/KYNTController.java

@@ -0,0 +1,161 @@
+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.KYNTService;
+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("/nt")
+public class KYNTController extends DDCallbackController {
+    @Autowired
+    private KYNTService kyntService;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_nt.token}")
+    private String token;
+
+    @Value("${dingtalk_nt.aesKey}")
+    private String aesKey;
+
+    @Value("${dingtalk_nt.appKey}")
+    private String appKey;
+
+    @GetMapping("/test")
+    public McR test() {
+        log.info("11111111");
+        return McR.success();
+    }
+
+    //获取在职员工userId列表
+    @GetMapping("/getEmployeeUserId")
+    McR getEmployeeUserId() {
+
+        List<String> result = kyntService.getEmployeeUserId();
+
+        return McR.success(result);
+    }
+
+    //获取员工花名册信息
+    @PostMapping("/getEmployeeRosterInfo")
+    McR getEmployeeRosterInfo(@RequestBody Map<String, Object> map) {
+        List<Map> result = kyntService.getEmployeeRosterInfo(map);
+
+        return McR.success(result);
+    }
+
+    //计算并设置员工年假数
+    @PostMapping("/getEmployeeAnnualLeaveNum")
+    McR getEmployeeAnnualLeaveNum(@RequestBody Map<String, Object> map) {
+        return kyntService.getEmployeeAnnualLeaveNum(map);
+    }
+
+    //每年1月1日 01:00定时更新员工年假数
+    /*@Scheduled(cron = "0 0 1 1 1 ? ")
+    @GetMapping("/cronUpdateEmployeeAnnualLeaveNum")
+    McR cronUpdateEmployeeAnnualLeaveNum(){
+        System.out.println("定时更新员工年假数开始执行"+new Date());
+        return kyntService.updateEmployeeAnnualLeaveNum();
+    }*/
+
+
+    @PostMapping("/getUserLeaveInfo")
+    McR getUserLeaveInfo(@RequestBody Map map) {
+        String userId = map.get("userId").toString();
+        Map result = kyntService.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]更新员工年假余额 -----");
+            kyntService.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);
+    }
+
+
+}

+ 161 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/controller/KYYTController.java

@@ -0,0 +1,161 @@
+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.KYYTService;
+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("/yt")
+public class KYYTController extends DDCallbackController {
+    @Autowired
+    private KYYTService kyytService;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_yt.token}")
+    private String token;
+
+    @Value("${dingtalk_yt.aesKey}")
+    private String aesKey;
+
+    @Value("${dingtalk_yt.appKey}")
+    private String appKey;
+
+    @PostMapping("test")
+    McR test() {
+
+        log.info("11111");
+        return McR.success();
+    }
+
+    //获取在职员工userId列表
+    @GetMapping("/getEmployeeUserId")
+    McR getEmployeeUserId() {
+
+        List<String> result = kyytService.getEmployeeUserId();
+
+        return McR.success(result);
+    }
+
+    //获取员工花名册信息
+    @PostMapping("/getEmployeeRosterInfo")
+    McR getEmployeeRosterInfo(@RequestBody Map<String, Object> map) {
+        List<Map> result = kyytService.getEmployeeRosterInfo(map);
+
+        return McR.success(result);
+    }
+
+    //计算并设置员工年假数
+    @PostMapping("/getEmployeeAnnualLeaveNum")
+    McR getEmployeeAnnualLeaveNum(@RequestBody Map<String, Object> map) {
+        return kyytService.getEmployeeAnnualLeaveNum(map);
+    }
+
+    //获取员工真实假期余额
+    @PostMapping("/getUserLeaveInfo")
+    McR getUserLeaveInfo(@RequestBody Map map) {
+        String userId = map.get("userId").toString();
+        Map result = kyytService.getUserLeaveInfo(userId);
+        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 kyytService.updateEmployeeAnnualLeaveNum();
+    }
+
+
+    //保存10s内已处理的回调事件
+    private Map<String, Long> eventList = new HashMap<>();
+
+    //钉钉事件回调
+    @SneakyThrows
+    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]更新员工年假余额 -----");
+                kyytService.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);
+    }
+
+}

+ 25 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/entity/AdvancedLeave.java

@@ -0,0 +1,25 @@
+package com.malk.kaiyue.entity;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+@TableName(value = "kaiyue_cd")
+@Data
+public class AdvancedLeave {
+    @TableId(value = "id", type = IdType.AUTO)
+    private int id;
+
+    //员工id
+    private String userId;
+
+    //预支假期余额
+    private int leaveNum;
+
+    //预支假期年份
+    private String year;
+
+    //有效位 1:正常  0:删除
+    private String validFlag;
+
+}

+ 10 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/mapper/AdvancedLeaveMapper.java

@@ -0,0 +1,10 @@
+package com.malk.kaiyue.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.malk.kaiyue.entity.AdvancedLeave;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface AdvancedLeaveMapper extends BaseMapper<AdvancedLeave> {
+
+}

+ 39 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/KYCDService.java

@@ -0,0 +1,39 @@
+package com.malk.kaiyue.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.malk.kaiyue.entity.AdvancedLeave;
+import com.malk.server.common.McR;
+import org.springframework.scheduling.annotation.Async;
+
+import java.util.List;
+import java.util.Map;
+
+
+public interface KYCDService extends IService<AdvancedLeave> {
+    List<String> getEmployeeUserId();
+
+    List<Map> getEmployeeRosterInfo(Map<String, Object> map);
+
+    @Async
+    McR getEmployeeAnnualLeaveNum(Map<String, Object> map);
+
+    /**
+     * 每年1月1日 00:00定时更新员工旧职级
+     * @return
+     */
+//    McR updateEmployeeOldPositionLevel();
+
+    /**
+     * 每年1月1日 01:00定时更新员工年假数
+     * @return
+     */
+    McR updateEmployeeAnnualLeaveNum();
+
+    /**
+     * 获取用户真实假期余额
+     * @param userId
+     * @return
+     */
+    Map getUserLeaveInfo(String userId);
+
+}

+ 38 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/KYNTService.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 KYNTService {
+    /**
+     * 获取员工花名册信息
+     * @param map
+     * @return
+     */
+    List<Map> getEmployeeRosterInfo(Map<String, Object> map);
+
+    /**
+     * 获取在职员工userId列表
+     * @return
+     */
+    List<String> getEmployeeUserId();
+
+    /**
+     * 计算并设置获取员工年假数
+     * @param map
+     * @return
+     */
+    @Async
+    McR getEmployeeAnnualLeaveNum(Map<String, Object> map);
+
+    /**
+     * 每年1月1日 01:00定时更新员工年假数
+     * @return
+     */
+    McR updateEmployeeAnnualLeaveNum();
+
+    Map getUserLeaveInfo(String userId);
+}

+ 40 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/KYYTService.java

@@ -0,0 +1,40 @@
+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 KYYTService {
+
+    /**
+     * 获取员工花名册信息
+     * @param map
+     * @return
+     */
+    List<Map> getEmployeeRosterInfo(Map<String, Object> map);
+
+    /**
+     * 获取在职员工userId列表
+     * @return
+     */
+    List<String> getEmployeeUserId();
+
+    /**
+     * 计算并设置获取员工年假数
+     * @param map
+     * @return
+     */
+    @Async
+    McR getEmployeeAnnualLeaveNum(Map<String, Object> map);
+
+    /**
+     * 每年1月1日 01:00定时更新员工年假数
+     * @return
+     */
+    McR updateEmployeeAnnualLeaveNum();
+
+    Map getUserLeaveInfo(String userId);
+}

+ 516 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/impl/KYCDServiceImpl.java

@@ -0,0 +1,516 @@
+package com.malk.kaiyue.service.impl;
+
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.NumberUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.malk.kaiyue.entity.AdvancedLeave;
+import com.malk.kaiyue.mapper.AdvancedLeaveMapper;
+import com.malk.kaiyue.service.KYCDService;
+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 KYCDServiceImpl extends ServiceImpl<AdvancedLeaveMapper, AdvancedLeave> implements KYCDService {
+    @Autowired
+    private DDClient ddClient;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Autowired
+    private AdvancedLeaveMapper advancedLeaveMapper;
+
+    @Value("${dingtalk_cd.appKey}")
+    private String appKey;
+
+    @Value("${dingtalk_cd.appSecret}")
+    private String appSecret;
+
+    @Value("${dingtalk_cd.agentId}")
+    private String agentId;
+
+    @Value("${dingtalk_cd.operator}")
+    private String opUserId;
+
+    //成都凯悦-年假(测试)
+    private static final String LEAVE_CODE = "609a84ed-54d4-4ecd-a44f-4c55b04c37ea";
+    //体验社-成都年假测试
+//    private static final String LEAVE_CODE = "609a84ed-54d4-4ecd-a44f-4c55b04c37ea";
+
+    @Override
+    public List<String> getEmployeeUserId() {
+        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集合
+        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;
+    }
+
+    @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;
+    }
+
+    //保存10s内已处理的更新假期余额事件
+    private Map<String, Long> bodyList = new HashMap<>();
+
+    @Async
+    public McR 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,sys00-confirmJoinTime,b433c687-c3b3-4f97-8498-d23944f3316b,80292628-1c88-4c25-9c40-3e91283552e7");
+        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 confirmJoinTime = "";
+                //职级
+                String positionLevel = "";
+                //姓名
+                String name = "";
+                //原职级
+                String oldPositionLevel = "";
+                //升职日期
+                String promotionTime = "";
+
+                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 "sys00-confirmJoinTime": confirmJoinTime = value;break;
+                            case "sys01-positionLevel": positionLevel = value;break;
+                            case "sys00-name": name = value;break;
+                            case "b433c687-c3b3-4f97-8498-d23944f3316b": oldPositionLevel = value;break;//成都原职级
+//                            case "7482b192-9f1d-49fa-adab-6f33f7d0951e": oldPositionLevel = value;break;//高级假期原职级
+                            case "80292628-1c88-4c25-9c40-3e91283552e7": promotionTime = value;break;//升职日期
+                            default:break;
+                        }
+                    }
+                }
+                //若没有原职级 则默认原职级是现职级
+                if ("".equals(oldPositionLevel)){
+                    oldPositionLevel = positionLevel;
+                }
+                //若没有升职日期 则默认升职日期是入职日期
+                if ("".equals(promotionTime)){
+                    promotionTime = confirmJoinTime;
+                }
+
+                if ("".equals(confirmJoinTime) || "".equals(positionLevel) || "".equals(name) || "".equals(oldPositionLevel)){
+                    log.info("更新员工年假余额:参数缺啦!");
+                    return McR.errorParam("参数缺啦!");
+                }
+
+
+                //假期有效开始日期为当年1月1日
+                DateTime beginDate = DateUtil.beginOfYear(new Date());
+                //假期有效截至日期为当年12月31日
+                DateTime endDate = DateUtil.endOfYear(new Date());
+                //今天
+                DateTime today = DateUtil.date();
+                //当年天数
+                int yearDays = DateUtil.dayOfYear(endDate);
+                //年假
+                double yearLeave = 0.0;
+                //预支年假
+                double advanceLeave = 0.0;
+
+
+                //获取原职级年假基数
+                int oldPositionLevelBaseNum = getAnnualLeaveBaseNum(oldPositionLevel);
+                //获取现职级年假基数
+                int positionLevelBaseNum = getAnnualLeaveBaseNum(positionLevel);
+
+                //获取入职日期的月日
+                String confirmJoinMonthAndDay = confirmJoinTime.substring(5);
+                String year = DateUtil.year(new Date()) + "";
+                //今年入职周年日
+                DateTime anniversaryOfEmployment = DateUtil.parse(year + "-" + confirmJoinMonthAndDay);
+
+                //计算今天是入职第几周年
+                long anniversary = DateUtil.betweenYear(DateUtil.parse(confirmJoinTime), anniversaryOfEmployment, true) - 1;
+                //判断员工是否当年新入职
+                if (DateUtil.year(DateUtil.parse(confirmJoinTime)) == DateUtil.year(new Date())){
+                    //分两段计算:入职日到升职日,升职日到年底
+                    long day1 = DateUtil.betweenDay(DateUtil.parse(confirmJoinTime), DateUtil.parse(promotionTime), true);
+                    long day2 = DateUtil.betweenDay(DateUtil.parse(promotionTime), endDate, true);
+                    yearLeave = (double) (day1 * oldPositionLevelBaseNum + day2 * positionLevelBaseNum ) / yearDays;
+                }else {
+                    //升职日期在今年入职周年日之前
+                    if (DateUtil.compare(anniversaryOfEmployment, DateUtil.parse(promotionTime)) >= 0){
+                        //升职日期在今年
+                        if (DateUtil.compare(DateUtil.parse(promotionTime),today) >= 0){
+                            //获取年初到升职日的天数
+                            int day1 = DateUtil.dayOfYear(DateUtil.parse(promotionTime));
+                            //获升职值日到入职周年日的天数
+                            long day2 = DateUtil.betweenDay(DateUtil.parse(promotionTime), anniversaryOfEmployment, true);
+                            //获取入职周年日到年底的天数
+                            long day3 = DateUtil.betweenDay(anniversaryOfEmployment, endDate, true);
+                            //分三段计算:年初-升职日,升职日到入职周年日,入职周年日到年底
+                            yearLeave = (double) (day1 * (oldPositionLevelBaseNum + (anniversary > 4 ? 4 : anniversary)) + day2 * (positionLevelBaseNum + (anniversary > 4 ? 4 : anniversary)) + day3 * (positionLevelBaseNum + (anniversary + 1 > 4 ? 4 : anniversary + 1))) / yearDays ;
+                        }else{
+                            //获取年初到入职周年日的天数
+                            int day1 = DateUtil.dayOfYear(anniversaryOfEmployment);
+                            //获取入职周年日到年底的天数
+                            long day2 = DateUtil.betweenDay(anniversaryOfEmployment, endDate, true);
+                            //分两段计算:年初到入职周年日,入职周年日到年底
+                            yearLeave = (double) (day1 * (positionLevelBaseNum + (anniversary > 4 ? 4 : anniversary)) + day2 * (positionLevelBaseNum + (anniversary + 1 > 4 ? 4 : anniversary + 1))) / yearDays ;
+                        }
+                    }else {
+                        //升职日在今年入职周年日之后
+                        //获取年初到入职周年日的天数
+                        int day1 = DateUtil.dayOfYear(anniversaryOfEmployment);
+                        //获取入职周年日到升职日的天数
+                        long day2 = DateUtil.betweenDay(anniversaryOfEmployment, DateUtil.parse(promotionTime), true);
+                        //获取升职日到年底的天数
+                        long day3 = DateUtil.betweenDay(DateUtil.parse(promotionTime), endDate, true);
+                        //分三段计算:年初-入职周年日,入职周年日到升职日,升职日到年底
+                        yearLeave = (double) (day1 * (oldPositionLevelBaseNum + (anniversary > 4 ? 4 : anniversary)) + day2 * (oldPositionLevelBaseNum + (anniversary + 1 > 4 ? 4 : anniversary + 1)) + day3 * (positionLevelBaseNum + (anniversary + 1 > 4 ? 4 : anniversary + 1))) / 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;
+                }*/
+                yearLeave = Math.round(yearLeave * 100.0) / 100.0;
+
+                //查询出用户消费年假记录 当返回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 (!"接口测试修改".equals(use.get("leaveReason").toString()) && !"期初假期发放".equals(use.get("leaveReason").toString())){
+                                //若是请假消耗或管理员手动减少
+                                if (!use.containsKey("calType") || Objects.isNull(use.get("calType")) || "delete".equals(use.get("calType").toString())){
+                                    useLeaveNum += (int) use.get("recordNumPerDay") / 100;
+                                }
+                                //注:若是管理员手动增加 则假期余额会多出一个BCXsunNm记录增加的假期天数  最终会在设置的假期余额的基础上加上这些天数
+                                //故此处手动新增的假期余额不做处理
+                            }
+                        }
+                    }
+                }
+
+                LambdaQueryWrapper<AdvancedLeave> advancedLeaveLambdaQueryWrapper = new LambdaQueryWrapper<>();;
+                advancedLeaveLambdaQueryWrapper.eq(AdvancedLeave::getUserId,userId)
+                        .eq(AdvancedLeave::getValidFlag,"1");
+                AdvancedLeave advancedLeave = advancedLeaveMapper.selectOne(advancedLeaveLambdaQueryWrapper);
+                if (Objects.nonNull(advancedLeave)){
+                    advanceLeave = advancedLeave.getLeaveNum();
+                }else {
+                    advanceLeave = NumberUtil.sub(yearLeave - useLeaveNum).doubleValue() >= 8 ? 8 : NumberUtil.sub(yearLeave - useLeaveNum).intValue();
+                    advancedLeave.setLeaveNum(Integer.parseInt(advanceLeave+""));
+                    advancedLeave.setUserId(userId);
+                    advancedLeave.setYear(String.valueOf(DateUtil.year(new Date())));
+                    advancedLeaveMapper.insert(advancedLeave);
+                }
+
+                //年假总数
+                int realYearLeave =  NumberUtil.sub(yearLeave, useLeaveNum, -advanceLeave).doubleValue() < 0 ? 0 : NumberUtil.sub(yearLeave, useLeaveNum, -advanceLeave).multiply(new BigDecimal(100)).intValue();
+
+                //获取员工原年假余额 取较大值
+                Map balanceMap = new HashMap();
+                balanceMap.put("leave_code",LEAVE_CODE);
+                balanceMap.put("op_userid",opUserId);
+                balanceMap.put("userids",userId);
+                balanceMap.put("offset",0);
+                balanceMap.put("size",10);
+                DDR balanceDdr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/list", null, DDConf.initTokenParams(access_token), balanceMap, DDR.class);
+                Map balanceResult = (Map) balanceDdr.getResult();
+                List<Map> leaveQuotas = (List<Map>) balanceResult.get("leave_quotas");
+                if (Objects.nonNull(leaveQuotas) && !leaveQuotas.isEmpty()){
+                    for (Map leaveQuota : leaveQuotas) {
+                        if (year.equals(leaveQuota.get("quota_cycle"))){
+                            int balance = (int) leaveQuota.get("quota_num_per_day");
+                            if (balance > realYearLeave){
+                                realYearLeave = balance;
+                            }
+                        }
+                    }
+                }
+
+
+                //更新假期余额接口的body
+                DateTime deadline = DateUtil.parse((DateUtil.year(new Date()) + 1) + "-03-31");
+                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",deadline.getTime());
+                //操作原因
+                leave_quotas.put("reason","接口测试修改");
+                //以天计算的额度总数 假期类型按天计算时,该值不为空且按百分之一天折算。 例如:1000=10天。
+                leave_quotas.put("quota_num_per_day",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 = realYearLeave + userId;
+
+                // 检查更新事件是否已经处理过,如果是,则忽略该更新
+                if (isUpdateLeave(bodyStr)) {
+                    log.info("更新事件已处理,忽略该回调...");
+                    return null;
+                }
+
+                // 将更新和当前时间戳添加到已处理集合中
+                long currentTime = System.currentTimeMillis();
+                bodyList.put(bodyStr, currentTime);
+
+                result.add("姓名:"+name+",职级:" + positionLevel+",原职级:" + oldPositionLevel+ ",年假基数:" + yearLeave + "天,入职日期:" + confirmJoinTime + ",升职日期:" + promotionTime + ",开始日期:" + beginDate+",截止日期:" + deadline);
+
+                //更新假期余额
+                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);
+    }
+
+    @Override
+    public McR updateEmployeeAnnualLeaveNum() {
+        //获取员工userId集合
+        List<String> userIdList = getEmployeeUserId();
+        //遍历集合给所有员工更新年假余额
+        if (Objects.nonNull(userIdList) && !userIdList.isEmpty()){
+            for (String userId : userIdList) {
+                Map map = new HashMap();
+                map.put("userid_list",userId);
+                getEmployeeAnnualLeaveNum(map);
+                //线程等待10s
+                try {
+                    log.info("线程睡眠10s");
+                    Thread.sleep(10000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return McR.success();
+    }
+
+    @Override
+    public Map getUserLeaveInfo(String userId) {
+
+        /*LambdaQueryWrapper<AdvancedLeave> advancedLeaveLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        advancedLeaveLambdaQueryWrapper.eq(AdvancedLeave::getUserId,userId)
+                .eq(AdvancedLeave::getYear,String.valueOf(DateUtil.year(new Date())))
+                .eq(AdvancedLeave::getValidFlag,"1");
+        AdvancedLeave advancedLeave = advancedLeaveMapper.selectOne(advancedLeaveLambdaQueryWrapper);
+*/
+        long currentTime = System.currentTimeMillis();
+
+        Map leaveMap = new HashMap();
+
+        //年假余额
+        BigDecimal annualLeaveNum = new BigDecimal(0.00);
+
+        //查询员工年假余额
+        List<Map> leaveQuotasList = new ArrayList<>();
+        getLeaveNum("f7e0de92-806f-457e-9aa1-c20b245d741e",userId,0,50,leaveQuotasList);
+
+        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_hour")) && Objects.nonNull(leaveQuotas.get("quota_num_per_day"))){
+                        annualLeaveNum = annualLeaveNum.add(new BigDecimal(String.valueOf(leaveQuotas.get("quota_num_per_day"))).divide(new BigDecimal(100)));
+                   }
+                    if (Objects.nonNull(leaveQuotas.get("used_num_per_day"))){
+                        annualLeaveNum = annualLeaveNum.subtract(new BigDecimal(String.valueOf(leaveQuotas.get("used_num_per_day"))).divide(new BigDecimal(100)));
+                   }
+                }
+            }
+        }
+
+        if (annualLeaveNum.compareTo(new BigDecimal(8)) == 1 || annualLeaveNum.compareTo(new BigDecimal(8)) == 0){
+            annualLeaveNum = annualLeaveNum.subtract(new BigDecimal(8));
+            leaveMap.put("可预支年假",8);
+            leaveMap.put("实际年假余额",annualLeaveNum);
+            leaveMap.put("已预支年假",0);
+        }else {
+            BigDecimal relYearLeave = annualLeaveNum.remainder(new BigDecimal(0.5));
+            leaveMap.put("可预支年假",annualLeaveNum.subtract(relYearLeave));
+            leaveMap.put("实际年假余额",annualLeaveNum.subtract(new BigDecimal(8)));
+            leaveMap.put("已预支年假",new BigDecimal(8).subtract(annualLeaveNum).add(relYearLeave));
+        }
+
+        //获取员工调休假余额
+        List<Map> leaveQuotasList2 = new ArrayList<>();
+        getLeaveNum("c7beb5e9-73ee-45f8-ae66-dfdb2a5ad0b8",userId,0,10,leaveQuotasList2);
+
+        //调休假余额
+        int compensatoryLeaveNum = 0;
+        if (Objects.nonNull(leaveQuotasList2)){
+            for (Map leaveQuotas : leaveQuotasList2) {
+                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 += ((int) leaveQuotas.get("quota_num_per_hour")) / 100;
+                    }
+                    if (Objects.nonNull(leaveQuotas.get("used_num_per_hour"))){
+                        compensatoryLeaveNum -= ((int) leaveQuotas.get("used_num_per_hour")) / 100;
+                    }
+                }
+            }
+        }
+        if (compensatoryLeaveNum >= 80){
+            leaveMap.put("可预支调休",80);
+            leaveMap.put("实际加班小时数",compensatoryLeaveNum-80);
+            leaveMap.put("已预支调休",0);
+        }else {
+            leaveMap.put("可预支调休",compensatoryLeaveNum);
+            leaveMap.put("实际加班小时数",0);
+            leaveMap.put("已预支调休",80-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;
+    }
+
+    private int getAnnualLeaveBaseNum(String positionLevel) {
+        int annualLeave = 0;
+        if (positionLevel.equals("MGR") || positionLevel.equals("DH") || positionLevel.equals("副主任医师")){
+            annualLeave = 12;
+        }else if (positionLevel.equals("LH") || positionLevel.equals("技术专员")){
+            annualLeave = 10;
+        }else if (positionLevel.equals("HST") || positionLevel.equals("高级管理员")){
+            annualLeave = 8;
+        }else if (positionLevel.equals("T") || positionLevel.equals("技术专家")){
+            annualLeave = 0;
+        }
+        return annualLeave;
+    }
+
+    /**
+     * 检查该更新事件在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);
+    }
+
+}

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

@@ -0,0 +1,512 @@
+package com.malk.kaiyue.service.impl;
+
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import com.malk.kaiyue.service.KYNTService;
+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.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+public class KYNTServiceImpl implements KYNTService {
+    @Autowired
+    private DDClient ddClient;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_nt.appKey}")
+    private String appKey;
+
+    @Value("${dingtalk_nt.appSecret}")
+    private String appSecret;
+
+    @Value("${dingtalk_nt.agentId}")
+    private String agentId;
+
+    @Value("${dingtalk_nt.operator}")
+    private String opUserId;
+
+    //南通凯悦-年假测试3
+    private static final String LEAVE_CODE = "42a0db4f-2929-4c59-8683-67ecacc73b7c";
+    //体验社-年假测试wzy
+//    private static final String LEAVE_CODE = "f9240c02-8fe7-4535-af2c-ca6740e1c654";
+
+    @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","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 McR 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,6ef81fb9-e178-4395-8a7f-6f33b3263bb2,52b25106-d588-43d7-a048-7661e3f90189,sys00-confirmJoinTime,sys02-joinWorkingTime,sys05-nowContractStartTime,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 oldPositionLevel = "";
+                //升职日期
+                String promotionTime = "";
+                //姓名
+                String name = "";
+                //现合同开始日期
+                String owContractStartTime = "";
+                //合同续签次数
+                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 "6ef81fb9-e178-4395-8a7f-6f33b3263bb2": oldPositionLevel = fieldValueList.get(0).get("label").toString();break;
+                            case "52b25106-d588-43d7-a048-7661e3f90189": promotionTime = value;break;
+                            case "sys00-name": name = value;break;
+                            case "sys05-nowContractStartTime": owContractStartTime = value;break;
+                            case "sys05-contractRenewCount": contractRenewCount = Integer.valueOf(value);break;
+                            default:break;
+                        }
+                    }else {
+                        log.info("更新员工年假余额:参数缺啦!");
+                        return McR.errorParam("参数缺啦!");
+                    }
+
+                }
+                //若没有原职级 则默认原职级是现职级
+                if ("".equals(oldPositionLevel)){
+                    oldPositionLevel = positionLevel;
+                }
+                //若没有升职日期 则默认当天是升职日期
+                if ("".equals(promotionTime)){
+                    promotionTime = DateUtil.today();
+                }
+                //若没有现合同开始日期
+                if ("".equals(owContractStartTime)){
+                    //若合同续签次数为0 则默认合同开始日期为入职日期
+                    if (contractRenewCount == 0){
+                        owContractStartTime = confirmJoinTime;
+                    }else {
+                        //若合同续签次数不为0 则默认合同开始日期为当天
+                        owContractStartTime = DateUtil.today();
+                    }
+
+                }
+                //旧合同续签次数等于合同续签数-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 --;
+                }
+
+                System.out.println("截至今年1月1日,工龄为:"+workAge + "年");
+                //年假数
+                double yearLeave = getLeaveNum(confirmJoinTime,beginDate,endDate,promotionTime,owContractStartTime,oldPositionLevel,positionLevel,workAge,oldContractRenewCount,contractRenewCount,yearDays);
+
+                //年假小数
+                double yearLeaveDecimalPart = yearLeave - (int) yearLeave;
+
+                result.add("姓名:"+name+",职级:"+positionLevel+",工龄:"+workAge+"年,合同续签数"+contractRenewCount+",年假数:" + yearLeave + "天"+",截止日期:"+endDate);
+
+                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.isNull(use.get("calType")) || "delete".equals(use.get("calType").toString())){
+                                        useLeaveNum += (int) use.get("recordNumPerDay") / 100;
+                                    }
+                                    //注:若是管理员手动增加 则假期余额会多出一个BCXsunNm记录增加的假期天数  最终会在设置的假期余额的基础上加上这些天数
+                                    //故此处手动新增的假期余额不做处理
+                                }
+                            }
+                        }
+                    }
+                }
+
+                //实际年假数
+                double realYearLeave = (yearLeave - useLeaveNum) < 0 ? 0 : (yearLeave - 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 * 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",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);
+            }
+        }
+        log.info(result.stream().collect(Collectors.joining(",")));
+        return McR.success(result);
+    }
+
+    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();
+        //遍历集合给所有员工更新年假余额
+        if (Objects.nonNull(userIdList) && !userIdList.isEmpty()){
+            for (String userId : userIdList) {
+                Map map = new HashMap();
+                map.put("userid_list",userId);
+                getEmployeeAnnualLeaveNum(map);
+                //线程等待10s
+                try {
+                    log.info("线程睡眠10s");
+                    Thread.sleep(10000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return McR.success();
+    }
+
+    @Override
+    public Map getUserLeaveInfo(String userId) {
+        long currentTime = System.currentTimeMillis();
+
+        Map leaveMap = new HashMap();
+
+        //获取员工调休假余额
+        List<Map> leaveQuotasList = new ArrayList<>();
+        getLeaveNum("8a10b574-4274-47bc-9bad-1df1c4308f60",userId,0,50,leaveQuotasList);
+
+        //查询员工调休假余额
+        int compensatoryLeaveNum = 0;
+        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 += ((int) leaveQuotas.get("quota_num_per_hour")) / 100;
+                    }
+                    if (Objects.nonNull(leaveQuotas.get("used_num_per_hour"))){
+                        compensatoryLeaveNum -= ((int) leaveQuotas.get("used_num_per_hour")) / 100;
+                    }
+                }
+            }
+        }
+
+
+        /*Map body2 = new HashMap();
+        body2.put("leave_code","8a10b574-4274-47bc-9bad-1df1c4308f60");//调休假
+        body2.put("op_userid",ddConf.getOperator());
+        body2.put("userids",userId);
+        body2.put("offset",0);
+        body2.put("size",10);
+        DDR_New ddrNew2 = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/list", null, ddClient.initTokenParams(), body2, DDR_New.class);
+        Map result2 = (Map)ddrNew2.getResult();
+        int leaveNum = 0;
+        if (Objects.nonNull(result2.get("leave_quotas"))){
+            for (Map leaveQuotas : (List<Map>) result2.get("leave_quotas")) {
+                if (Objects.isNull(leaveQuotas.get("quota_num_per_day")) && Objects.nonNull(leaveQuotas.get("quota_num_per_hour"))){
+                    leaveNum += ((int) leaveQuotas.get("quota_num_per_hour")) / 100;
+                }
+            }
+
+        }*/
+        if (compensatoryLeaveNum >= 80){
+            leaveMap.put("可预支调休",80);
+            leaveMap.put("实际加班小时数",compensatoryLeaveNum-80);
+            leaveMap.put("已预支调休",0);
+        }else {
+            leaveMap.put("可预支调休",compensatoryLeaveNum);
+            leaveMap.put("实际加班小时数",0);
+            leaveMap.put("已预支调休",80-compensatoryLeaveNum);
+        }
+        /*leaveMap.put("可预支调休",leaveNum);
+        leaveMap.put("实际加班小时数",Math.max(0,leaveNum-80));
+        leaveMap.put("已预支调休",Math.max(0,80-leaveNum));*/
+
+        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);
+    }
+
+}

+ 477 - 0
mjava-kaiyue/src/main/java/com/malk/kaiyue/service/impl/KYYTServiceImpl.java

@@ -0,0 +1,477 @@
+package com.malk.kaiyue.service.impl;
+
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import com.malk.kaiyue.service.KYYTService;
+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 KYYTServiceImpl implements KYYTService {
+    @Autowired
+    private DDClient ddClient;
+
+    @Autowired
+    private DDConf ddConf;
+
+    @Value("${dingtalk_yt.appKey}")
+    private String appKey;
+
+    @Value("${dingtalk_yt.appSecret}")
+    private String appSecret;
+
+    @Value("${dingtalk_yt.agentId}")
+    private String agentId;
+
+    @Value("${dingtalk_yt.operator}")
+    private String opUserId;
+
+
+    //成都凯悦-年假(测试)
+    private static final String LEAVE_CODE = "28abb7bf-e1b6-4387-9e2a-e1b2ae983e7a";
+    //体验社-年假测试wzy
+//    private static final String LEAVE_CODE = "f9240c02-8fe7-4535-af2c-ca6740e1c654";
+
+    @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","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 McR 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,2a8b3a12-5102-4e60-b5d2-5f3bca0f0b7c,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 oldPositionLevel = "";
+                //姓名
+                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 "2a8b3a12-5102-4e60-b5d2-5f3bca0f0b7c": oldPositionLevel = value;break;
+                            case "sys00-name": name = value;break;
+                            case "sys05-contractRenewCount": contractRenewCount = Integer.valueOf(value);break;
+                            default:break;
+                        }
+                    }else {
+                        log.info("更新员工年假余额:参数缺啦!");
+                        return McR.errorParam("参数缺啦!");
+                    }
+
+                }
+                //若没有原职级 则默认原职级是现职级
+                if ("".equals(oldPositionLevel)){
+                    oldPositionLevel = positionLevel;
+                }
+
+                //假期有效开始日期为当年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 --;
+                }
+
+                System.out.println("截至今年1月1日,工龄为:"+workAge + "年");
+                //年假基数
+                double yearLeave = 0.0;
+                //预支年假
+                double advanceLeave = 8.0;
+
+                int yearLeaveBaseNum = getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount);
+                int oldYearLeaveBaseNum = getAnnualLeaveBaseNum(oldPositionLevel,workAge,contractRenewCount);
+                long day1 = 0;
+                long day2 = 0;
+
+                //判断员工是否当年新入职
+                if (DateUtil.year(DateUtil.parse(confirmJoinTime)) == DateUtil.year(new Date())){
+                    day1 = DateUtil.betweenDay(DateUtil.parse(confirmJoinTime),new Date(),true);
+                    day2 = DateUtil.betweenDay(new Date(),endDate,true);
+                }else {
+                    day1 = DateUtil.betweenDay(beginDate,new Date(),true);
+                    day2 = DateUtil.betweenDay(new Date(),endDate,true);
+                }
+                yearLeave = (double) (day1 * oldYearLeaveBaseNum + day2 * yearLeaveBaseNum) / yearDays;
+                double yearLeaveDecimalPart = yearLeave - (int) yearLeave;
+
+                result.add("姓名:"+name+",职级:"+positionLevel+",工龄:"+workAge+"年,合同续签数"+contractRenewCount+",年假数:" + yearLeave + "天"+",截止日期:"+endDate);
+
+                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 (!"接口测试修改".equals(use.get("leaveReason").toString()) && !"期初假期发放".equals(use.get("leaveReason").toString())){
+                                //若是请假消耗或管理员手动减少
+                                if (!use.containsKey("calType") || Objects.isNull(use.get("calType")) || "delete".equals(use.get("calType").toString())){
+                                    useLeaveNum += (int) use.get("recordNumPerDay") / 100;
+                                }
+                                //注:若是管理员手动增加 则假期余额会多出一个BCXsunNm记录增加的假期天数  最终会在设置的假期余额的基础上加上这些天数
+                                //故此处手动新增的假期余额不做处理
+                            }
+                        }else if (DateUtil.year(gmtCreate) == (DateUtil.year(new Date()) - 1)){
+                            //获取去年预支年假使用情况
+                            if ("预支年假".equals(use.get("leaveReason").toString())){
+                                //若去年预支过则在今年预支年假数中相应减少
+                                if (!use.containsKey("calType") || Objects.isNull(use.get("calType")) || "delete".equals(use.get("calType").toString())){
+                                    advanceLeave -= (int) use.get("recordNumPerDay") / 100;
+                                }
+                            }
+                        }
+                    }
+                }
+
+                //实际年假数
+                double realYearLeave = (yearLeave - useLeaveNum + advanceLeave) < 0 ? 0 : (yearLeave - useLeaveNum + advanceLeave);
+
+                //更新假期余额接口的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 * 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",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);
+            }
+        }
+        log.info(result.stream().collect(Collectors.joining(",")));
+        return McR.success(result);
+    }
+
+    private int getAnnualLeaveBaseNum(String positionLevel, int workAge, int contractRenewCount) {
+        //法定年假
+        int legalAnnualLeave = 0;
+        //福利年假
+        int welfareAnnualLeave = 0;
+        //根据职级、工龄和合同续签数计算年假基数
+        if (positionLevel.equals("5") || 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("4") || positionLevel.equals("3") || 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("2") || 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("1") || 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();
+        //遍历集合给所有员工更新年假余额
+        if (Objects.nonNull(userIdList) && !userIdList.isEmpty()){
+            for (String userId : userIdList) {
+                Map map = new HashMap();
+                map.put("userid_list",userId);
+                getEmployeeAnnualLeaveNum(map);
+                //线程等待10s
+                try {
+                    log.info("线程睡眠10s");
+                    Thread.sleep(10000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return McR.success();
+    }
+
+    @Override
+    public Map getUserLeaveInfo(String userId) {
+        long currentTime = System.currentTimeMillis();
+
+        Map leaveMap = new HashMap();
+
+        //年假余额
+        BigDecimal annualLeaveNum = new BigDecimal(0.00);
+
+        //查询员工年假余额
+        List<Map> leaveQuotasList = new ArrayList<>();
+        getLeaveNum("3dcd4b8c-1b88-4206-9b52-cc9e2fde2e38",userId,0,50,leaveQuotasList);
+
+        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_hour")) && Objects.nonNull(leaveQuotas.get("quota_num_per_day"))){
+                        annualLeaveNum = annualLeaveNum.add(new BigDecimal(String.valueOf(leaveQuotas.get("quota_num_per_day"))).divide(new BigDecimal(100)));
+                    }
+                    if (Objects.nonNull(leaveQuotas.get("used_num_per_day"))){
+                        annualLeaveNum = annualLeaveNum.subtract(new BigDecimal(String.valueOf(leaveQuotas.get("used_num_per_day"))).divide(new BigDecimal(100)));
+                    }
+                }
+            }
+        }
+
+        if (annualLeaveNum.compareTo(new BigDecimal(8)) == 1 || annualLeaveNum.compareTo(new BigDecimal(8)) == 0){
+            annualLeaveNum = annualLeaveNum.subtract(new BigDecimal(8));
+            leaveMap.put("可预支年假",8);
+            leaveMap.put("实际年假余额",annualLeaveNum);
+            leaveMap.put("已预支年假",0);
+        }else {
+            BigDecimal relYearLeave = annualLeaveNum.remainder(new BigDecimal(0.5));
+            leaveMap.put("可预支年假",annualLeaveNum.subtract(relYearLeave));
+            leaveMap.put("实际年假余额",annualLeaveNum.subtract(new BigDecimal(8)));
+            leaveMap.put("已预支年假",new BigDecimal(8).subtract(annualLeaveNum).add(relYearLeave));
+        }
+
+        //获取员工调休假余额
+        List<Map> leaveQuotasList2 = new ArrayList<>();
+        getLeaveNum("b6301b0e-193a-4bce-b225-2f67a5d6244b",userId,0,10,leaveQuotasList2);
+
+        //调休假余额
+        int compensatoryLeaveNum = 0;
+        if (Objects.nonNull(leaveQuotasList2)){
+            for (Map leaveQuotas : leaveQuotasList2) {
+                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 += ((int) leaveQuotas.get("quota_num_per_hour")) / 100;
+                    }
+                    if (Objects.nonNull(leaveQuotas.get("used_num_per_hour"))){
+                        compensatoryLeaveNum -= ((int) leaveQuotas.get("used_num_per_hour")) / 100;
+                    }
+                }
+            }
+        }
+        if (compensatoryLeaveNum >= 80){
+            leaveMap.put("可预支调休",80);
+            leaveMap.put("实际加班小时数",compensatoryLeaveNum-80);
+            leaveMap.put("已预支调休",0);
+        }else {
+            leaveMap.put("可预支调休",compensatoryLeaveNum);
+            leaveMap.put("实际加班小时数",0);
+            leaveMap.put("已预支调休",80-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);
+    }
+
+}

+ 97 - 0
mjava-kaiyue/src/main/resources/application-dev.yml

@@ -0,0 +1,97 @@
+# 环境配置
+server:
+  port: 9002
+  servlet:
+    context-path: /api/kaiyue
+
+# condition
+spel:
+  scheduling: false        # 定时任务是否执行
+  multiSource: false       # 是否多数据源配置
+
+spring:
+  # database
+  datasource:
+    hikari:
+      connection-init-sql: SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci           # SqlServer, Oracle 无需设置类型
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    username: root
+    password: cp-root@2022++
+    url: jdbc:mysql://47.97.181.40:3306/dingtalk?serverTimezone=GMT%2B8
+    # 主库
+    primary:
+      username: root
+      password: cp-root@2022++
+      url: jdbc:mysql://47.97.181.40:3306/dingtalk?serverTimezone=GMT%2B8
+    # 从库
+    slave:
+      username: root
+      password: cp-root@2022++
+      url: jdbc:mysql://47.97.181.40:3306/dingtalk?serverTimezone=GMT%2B8
+  jpa:
+    hibernate:
+      ddl-auto: none      # JPA对表没有任何操作
+    show-sql: true
+    database: MYSQL
+    database-platform: org.hibernate.dialect.MySQL57Dialect
+
+mybatis-plus:
+  configuration:
+    #开启驼峰命名自动映射
+    map-underscore-to-camel-case: true
+    #开启日志打印
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+  type-aliases-package: com.malk.kaiyue_cd.entity
+  #扫描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
+
+#高级假期
+#agentId: 3078222540
+#appKey: dingtnxmtnun7me39ew0
+#appSecret: 30NLshrf2cbQSqAve5XAqsWmAfWgBghMmdks7g-picxd6Ipi1ZbONDrpK53V4a9p
+#corpId: dingc905d8a60b6a641b24f2f5cc6abecb85
+#aesKey: Uryl5pwvTd3XpAOSAV1z8CmnppzlzijqIqKW5XAJWjR
+#token: XWhjoLDp8ZgHIRiOTvJYk7aR8CEPuJb6J8fUTi7CduGtqaInQTmXxJ85Nmb
+#operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-cd
+dingtalk_cd:
+  agentId: 3047759151
+  appKey: dingl3bjoikl5x6lg3cr
+  appSecret: 68vsttGMji5GBOK81hR-BlYcmF6DROnmnB20LRmCuXES5FvKvXfuhJb6x8DNrf9l
+  corpId: ding3d180832e53201ee35c2f4657eb6378f
+  aesKey: 2z3Xuekm3uPRCc3nLGEISf33OIo0r5pSdB9bLDdT7DL
+  token: 4DRvfMdccRp4xnOdoRpK3vymMqJF7WO
+  operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-nt
+dingtalk_nt:
+  agentId: 3103808497
+  appKey: dingrqlcenh5w4ibo83n
+  appSecret: _gwZ64kLPq1E7_h9nlBQULR5DEC1Ol-kNRE4X8enUFjIHrR5K-l5aM8Ib3mEAq29
+  corpId: ding5bdfeeb522ace222bc961a6cb783455b
+  aesKey: Xj5W56OQx0HyRclconpklFg4Tgk3orTty9gIaxL8Vwr
+  token: UDcVvKtr8qB
+  operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-yt
+dingtalk_yt:
+  agentId: 2999477924
+  appKey: dingzorik72leqm5qgpj
+  appSecret: csxfrSOZy02aXvc4IkqM9dFqz7cEDgUogvJaBIq_rtIbvjZLDKiVkHdVgKeNfoVQ
+  corpId: ding43bb7be8e7bdc63224f2f5cc6abecb85
+  aesKey: 7txhFmSyWIXIrEvwlNfcuMfOQe19K6hqCdIaXMHLO2K
+  token: 24VR2Bnu
+  operator: "02421908021891243060"   # OA管理员账号 [0开头需要转一下字符串]

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

@@ -0,0 +1,54 @@
+# 环境配置
+server:
+  port: 8113
+  servlet:
+    context-path: /api/kaiyue
+
+# condition
+spel:
+  scheduling: true        # 定时任务是否执行
+  multiSource: false      # 是否多数据源配置
+
+spring:
+  # database
+  datasource:
+    hikari:
+      connection-init-sql: SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci           # SqlServer, Oracle 无需设置类型
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    username: root
+    password: mu123
+    url: jdbc:mysql://127.0.0.1:3306/mjava?serverTimezone=Asia/Shanghai&useUnicode=yes&characterEncoding=UTF-8&useSSL=true
+  jpa:
+    database: MYSQL
+    database-platform: org.hibernate.dialect.MySQL57Dialect
+
+# dingtalk-cd
+dingtalk_cd:
+  agentId: 3047759151
+  appKey: dingl3bjoikl5x6lg3cr
+  appSecret: 68vsttGMji5GBOK81hR-BlYcmF6DROnmnB20LRmCuXES5FvKvXfuhJb6x8DNrf9l
+  corpId: ding3d180832e53201ee35c2f4657eb6378f
+  aesKey: 2z3Xuekm3uPRCc3nLGEISf33OIo0r5pSdB9bLDdT7DL
+  token: 4DRvfMdccRp4xnOdoRpK3vymMqJF7WO
+  operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-nt
+dingtalk_nt:
+  agentId: 3103808497
+  appKey: dingrqlcenh5w4ibo83n
+  appSecret: _gwZ64kLPq1E7_h9nlBQULR5DEC1Ol-kNRE4X8enUFjIHrR5K-l5aM8Ib3mEAq29
+  corpId: ding5bdfeeb522ace222bc961a6cb783455b
+  aesKey: Xj5W56OQx0HyRclconpklFg4Tgk3orTty9gIaxL8Vwr
+  token: UDcVvKtr8qB
+  operator: "344749020127590108"   # OA管理员账号 [0开头需要转一下字符串]
+
+# dingtalk-yt
+dingtalk_yt:
+  agentId: 2999477924
+  appKey: dingzorik72leqm5qgpj
+  appSecret: csxfrSOZy02aXvc4IkqM9dFqz7cEDgUogvJaBIq_rtIbvjZLDKiVkHdVgKeNfoVQ
+  corpId: ding43bb7be8e7bdc63224f2f5cc6abecb85
+  aesKey: 7txhFmSyWIXIrEvwlNfcuMfOQe19K6hqCdIaXMHLO2K
+  token: 24VR2Bnu
+  operator: "02421908021891243060"   # OA管理员账号 [0开头需要转一下字符串]
+

+ 3 - 0
mjava-kaiyue/src/main/resources/application.yml

@@ -0,0 +1,3 @@
+spring:
+  profiles:
+    active: prod

+ 8 - 0
mjava-kaiyue/src/main/resources/mapper/AdvancedLeaveMapper.xml

@@ -0,0 +1,8 @@
+<?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="com.malk.kaiyue_cd.mapper.AdvancedLeaveMapper">
+
+
+</mapper>
+

+ 1 - 0
pom.xml

@@ -15,6 +15,7 @@
         <module>mjava-kaiyue_cd</module>
         <module>mjava-zhixingtongde</module>
         <module>mjava-kaiyue_nt</module>
+        <module>mjava-kaiyue</module>
     </modules>
     <packaging>pom</packaging>