BigDataTableBackupTask.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package cn.com.lzt.timertask;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.apache.log4j.Logger;
  5. import org.jeecgframework.core.constant.Globals;
  6. import org.jeecgframework.core.util.ApplicationContextUtil;
  7. import org.jeecgframework.core.util.DataUtils;
  8. import org.jeecgframework.web.system.service.SystemService;
  9. import org.quartz.JobExecutionContext;
  10. import org.quartz.JobExecutionException;
  11. import org.springframework.stereotype.Service;
  12. import com.xcgl.utils.XcglDateUtils;
  13. @Service("bigDataTableBackupTask")
  14. public class BigDataTableBackupTask implements org.quartz.Job {
  15. private static final Logger logger = Logger.getLogger(BigDataTableBackupTask.class);
  16. @Override
  17. public void execute(JobExecutionContext arg0) throws JobExecutionException {
  18. long start = System.currentTimeMillis();
  19. logger.error("===================大数据量表备份任务开始===================");
  20. try {
  21. logger.error("===================大数据量表备份任务开始"+DataUtils.datetimeFormat.format(new Date()));
  22. //外包服务合同生效
  23. SystemService service = ApplicationContextUtil.getContext().getBean(SystemService.class);
  24. String attendance_totalSQL = "INSERT INTO t_bus_user_attendance_total_h(id, user_id, user_code, pid, departid, in_postid, workingdays, yearmonth, datum_attendance_date, actual_attendance_date, time_difference_total, adjust_time, owe_time, overtime_total_time, leave_total_time, adjust_record, attendance_status, early_leave_amount, late_amount, absenteeism_amount_orig, absenteeism_amount, no_punch_card_amount, paid_leave, thing_leave, sick_leave, marriage_leave, funeral_leave, maternity_leave, accompany_maternity_leave, national_duty_duration, timeout_overtime_duration_orig, timeout_overtime_duration, national_overtime_duration, duty_duration, special_overtime, day_shift_subsidy, night_shift_subsidy, day_shift, regular_shift, fixed_value, extra_overtime, confirm_status, confirm_date, confirmor, ifmoved, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code, bpm_status, json_obj,exchangetime) \n" +
  25. "select id, user_id, user_code, pid, departid, in_postid, workingdays, yearmonth, datum_attendance_date, actual_attendance_date, time_difference_total, adjust_time, owe_time, overtime_total_time, leave_total_time, adjust_record, attendance_status, early_leave_amount, late_amount, absenteeism_amount_orig, absenteeism_amount, no_punch_card_amount, paid_leave, thing_leave, sick_leave, marriage_leave, funeral_leave, maternity_leave, accompany_maternity_leave, national_duty_duration, timeout_overtime_duration_orig, timeout_overtime_duration, national_overtime_duration, duty_duration, special_overtime, day_shift_subsidy, night_shift_subsidy, day_shift, regular_shift, fixed_value, extra_overtime, confirm_status, confirm_date, confirmor, ifmoved, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code, bpm_status, json_obj,exchangetime \n" +
  26. "from t_bus_user_attendance_total \n"
  27. +" where yearmonth < ?;";
  28. String attendance_detailSQL = "INSERT INTO t_bus_user_attendance_detail_h(id, attendance_id, status, attendance_date, retreat_date, duty_id, datum_date, time_difference, fine_tuning_type, time_fine_tuning, adjust_reason, borrowstatus, borrowspid, borrowsdepartid, attendance_mins, timeout_overtime_duration, national_overtime_duration, special_overtime, day_pay, actualattendance_date, json_obj, ymd_date) \n" +
  29. "select id, attendance_id, status, attendance_date, retreat_date, duty_id, datum_date, time_difference, fine_tuning_type, time_fine_tuning, adjust_reason, borrowstatus, borrowspid, borrowsdepartid, attendance_mins, timeout_overtime_duration, national_overtime_duration, special_overtime, day_pay, actualattendance_date, json_obj, ymd_date\n" +
  30. "from t_bus_user_attendance_detail \n" +
  31. "where left(ymd_date,7) < ?";
  32. String attendance_tempSQL = "INSERT INTO t_bus_attendance_temp_h(id, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code, bpm_status, user_id, user_code, realname, belong_unitid, attendance_date, datum_stime, datum_etime, sign_date, newest_punchcard_date, attendance_status, arrange_duty_id, attendance_datetime, retreat_datetime, alldatetime, lateminute, leaveearlyminute)\n" +
  33. "select id, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code, bpm_status, user_id, user_code, realname, belong_unitid, attendance_date, datum_stime, datum_etime, sign_date, newest_punchcard_date, attendance_status, arrange_duty_id, attendance_datetime, retreat_datetime, alldatetime, lateminute, leaveearlyminute\n" +
  34. "from t_bus_attendance_temp \n" +
  35. "where left(attendance_date,7) < ?";
  36. String dingdingSQL = "INSERT INTO t_bus_ding_attendanceinfo_h(id, user_code, belong_unitid, attendance_date, create_by, create_name, sys_company_code, ding_uniqueid, update_by, sys_org_code, bpm_status, create_date, update_name, update_date, attendance_time, user_id, realname, is_dispose)\n" +
  37. "select id, user_code, belong_unitid, attendance_date, create_by, create_name, sys_company_code, ding_uniqueid, update_by, sys_org_code, bpm_status, create_date, update_name, update_date, attendance_time, user_id, realname, is_dispose\n" +
  38. "from t_bus_ding_attendanceinfo\n" +
  39. "where left(attendance_date,7) < ?";
  40. String arrange_general = "INSERT INTO t_bus_projarrange_general_h(id, yearmonth, belong_unitid, people_quantity, arrange_duty_status, reporter_id, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code) \n" +
  41. "select id, yearmonth, belong_unitid, people_quantity, arrange_duty_status, reporter_id, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code\n" +
  42. "from t_bus_projarrange_general \n" +
  43. "where yearmonth < ?";
  44. String arrange_detail = "INSERT INTO t_bus_projarrange_detail_h(id, projarrange_generalid, pjt_postid, userid, working_days, timeout_overtime_hours, country_overtime_hours, onduty_days, remark, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code, yearmonth, pid)\n" +
  45. "select id, projarrange_generalid, pjt_postid, userid, working_days, timeout_overtime_hours, country_overtime_hours, onduty_days, remark, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code, yearmonth, pid \n" +
  46. "from t_bus_projarrange_detail \n" +
  47. "where yearmonth < ?";
  48. String arrange_user = "INSERT INTO t_bus_user_arrange_detail_h(id, calendarid, shiftid, projarrange_detailid, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code) \n" +
  49. "select id, calendarid, shiftid, projarrange_detailid, create_name, create_by, create_date, update_name, update_by, update_date, sys_org_code, sys_company_code\n" +
  50. "from t_bus_user_arrange_detail\n" +
  51. "where projarrange_detailid in (select id from t_bus_projarrange_detail where yearmonth < ?)";
  52. String attendance_totalSQL_Del = "delete " +
  53. "from t_bus_user_attendance_total \n"
  54. +" where yearmonth < ?;";
  55. String attendance_detailSQL_Del = "delete " +
  56. "from t_bus_user_attendance_detail \n" +
  57. "where left(ymd_date,7) < ?";
  58. String attendance_tempSQL_Del = "delete " +
  59. "from t_bus_attendance_temp \n" +
  60. "where left(attendance_date,7) < ?";
  61. String dingdingSQL_Del = "delete " +
  62. "from t_bus_ding_attendanceinfo\n" +
  63. "where left(attendance_date,7) < ?";
  64. String arrange_user_Del = "delete " +
  65. "from t_bus_user_arrange_detail \n"
  66. +" where projarrange_detailid in (select id from t_bus_projarrange_detail where yearmonth < ?)";
  67. String arrange_detail_Del = "delete " +
  68. "from t_bus_projarrange_detail \n"
  69. +" where yearmonth < ?";
  70. String arrange_general_Del = "delete " +
  71. "from t_bus_projarrange_general \n"
  72. +" where yearmonth < ?";
  73. //例如:7月份备份5月份之前的数据,保留5、6月份2个月
  74. String backupMonth = XcglDateUtils.addDateMonth(new Date(), -2,new SimpleDateFormat("yyyy-MM"));
  75. Integer count = 0;
  76. //考勤数据
  77. count = service.executeSql(attendance_totalSQL, backupMonth);
  78. logger.error("备份"+backupMonth+"前数据,表:attendance_total,数据"+count+"条");
  79. count = service.executeSql(attendance_detailSQL, backupMonth);
  80. logger.error("备份"+backupMonth+"前数据,表:attendance_detail,数据"+count+"条");
  81. count = service.executeSql(attendance_tempSQL, backupMonth);
  82. logger.error("备份"+backupMonth+"前数据,表:attendance_temp,数据"+count+"条");
  83. //钉钉数据
  84. count = service.executeSql(dingdingSQL, backupMonth);
  85. logger.error("备份"+backupMonth+"前数据,表:ding_attendanceinfo,数据"+count+"条");
  86. //排班主子孙表
  87. count = service.executeSql(arrange_general, backupMonth);
  88. logger.error("备份"+backupMonth+"前数据,表:arrange_general,数据"+count+"条");
  89. count = service.executeSql(arrange_detail, backupMonth);
  90. logger.error("备份"+backupMonth+"前数据,表:arrange_detail,数据"+count+"条");
  91. count = service.executeSql(arrange_user, backupMonth);
  92. logger.error("备份"+backupMonth+"前数据,表:arrange_user,数据"+count+"条");
  93. //删除原表数据
  94. count = service.executeSql(dingdingSQL_Del, backupMonth);
  95. logger.error("删除"+backupMonth+"前数据,表:ding_attendanceinfo,数据"+count+"条");
  96. count = service.executeSql(attendance_tempSQL_Del, backupMonth);
  97. logger.error("删除"+backupMonth+"前数据,表:attendance_temp,数据"+count+"条");
  98. count = service.executeSql(attendance_detailSQL_Del, backupMonth);
  99. logger.error("删除"+backupMonth+"前数据,表:attendance_detail,数据"+count+"条");
  100. count = service.executeSql(attendance_totalSQL_Del, backupMonth);
  101. logger.error("删除"+backupMonth+"前数据,表:attendance_total,数据"+count+"条");
  102. count = service.executeSql(arrange_user_Del, backupMonth);
  103. logger.error("删除"+backupMonth+"前数据,表:arrange_user,数据"+count+"条");
  104. count = service.executeSql(arrange_detail_Del, backupMonth);
  105. logger.error("删除"+backupMonth+"前数据,表:arrange_detail,数据"+count+"条");
  106. count = service.executeSql(arrange_general_Del, backupMonth);
  107. logger.error("删除"+backupMonth+"前数据,表:arrange_general,数据"+count+"条");
  108. logger.error("===================大数据量表备份任务结束"+DataUtils.datetimeFormat.format(new Date()));
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. logger.error("===================大数据量表备份任务结束===================");
  113. long end = System.currentTimeMillis();
  114. long times = end - start;
  115. logger.error("总耗时"+times+"毫秒");
  116. }
  117. }