Commit c40d7ec7 by 刘栋

UDI-转换注册证

parent 61b75606
package com.infynova.udi.service.core;
import com.infynova.udi.entity.Udi;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.util.CollectionUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author liudong
* 2024/5/20 19:48
* @version 1.0
*/
@Slf4j
public class RegistryMain {
private static final HikariDataSource dataSource;
private static final String url = "jdbc:mysql://rm-wz92456b388ahxujbuo.mysql.rds.aliyuncs.com:3306/saas_base_test";
private static final String username = "saas_test";
private static final String password = "qjCEgXg2z9U5mjL7";
static {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
dataSource = new HikariDataSource(config);
}
public static void main(String[] args) {
RegistryMain registryMain = new RegistryMain();
registryMain.getProductDataFromAPI();
}
// 接口获取产品数据的方法
public void getProductDataFromAPI() {
ExecutorService executorService = Executors.newFixedThreadPool(10);
cn.hutool.core.date.StopWatch sw = new cn.hutool.core.date.StopWatch();
sw.start("开始");
// 在这里实现从接口获取产品数据的逻辑
// 返回一个包含产品对象的列表
int pageNo = 1;
int pageSize = 5000;
Long lastId = 0L;
int insertNum = 0;
while (true){
if(lastId % 100000 == 0){
sw.stop();
log.info("完成10万的耗时:{}s",sw.getTotalTimeSeconds());
sw.start();
}
StopWatch stopWatch = new StopWatch();
try {
stopWatch.start();
List<Udi> udiList = queryMysqlUdi(lastId,pageSize);
if(CollectionUtils.isEmpty(udiList)){
break;
}
// 保存到新的注册证表
this.saveRegistry(udiList);
lastId = udiList.get(udiList.size() - 1).getId();
} catch (Exception e) {
e.printStackTrace();
log.info("循环错误中断:lastId:{} pageSize:{}",lastId,pageSize);
break;
}finally {
stopWatch.stop();
log.info("查询udi数据库: lastId:{} pageSize:{} 查询+插入耗时:{}", lastId, pageSize, stopWatch.getTime());
}
pageNo++;
}
executorService.shutdown();
sw.stop();
log.info("完成全部的耗时:{}s",sw.getTotalTimeSeconds());
}
private List<Udi> queryMysqlUdi(Long lastId, int pageSize){
List<Udi> udiList = new ArrayList<>();
String sql = "SELECT * FROM product WHERE id > ? ORDER BY id LIMIT ?";
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, lastId); // OFFSET
statement.setInt(2, pageSize); // LIMIT
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
Udi udi = mapResultSetToUdi(resultSet);
udiList.add(udi);
}
}
} catch (SQLException e) {
e.printStackTrace();
// 处理异常
}
return udiList;
}
private Udi mapResultSetToUdi(ResultSet resultSet) throws SQLException {
// 获取每一行数据的字段值
Long id = resultSet.getLong("id");
String udiCode = resultSet.getString("udi_code");
String yiBaoOneCode = resultSet.getString("yi_bao_one_code");
String yiBaoTwoCode = resultSet.getString("yi_bao_two_code");
String yiBaoCode = resultSet.getString("yi_bao_code");
String yiBaoCodePrefix = resultSet.getString("yi_bao_code_prefix");
String companyName = resultSet.getString("company_name");
String brandName = resultSet.getString("brand_name");
String registryNo = resultSet.getString("registry_no");
String registryName = resultSet.getString("registry_name");
LocalDate registryStartTime = resultSet.getDate("registry_start_time").toLocalDate();
LocalDate registryEndTime = resultSet.getDate("registry_end_time").toLocalDate();
String registry = resultSet.getString("registry");
String productCode = resultSet.getString("product_code");
String productFactoryCode = resultSet.getString("product_factory_code");
String infynovaCode = resultSet.getString("infynova_code");
String productName = resultSet.getString("product_name");
String productType = resultSet.getString("product_type");
String specification = resultSet.getString("specification");
String model = resultSet.getString("model");
String material = resultSet.getString("material");
Boolean asepticPackaging = resultSet.getBoolean("aseptic_packaging");
Boolean beforeSterilize = resultSet.getBoolean("before_sterilize");
String sterilizationMethod = resultSet.getString("sterilization_method");
String yjForeignId = resultSet.getString("yj_foreign_id");
String ybForeignId = resultSet.getString("yb_foreign_id");
String sourceName = resultSet.getString("source_name");
LocalDateTime createTime = resultSet.getTimestamp("create_time").toLocalDateTime();
LocalDateTime updateTime = resultSet.getTimestamp("update_time").toLocalDateTime();
String version = resultSet.getString("version");
// 创建 Udi 对象并进行进一步处理
Udi udi = new Udi();
udi.setId(id);
udi.setUdiCode(udiCode);
udi.setYiBaoOneCode(yiBaoOneCode);
udi.setYiBaoTwoCode(yiBaoTwoCode);
udi.setYiBaoCode(yiBaoCode);
udi.setYiBaoCodePrefix(yiBaoCodePrefix);
udi.setCompanyName(companyName);
udi.setBrandName(brandName);
udi.setRegistryNo(registryNo);
udi.setRegistryName(registryName);
udi.setRegistryStartTime(registryStartTime);
udi.setRegistryEndTime(registryEndTime);
udi.setRegistry(registry);
udi.setProductCode(productCode);
udi.setProductFactoryCode(productFactoryCode);
udi.setInfynovaCode(infynovaCode);
udi.setProductName(productName);
udi.setProductType(productType);
udi.setSpecification(specification);
udi.setModel(model);
udi.setMaterial(material);
udi.setAsepticPackaging(asepticPackaging);
udi.setBeforeSterilize(beforeSterilize);
udi.setSterilizationMethod(sterilizationMethod);
udi.setYjForeignId(yjForeignId);
udi.setYbForeignId(ybForeignId);
udi.setSourceName(sourceName);
udi.setCreateTime(createTime);
udi.setUpdateTime(updateTime);
udi.setVersion(version);
// 将其他字段的映射也移到这里
return udi;
}
private int saveRegistry(List<Udi> udiList){
Set<String> registryNos = new HashSet<>();
for (Udi udi : udiList) {
String registryNo = udi.getRegistryNo();
String[] split = StringUtils.split(registryNo,",");
if(Objects.nonNull(split)){
for (String registry : split) {
registryNos.add(registry);
}
}
}
log.info("registry_no:{}", String.join(",", registryNos));
return sqlSave(registryNos);
}
private int sqlSave(Set<String> set){
int total = 0;
String insertSql = "INSERT INTO registry_temp(registry_no) values(?)";
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(insertSql)) {
conn.setAutoCommit(false);
for (String registryNo : set) {
pstmt.setString(1, registryNo);
pstmt.addBatch();
}
int[] ints = pstmt.executeBatch();
conn.commit();
total = ints.length;
log.info("原本set数量:{},插入registry数量:{}",set.size(),total);
} catch (SQLException e) {
log.info("Error saving product data to SQLite: " + e.getMessage());
}
return total;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment