From e9b8260d9494ee3bb89a8a88b19f9a4afb916d89 Mon Sep 17 00:00:00 2001 From: xd <844539747@qq.com> Date: Wed, 13 Mar 2024 08:36:41 +0800 Subject: [PATCH] =?UTF-8?q?'=E5=AE=9A=E6=97=B6=E5=90=8C=E6=AD=A5=E7=BA=A2?= =?UTF-8?q?=E6=9C=AC=E7=89=A9=E6=96=99=E4=BB=B7=E6=A0=BC'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quartz/domain/c_rb_product_price.java | 40 +++++ .../quartz/domain/c_rb_productbase_price.java | 40 +++++ .../com/ruoyi/quartz/mapper/SapTjMapper.java | 68 ++++++++ .../ruoyi/quartz/service/SapTjService.java | 69 ++++++++ .../quartz/service/impl/SapTjServiceImpl.java | 110 ++++++++++++ .../java/com/ruoyi/quartz/task/RbTask.java | 65 +++++++ .../ruoyi/quartz/util/JDBCBatchInsert.java | 158 ++++++++++++++++++ .../resources/mapper/quartz/SapTjMapper.xml | 41 +++++ 8 files changed, 591 insertions(+) create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_product_price.java create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_productbase_price.java create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SapTjMapper.java create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/SapTjService.java create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SapTjServiceImpl.java create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RbTask.java create mode 100644 ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JDBCBatchInsert.java create mode 100644 ruoyi-quartz/src/main/resources/mapper/quartz/SapTjMapper.xml diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_product_price.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_product_price.java new file mode 100644 index 0000000..5a3028a --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_product_price.java @@ -0,0 +1,40 @@ +package com.ruoyi.quartz.domain; + +public class c_rb_product_price { + private String material_xingh; + private String material_guig; + private String material_diany; + private String material_rb_price; + + public String getMaterial_xingh() { + return material_xingh; + } + + public void setMaterial_xingh(String material_xingh) { + this.material_xingh = material_xingh; + } + + public String getMaterial_guig() { + return material_guig; + } + + public void setMaterial_guig(String material_guig) { + this.material_guig = material_guig; + } + + public String getMaterial_diany() { + return material_diany; + } + + public void setMaterial_diany(String material_diany) { + this.material_diany = material_diany; + } + + public String getMaterial_rb_price() { + return material_rb_price; + } + + public void setMaterial_rb_price(String material_rb_price) { + this.material_rb_price = material_rb_price; + } +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_productbase_price.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_productbase_price.java new file mode 100644 index 0000000..e2ceee4 --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/c_rb_productbase_price.java @@ -0,0 +1,40 @@ +package com.ruoyi.quartz.domain; + +public class c_rb_productbase_price { + private String material_xingh; + private String material_guig; + private String material_diany; + private String material_cb_price; + + public String getMaterial_xingh() { + return material_xingh; + } + + public void setMaterial_xingh(String material_xingh) { + this.material_xingh = material_xingh; + } + + public String getMaterial_guig() { + return material_guig; + } + + public void setMaterial_guig(String material_guig) { + this.material_guig = material_guig; + } + + public String getMaterial_diany() { + return material_diany; + } + + public void setMaterial_diany(String material_diany) { + this.material_diany = material_diany; + } + + public String getMaterial_cb_price() { + return material_cb_price; + } + + public void setMaterial_cb_price(String material_cb_price) { + this.material_cb_price = material_cb_price; + } +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SapTjMapper.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SapTjMapper.java new file mode 100644 index 0000000..5b68050 --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SapTjMapper.java @@ -0,0 +1,68 @@ +package com.ruoyi.quartz.mapper; + +import com.ruoyi.quartz.domain.c_rb_product_price; +import com.ruoyi.quartz.domain.c_rb_productbase_price; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * Created by xuxueli on 16/9/30. + */ +public interface SapTjMapper { + /** + * 获取红本调价版本 + * @return + */ + String selRbVersionUid(); + + /** + * 获取本地红本调价版本 + * @return + */ + String selLocVersionUid(); + + /** + * 获取红本价格 + * @param rbVersionUid + * @return + */ + List getProductPriceList(String rbVersionUid); + + /** + * 获取红本成本价格 + * @param rbVersionUid + * @return + */ + List getProductBasePriceList(String rbVersionUid); + + /** + * 删除本地红本价格表 + */ + void deleteLocProductPrice(); + + /** + * 删除本地红本成本价格表 + */ + void deleteLocProductBasePrice(); + + /** + * RbProductPrice表是否有更新 + * @param rbVersionUid + * @return + */ + String selRbProductPriceVersionUid(String rbVersionUid); + + /** + * RbProductBasePrice表是否有更新 + * @param rbVersionUid + * @return + */ + String selRbProductBasePriceVersionUid(String rbVersionUid); + + /** + * 更新本地c_rb_version表 + * @param rbVersionUid + */ + void updateLocRbVersion(String rbVersionUid); +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/SapTjService.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/SapTjService.java new file mode 100644 index 0000000..e9dc3bd --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/SapTjService.java @@ -0,0 +1,69 @@ +package com.ruoyi.quartz.service; + +import com.ruoyi.quartz.domain.c_rb_product_price; +import com.ruoyi.quartz.domain.c_rb_productbase_price; + +import java.util.List; + +/** + * SAP库存数据批量插入中间表 + */ + +public interface SapTjService { + + /** + * 获取红本调价版本 + * @return + */ + public String selRbVersionUid(); + + /** + * 获取本地红本调价版本 + * @return + */ + public String selLocVersionUid(); + + /** + * 获取红本价格 + * @param rbVersionUid + * @return + */ + List getProductPriceList(String rbVersionUid); + + /** + * 获取红本成本价格 + * @param rbVersionUid + * @return + */ + List getProductBasePriceList(String rbVersionUid); + + /** + * 删除本地红本价格表 + */ + void deleteLocProductPrice(); + + /** + * 删除本地红本成本价格表 + */ + void deleteLocProductBasePrice(); + + /** + * RbProductPrice表是否有更新 + * @param rbVersionUid + * @return + */ + String selRbProductPriceVersionUid(String rbVersionUid); + + /** + * RbProductBasePrice表是否有更新 + * @param rbVersionUid + * @return + */ + String selRbProductBasePriceVersionUid(String rbVersionUid); + + /** + * 更新本地c_rb_version表 + * @param rbVersionUid + */ + void updateLocRbVersion(String rbVersionUid); +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SapTjServiceImpl.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SapTjServiceImpl.java new file mode 100644 index 0000000..8d8d073 --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SapTjServiceImpl.java @@ -0,0 +1,110 @@ +package com.ruoyi.quartz.service.impl; +import com.ruoyi.common.annotation.DataSource; +import com.ruoyi.common.enums.DataSourceType; +import com.ruoyi.quartz.domain.c_rb_product_price; +import com.ruoyi.quartz.domain.c_rb_productbase_price; +import com.ruoyi.quartz.mapper.SapTjMapper; +import com.ruoyi.quartz.service.SapTjService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + + +/** + * core job action for xxl-job + * @author xuxueli 2016-5-28 15:30:33 + */ +@Service +public class SapTjServiceImpl implements SapTjService { + + @Resource + private SapTjMapper sapTjMapper; + + /** + * 获取红本调价版本 + * @return + */ + @DataSource(DataSourceType.REDBOOK) + @Override + public String selRbVersionUid() { + return sapTjMapper.selRbVersionUid(); + } + + /** + * 获取本地红本调价版本 + * @return + */ + @DataSource(DataSourceType.QUOT) + @Override + public String selLocVersionUid() {return sapTjMapper.selLocVersionUid(); } + + + /** + * 获取红本价格 + * @param rbVersionUid + * @return + */ + @DataSource(DataSourceType.REDBOOK) + @Override + public List getProductPriceList(String rbVersionUid) {return sapTjMapper.getProductPriceList(rbVersionUid); } + + /** + * 获取红本成本价格 + * @param rbVersionUid + * @return + */ + @DataSource(DataSourceType.REDBOOK) + @Override + public List getProductBasePriceList(String rbVersionUid) {return sapTjMapper.getProductBasePriceList(rbVersionUid); } + + /** + * 删除本地红本价格表 + */ + @DataSource(DataSourceType.QUOT) + @Override + public void deleteLocProductPrice() {sapTjMapper.deleteLocProductPrice(); } + + /** + * 删除本地红本成本价格表 + */ + @DataSource(DataSourceType.QUOT) + @Override + public void deleteLocProductBasePrice() {sapTjMapper.deleteLocProductBasePrice(); } + + /** + * RbProductPrice表是否有更新 + * @param rbVersionUid + * @return + */ + @DataSource(DataSourceType.REDBOOK) + @Override + public String selRbProductPriceVersionUid(String rbVersionUid) { + return sapTjMapper.selRbProductPriceVersionUid(rbVersionUid); + } + + /** + * RbProductBasePrice表是否有更新 + * @param rbVersionUid + * @return + */ + @DataSource(DataSourceType.REDBOOK) + @Override + public String selRbProductBasePriceVersionUid(String rbVersionUid) { + return sapTjMapper.selRbProductBasePriceVersionUid(rbVersionUid); + } + + /** + * 更新本地c_rb_version表 + * @param rbVersionUid + */ + @DataSource(DataSourceType.QUOT) + @Override + public void updateLocRbVersion(String rbVersionUid) { + sapTjMapper.updateLocRbVersion(rbVersionUid); + } + + +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RbTask.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RbTask.java new file mode 100644 index 0000000..6d7a97f --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RbTask.java @@ -0,0 +1,65 @@ +package com.ruoyi.quartz.task; + +import com.ruoyi.common.annotation.DataSource; +import com.ruoyi.common.enums.DataSourceType; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.quartz.domain.c_rb_product_price; +import com.ruoyi.quartz.domain.c_rb_productbase_price; +import com.ruoyi.quartz.service.SapTjService; +import com.ruoyi.quartz.util.JDBCBatchInsert; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.List; + +/** + * 定时任务调度-同步红本数据库 + * + * @author ruoyi + */ +@Component("rbTask") +public class RbTask +{ + @Resource + private SapTjService sapTjService; + + public static RbTask testUtils; + + @PostConstruct + public void init() { + testUtils = this; + } + + /** + * 同步红本数据库 + */ + public void tongb() throws Exception { + //判断是否已执行调价 (6.3 redbook rb_productVersion 是否有新增uid and sta='1'、sdmdm1 调价日期今日的数据是否存在) + String rbVersionUid = testUtils.sapTjService.selRbVersionUid();//红本调价记录uid + String locVersionUid = testUtils.sapTjService.selLocVersionUid();//本地红本调价记录uid + + String RbProductPriceCount = testUtils.sapTjService.selRbProductPriceVersionUid(rbVersionUid);//RbProductPrice表是否有更新 + String RbProductBasePriceCount = testUtils.sapTjService.selRbProductBasePriceVersionUid(rbVersionUid);//RbProductBasePrice表是否有更新 + + try{ + if(!rbVersionUid.equals(locVersionUid) && Integer.valueOf(RbProductPriceCount)>0 && Integer.valueOf(RbProductBasePriceCount)>0) {//有新增调价版本并且有调价记录 + System.out.println("=======================开始同步!======================"); + testUtils.sapTjService.deleteLocProductPrice();//删除本地c_rb_product_price表数据 + List productPriceList = testUtils.sapTjService.getProductPriceList(rbVersionUid); + JDBCBatchInsert.insertRbProductPrice(productPriceList);//批量插入 + + testUtils.sapTjService.deleteLocProductBasePrice();//删除本地c_rb_productbase_price表数据 + List productBasePriceList = testUtils.sapTjService.getProductBasePriceList(rbVersionUid); + JDBCBatchInsert.insertRbProductBasePrice(productBasePriceList);//批量插入 + + testUtils.sapTjService.updateLocRbVersion(rbVersionUid);//更新本地c_rb_version表 + } + }catch (Exception e) { + throw new Exception(e.getMessage(), e); + } + } +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JDBCBatchInsert.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JDBCBatchInsert.java new file mode 100644 index 0000000..9a0aff7 --- /dev/null +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JDBCBatchInsert.java @@ -0,0 +1,158 @@ +package com.ruoyi.quartz.util; + +import com.ruoyi.quartz.domain.c_rb_product_price; +import com.ruoyi.quartz.domain.c_rb_productbase_price; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; +import java.util.Random; + +@Component +public class JDBCBatchInsert { + + private static String databaseURL; + public static String getDatabaseURL() { + return databaseURL; + } + + @Value(value = "${spring.datasource.druid.quot.url}") + public void setDatabaseUR(String databaseURL) { + JDBCBatchInsert.databaseURL = databaseURL; + } + + private static String username; + public static String getUsername() {return username;} + + @Value(value = "${spring.datasource.druid.quot.username}") + public void setUsername(String username) { + JDBCBatchInsert.username = username; + } + + private static String password; + public static String getPassword() { + return password; + } + + @Value(value = "${spring.datasource.druid.quot.password}") + public void setPassword(String password) { + JDBCBatchInsert.password = password; + } + + public static void insertRbProductPrice(List list) throws IOException { + Connection connection = null; + PreparedStatement preparedStatement = null; + + try { + connection = DriverManager.getConnection(databaseURL, username, password); + // 关闭自动提交事务,改为手动提交 + connection.setAutoCommit(false); + System.out.println("===== 开始插入数据 ====="); + long startTime = System.currentTimeMillis(); + String sqlInsert = "INSERT INTO c_rb_product_price ( material_xingh, material_guig, material_diany, material_rb_price) VALUES ( ?, ?, ?, ?)"; + preparedStatement = connection.prepareStatement(sqlInsert); + + Random random = new Random(); + for (int i = 0; i < list.size(); i++) { + preparedStatement.setString(1, list.get(i).getMaterial_xingh()); + preparedStatement.setString(2, list.get(i).getMaterial_guig()); + preparedStatement.setString(3, list.get(i).getMaterial_diany()); + preparedStatement.setString(4, list.get(i).getMaterial_rb_price()); + // 添加到批处理中 + preparedStatement.addBatch(); + + if (i % 10000 == 0) { + // 每1000条数据提交一次 + preparedStatement.executeBatch(); + connection.commit(); + System.out.println("成功插入第 "+ i+" 条数据"); + } + + } + // 处理剩余的数据 + preparedStatement.executeBatch(); + connection.commit(); + long spendTime = System.currentTimeMillis()-startTime; + System.out.println("成功插入"+ list.size()+" 条数据,耗时:"+spendTime+"毫秒"); + } catch (SQLException e) { + System.out.println("Error: " + e.getMessage()); + } finally { + if (preparedStatement != null) { + try { + preparedStatement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } + + public static void insertRbProductBasePrice(List list) throws IOException { + Connection connection = null; + PreparedStatement preparedStatement = null; + + try { + connection = DriverManager.getConnection(databaseURL, username, password); + // 关闭自动提交事务,改为手动提交 + connection.setAutoCommit(false); + System.out.println("===== 开始插入数据 ====="); + long startTime = System.currentTimeMillis(); + String sqlInsert = "INSERT INTO c_rb_productbase_price ( material_xingh, material_guig, material_diany, material_cb_price) VALUES ( ?, ?, ?, ?)"; + preparedStatement = connection.prepareStatement(sqlInsert); + + Random random = new Random(); + for (int i = 0; i < list.size(); i++) { + preparedStatement.setString(1, list.get(i).getMaterial_xingh()); + preparedStatement.setString(2, list.get(i).getMaterial_guig()); + preparedStatement.setString(3, list.get(i).getMaterial_diany()); + preparedStatement.setString(4, list.get(i).getMaterial_cb_price()); + // 添加到批处理中 + preparedStatement.addBatch(); + + if (i % 10000 == 0) { + // 每1000条数据提交一次 + preparedStatement.executeBatch(); + connection.commit(); + System.out.println("成功插入第 "+ i+" 条数据"); + } + + } + // 处理剩余的数据 + preparedStatement.executeBatch(); + connection.commit(); + long spendTime = System.currentTimeMillis()-startTime; + System.out.println("成功插入"+ list.size()+" 条数据,耗时:"+spendTime+"毫秒"); + } catch (SQLException e) { + System.out.println("Error: " + e.getMessage()); + } finally { + if (preparedStatement != null) { + try { + preparedStatement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/ruoyi-quartz/src/main/resources/mapper/quartz/SapTjMapper.xml b/ruoyi-quartz/src/main/resources/mapper/quartz/SapTjMapper.xml new file mode 100644 index 0000000..b296f19 --- /dev/null +++ b/ruoyi-quartz/src/main/resources/mapper/quartz/SapTjMapper.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + delete from c_rb_product_price + + + + delete from c_rb_productbase_price + + + + update c_rb_version set version_uid = #{rbVersionUid} + +