KYSXServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package com.malk.kaiyue.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import com.malk.kaiyue.service.KYNTService;
  6. import com.malk.kaiyue.service.KYSXService;
  7. import com.malk.server.common.McR;
  8. import com.malk.server.dingtalk.DDConf;
  9. import com.malk.server.dingtalk.DDR;
  10. import com.malk.server.dingtalk.DDR_New;
  11. import com.malk.service.dingtalk.DDClient;
  12. import com.malk.utils.UtilHttp;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.scheduling.annotation.Async;
  17. import org.springframework.stereotype.Service;
  18. import java.math.BigDecimal;
  19. import java.util.*;
  20. import java.util.concurrent.TimeUnit;
  21. import java.util.stream.Collectors;
  22. @Slf4j
  23. @Service
  24. public class KYSXServiceImpl implements KYSXService {
  25. @Autowired
  26. private DDClient ddClient;
  27. @Autowired
  28. private DDConf ddConf;
  29. @Value("${dingtalk_sx.appKey}")
  30. private String appKey;
  31. @Value("${dingtalk_sx.appSecret}")
  32. private String appSecret;
  33. @Value("${dingtalk_sx.agentId}")
  34. private String agentId;
  35. @Value("${dingtalk_sx.operator}")
  36. private String opUserId;
  37. //绍兴凯悦-年假(测)
  38. // private static final String LEAVE_CODE = "1eecac0a-18b3-4596-b696-ba60d71c8306";
  39. //绍兴凯悦-年假
  40. private static final String LEAVE_CODE = "597d0415-b863-4b16-bc3a-3a5e9d9d2b77";
  41. //绍兴凯悦-调休假
  42. private static final String COMPENSATORY_LEAVE_CODE = "4429b500-6492-403e-891b-6aeca4590bef";
  43. @Override
  44. public List<Map> getEmployeeRosterInfo(Map<String, Object> map) {
  45. //获取accessToken
  46. String access_token = ddClient.getAccessToken(appKey,appSecret);
  47. //获取员工花名册字段信息
  48. if (Objects.nonNull(map)){
  49. DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list", null, DDConf.initTokenParams(access_token), map, DDR.class);
  50. return (List<Map>)ddr.getResult();
  51. }
  52. return null;
  53. }
  54. @Override
  55. public List<String> getEmployeeUserId() {
  56. Map<String,Object> map = new HashMap<>();
  57. //在职员工状态筛选,可以查询多个状态。不同状态之间使用英文逗号分隔。
  58. //2:试用期 3:正式 5:待离职 -1:无状态
  59. map.put("status_list","2,3");
  60. //分页游标,从0开始。根据返回结果里的next_cursor是否为空来判断是否还有下一页,且再次调用时offset设置成next_cursor的值。
  61. map.put("offset",0);
  62. //分页大小,最大50。
  63. map.put("size",50);
  64. //获取员工userId集合
  65. List<String> userIdList = new ArrayList<>();
  66. getUserIdList(map,userIdList);
  67. return userIdList;
  68. }
  69. public List<String> getUserIdList(Map map,List<String> userIdList){
  70. //获取accessToken
  71. String access_token = ddClient.getAccessToken(appKey,appSecret);
  72. //调用钉钉接口获取在职员工userId集合
  73. DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob", null, DDConf.initTokenParams(access_token), map, DDR.class);
  74. Map result = (Map) ddr.getResult();
  75. //将返回结果里的data_list合并到userIdList
  76. userIdList.addAll((List<String>) result.get("data_list"));
  77. //判断是否还有下一页
  78. if (Objects.nonNull(result.get("next_cursor"))){
  79. map.put("offset",result.get("next_cursor"));
  80. //递归将集合合并到userIdList
  81. getUserIdList(map,userIdList);
  82. }
  83. return userIdList;
  84. }
  85. //保存5s内已处理的更新假期余额事件
  86. private Map<String, Long> bodyList = new HashMap<>();
  87. @Async
  88. public Map getEmployeeAnnualLeaveNum(Map<String, Object> map) {
  89. //获取accessToken
  90. String access_token = ddClient.getAccessToken(appKey,appSecret);
  91. //查询接口body添加参数
  92. //field_filter_list:要查询字段(花名册字段信息参考:https://open.dingtalk.com/document/orgapp/roster-custom-field-business-code)
  93. //agentid:企业内部应用AgentId
  94. map.put("field_filter_list","sys00-name,sys01-positionLevel,sys01-employeeType,9bd53d78-3008-4927-aef8-152a1b44f29b,434d3cd0-3b02-4250-9e7d-7748c31efa84,sys00-confirmJoinTime,sys02-joinWorkingTime,sys05-nowContractStartTime,sys05-contractRenewCount");
  95. map.put("agentid",agentId);
  96. List<Map> errorList = new ArrayList<>();
  97. List<Map> successList = new ArrayList<>();
  98. //获取员工花名册字段信息
  99. if (Objects.nonNull(map)){
  100. DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list", null, DDConf.initTokenParams(access_token), map, DDR.class);
  101. List<Map> employeeData = (List<Map>) ddr.getResult();
  102. //遍历员工信息
  103. for (Map data : employeeData) {
  104. String userId = data.get("userid").toString();
  105. try {
  106. //首次参加工作日期(计算工龄)
  107. String joinWorkingTime = "";
  108. //入职日期
  109. String confirmJoinTime = "";
  110. //职级
  111. String positionLevel = "";
  112. //原职级
  113. String oldPositionLevel = "";
  114. //升职日期
  115. String promotionTime = "";
  116. //姓名
  117. String name = "";
  118. //现合同开始日期
  119. String contractStartTime = "";
  120. //合同续签次数
  121. int contractRenewCount = 0;
  122. //员工类型
  123. String employeeType = "";
  124. List<Map> fieldDataList = (List<Map>) data.get("field_data_list");
  125. for (Map fieldData : fieldDataList) {
  126. String fieldCode = fieldData.get("field_code").toString();
  127. List<Map> fieldValueList = (List<Map>) fieldData.get("field_value_list");
  128. String value = getString(fieldValueList.get(0).get("value"));
  129. String label = getString(fieldValueList.get(0).get("label"));
  130. switch (fieldCode){
  131. case "sys02-joinWorkingTime": joinWorkingTime = value;break;
  132. case "sys00-confirmJoinTime": confirmJoinTime = value;break;
  133. case "sys01-positionLevel": positionLevel = value;break;
  134. case "9bd53d78-3008-4927-aef8-152a1b44f29b": oldPositionLevel = label;break;
  135. case "434d3cd0-3b02-4250-9e7d-7748c31efa84": promotionTime = value;break;
  136. case "sys00-name": name = value;break;
  137. case "sys05-nowContractStartTime": contractStartTime = value;break;
  138. case "sys05-contractRenewCount": contractRenewCount = value.equals("") ? 0 : Integer.valueOf(value);break;
  139. case "sys01-employeeType": employeeType = label;break;
  140. default:break;
  141. }
  142. }
  143. if (employeeType.equals("劳务外包") || employeeType.equals("实习") || employeeType.equals("兼职")){
  144. Map successMap = new HashMap();
  145. successMap.put(userId,"姓名:"+name+",劳务外包、实习、兼职员工不发放年假");
  146. successList.add(successMap);
  147. continue;
  148. }
  149. //若没有原职级 则默认原职级是现职级
  150. if ("".equals(oldPositionLevel)){
  151. oldPositionLevel = positionLevel;
  152. }
  153. //若没有升职日期 则默认当天是升职日期
  154. if ("".equals(promotionTime)){
  155. promotionTime = confirmJoinTime;
  156. }
  157. //若没有现合同开始日期 则默认现合同开始日期是入职日期
  158. if ("".equals(contractStartTime)){
  159. contractStartTime = confirmJoinTime;
  160. }
  161. //若没有首次参加工作日期 则默认入职日期是首次参加工作日期
  162. if ("".equals(joinWorkingTime)){
  163. joinWorkingTime = confirmJoinTime;
  164. }
  165. if ("".equals(confirmJoinTime) || "".equals(positionLevel) || "".equals(name)){
  166. Map errorMap = new HashMap();
  167. errorMap.put(userId,"参数缺失!");
  168. errorList.add(errorMap);
  169. // log.info("更新员工userid:{} 参数缺失!", userId);
  170. continue;
  171. }
  172. //旧合同续签次数等于合同续签数-1 最小为0
  173. int oldContractRenewCount = Math.max(0, contractRenewCount - 1);
  174. //假期有效开始日期为当年1月1日
  175. DateTime beginDate = DateUtil.beginOfYear(new Date());
  176. //假期有效截至日期为当年12月31日
  177. DateTime endDate = DateUtil.endOfYear(new Date());
  178. //当年天数
  179. int yearDays = DateUtil.dayOfYear(endDate);
  180. //工龄(年) 计算规则:首次工作时间至当年一月一日 数值向下取整
  181. int workAge =(int) (DateUtil.betweenYear(DateUtil.parse(joinWorkingTime), beginDate, true));
  182. if (DateUtil.dayOfYear(DateUtil.parse(joinWorkingTime)) != 1 && workAge > 0){
  183. workAge --;
  184. }
  185. System.out.println("截至今年1月1日,工龄为:"+workAge + "年");
  186. //年假数
  187. double yearLeave = getLeaveNum(confirmJoinTime,beginDate,endDate,promotionTime,contractStartTime,oldPositionLevel,positionLevel,workAge,oldContractRenewCount,contractRenewCount,yearDays);
  188. //年假小数
  189. double yearLeaveDecimalPart = yearLeave - (int) yearLeave;
  190. if (yearLeaveDecimalPart < 0.25){
  191. yearLeave = (int) yearLeave;
  192. }else if (yearLeaveDecimalPart < 0.75){
  193. yearLeave = (int) yearLeave + 0.5;
  194. }else if (yearLeaveDecimalPart < 1){
  195. yearLeave = (int) yearLeave + 1;
  196. }
  197. //查询出用户消费年假记录 当返回leaveRecords中calType为null或不返回该字段则为请假消耗 将计算出的年假数减去请假消耗的数量
  198. Map body = new HashMap();
  199. body.put("opUserId",opUserId);
  200. body.put("leaveCode",LEAVE_CODE);
  201. body.put("userIds",new String[]{userId});
  202. body.put("pageNumber",0);
  203. body.put("pageSize",50);
  204. 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);
  205. Map useResult = (Map) useDdr.getResult();
  206. List<Map> useList = (List<Map>) useResult.get("leaveRecords");
  207. Double useLeaveNum = 0d;
  208. if (Objects.nonNull(useList) && !useList.isEmpty()){
  209. for (Map use : useList) {
  210. //判断是否为今年请假
  211. DateTime gmtCreate = DateUtil.date((long) use.get("gmtCreate"));
  212. if (DateUtil.year(gmtCreate) == DateUtil.year(new Date())){
  213. //判断是否为正常请假而不是接口测试或期初假期发放
  214. if (Objects.nonNull(use.get("leaveReason"))){
  215. if (!"接口年假发放".equals(use.get("leaveReason").toString()) && !"期初假期发放".equals(use.get("leaveReason").toString())){
  216. //若是请假消耗或管理员手动减少
  217. if (use.containsKey("calType") && Objects.nonNull(use.get("calType"))){
  218. if ("delete".equals(use.get("calType").toString())){
  219. useLeaveNum += (int) use.get("recordNumPerDay");
  220. }
  221. }
  222. /*if (!use.containsKey("calType") || Objects.isNull(use.get("calType")) || "delete".equals(use.get("calType").toString())){
  223. useLeaveNum += (int) use.get("recordNumPerDay") / 100;
  224. }*/
  225. //注:若是管理员手动增加 则假期余额会多出一个高级假期记录增加的假期天数 最终会在设置的假期余额的基础上加上这些天数
  226. //故此处手动新增的假期余额不做处理
  227. }
  228. }
  229. }
  230. }
  231. }
  232. //实际年假数
  233. double realYearLeave = (yearLeave * 100 - useLeaveNum) < 0 ? 0 : (yearLeave * 100 - useLeaveNum);
  234. //更新假期余额接口的body
  235. Map<String,Object> updateBody = new HashMap<>();
  236. Map<String,Object> leave_quotas = new HashMap<>();
  237. //额度有效期开始时间,毫秒级时间戳
  238. leave_quotas.put("start_time",beginDate.getTime());
  239. //额度有效期结束时间,毫秒级时间戳。
  240. leave_quotas.put("end_time",endDate.getTime());
  241. //操作原因
  242. leave_quotas.put("reason","接口年假发放");
  243. //以天计算的额度总数 假期类型按天计算时,该值不为空且按百分之一天折算。 例如:1000=10天。
  244. leave_quotas.put("quota_num_per_day",(int) realYearLeave );
  245. //以小时计算的额度总数 假期类型按小时,计算该值不为空且按百分之一小时折算。例如:1000=10小时。
  246. leave_quotas.put("quota_num_per_hour",0);
  247. //额度所对应的周期,格式必须是"yyyy",例如"2021"
  248. leave_quotas.put("quota_cycle",DateUtil.year(new Date())+"");
  249. //自定义添加的假期类型:年假开发测试的leave_code
  250. leave_quotas.put("leave_code",LEAVE_CODE);
  251. //要更新的员工的userId
  252. leave_quotas.put("userid",userId);
  253. updateBody.put("leave_quotas",leave_quotas);
  254. //当前企业内拥有OA审批应用权限的管理员的userId
  255. updateBody.put("op_userid",opUserId);
  256. String bodyStr = yearLeave + userId;
  257. // 检查更新事件是否已经处理过,如果是,则忽略该更新
  258. if (isUpdateLeave(bodyStr)) {
  259. log.info("更新事件已处理,忽略该回调...");
  260. return null;
  261. }
  262. // 将更新和当前时间戳添加到已处理集合中
  263. long currentTime = System.currentTimeMillis();
  264. bodyList.put(bodyStr, currentTime);
  265. //更新假期余额
  266. UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/update", null, DDConf.initTokenParams(access_token), updateBody, DDR.class);
  267. Map successMap = new HashMap();
  268. successMap.put(userId,"姓名:"+name+",职级:" + positionLevel+",原职级:" + oldPositionLevel+",入职日期:" + confirmJoinTime + ",升职日期:" + promotionTime + ",年假数:"+ realYearLeave + ",开始日期:" + beginDate+",截止日期:" + endDate);
  269. successList.add(successMap);
  270. }catch (Exception e){
  271. log.info("更新员工年假失败:{}",e);
  272. Map errorMap = new HashMap();
  273. errorMap.put(userId,e.getMessage());
  274. errorList.add(errorMap);
  275. }
  276. }
  277. }
  278. log.info("更新失败列表:{}",errorList);
  279. log.info("更新成功列表:{}",successList);
  280. Map result = new HashMap();
  281. result.put("errorList",errorList);
  282. result.put("successList",successList);
  283. return result;
  284. }
  285. private String getString(Object label) {
  286. return Objects.isNull(label) ? "" : label.toString();
  287. }
  288. 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) {
  289. double yearLeave = 0.0;
  290. long day1 = 0;
  291. long day2 = 0;
  292. long day3 = 0;
  293. //判断员工是否当年新入职
  294. if (DateUtil.year(DateUtil.parse(confirmJoinTime)) == DateUtil.year(new Date())){
  295. beginDate = DateUtil.parse(confirmJoinTime);
  296. }
  297. //判断员工是否当年升职
  298. if (DateUtil.year(DateUtil.parse(promotionTime)) == DateUtil.year(new Date())){
  299. //判断现合同开始日期是否当年
  300. if (DateUtil.year(DateUtil.parse(owContractStartTime)) == DateUtil.year(new Date())){
  301. //判断现合同开始日期和升职日期先后顺序
  302. if (DateUtil.parse(owContractStartTime).before(DateUtil.parse(promotionTime))){
  303. day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(owContractStartTime),true);
  304. day2 = DateUtil.betweenDay(DateUtil.parse(owContractStartTime),DateUtil.parse(promotionTime),true);
  305. day3 = DateUtil.betweenDay(DateUtil.parse(promotionTime),endDate,true);
  306. yearLeave = (double) (day1 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,oldContractRenewCount) + day2 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,contractRenewCount) + day3 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
  307. }else {
  308. day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(promotionTime),true);
  309. day2 = DateUtil.betweenDay(DateUtil.parse(promotionTime),DateUtil.parse(owContractStartTime),true);
  310. day3 = DateUtil.betweenDay(DateUtil.parse(owContractStartTime),endDate,true);
  311. yearLeave = (double) (day1 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,oldContractRenewCount) + day2 * getAnnualLeaveBaseNum(positionLevel,workAge,oldContractRenewCount) + day3 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
  312. }
  313. }else {
  314. day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(promotionTime),true);
  315. day2 = DateUtil.betweenDay(DateUtil.parse(promotionTime),endDate,true);
  316. yearLeave = (double) (day1 * getAnnualLeaveBaseNum(oldPositionLevel,workAge,contractRenewCount) + day2 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
  317. }
  318. }else {
  319. //判断现合同开始日期是否当年
  320. if (DateUtil.year(DateUtil.parse(owContractStartTime)) == DateUtil.year(new Date())){
  321. day1 = DateUtil.betweenDay(beginDate,DateUtil.parse(owContractStartTime),true);
  322. day2 = DateUtil.betweenDay(DateUtil.parse(owContractStartTime),endDate,true);
  323. yearLeave = (double) (day1 * getAnnualLeaveBaseNum(positionLevel,workAge,oldContractRenewCount) + day2 * getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount)) / yearDays;
  324. }else {
  325. yearLeave = getAnnualLeaveBaseNum(positionLevel,workAge,contractRenewCount);
  326. }
  327. }
  328. return yearLeave;
  329. }
  330. private int getAnnualLeaveBaseNum(String positionLevel, int workAge, int contractRenewCount) {
  331. //法定年假
  332. int legalAnnualLeave = 0;
  333. //福利年假
  334. int welfareAnnualLeave = 0;
  335. //根据职级、工龄和合同续签数计算年假基数
  336. if (positionLevel.equals("总经理")){
  337. if (workAge < 10){
  338. legalAnnualLeave = 5;
  339. welfareAnnualLeave = 15;
  340. }else if (workAge >= 10 && workAge < 20){
  341. legalAnnualLeave = 10;
  342. welfareAnnualLeave = 10;
  343. }else if (workAge >= 20){
  344. legalAnnualLeave = 15;
  345. welfareAnnualLeave = 5;
  346. }
  347. }else if (positionLevel.equals("部门总监") || positionLevel.equals("经理级")){
  348. if (workAge < 10){
  349. legalAnnualLeave = 5;
  350. welfareAnnualLeave = 7 + 2 * (Math.min(contractRenewCount, 2));
  351. }else if (workAge >= 10 && workAge < 20){
  352. legalAnnualLeave = 10;
  353. welfareAnnualLeave = 2 + 2 * (Math.min(contractRenewCount, 2));
  354. }else if (workAge >= 20){
  355. legalAnnualLeave = 15;
  356. welfareAnnualLeave = 1;
  357. }
  358. }else if (positionLevel.equals("主管")){
  359. if (workAge < 10){
  360. legalAnnualLeave = 5;
  361. welfareAnnualLeave = 5 + 2 * (Math.min(contractRenewCount, 2));
  362. }else if (workAge >= 10 && workAge < 20){
  363. legalAnnualLeave = 10;
  364. welfareAnnualLeave = 0 + 2 * (Math.min(contractRenewCount, 2));
  365. }else if (workAge >= 20){
  366. legalAnnualLeave = 15;
  367. welfareAnnualLeave = 0;
  368. }
  369. }else if (positionLevel.equals("员工")){
  370. if (workAge < 10){
  371. legalAnnualLeave = 5;
  372. welfareAnnualLeave = 3 + 2 * (Math.min(contractRenewCount, 2));
  373. }else if (workAge >= 10 && workAge < 20){
  374. legalAnnualLeave = 10;
  375. welfareAnnualLeave = 0 + 2 * (Math.min(contractRenewCount, 1));
  376. }else if (workAge >= 20){
  377. legalAnnualLeave = 15;
  378. welfareAnnualLeave = 0;
  379. }
  380. }else {
  381. legalAnnualLeave = 0;
  382. welfareAnnualLeave = 0;
  383. }
  384. int annualLeave = legalAnnualLeave + welfareAnnualLeave;
  385. return annualLeave;
  386. }
  387. @Override
  388. public McR updateEmployeeAnnualLeaveNum() {
  389. //获取员工userId集合
  390. List<String> userIdList = getEmployeeUserId();
  391. //遍历集合给所有员工更新年假余额
  392. List<Map> result = new ArrayList<>();
  393. if (Objects.nonNull(userIdList) && !userIdList.isEmpty()){
  394. Map map = new HashMap();
  395. //将userIdList拆分成50长度的多个数组
  396. List<List<String>> userIdListList = CollectionUtil.splitList(userIdList,50);
  397. for (List<String> userIdList1 : userIdListList) {
  398. map.put("userid_list",String.join(",",userIdList1));
  399. result.add(getEmployeeAnnualLeaveNum(map));
  400. }
  401. log.info("result:{}",result);
  402. }
  403. return McR.success();
  404. }
  405. @Override
  406. public Map getUserLeaveInfo(String userId) {
  407. long currentTime = System.currentTimeMillis();
  408. Map leaveMap = new HashMap();
  409. //获取员工调休假余额
  410. List<Map> leaveQuotasList = new ArrayList<>();
  411. getLeaveNum(COMPENSATORY_LEAVE_CODE,userId,0,50,leaveQuotasList);
  412. //查询员工调休假余额
  413. BigDecimal compensatoryLeaveNum = new BigDecimal(0.00);
  414. if (Objects.nonNull(leaveQuotasList)){
  415. for (Map leaveQuotas : leaveQuotasList) {
  416. if ((long) leaveQuotas.get("start_time") <= currentTime && currentTime <= (long) leaveQuotas.get("end_time")){
  417. if (Objects.isNull(leaveQuotas.get("quota_num_per_day")) && Objects.nonNull(leaveQuotas.get("quota_num_per_hour"))){
  418. compensatoryLeaveNum = compensatoryLeaveNum.add(new BigDecimal(String.valueOf(leaveQuotas.get("quota_num_per_hour"))).divide(new BigDecimal(100)));
  419. }
  420. if (Objects.nonNull(leaveQuotas.get("used_num_per_hour"))){
  421. compensatoryLeaveNum = compensatoryLeaveNum.subtract(new BigDecimal(String.valueOf(leaveQuotas.get("used_num_per_hour"))).divide(new BigDecimal(100)));
  422. }
  423. }
  424. }
  425. }
  426. if (compensatoryLeaveNum.compareTo(new BigDecimal(80)) >= 0){
  427. leaveMap.put("可预支调休",80);
  428. leaveMap.put("实际加班小时数",compensatoryLeaveNum.subtract(new BigDecimal(80)));
  429. leaveMap.put("已预支调休",0);
  430. }else {
  431. leaveMap.put("可预支调休",compensatoryLeaveNum);
  432. leaveMap.put("实际加班小时数",0);
  433. leaveMap.put("已预支调休",new BigDecimal(80).subtract(compensatoryLeaveNum));
  434. }
  435. return leaveMap;
  436. }
  437. private List<Map> getLeaveNum(String leave_code,String userId,int offset,int size,List<Map> leaveQuotasList) {
  438. String access_token = ddClient.getAccessToken(appKey,appSecret);
  439. Map body = new HashMap();
  440. body.put("leave_code",leave_code);
  441. body.put("op_userid",opUserId);
  442. body.put("userids",userId);
  443. body.put("offset",offset);
  444. body.put("size",size);
  445. 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);
  446. Map result = (Map) ddrNew.getResult();
  447. if (Objects.nonNull(result.get("leave_quotas"))){
  448. leaveQuotasList.addAll((List<Map>) result.get("leave_quotas"));
  449. }
  450. if ((boolean) result.get("has_more")){
  451. getLeaveNum(leave_code,userId,offset+size,size,leaveQuotasList);
  452. }
  453. return leaveQuotasList;
  454. }
  455. /**
  456. * 检查该更新事件在5s内是否处理过,应对钉钉瞬间重复回调
  457. *
  458. * @param msg 回调事件
  459. * @return 是否处理过
  460. */
  461. private boolean isUpdateLeave(String msg) {
  462. // 清理超过10s的回调事件
  463. long currentTime = System.currentTimeMillis();
  464. long expirationTime = currentTime - TimeUnit.SECONDS.toMillis(5);
  465. bodyList.entrySet().removeIf(entry -> entry.getValue() < expirationTime);
  466. return bodyList.containsKey(msg);
  467. }
  468. }