|
@@ -0,0 +1,100 @@
|
|
|
+package com.nokia.tsl_data.dao;
|
|
|
+
|
|
|
+import com.nokia.tsl_data.entity.WorkFlowBasicData;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.object.BatchSqlUpdate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.sql.Types;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * tslData 直接使用jdbcTemplate完成的操作
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class TslDataDao {
|
|
|
+
|
|
|
+ private final JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ public TslDataDao(JdbcTemplate jdbcTemplate) {
|
|
|
+ this.jdbcTemplate = jdbcTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 采用BatchSqlUpdate的方法批量insert到表tableName(可以是临时表)
|
|
|
+ */
|
|
|
+ public void batchInsertWorkFlowBasicData(List<WorkFlowBasicData> data, String tableName) {
|
|
|
+ String sqlFormat = "INSERT INTO tsl_data.%s\n" +
|
|
|
+ "(city_id, kfsn, region_id, work_flow_create_time, work_flow_update_time)\n" +
|
|
|
+ "VALUES(?,?,?,?,?)";
|
|
|
+ BatchSqlUpdate batchSqlUpdate = new BatchSqlUpdate(Objects.requireNonNull(jdbcTemplate.getDataSource()), String.format(sqlFormat, tableName));
|
|
|
+ batchSqlUpdate.setBatchSize(1000);
|
|
|
+ batchSqlUpdate.setTypes(new int[]{
|
|
|
+ Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP
|
|
|
+ });
|
|
|
+ for (WorkFlowBasicData item : data) {
|
|
|
+ batchSqlUpdate.update(
|
|
|
+ item.getCityId(),
|
|
|
+ item.getKfsn(),
|
|
|
+ item.getRegionId(),
|
|
|
+ item.getWorkFlowCreateTime() == null ? null : Timestamp.from(item.getWorkFlowCreateTime()),
|
|
|
+ item.getWorkFlowUpdateTime() == null ? null : Timestamp.from(item.getWorkFlowUpdateTime())
|
|
|
+ );
|
|
|
+ }
|
|
|
+ batchSqlUpdate.flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建临时表,并返回临时表表名
|
|
|
+ */
|
|
|
+ public String createTempTableOfWorkFlowBasicData() {
|
|
|
+ // 临时表添加一个时间戳
|
|
|
+ String tempTableName = "work_flow_basic_data_temp_" + System.currentTimeMillis();
|
|
|
+ String sqlFormat = "CREATE TABLE tsl_data.%s (\n" +
|
|
|
+ "\tcity_id varchar(50) NULL,\n" +
|
|
|
+ "\tkfsn varchar(50) NOT NULL,\n" +
|
|
|
+ "\tregion_id varchar(50) NULL,\n" +
|
|
|
+ "\twork_flow_create_time timestamp NULL,\n" +
|
|
|
+ "\twork_flow_update_time timestamp NULL\n" +
|
|
|
+ ");";
|
|
|
+ jdbcTemplate.execute(String.format(sqlFormat, tempTableName));
|
|
|
+ return tempTableName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除临时表
|
|
|
+ */
|
|
|
+ public void dropTempTable(String tempTableName) {
|
|
|
+ if (!tempTableName.contains("_temp_")) {
|
|
|
+ throw new RuntimeException("仅支持删除命名带有 _temp_ 的临时表");
|
|
|
+ }
|
|
|
+ jdbcTemplate.execute("drop table tsl_data." + tempTableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从临时表中查找新增内容并插入work_flow_basic_data表
|
|
|
+ */
|
|
|
+ public int insertWorkFlowBasicDataFromTempTable(String tempTableName) {
|
|
|
+ String sqlFormat = "insert into tsl_data.work_flow_basic_data \n" +
|
|
|
+ "(city_id, kfsn, region_id, work_flow_create_time, work_flow_update_time, create_date, last_update_date) \n" +
|
|
|
+ "select t.city_id, t.kfsn, t.region_id, t.work_flow_create_time, t.work_flow_update_time, now(), now() \n" +
|
|
|
+ "from tsl_data.%s t \n" +
|
|
|
+ "left join tsl_data.work_flow_basic_data b \n" +
|
|
|
+ "on t.kfsn = b.kfsn \n" +
|
|
|
+ "where b.kfsn is null";
|
|
|
+ return jdbcTemplate.update(String.format(sqlFormat, tempTableName));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从临时表中查找需要更新内容并更新到work_flow_basic_data表
|
|
|
+ */
|
|
|
+ public int updateWorkFlowBasicDataFromTempTable(String tempTableName) {
|
|
|
+ String sqlFormat = "update tsl_data.work_flow_basic_data b\n" +
|
|
|
+ "set city_id = t.city_id, region_id = t.region_id, work_flow_update_time = t.work_flow_update_time, last_update_date = now()\n" +
|
|
|
+ "from (select city_id, kfsn, region_id, work_flow_create_time, work_flow_update_time from tsl_data.%s) t\n" +
|
|
|
+ "where b.kfsn = t.kfsn and (b.city_id != t.city_id or b.region_id != t.region_id)";
|
|
|
+ return jdbcTemplate.update(String.format(sqlFormat, tempTableName));
|
|
|
+ }
|
|
|
+}
|