KYServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.malk.kaiyue.service.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.http.HttpUtil;
  6. import com.malk.kaiyue.service.KYService;
  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.service.dingtalk.DDClient;
  11. import com.malk.utils.UtilHttp;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.util.*;
  16. import java.util.stream.Collectors;
  17. @Slf4j
  18. @Service
  19. public class KYServiceImpl implements KYService {
  20. @Autowired
  21. private DDClient ddClient;
  22. @Autowired
  23. private DDConf ddConf;
  24. @Override
  25. public List<Map> getEmployeeRosterInfo(Map<String, Object> map) {
  26. //获取accessToken
  27. String access_token = ddClient.getAccessToken();
  28. //获取员工花名册字段信息
  29. if (Objects.nonNull(map)){
  30. DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list", null, DDConf.initTokenParams(access_token), map, DDR.class);
  31. return (List<Map>)ddr.getResult();
  32. }
  33. return null;
  34. }
  35. @Override
  36. public Map getEmployeeUserId() {
  37. //获取accessToken
  38. String access_token = ddClient.getAccessToken();
  39. Map<String,Object> map = new HashMap<>();
  40. //在职员工状态筛选,可以查询多个状态。不同状态之间使用英文逗号分隔。
  41. //2:试用期 3:正式 5:待离职 -1:无状态
  42. map.put("status_list","3");
  43. //分页游标,从0开始。根据返回结果里的next_cursor是否为空来判断是否还有下一页,且再次调用时offset设置成next_cursor的值。
  44. map.put("offset",0);
  45. //分页大小,最大50。
  46. map.put("size",50);
  47. //获取员工userId集合
  48. DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob", null, DDConf.initTokenParams(access_token), map, DDR.class);
  49. return (Map)ddr.getResult();
  50. }
  51. public McR getEmployeeAnnualLeaveNum(Map<String, Object> map) {
  52. //获取accessToken
  53. String access_token = ddClient.getAccessToken();
  54. //获取agentId
  55. String agentId = ddConf.getAgentId().toString();
  56. //查询接口body添加参数
  57. //field_filter_list:要查询字段(花名册字段信息参考:https://open.dingtalk.com/document/orgapp/roster-custom-field-business-code)
  58. //agentid:企业内部应用AgentId
  59. map.put("field_filter_list","sys00-name,sys01-positionLevel,sys00-confirmJoinTime,sys02-joinWorkingTime,sys05-contractRenewCount");
  60. map.put("agentid",agentId);
  61. List<String> result = new ArrayList<>();
  62. //获取员工花名册字段信息
  63. if (Objects.nonNull(map)){
  64. DDR ddr = (DDR) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/v2/list", null, DDConf.initTokenParams(access_token), map, DDR.class);
  65. List<Map> employeeData = (List<Map>) ddr.getResult();
  66. //遍历员工信息
  67. for (Map data : employeeData) {
  68. String userId = data.get("userid").toString();
  69. //首次参加工作日期(计算工龄)
  70. String joinWorkingTime = "";
  71. //入职日期
  72. String confirmJoinTime = "";
  73. //职级
  74. String positionLevel = "";
  75. //姓名
  76. String name = "";
  77. //合同续签次数
  78. int contractRenewCount = 0;
  79. List<Map> fieldDataList = (List<Map>) data.get("field_data_list");
  80. for (Map fieldData : fieldDataList) {
  81. String fieldCode = fieldData.get("field_code").toString();
  82. List<Map> fieldValueList = (List<Map>) fieldData.get("field_value_list");
  83. if (Objects.nonNull(fieldValueList.get(0).get("value"))){
  84. String value = fieldValueList.get(0).get("value").toString();
  85. switch (fieldCode){
  86. case "sys02-joinWorkingTime": joinWorkingTime = value;break;
  87. case "sys00-confirmJoinTime": confirmJoinTime = value;break;
  88. case "sys01-positionLevel": positionLevel = value;break;
  89. case "sys00-name": name = value;break;
  90. case "sys05-contractRenewCount": contractRenewCount = Integer.valueOf(value);break;
  91. default:break;
  92. }
  93. }else {
  94. log.info("更新员工年假余额:参数缺啦!");
  95. return McR.errorParam("参数缺啦!");
  96. }
  97. }
  98. //工龄(年) 计算规则:首次工作时间至当年一月一日 数值向下取整
  99. int workAge =(int) (DateUtil.betweenDay(DateUtil.parse(joinWorkingTime), DateUtil.beginOfYear(new Date()), true) / 365);
  100. System.out.println(workAge + "年");
  101. //法定年假
  102. int legalAnnualLeave = 0;
  103. //福利年假
  104. int welfareAnnualLeave = 0;
  105. //根据职级、工龄和合同续签数计算年假基数
  106. if (positionLevel.equals("5")){
  107. if (workAge < 10){
  108. legalAnnualLeave = 5;
  109. welfareAnnualLeave = 15;
  110. }else if (workAge >= 10 && workAge < 20){
  111. legalAnnualLeave = 10;
  112. welfareAnnualLeave = 10;
  113. }else if (workAge >= 20){
  114. legalAnnualLeave = 15;
  115. welfareAnnualLeave = 5;
  116. }
  117. }else if (positionLevel.equals("4") || positionLevel.equals("3")){
  118. if (workAge < 10){
  119. legalAnnualLeave = 5;
  120. welfareAnnualLeave = 7 + 2 * contractRenewCount;
  121. }else if (workAge >= 10 && workAge < 20){
  122. legalAnnualLeave = 10;
  123. welfareAnnualLeave = 2 + 2 * contractRenewCount;
  124. }else if (workAge >= 20){
  125. legalAnnualLeave = 15;
  126. welfareAnnualLeave = 1;
  127. }
  128. }else if (positionLevel.equals("2")){
  129. if (workAge < 10){
  130. legalAnnualLeave = 5;
  131. welfareAnnualLeave = 5 + 2 * contractRenewCount;
  132. }else if (workAge >= 10 && workAge < 20){
  133. legalAnnualLeave = 10;
  134. welfareAnnualLeave = 0 + 2 * contractRenewCount;
  135. }else if (workAge >= 20){
  136. legalAnnualLeave = 15;
  137. welfareAnnualLeave = 0;
  138. }
  139. }else if (positionLevel.equals("1")){
  140. if (workAge < 10){
  141. legalAnnualLeave = 5;
  142. welfareAnnualLeave = 3 + 2 * contractRenewCount;
  143. }else if (workAge >= 10 && workAge < 20){
  144. legalAnnualLeave = 10;
  145. welfareAnnualLeave = 0 + 2 * contractRenewCount;
  146. }else if (workAge >= 20){
  147. legalAnnualLeave = 15;
  148. welfareAnnualLeave = 0;
  149. }
  150. }else {
  151. legalAnnualLeave = 0;
  152. welfareAnnualLeave = 0;
  153. }
  154. //开始日期为当年1月1日
  155. DateTime beginDate = DateUtil.beginOfYear(new Date());
  156. //有效日期至当年12月31日
  157. DateTime endDate = DateUtil.offsetDay(DateUtil.endOfYear(new Date()),-1);
  158. result.add("姓名:"+name+",职级:"+positionLevel+",工龄:"+workAge+"年,合同续签数"+contractRenewCount+",法定年假:" + legalAnnualLeave + "天,福利年假:" + welfareAnnualLeave + "天"+",截止日期:"+endDate);
  159. //年假基数
  160. double yearLeave = legalAnnualLeave + welfareAnnualLeave;
  161. double yearLeaveDecimalPart = 0;
  162. //判断员工是否当年新入职
  163. if (DateUtil.year(DateUtil.parse(confirmJoinTime)) == DateUtil.year(new Date())){
  164. int workDay = (int) DateUtil.betweenDay(DateUtil.parse(confirmJoinTime),endDate,true);
  165. yearLeave = workDay * yearLeave / 365.0;
  166. yearLeaveDecimalPart = yearLeave - (int) yearLeave;
  167. if (yearLeaveDecimalPart < 0.25){
  168. yearLeave = (int) yearLeave;
  169. }else if (yearLeaveDecimalPart < 0.75){
  170. yearLeave = (int) yearLeave + 0.5;
  171. }else if (yearLeaveDecimalPart < 1){
  172. yearLeave = (int) yearLeave + 1;
  173. }
  174. }
  175. //更新假期余额接口的body
  176. Map<String,Object> updateBody = new HashMap<>();
  177. Map<String,Object> leave_quotas = new HashMap<>();
  178. //额度有效期开始时间,毫秒级时间戳
  179. leave_quotas.put("start_time",beginDate.getTime());
  180. //额度有效期结束时间,毫秒级时间戳。
  181. leave_quotas.put("end_time",endDate.getTime());
  182. //操作原因
  183. leave_quotas.put("reason","接口测试修改");
  184. //以天计算的额度总数 假期类型按天计算时,该值不为空且按百分之一天折算。 例如:1000=10天。
  185. leave_quotas.put("quota_num_per_day",(int) (yearLeave * 100) );
  186. //以小时计算的额度总数 假期类型按小时,计算该值不为空且按百分之一小时折算。例如:1000=10小时。
  187. leave_quotas.put("quota_num_per_hour",0);
  188. //额度所对应的周期,格式必须是"yyyy",例如"2021"
  189. leave_quotas.put("quota_cycle",DateUtil.year(new Date())+"");
  190. //自定义添加的假期类型:年假开发测试的leave_code
  191. leave_quotas.put("leave_code","28abb7bf-e1b6-4387-9e2a-e1b2ae983e7a");
  192. //要更新的员工的userId
  193. leave_quotas.put("userid",userId);
  194. updateBody.put("leave_quotas",leave_quotas);
  195. //当前企业内拥有OA审批应用权限的管理员的userId(飞超)
  196. updateBody.put("op_userid","02421908021891243060");
  197. //更新假期余额
  198. UtilHttp.doPost("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/update", null, DDConf.initTokenParams(access_token), updateBody, DDR.class);
  199. }
  200. }
  201. log.info(result.stream().collect(Collectors.joining(",")));
  202. return McR.success(result);
  203. }
  204. }