|
|
|
|
package com.dashboard.aws.lambda.handler;
|
|
|
|
|
|
|
|
|
|
import com.amazonaws.services.lambda.runtime.Context;
|
|
|
|
|
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
|
|
|
|
import com.dashboard.aws.lambda.Constants;
|
|
|
|
|
import com.dashboard.aws.lambda.service.MySQLService;
|
|
|
|
|
import com.dashboard.aws.lambda.service.S3Service;
|
|
|
|
|
import com.dashboard.aws.lambda.util.CsvUtil;
|
|
|
|
|
import com.dashboard.aws.lambda.util.DateUtil;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class S3ToMySQLHandler implements RequestHandler<Map<String, Object>, String> {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(S3ToMySQLHandler.class);
|
|
|
|
|
|
|
|
|
|
private final MySQLService mysqlService = new MySQLService();
|
|
|
|
|
private final S3Service s3Service = new S3Service();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String handleRequest(Map<String, Object> event, Context context) {
|
|
|
|
|
try {
|
|
|
|
|
// 同步目标日期(这里同步 3 天前的数据)
|
|
|
|
|
// LocalDate targetDate = LocalDate.now(ZoneId.of("Asia/Tokyo")).minusDays(3);
|
|
|
|
|
// String dateStr = targetDate.format(java.time.format.DateTimeFormatter.ISO_DATE);
|
|
|
|
|
// String dateStr = "2025-10-08";
|
|
|
|
|
ZoneId zone = ZoneId.of("Asia/Tokyo");
|
|
|
|
|
LocalDate now = DateUtil.resolveEventDate( event, zone);
|
|
|
|
|
String dateStr = DateUtil.getLastYearSameIsoWeekDayStr(now);
|
|
|
|
|
|
|
|
|
|
// --- 解析出 fileDate(用于删除早于该日期的数据) ---
|
|
|
|
|
LocalDate fileDate = LocalDate.parse(dateStr);
|
|
|
|
|
|
|
|
|
|
// 查询符合条件的企业ID
|
|
|
|
|
List<Long> companyIds = mysqlService.getActiveCompanyIds();
|
|
|
|
|
logger.info("company id list: {}", companyIds);
|
|
|
|
|
|
|
|
|
|
List<String> tables = Constants.tables;
|
|
|
|
|
|
|
|
|
|
for (Long companyId : companyIds) {
|
|
|
|
|
String schema = "data_center_dongjian_" + companyId;
|
|
|
|
|
|
|
|
|
|
for (String table : tables) {
|
|
|
|
|
//先删除旧数据
|
|
|
|
|
int deleted = mysqlService.deleteBeforeDate(schema, table, fileDate);
|
|
|
|
|
logger.info("[{}][{}] deleted {} records before {}", schema, table, deleted, fileDate);
|
|
|
|
|
|
|
|
|
|
// 构造 S3 文件路径
|
|
|
|
|
String s3Key = String.format("%s/%s/%s.csv", table, companyId, dateStr);
|
|
|
|
|
logger.info("processing s3 file: {}", s3Key);
|
|
|
|
|
|
|
|
|
|
// 下载 CSV
|
|
|
|
|
byte[] content = s3Service.download(s3Key);
|
|
|
|
|
if (content == null || content.length == 0) {
|
|
|
|
|
logger.warn("[{}][{}] file empty or not found: {}", schema, table, s3Key);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析 CSV
|
|
|
|
|
List<Map<String, String>> rows = CsvUtil.parseCsv(content);
|
|
|
|
|
if (rows.isEmpty()) {
|
|
|
|
|
logger.warn("[{}][{}] csv empty: {}", schema, table, s3Key);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入到 MySQL
|
|
|
|
|
try {
|
|
|
|
|
int inserted = mysqlService.insertRows(schema, table, rows);
|
|
|
|
|
logger.info("[{}][{}] inserted {} rows", schema, table, inserted);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("[{}][{}] insert failed -> {}", schema, table, s3Key, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "sync process finished";
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("sync error", e);
|
|
|
|
|
return "sync error:" + e.getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|