package com.engine.kq.job; import org.apache.commons.lang3.StringUtils; import weaver.conn.RecordSet; import weaver.general.TimeUtil; import weaver.general.Util; import weaver.interfaces.schedule.BaseCronJob; import java.text.SimpleDateFormat; import java.util.*; /** * @Title * @Author wangchaofa * @CreateDate 2024/1/7 * @Version 1.0 * @Description */ public class SynClockInTimeJob extends BaseCronJob { @Override public void execute() { RecordSet rs = new RecordSet(); rs.writeLog("----------- start to syn overtime work card time ------------"); String currentdate = TimeUtil.getCurrentDateString(); String currenttime = TimeUtil.getOnlyCurrentTimeString(); String currentdatetime = currentdate+" "+currenttime; String lastdate = addDateDay(currentdate,-50); String lastdatetime = lastdate+" "+currenttime; try{ RecordSet rs1 = new RecordSet(); //只查询 来源 是 导入的 rs.executeQuery("select * from uf_jbtz where ly=1 and ksrq>=? and jsrq<=?",lastdate,currentdate); while(rs.next()){ String mainid = Util.null2String(rs.getString("id")); String userid = Util.null2String(rs.getString("jbr")); String ksrq = Util.null2String(rs.getString("ksrq")); String kssj = Util.null2String(rs.getString("kssj")); String jsrq = Util.null2String(rs.getString("jsrq")); String jssj = Util.null2String(rs.getString("jssj")); String gzrq = Util.null2String(rs.getString("gzrq")); String starttime = ksrq+" "+kssj+":00"; String endtime = jsrq+" "+jssj+":00"; String beforetwohours_starttime = addDateHour(starttime,-2); String aftertwohours_endtime = addDateHour(endtime,2); String earlystarttime = getEarlyStartTime(beforetwohours_starttime,starttime,ksrq,userid); String afterendtime = getAfterEndTime(endtime,aftertwohours_endtime,jsrq,userid); boolean bool = rs1.executeUpdate("update uf_jbtz set zzdksj=?,zwdkrq=? where id=?",earlystarttime,afterendtime,mainid); if(bool){ rs.writeLog(userid+ " ------------ "+ kssj +" ------------ "+ jsrq +" ---------- "+ bool); // 计算有效时长 rs.writeLog("计算有效时长开始"); rs.writeLog("earlystarttime===" + earlystarttime + ",afterendtime===" + afterendtime + ",gzrq===" + gzrq); // 来源为导入,且最早、最晚打卡时间,归属日期不为空的数据 if (StringUtils.isNoneBlank(earlystarttime, afterendtime, gzrq)) { UpdateEffectiveDuration.execute(mainid, userid, gzrq, earlystarttime, afterendtime,starttime,endtime); } rs.writeLog("计算有效时长结束"); } } }catch(Exception e){ e.printStackTrace(); rs.writeLog("---------- syn overtime work card time error ---------- "+ e); } rs.writeLog("---------- end to syn overtime work card time ----------"); } /** * 获取 最早开始日期时间 * @param beforetwohours_starttime * @param starttime * @param ksrq * @param userid * @return */ public static String getEarlyStartTime(String beforetwohours_starttime,String starttime,String ksrq,String userid){ String result = starttime; RecordSet rs = new RecordSet(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List list = new ArrayList<>(); try { rs.executeQuery("select a.*,b.subcompanyId1,b.departmentId,b.jobtitle,b.workcode " + " from HrmScheduleSign a,HrmResource b " + " where a.userId=b.id and (signfrom is null or signfrom='' or signfrom not like 'card%') " + " and (signDate is not null and signDate>='"+ ksrq +"') " + " and (signDate is not null and signDate<='"+ ksrq +"') " + " and (loginId is not null and loginId<>'') " + " and signType=1 "+ " and (b.status = 0 or b.status = 1 or b.status = 2 or b.status = 3)" + " and userId in ("+ userid +")" + " order by signDate,signTime"); while (rs.next()) { String signDate = Util.null2String(rs.getString("signDate")); String signTime = Util.null2String(rs.getString("signTime")); String signdatetime = signDate + " " + signTime; boolean bool = belongCalendar(signdatetime, beforetwohours_starttime, starttime); if (bool) { list.add(format.parse(signdatetime)); } } if(list.size()>0){ if(list.size()>1){ result = format.format(Collections.min(list)); }else{ result = format.format(list.get(0)); } } }catch (Exception e){ e.printStackTrace(); } return result; } /** * 获取 最晚结束日期时间 * @param endtime * @param aftertwohours_endtime * @param jsrq * @param userid * @return */ public static String getAfterEndTime(String endtime,String aftertwohours_endtime,String jsrq,String userid){ String result = endtime; RecordSet rs = new RecordSet(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List list = new ArrayList<>(); try { rs.executeQuery("select a.*,b.subcompanyId1,b.departmentId,b.jobtitle,b.workcode " + " from HrmScheduleSign a,HrmResource b " + " where a.userId=b.id and (signfrom is null or signfrom='' or signfrom not like 'card%') " + " and (signDate is not null and signDate>='"+ jsrq +"') " + " and (signDate is not null and signDate<='"+ jsrq +"') " + " and (loginId is not null and loginId<>'') " + " and signType=2 "+ " and (b.status = 0 or b.status = 1 or b.status = 2 or b.status = 3)" + " and userId in ("+ userid +")" + " order by signDate,signTime"); while (rs.next()) { String signDate = Util.null2String(rs.getString("signDate")); String signTime = Util.null2String(rs.getString("signTime")); String signdatetime = signDate + " " + signTime; boolean bool = belongCalendar(signdatetime, endtime, aftertwohours_endtime); if (bool) { list.add(format.parse(signdatetime)); } } if(list.size()>0){ if(list.size()>1){ result = format.format(Collections.max(list)); }else{ result = format.format(list.get(0)); } } }catch (Exception e){ e.printStackTrace(); } return result; } /** * 判断一个时间是否在一个时间段内 * * @param nowString 当前时间 * @param beginString 开始时间 * @param endString 结束时间 */ public static boolean belongCalendar(String nowString, String beginString, String endString) { boolean b = false; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { if(nowString.equals(beginString)){ b = true; }else if(nowString.equals(endString)){ b = true; }else { Date nowTime = format.parse(nowString); Calendar date = Calendar.getInstance(); date.setTime(nowTime); Date beginTime = format.parse(beginString); Calendar begin = Calendar.getInstance(); begin.setTime(beginTime); Date endTime = format.parse(endString); Calendar end = Calendar.getInstance(); end.setTime(endTime); if (date.after(begin) && date.before(end)) {//在时间段内 b = true; } } }catch (Exception e){ e.printStackTrace(); } return b; } /** * 对时间的天数进行加减 * @param daytime 时间的格式:yyyy-MM-dd * @param day 天数 -1则代表减一天 * @return */ public static String addDateDay(String daytime, int day){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = format.parse(daytime); if (date == null){ return ""; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, day);// 加一天 date = cal.getTime(); cal = null; return format.format(date); } catch (Exception ex) { ex.printStackTrace(); } return ""; } /** * 给时间加上几个小时 * @param day 时间的格式:yyyy-MM-dd HH:mm:ss * @param hour 需要加的小时数 -1 则代表减1小时 * @return */ public static String addDateHour(String day, int hour){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(day); if (date == null){ return ""; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.HOUR, hour);// 24小时制 date = cal.getTime(); cal = null; return format.format(date); } catch (Exception ex) { ex.printStackTrace(); } return ""; } }