Commit 057401b8 by 张富康

refactor(perfSalary): 支持考勤数据清洗传入可配置日期参数

- AttendanceDataCleansingJob注解说明添加可配置日期格式提示
- execute方法调用attendanceDataCleansing时传入JobParameter参数
- AttendanceDataCleansingService方法attendanceDataCleansing增加日期参数statDate
- 按传入日期参数或默认上月计算处理考勤数据时间范围
- 修改相关SQL条件设置为动态月份参数
- 批处理和单个员工处理方法调用改为传入动态月份日期对象
- 失败记录中考勤月份改为动态传入参数对应月份
1 parent 924059b2
......@@ -9,7 +9,7 @@ import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf;
import javax.annotation.Resource;
@Slf4j
@ElasticJobConf(name = "AttendanceDataCleansing", cron = "0 0 6 1 * ? ", description = "考勤数据清洗", overwrite = false)
@ElasticJobConf(name = "AttendanceDataCleansing", cron = "0 0 6 1 * ? ", description = "考勤数据清洗-可配参数格式(yyyy-MM-dd)", overwrite = false)
public class AttendanceDataCleansingJob implements SimpleJob {
@Resource
......@@ -20,7 +20,7 @@ public class AttendanceDataCleansingJob implements SimpleJob {
public void execute(ShardingContext shardingContext) {
log.info("----------------考勤数据清洗开始-----------------");
try {
attendanceDataCleansingService.attendanceDataCleansing();
attendanceDataCleansingService.attendanceDataCleansing(shardingContext.getJobParameter());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
......
......@@ -107,7 +107,7 @@ public class AttendanceDataCleansingService {
* 5. 优化内存管理,避免内存溢出
* 6. 使用数据库唯一索引实现幂等性
*/
public void attendanceDataCleansing() {
public void attendanceDataCleansing(String statDate) {
long startTime = System.currentTimeMillis();
try {
// 1. 初始化考勤打卡门店清单
......@@ -119,15 +119,16 @@ public class AttendanceDataCleansingService {
// 2. 获取上月在职或当月离职的员工数据
Example staffBeisenBaseExample = new Example(StaffBeisenBaseInfo.class);
// 上个月
DateTime lastMonthDate = DateUtil.offsetMonth(new Date(), -1);
DateTime lastMonthYMD = DateUtil.beginOfMonth(lastMonthDate);
String lastMonth = DateUtil.format(lastMonthDate, DatePattern.NORM_MONTH_PATTERN);
log.info("开始处理考勤数据,时间范围:{}", lastMonth);
DateTime monthDate = StrUtil.isNotBlank(statDate) ? DateUtil.parse(statDate) : DateUtil.offsetMonth(new Date(), -1);
DateTime MonthYMD = DateUtil.beginOfMonth(monthDate);
// yyyy-MM
String month = DateUtil.format(monthDate, DatePattern.NORM_MONTH_PATTERN);
log.info("开始处理考勤数据,时间范围:{}", month);
String staffBeisenBaseCondition1 = "(DATE_FORMAT(last_work_date, '%Y-%m') = '{}' or last_work_date is null)";
staffBeisenBaseCondition1 = StrUtil.format(staffBeisenBaseCondition1, lastMonth);
staffBeisenBaseCondition1 = StrUtil.format(staffBeisenBaseCondition1, month);
String staffBeisenBaseCondition2 = "DATE_FORMAT(entry_date, '%Y-%m') <= '{}'";
staffBeisenBaseCondition2 = StrUtil.format(staffBeisenBaseCondition2, lastMonth);
staffBeisenBaseCondition2 = StrUtil.format(staffBeisenBaseCondition2, month);
staffBeisenBaseExample.createCriteria().andEqualTo("deletedFlag", 'N')
.andCondition(staffBeisenBaseCondition1).andCondition(staffBeisenBaseCondition2);
List<StaffBeisenBaseInfo> staffBeiSenBaseInfos = staffBeisenBaseInfoMapper.selectByExample(staffBeisenBaseExample);
......@@ -150,12 +151,12 @@ public class AttendanceDataCleansingService {
for (List<StaffBeisenBaseInfo> batch : employeeBatches) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
processEmployeeBatch(batch, lastMonthYMD);
processEmployeeBatch(batch, MonthYMD);
} catch (Exception e) {
log.error("处理员工批次失败", e);
// 记录失败到失败列表
batch.forEach(employee -> {
recordFailure(employee.getStaffPhone(), lastMonth, e.getMessage());
recordFailure(employee.getStaffPhone(), month, e.getMessage());
});
}
}, taskExecutor);
......@@ -172,7 +173,7 @@ public class AttendanceDataCleansingService {
} else {
log.info("使用串行处理考勤数据,批处理大小: {}", EMPLOYEE_BATCH_SIZE);
for (List<StaffBeisenBaseInfo> batch : employeeBatches) {
processEmployeeBatch(batch, lastMonthYMD);
processEmployeeBatch(batch, MonthYMD);
}
}
......@@ -194,12 +195,12 @@ public class AttendanceDataCleansingService {
/**
* 处理单个员工批次
*/
private void processEmployeeBatch(List<StaffBeisenBaseInfo> employeeList, DateTime lastMonthYMD) {
private void processEmployeeBatch(List<StaffBeisenBaseInfo> employeeList, DateTime monthYMD) {
// 1. 根据手机号查询当月考勤数据
List<String> mobilePhones = employeeList.stream().map(StaffBeisenBaseInfo::getStaffPhone).collect(Collectors.toList());
Example staffQwClockInInfoExample = new Example(StaffQwClockClean.class);
String staffQwClockInInfoCondition = "(DATE_FORMAT(clock_in_date, '%Y-%m') = '{}')";
staffQwClockInInfoCondition = StrUtil.format(staffQwClockInInfoCondition, DateUtil.format(lastMonthYMD, DatePattern.NORM_MONTH_PATTERN));
staffQwClockInInfoCondition = StrUtil.format(staffQwClockInInfoCondition, DateUtil.format(monthYMD, DatePattern.NORM_MONTH_PATTERN));
staffQwClockInInfoExample.createCriteria().andIn("staffPhone", mobilePhones).andCondition(staffQwClockInInfoCondition).andEqualTo("deletedFlag", 'N');
List<StaffQwClockClean> staffQwClockCleans = staffQwClockCleanMapper.selectByExample(staffQwClockInInfoExample);
log.info("获取到 {} 条员工考勤数据", staffQwClockCleans.size());
......@@ -213,11 +214,11 @@ public class AttendanceDataCleansingService {
for (StaffBeisenBaseInfo employee : employeeList) {
try {
// 4. 生成考勤摘要记录
AttendanceSummary attendanceSummary = createAttendanceSummary(employee, staffQwClockCleanMap, lastMonthYMD);
AttendanceSummary attendanceSummary = createAttendanceSummary(employee, staffQwClockCleanMap, monthYMD);
attendanceSummariesInsert.add(attendanceSummary);
} catch (Exception e) {
log.error("处理员工 {} ({}) 考勤数据失败", employee.getStaffName(), employee.getStaffPhone(), e);
recordFailure(employee.getStaffPhone(), DateUtil.format(lastMonthYMD, DatePattern.NORM_MONTH_PATTERN), e.getMessage());
recordFailure(employee.getStaffPhone(), DateUtil.format(monthYMD, DatePattern.NORM_MONTH_PATTERN), e.getMessage());
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!