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