Commit d90e12c3 authored by lisong's avatar lisong

Merge branch 'develop_tas_new_patrol_sharding' into develop_tzs_new_patrol

# Conflicts: # amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/java/com/yeejoin/amos/patrol/business/service/impl/PlanTaskServiceImpl.java
parents f48ba754 48715d71
......@@ -80,6 +80,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.2.1</version>
</dependency>
<!-- 添加fastjson 依赖包. -->
<dependency>
<groupId>com.alibaba</groupId>
......
......@@ -291,6 +291,9 @@ public interface PlanTaskMapper extends BaseMapper {
List<Check> findCheck();
void truncateTable(@Param("tableName")String tableName);
List<CheckInput> findCheckInput();
List<CheckShot> findCheckShot();
......
......@@ -7,6 +7,14 @@ public interface RepositoryTs {
// 批量存储的方法
<S> Iterable<S> batchSave(Iterable<S> var1);
/**
* 分库分表数据源
* @param var1
* @param <S>
* @return
*/
<S> Iterable<S> batchSaveBySharding(Iterable<S> var1);
// 批量更新的方法
<S> Iterable<S> batchUpdate(Iterable<S> var1);
<S> Iterable<S> batchSaveNoAsync(Iterable<S> var1);
......
......@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.base.Joiner;
import com.yeejoin.amos.boot.biz.common.constants.RuleConstant;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
......@@ -51,6 +52,9 @@ import com.yeejoin.amos.patrol.business.service.intfc.IPlanTaskService;
import com.yeejoin.amos.patrol.business.util.MyByteArrayMultipartFile;
import com.yeejoin.amos.patrol.business.util.PlanTaskUtil;
import com.yeejoin.amos.patrol.business.util.WordTemplateUtils;
import com.yeejoin.amos.patrol.business.vo.*;
import com.yeejoin.amos.patrol.common.enums.*;
import com.yeejoin.amos.patrol.config.DataSourceConfiguration;
import com.yeejoin.amos.patrol.business.vo.CalDateVo;
import com.yeejoin.amos.patrol.business.vo.CodeOrderVo;
import com.yeejoin.amos.patrol.business.vo.DefectVo;
......@@ -2538,7 +2542,8 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
return d;
}).collect(Collectors.toList());
repositoryTs.batchSave(collect);
repositoryTs.batchSaveBySharding(collect);
planTaskMapper.truncateTable("p_plan_task");
log.info("归档p_plan_task表完成========");
}
......@@ -2551,7 +2556,8 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
BeanUtils.copyProperties(e, d);
return d;
}).collect(Collectors.toList());
repositoryTs.batchSave(collect);
repositoryTs.batchSaveBySharding(collect);
planTaskMapper.truncateTable("p_plan_task_detail");
log.info("归档p_plan_task_detail表完成========");
}
......@@ -2564,7 +2570,8 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
BeanUtils.copyProperties(e, d);
return d;
}).collect(Collectors.toList());
repositoryTs.batchSave(collect);
repositoryTs.batchSaveBySharding(collect);
planTaskMapper.truncateTable("p_check");
log.info("归档p_check表完成========");
}
......@@ -2577,7 +2584,8 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
BeanUtils.copyProperties(e, d);
return d;
}).collect(Collectors.toList());
repositoryTs.batchSave(collect);
repositoryTs.batchSaveBySharding(collect);
planTaskMapper.truncateTable("p_check_input");
log.info("归档p_check_input表完成========");
}
......@@ -2591,7 +2599,8 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
return d;
}).collect(Collectors.toList());
repositoryTs.batchSave(collect);
repositoryTs.batchSaveBySharding(collect);
planTaskMapper.truncateTable("p_check_shot");
log.info("归档p_check_shot表完成========");
}
......
package com.yeejoin.amos.patrol.business.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.yeejoin.amos.patrol.business.dao.repository.RepositoryTs;
import com.yeejoin.amos.patrol.config.DataSourceConfiguration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Iterator;
......@@ -43,6 +44,28 @@ public class RepositoryTImpl implements RepositoryTs {
}
@Override
@Transactional
@Async("asyncServiceExecutor")
@DS(DataSourceConfiguration.SHARDING_DATA_SOURCE_NAME)
public <S> Iterable<S> batchSaveBySharding(Iterable<S> var1) {
Iterator<S> iterator = var1.iterator();
int index = 0;
while (iterator.hasNext()){
em.persist(iterator.next());
index++;
if (index % BATCH_SIZE == 0){
em.flush();
em.clear();
}
}
if (index % BATCH_SIZE != 0){
em.flush();
em.clear();
}
return var1;
}
@Override
public <S> Iterable<S> batchUpdate(Iterable<S> var1) {
Iterator<S> iterator = var1.iterator();
int index = 0;
......
package com.yeejoin.amos.patrol.config;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author LiuLin
* @date 2023年12月01日 09:57
*/
@Configuration
public class DataSourceConfiguration {
private final DynamicDataSourceProperties properties;
private final DefaultDataSourceCreator dataSourceCreator;
private final DataSource shardingSphereDataSource;
public static final String SHARDING_DATA_SOURCE_NAME = "gits_sharding";
public DataSourceConfiguration(DynamicDataSourceProperties properties,
DefaultDataSourceCreator dataSourceCreator,
@Qualifier("shardingSphereDataSource") DataSource shardingSphereDataSource) {
this.properties = properties;
this.dataSourceCreator = dataSourceCreator;
this.shardingSphereDataSource = shardingSphereDataSource;
}
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
return new AbstractDataSourceProvider(dataSourceCreator) {
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put(SHARDING_DATA_SOURCE_NAME, shardingSphereDataSource);
return dataSourceMap;
}
};
}
/**
* 将dynamic-datasource设置为首选
* 当Spring存在多个数据源时,自动注入首选对象
* 设置为主要数据源之后,就可以支持shardingSphere原生的配置方式了
*/
@Primary
@Bean
public DataSource dataSource(List<DynamicDataSourceProvider> providers) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers);
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
\ No newline at end of file
package com.yeejoin.amos.patrol.config;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
public class DataSourceHealthConfig extends DataSourceHealthContributorAutoConfiguration {
public DataSourceHealthConfig(Map<String, DataSource> dataSources, ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
super(dataSources, metadataProviders);
}
@Override
protected AbstractHealthIndicator createIndicator(DataSource source) {
DataSourceHealthIndicator indicator = (DataSourceHealthIndicator) super.createIndicator(source);
if (!StringUtils.hasText(indicator.getQuery())) {
indicator.setQuery("select 1");
}
return indicator;
}
}
\ No newline at end of file
......@@ -1711,6 +1711,9 @@
no_risk_end = no_risk_end + #{noRiskEnd}
where org_code = #{orgCode} AND check_time = #{checkTime}
</update>
<update id="truncateTable">
TRUNCATE ${tableName};
</update>
<select id="selectPlanTaskIdList" resultType="java.lang.String">
SELECT
......
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 to comment