钉钉考勤接口调用与OA系统数据对接(多线程版)
公司由原来的指纹打卡更换为钉钉打卡,需要钉钉和现有的OA考勤数据对接(合并钉钉打卡数据和OA上的请假,外出,出差数据),因为人数增减单线程定时任务数据抓取时间较长,做一下优化
1.线程池
class CountableThreadPool {
private int threadNum;
private AtomicInteger threadAlive = new AtomicInteger();
private ReentrantLock reentrantLock = new ReentrantLock();
private Condition condition = reentrantLock.newCondition();
public CountableThreadPool(int threadNum) {
this.threadNum = threadNum;
this.executorService = Executors.newFixedThreadPool(threadNum);
}
public CountableThreadPool(int threadNum, ExecutorService executorService) {
this.threadNum = threadNum;
this.executorService = executorService;
}
public void setExecutorService(ExecutorService executorService) {
this.executorService = executorService;
}
public int getThreadAlive() {
return threadAlive.get();
}
public int getThreadNum() {
return threadNum;
}
private ExecutorService executorService;
public void execute(final Runnable runnable) {
if (threadAlive.get() >= threadNum) {
try {
reentrantLock.lock();
while (threadAlive.get() >= threadNum) {
try {
condition.await();
} catch (InterruptedException e) {
}
}
} finally {
reentrantLock.unlock();
}
}
threadAlive.incrementAndGet();
executorService.execute(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} finally {
try {
reentrantLock.lock();
threadAlive.decrementAndGet();
condition.signal();
} finally {
reentrantLock.unlock();
}
}
}
});
}
public boolean isShutdown() {
return executorService.isShutdown();
}
public void shutdown() {
executorService.shutdown();
}
}
2.调用线程池,启用多线程定时任务
public class TimerTaskService extends TimerTask {
CountableThreadPool threadPool;
protected Logger logger = LoggerFactory.getLogger(getClass());
// 待抓取队列
LinkedBlockingQueue<String> taskQueue;
// 抓取数量
private final AtomicLong pageCount = new AtomicLong(0);
//考勤数据整合
public void processAttendance(String dingID_OaID_Name) {
try {
logger.info("OA数据同步开始!!!");
AttendanceService attendanceService = (AttendanceService) ApplicationContextUtil.getBean("attendanceService");
OaLeaveService oaLeaveService = (OaLeaveService) ApplicationContextUtil.getBean("oaLeaveService");
OaYearLeaveService oaYearLeaveService = (OaYearLeaveService) ApplicationContextUtil.getBean("oaYearLeaveService");
OaBusinessService oaBusinessService = (OaBusinessService) ApplicationContextUtil.getBean("oaBusinessService");
OaGooutService oaGooutService = (OaGooutService) ApplicationContextUtil.getBean("oaGooutService");
String flow_data=null;
String[] split = dingID_OaID_Name.split("/");
//更新oa信息
String oa_id = split[1];//OAID
String ding_id = split[0];
String user_name = split[2];
//判断是否有OA账号
if (null!=ding_id) {
if (null==oa_id) {
return;
}else {
//取OA未记录数据
Map<String, Object> oaLeaveMap = oaLeaveService.getOaLeaveList("0",oa_id,"values", "", "", "flow_data_507_copy1", "批准", "'"+"2019-08-01"+"'", "", 1, 5);
Map<String, Object> oaLeaveMap1 = oaLeaveService.getOaLeaveList("0",oa_id,"values", "", "", "flow_data_508_copy1", "批准", "'"+"2019-08-01"+"'", "", 1, 5);
Map<String, Object> oaLeaveMap2 = oaLeaveService.getOaLeaveList("0",oa_id,"values", "", "", "flow_data_512_copy1", "批准", "'"+"2019-08-01"+"'", "", 1, 5);
Map<String, Object> oaYearLeaveMap = oaYearLeaveService.getOaYearLeaveList("0",oa_id,"values", "", "", "flow_data_535_copy1", "批准", "'"+"2019-08-01"+"'", "", 1, 5);
Map<String, Object> oaBusinessMap = oaBusinessService.getOaBusinessList("0",oa_id, "values", "", "", "flow_data_103_copy1", "批准", "'"+"2019-08-01"+"'", "", 1, 5);
Map<String, Object> oaGooutMap = oaGooutService.getOaGooutList("0",oa_id, "values", "", "", "flow_data_85_copy1", "批准", "'"+"2019-08-01"+"'", "", 1, 5);
List<OaLeave> oaLeaveList =(List<OaLeave>) oaLeaveMap.get("rows");
List<OaLeave> oaLeaveList1 =(List<OaLeave>) oaLeaveMap1.get("rows");
List<OaLeave> oaLeaveList2 =(List<OaLeave>) oaLeaveMap2.get("rows");
List<OaYearLeave> oaYearLeaveList =(List<OaYearLeave>) oaYearLeaveMap.get("rows");
List<OaBusiness> oaBusinessList = (List<OaBusiness>) oaBusinessMap.get("rows");
List<OaGoout> oaGooutList = (List<OaGoout>) oaGooutMap.get("rows");
//请假信息
if (oaLeaveList.isEmpty()&&oaLeaveList1.isEmpty()&&oaLeaveList2.isEmpty()&&
oaYearLeaveList.isEmpty()&&oaBusinessList.isEmpty()&&oaGooutList.isEmpty()) {
logger.info("没有请假信息!!!");
}else {
//构造打卡信息
JSONObject getuserinfo = DingRecordSyncController.getuserinfo(ding_id);
JSONArray departmentid = getuserinfo.getJSONArray("department");//部门id
String department=null;
if (null!=departmentid) {
department = DingRecordSyncController.getdepartment(departmentid.getString(0));
}
//遍历请假出差外出信息
if (!oaLeaveList.isEmpty()||!oaLeaveList1.isEmpty()||!oaLeaveList2.isEmpty()) {
List<OaLeave> list2 = new ArrayList<OaLeave>();
if (!oaLeaveList.isEmpty()) {
list2=oaLeaveList;
flow_data="flow_data_507";
}else if (!oaLeaveList1.isEmpty()) {
list2=oaLeaveList1;
flow_data="flow_data_508";
}else if (!oaLeaveList2.isEmpty()) {
list2=oaLeaveList2;
flow_data="flow_data_512";
}
for (int j = 0; j < list2.size(); j++) {
OaLeave oaLeave = list2.get(j);
String id = oaLeave.getId();
String begin_date_type = oaLeave.getBegin_date_type();//开始时间的上下午
String end_date_type = oaLeave.getEnd_date_type();//结束时间的上下午
String begin_date = oaLeave.getBegin_date();//开始日期
String end_date = oaLeave.getEnd_date();//结束日期
String leave_name = oaLeave.getLeave_name();//请假类别
String date_count = oaLeave.getDate_count();//请假时长
if (null!=date_count&&date_count.equals("半")) {
date_count="0.5";
}
if (null!=date_count&&date_count.equals("半天")) {
date_count="0.5";
}
float count = Float.parseFloat(date_count);
if (begin_date_type.equals("上午")) {
if (count==0.5) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,null,null,null,0.5f,null,null,null));
}
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}else if (count==1) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}else if (count>1) {
if (end_date_type.equals("上午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
if (DateUtil.compateDate(end_date, nextDate)>0) {
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>0);
}
Attendance attendance4 = attendanceService.getAttendance(ding_id, end_date);
if (null!=attendance4&&attendance4.getAttendanceTime()!=null) {
attendance4.setOnDuty1checkResult(leave_name);
attendance4.setOffDuty1checkResult(leave_name);
attendance4.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance4);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,end_date,null,leave_name,null,leave_name,null,null,null,null,0.5f,null,null,null));
}
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}else if (end_date_type.equals("下午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}
}//开始时间是上午
}else if (begin_date_type.equals("下午")) {
if (count==0.5) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,0.5f,null,null,null));
}
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}else if (count==1) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,0.5f,null,null,null));
}
Attendance attendance3 = attendanceService.getAttendance(ding_id, DateUtil.getNextDate(begin_date));
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,DateUtil.getNextDate(begin_date),null,leave_name,null,leave_name,null,null,null,null,0.5f,null,null,null));
}
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}else if (count>1) {
if (end_date_type.equals("下午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,0.5f,null,null,null));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}else if (end_date_type.equals("上午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,0.5f,null,null,null));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,1f,null,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>0);
Attendance attendance4 = attendanceService.getAttendance(ding_id, end_date);
if (null!=attendance4&&attendance4.getAttendanceTime()!=null) {
attendance4.setOnDuty1checkResult(leave_name);
attendance4.setOffDuty1checkResult(leave_name);
attendance4.setLeave_count(0.5f);
attendanceService.updateAttendance(attendance4);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,end_date,null,leave_name,null,leave_name,null,null,null,null,0.5f,null,null,null));
}
if (flow_data.equals("flow_data_507")) {
oaLeaveService.updateOaLeave(id,1);
}else if (flow_data.equals("flow_data_508")) {
oaLeaveService.updateOaLeave1(id,1);
}else if (flow_data.equals("flow_data_512")) {
oaLeaveService.updateOaLeave2(id,1);
}
continue;
}
}//开始时间是下午
}
}
}
//年假
if (!oaYearLeaveList.isEmpty()) {
for (int j = 0; j < oaYearLeaveList.size(); j++) {
OaYearLeave oaLeave = oaYearLeaveList.get(j);
String id = oaLeave.getId();
String begin_date_type = oaLeave.getBegin_date_type();//开始时间的上下午
String end_date_type = oaLeave.getEnd_date_type();//结束时间的上下午
String begin_date = oaLeave.getBegin_date();//开始日期
String end_date = oaLeave.getEnd_date();//结束日期
String leave_name = oaLeave.getLeave_name();//请假类别
String date_count = oaLeave.getDate_count();//请假时长
float count = Float.parseFloat(date_count);
if (begin_date_type.equals("上午")) {
if (count==0.5) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,null,null,null,null,0.5f,null,null));
}
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}else if (count==1) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}else if (count>1) {
if (end_date_type.equals("上午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
if (DateUtil.compateDate(end_date, nextDate)>0) {
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>0);
}
Attendance attendance4 = attendanceService.getAttendance(ding_id, end_date);
if (null!=attendance4&&attendance4.getAttendanceTime()!=null) {
attendance4.setOnDuty1checkResult(leave_name);
attendance4.setOffDuty1checkResult(leave_name);
attendance4.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance4);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,end_date,null,leave_name,null,leave_name,null,null,null,null,null,0.5f,null,null));
}
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}else if (end_date_type.equals("下午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}
}//开始时间是上午
}else if (begin_date_type.equals("下午")) {
if (count==0.5) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,0.5f,null,null));
}
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}else if (count==1) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,0.5f,null,null));
}
Attendance attendance3 = attendanceService.getAttendance(ding_id, DateUtil.getNextDate(begin_date));
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,DateUtil.getNextDate(begin_date),null,leave_name,null,leave_name,null,null,null,null,null,0.5f,null,null));
}
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}else if (count>1) {
if (end_date_type.equals("下午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,0.5f,null,null));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}else if (end_date_type.equals("上午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,0.5f,null,null));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setYearLeave_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,1f,null,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>0);
Attendance attendance4 = attendanceService.getAttendance(ding_id, end_date);
if (null!=attendance4&&attendance4.getAttendanceTime()!=null) {
attendance4.setOnDuty1checkResult(leave_name);
attendance4.setOffDuty1checkResult(leave_name);
attendance4.setYearLeave_count(0.5f);
attendanceService.updateAttendance(attendance4);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,end_date,null,leave_name,null,leave_name,null,null,null,null,null,0.5f,null,null));
}
oaYearLeaveService.updateOaYearLeave(id,1);
continue;
}
}//开始时间是下午
}
}
}
//出差
if (!oaBusinessList.isEmpty()) {
for (int j = 0; j < oaBusinessList.size(); j++) {
OaBusiness oaBusiness = oaBusinessList.get(j);
String id = oaBusiness.getId();
String begin_date_type = oaBusiness.getBegin_date_type();//开始时间的上下午
String end_date_type = oaBusiness.getEnd_date_type();//结束时间的上下午
String begin_date = oaBusiness.getBegin_date();//开始日期
String end_date = oaBusiness.getEnd_date();//结束日期
String leave_name = "出差";//类别
String date_count = oaBusiness.getDate_count();//请假时长
float count = Float.parseFloat(date_count);
if (begin_date_type.equals("上午")) {
if (count==0.5) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,null,null,null,null,null,null,0.5f));
}
oaBusinessService.updateBusiness(id, 1);
continue;
}else if (count==1) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
oaBusinessService.updateBusiness(id, 1);
continue;
}else if (count>1) {
if (end_date_type.equals("上午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setBussiness_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>0);
Attendance attendance4 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance4&&attendance4.getAttendanceTime()!=null) {
attendance4.setOnDuty1checkResult(leave_name);
attendance4.setOffDuty1checkResult(leave_name);
attendance4.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance4);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,null,null,null,null,null,null,0.5f));
}
oaBusinessService.updateBusiness(id, 1);
continue;
}else if (end_date_type.equals("下午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setBussiness_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
oaBusinessService.updateBusiness(id, 1);
continue;
}
}//开始时间是上午
}else if (begin_date_type.equals("下午")) {
if (count==0.5) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,null,null,0.5f));
}
oaBusinessService.updateBusiness(id, 1);
continue;
}else if (count==1) {
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,null,null,0.5f));
}
Attendance attendance3 = attendanceService.getAttendance(ding_id, DateUtil.getNextDate(begin_date));
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,DateUtil.getNextDate(begin_date),null,leave_name,null,leave_name,null,null,null,null,null,null,null,0.5f));
}
oaBusinessService.updateBusiness(id, 1);
continue;
}else if (count>1) {
if (end_date_type.equals("下午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,null,null,0.5f));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setBussiness_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
oaBusinessService.updateBusiness(id, 1);
continue;
}else if (end_date_type.equals("上午")) {
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2&&attendance2.getAttendanceTime()!=null) {
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,null,null,null,null,leave_name,null,leave_name,null,null,null,0.5f));
}
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setBussiness_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,null,1f));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>0);
Attendance attendance4 = attendanceService.getAttendance(ding_id, end_date);
if (null!=attendance4&&attendance4.getAttendanceTime()!=null) {
attendance4.setOnDuty1checkResult(leave_name);
attendance4.setOffDuty1checkResult(leave_name);
attendance4.setBussiness_count(0.5f);
attendanceService.updateAttendance(attendance4);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,null,null,null,null,null,null,05f));
}
oaBusinessService.updateBusiness(id, 1);
continue;
}
}//开始时间是下午
}
}
}
//外出
if (!oaGooutList.isEmpty()) {
for (int j = 0; j < oaGooutList.size(); j++) {
OaGoout oaGoout = oaGooutList.get(j);
String id = oaGoout.getId();
String begin_date_type = oaGoout.getBegin_date_type();//开始时间的上下午
String end_date_type = oaGoout.getEnd_date_type();//结束时间的上下午
String begin_date = oaGoout.getBegin_date();//开始日期
String end_date = oaGoout.getEnd_date();//结束日期
String leave_name = "外出";//类别
String nextDate = DateUtil.getNextDate(begin_date);
Attendance attendance2 = attendanceService.getAttendance(ding_id, begin_date);
if (null!=attendance2) {
attendance2.setOnDuty1checkResult(leave_name);
attendance2.setOnDuty2checkResult(leave_name);
attendance2.setOffDuty1checkResult(leave_name);
attendance2.setOffDuty2checkResult(leave_name);
attendance2.setGoout_count(1f);
attendanceService.updateAttendance(attendance2);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,begin_date,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,1f,null));
}
if (DateUtil.compateDate(end_date, begin_date)>0) {
do {
Attendance attendance3 = attendanceService.getAttendance(ding_id, nextDate);
if (null!=attendance3&&attendance3.getAttendanceTime()!=null) {
attendance3.setOnDuty1checkResult(leave_name);
attendance3.setOnDuty2checkResult(leave_name);
attendance3.setOffDuty1checkResult(leave_name);
attendance3.setOffDuty2checkResult(leave_name);
attendance3.setBussiness_count(1f);
attendanceService.updateAttendance(attendance3);
}else {
attendanceService.saveAttendance(new Attendance(ding_id,user_name,department,nextDate,null,leave_name,null,leave_name,null,leave_name,null,leave_name,null,null,1f,null));
}
nextDate=DateUtil.getNextDate(nextDate);
} while (DateUtil.compateDate(end_date, nextDate)>=0);
}
oaGooutService.updateGoout(id, 1);
continue;
}
}
}
}
}
Attendance attendance2 = attendanceService.getAttendance(ding_id, DateUtil.getlastDate());
if (null!=attendance2) {
Float yearLeave_count = attendance2.getYearLeave_count();
Float leave_count = attendance2.getLeave_count();
String onDuty1checkResult = attendance2.getOnDuty1checkResult();
String onDuty2checkResult = attendance2.getOnDuty2checkResult();
Attendance attendance = DingRecordSyncController.getAttendances(ding_id);
if (null!=leave_count&&leave_count==0.5) {
if (null!=onDuty1checkResult&&onDuty1checkResult!="") {
attendance2.setOnDuty2checkTime(attendance.getOnDuty2checkTime());
attendance2.setOnDuty2checkResult(attendance.getOnDuty2checkResult());
attendance2.setOffDuty2checkTime(attendance.getOffDuty2checkTime());
attendance2.setOffDuty2checkResult(attendance.getOffDuty2checkResult());
}else if (null!=onDuty2checkResult&&onDuty2checkResult!="") {
attendance2.setOnDuty1checkTime(attendance.getOnDuty1checkTime());
attendance2.setOnDuty1checkResult(attendance.getOnDuty1checkResult());
attendance2.setOffDuty1checkTime(attendance.getOffDuty1checkTime());
attendance2.setOffDuty1checkResult(attendance.getOffDuty1checkResult());
}
attendanceService.updateAttendance(attendance2);
}
if (null!=yearLeave_count&&yearLeave_count==0.5) {
if (null!=onDuty1checkResult&&onDuty1checkResult!="") {
attendance2.setOnDuty2checkTime(attendance.getOnDuty2checkTime());
attendance2.setOnDuty2checkResult(attendance.getOnDuty2checkResult());
attendance2.setOffDuty2checkTime(attendance.getOffDuty2checkTime());
attendance2.setOffDuty2checkResult(attendance.getOffDuty2checkResult());
}else if (null!=onDuty2checkResult&&onDuty2checkResult!="") {
attendance2.setOnDuty1checkTime(attendance.getOnDuty1checkTime());
attendance2.setOnDuty1checkResult(attendance.getOnDuty1checkResult());
attendance2.setOffDuty1checkTime(attendance.getOffDuty1checkTime());
attendance2.setOffDuty1checkResult(attendance.getOffDuty1checkResult());
}
attendanceService.updateAttendance(attendance2);
}
}
//取钉钉打卡记录
Attendance attendance1 = DingRecordSyncController.getAttendances(ding_id);
if (attendance1!=null&&attendance1.getName()!=null) {
attendanceService.saveAttendance(attendance1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
logger.info("OA数据同步结束!");
}
}
@Override
public void run() {
try {
UserService userService = (UserService) ApplicationContextUtil.getBean("userService");
List<UserBean> list = userService.getuserlist();
for (UserBean userBean : list) {
this.taskQueue.add(userBean.getDing_id()+"/"+userBean.getUser_account()+"/"+userBean.getUser_name());
}
logger.info("钉钉数据抓取开始!!!");
while (!Thread.currentThread().isInterrupted()) {
final String request = taskQueue.poll();
// 如果获取 request为空并且当前的线程采已经没有线程在运行
if (request == null) {
if (threadPool.getThreadAlive() == 0) {
break;
}
} else {
// 执行抓取任务
threadPool.execute(new Runnable() {
@Override
public void run() {
try {
processAttendance(request);
logger.info(pageCount.toString());
} catch (Exception e) {
logger.error("process request " + request + " error", e);
} finally {
pageCount.incrementAndGet();
}
}
});
}
}
threadPool.shutdown();
logger.info("钉钉数据抓取结束!!!", pageCount.get());
} catch (Exception e) {
e.printStackTrace();
}
}
public TimerTaskService() {
this.threadPool = new CountableThreadPool(3);
this.taskQueue = new LinkedBlockingQueue<>();
}
}
[源码](https://download.csdn.net/download/BinYanShao/12000832)