no message
parent
68d88d190e
commit
ff228f6f88
|
|
@ -26,6 +26,8 @@ import com.youchain.businessdata.service.dto.InventoryDto;
|
||||||
import com.youchain.businessdata.service.dto.InventoryQueryCriteria;
|
import com.youchain.businessdata.service.dto.InventoryQueryCriteria;
|
||||||
import com.youchain.modules.system.domain.Dept;
|
import com.youchain.modules.system.domain.Dept;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
|
||||||
|
|
@ -105,18 +105,4 @@ public interface MoService {
|
||||||
* @return Map<String,Mo>
|
* @return Map<String,Mo>
|
||||||
*/
|
*/
|
||||||
Map<String,Mo> findBylabelNos(Set labelNos);
|
Map<String,Mo> findBylabelNos(Set labelNos);
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量创建MO
|
|
||||||
* @param mos MO集合
|
|
||||||
*/
|
|
||||||
@Async
|
|
||||||
void batchCreateMos(List<Mo> mos);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量修改MO
|
|
||||||
* @param mos MO集合
|
|
||||||
*/
|
|
||||||
@Async
|
|
||||||
void batchUpdateMos(List<Mo> mos);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,16 +82,6 @@ public interface OrderService {
|
||||||
*/
|
*/
|
||||||
OrderDto create(Order resources);
|
OrderDto create(Order resources);
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量创建
|
|
||||||
* @param orders 订单列表
|
|
||||||
*/
|
|
||||||
@Async
|
|
||||||
void batchCreateOrders(List<Order> orders);
|
|
||||||
|
|
||||||
@Async
|
|
||||||
void batchUpdateOrders(List<Order> orders);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编辑
|
* 编辑
|
||||||
* @param resources /
|
* @param resources /
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,8 @@ import com.youchain.businessdata.domain.PickDetail;
|
||||||
import com.youchain.businessdata.service.dto.PickDetailDto;
|
import com.youchain.businessdata.service.dto.PickDetailDto;
|
||||||
import com.youchain.businessdata.service.dto.PickDetailQueryCriteria;
|
import com.youchain.businessdata.service.dto.PickDetailQueryCriteria;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,9 @@ import com.youchain.basicdata.domain.Point;
|
||||||
import com.youchain.basicdata.domain.Stock;
|
import com.youchain.basicdata.domain.Stock;
|
||||||
import com.youchain.basicdata.service.ItemService;
|
import com.youchain.basicdata.service.ItemService;
|
||||||
import com.youchain.basicdata.service.PointService;
|
import com.youchain.basicdata.service.PointService;
|
||||||
import com.youchain.basicdata.service.dto.ItemDto;
|
|
||||||
import com.youchain.businessdata.domain.*;
|
import com.youchain.businessdata.domain.*;
|
||||||
import com.youchain.businessdata.inputJson.*;
|
import com.youchain.businessdata.inputJson.*;
|
||||||
import com.youchain.businessdata.service.*;
|
import com.youchain.businessdata.service.*;
|
||||||
import com.youchain.businessdata.service.dto.MoDto;
|
|
||||||
import com.youchain.businessdata.service.dto.OrderDto;
|
|
||||||
import com.youchain.exception.handler.ApiResult;
|
|
||||||
import com.youchain.modules.system.domain.Dept;
|
import com.youchain.modules.system.domain.Dept;
|
||||||
import com.youchain.modules.system.service.DeptService;
|
import com.youchain.modules.system.service.DeptService;
|
||||||
import com.youchain.modules.system.service.dto.DeptDto;
|
import com.youchain.modules.system.service.dto.DeptDto;
|
||||||
|
|
@ -36,12 +32,10 @@ public class MlsServiceImpl implements MlsService {
|
||||||
private final ItemService itemService;
|
private final ItemService itemService;
|
||||||
private final DeptService deptService;
|
private final DeptService deptService;
|
||||||
private final MoService moService;
|
private final MoService moService;
|
||||||
private final TaskService taskService;
|
|
||||||
private final PointService pointService;
|
private final PointService pointService;
|
||||||
private final AgvTaskService agvTaskService;
|
|
||||||
private final PickDetailService pickDetailService;
|
|
||||||
private final InventoryService inventoryService;
|
private final InventoryService inventoryService;
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
|
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -150,11 +144,11 @@ public class MlsServiceImpl implements MlsService {
|
||||||
}
|
}
|
||||||
//批量新增订单
|
//批量新增订单
|
||||||
if (!ordersToCreate.isEmpty()) {
|
if (!ordersToCreate.isEmpty()) {
|
||||||
orderService.batchCreateOrders(ordersToCreate);
|
batchCreateOrUpdate.batchCreate(ordersToCreate);
|
||||||
}
|
}
|
||||||
//批量更新订单
|
//批量更新订单
|
||||||
if (!ordersToUpdate.isEmpty()) {
|
if (!ordersToUpdate.isEmpty()) {
|
||||||
orderService.batchUpdateOrders(ordersToUpdate);
|
batchCreateOrUpdate.batchUpdate(ordersToUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -283,6 +277,7 @@ public class MlsServiceImpl implements MlsService {
|
||||||
//获取已存在的MO票
|
//获取已存在的MO票
|
||||||
Map<String, Mo> existingMos = moService.findBylabelNos(labelNos);
|
Map<String, Mo> existingMos = moService.findBylabelNos(labelNos);
|
||||||
|
|
||||||
|
//获取仓库
|
||||||
Dept dept = null;
|
Dept dept = null;
|
||||||
boolean flag = redisUtils.hasKey("dept");
|
boolean flag = redisUtils.hasKey("dept");
|
||||||
if (flag) {
|
if (flag) {
|
||||||
|
|
@ -325,11 +320,11 @@ public class MlsServiceImpl implements MlsService {
|
||||||
|
|
||||||
//批量新增Mo票
|
//批量新增Mo票
|
||||||
if (!mosToCreate.isEmpty()) {
|
if (!mosToCreate.isEmpty()) {
|
||||||
moService.batchCreateMos(mosToCreate);
|
batchCreateOrUpdate.batchCreate(mosToCreate);
|
||||||
}
|
}
|
||||||
//批量更新Mo票
|
//批量更新Mo票
|
||||||
if (!mosToUpdate.isEmpty()) {
|
if (!mosToUpdate.isEmpty()) {
|
||||||
moService.batchUpdateMos(mosToUpdate);
|
batchCreateOrUpdate.batchUpdate(mosToUpdate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -403,12 +398,12 @@ public class MlsServiceImpl implements MlsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void getIssueInfo(IssueInfo issueInfo) {
|
public synchronized void getIssueInfo(IssueInfo issueInfo) {
|
||||||
String taskNumber = issueInfo.getTaskNumber();//任务号
|
String taskNumber = issueInfo.getTaskNumber();//任务号
|
||||||
// 指定Set的类型
|
// 指定Set的类型
|
||||||
Set<String> workOrderNameSet = issueInfo.getWorkOrderName();
|
Set<String> workOrderNameSet = issueInfo.getWorkOrderName();
|
||||||
if (workOrderNameSet.isEmpty()) {
|
if (workOrderNameSet.isEmpty()) {
|
||||||
throw new RuntimeException("没有获取到工单号!");
|
throw new IllegalArgumentException("没有获取到工单号!");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Inventory> inventoryList = inventoryService.findInvByBillCode(workOrderNameSet);//根据工单号查询库存信息
|
List<Inventory> inventoryList = inventoryService.findInvByBillCode(workOrderNameSet);//根据工单号查询库存信息
|
||||||
|
|
@ -417,15 +412,30 @@ public class MlsServiceImpl implements MlsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
//目标点
|
//目标点
|
||||||
Point endPoint = pointService.findByCode(null, null, null, "出库接驳口", null);
|
Point endPoint = null;
|
||||||
|
boolean flag = redisUtils.hasKey("endPoint");
|
||||||
|
if (flag) {
|
||||||
|
endPoint = (Point) redisUtils.get("endPoint");
|
||||||
|
} else {
|
||||||
|
endPoint = pointService.findByCode(null, null, null, "出库接驳口", null);
|
||||||
|
redisUtils.set("endPoint", endPoint, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Inventory> inventoryToUpdate = new ArrayList<>();
|
||||||
|
List<PickDetail> pickDetailToCreate = new ArrayList<>();
|
||||||
|
List<Task> taskToCreate = new ArrayList<>();
|
||||||
|
List<AgvTask> agvTaskToCreate = new ArrayList<>();
|
||||||
|
|
||||||
for (Inventory inv : inventoryList) {
|
for (Inventory inv : inventoryList) {
|
||||||
//根据库存信息生成叫料任务
|
//根据库存信息生成叫料任务
|
||||||
Stock stock = inv.getStock();//容器
|
Stock stock = inv.getStock();//容器
|
||||||
Point startPoint = inv.getPoint();//起始点位
|
Point startPoint = inv.getPoint();//起始点位
|
||||||
Item item = inv.getItemKey().getItem();
|
Item item = inv.getItemKey().getItem();
|
||||||
|
|
||||||
inv.setQueuedQty(inv.getQuantity());
|
//更新库存信息
|
||||||
inventoryService.update(inv);
|
inventoryToUpdate.add(updateInventory(inv));
|
||||||
|
|
||||||
|
//生成出库明细
|
||||||
PickDetail pd = new PickDetail();
|
PickDetail pd = new PickDetail();
|
||||||
pd.setItem(item);
|
pd.setItem(item);
|
||||||
pd.setPo(inv.getBillCode());
|
pd.setPo(inv.getBillCode());
|
||||||
|
|
@ -434,22 +444,23 @@ public class MlsServiceImpl implements MlsService {
|
||||||
pd.setOrderQty(inv.getQuantity());
|
pd.setOrderQty(inv.getQuantity());
|
||||||
pd.setAllocatedQty(inv.getQuantity());
|
pd.setAllocatedQty(inv.getQuantity());
|
||||||
pd.setStatus(BizStatus.ALLOCATE);
|
pd.setStatus(BizStatus.ALLOCATE);
|
||||||
pickDetailService.create(pd);
|
pickDetailToCreate.add(pd);
|
||||||
|
|
||||||
//生成搬运任务
|
//生成搬运任务
|
||||||
AgvTask agvTask = new AgvTask(BizStatus.PICK, stock.getCode(), startPoint.getCode(), endPoint.getCode(), BizStatus.OPEN, "01");
|
AgvTask agvTask = new AgvTask(BizStatus.PICK, stock.getCode(), startPoint.getCode(), endPoint.getCode(), BizStatus.OPEN, "01");
|
||||||
agvTaskService.create(agvTask);
|
agvTaskToCreate.add(agvTask);
|
||||||
|
|
||||||
//生成Task
|
//生成Task
|
||||||
Task task = new Task();
|
Task task = new Task();
|
||||||
task.setItem(item);
|
task.setItem(item);
|
||||||
task.setItemKey(inv.getItemKey());
|
task.setItemKey(inv.getItemKey());
|
||||||
task.setPickDetail(pd);
|
task.setPickDetail(pd);
|
||||||
task.setBillCode(taskNumber);
|
task.setBillCode(taskNumber);
|
||||||
task.setSrcStock(inv.getStock());
|
task.setSrcStock(stock);
|
||||||
task.setDstStock(inv.getStock());
|
task.setDstStock(stock);
|
||||||
task.setSrcPoint(startPoint);
|
task.setSrcPoint(startPoint);
|
||||||
task.setDstPoint(endPoint);
|
task.setDstPoint(endPoint);
|
||||||
task.setSrcStockCode(inv.getStock().getCode());
|
task.setSrcStockCode(stock.getCode());
|
||||||
task.setSrcPointCode(startPoint.getCode());
|
task.setSrcPointCode(startPoint.getCode());
|
||||||
task.setDstPointCode(endPoint.getCode());
|
task.setDstPointCode(endPoint.getCode());
|
||||||
task.setInvStatus(inv.getStatus());
|
task.setInvStatus(inv.getStatus());
|
||||||
|
|
@ -459,12 +470,41 @@ public class MlsServiceImpl implements MlsService {
|
||||||
task.setInvId(inv.getId());
|
task.setInvId(inv.getId());
|
||||||
task.setDept(item.getDept());
|
task.setDept(item.getDept());
|
||||||
task.setAgvTask(agvTask);
|
task.setAgvTask(agvTask);
|
||||||
taskService.create(task);
|
taskToCreate.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
//批量更新库存信息
|
||||||
|
if (!inventoryToUpdate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchUpdate(inventoryToUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pickDetailToCreate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchCreate(pickDetailToCreate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!agvTaskToCreate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchCreate(agvTaskToCreate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!taskToCreate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchCreate(taskToCreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新库存信息
|
||||||
|
*
|
||||||
|
* @param inventory
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private Inventory updateInventory(Inventory inventory) {
|
||||||
|
inventory.setQueuedQty(inventory.getQuantity());
|
||||||
|
return inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mo票回传
|
* Mo票回传
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -233,37 +233,6 @@ public class MoServiceImpl implements MoService {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public void batchCreateMos(List<Mo> mos) {
|
|
||||||
int batchSize = 50; // 根据需求调整批处理大小
|
|
||||||
for (int i = 0; i < mos.size(); i++) {
|
|
||||||
entityManager.persist(mos.get(i));
|
|
||||||
if (i % batchSize == 0 && i > 0) {
|
|
||||||
entityManager.flush(); // 批量提交
|
|
||||||
entityManager.clear(); // 清理缓存
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entityManager.flush(); // 最后确保所有剩余的实体也被flush到数据库
|
|
||||||
entityManager.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public void batchUpdateMos(List<Mo> mos) {
|
|
||||||
int batchSize = 50; // 根据需求调整批处理大小
|
|
||||||
for (int i = 0; i < mos.size(); i++) {
|
|
||||||
Mo mo = mos.get(i);
|
|
||||||
entityManager.merge(mo); // 更新实体
|
|
||||||
if (i % batchSize == 0 && i > 0) { // 每处理50个实体执行一次flush和clear
|
|
||||||
entityManager.flush();
|
|
||||||
entityManager.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entityManager.flush(); // 确保最后的更改也被写入数据库
|
|
||||||
entityManager.clear(); // 清理持久化上下文以释放内存
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createTasks(Item item, Stock stock, Point srcPoint, Point endPoint, String propC1, MoDto moDto, OrderDto orderDto) {
|
public void createTasks(Item item, Stock stock, Point srcPoint, Point endPoint, String propC1, MoDto moDto, OrderDto orderDto) {
|
||||||
// 生成AGV任务
|
// 生成AGV任务
|
||||||
AgvTask agvTask = new AgvTask(BizStatus.ASN, stock.getCode(), srcPoint.getCode(), endPoint.getCode(), BizStatus.OPEN, "RACK_MOVE_FULL");
|
AgvTask agvTask = new AgvTask(BizStatus.ASN, stock.getCode(), srcPoint.getCode(), endPoint.getCode(), BizStatus.OPEN, "RACK_MOVE_FULL");
|
||||||
|
|
|
||||||
|
|
@ -121,37 +121,6 @@ public class OrderServiceImpl implements OrderService {
|
||||||
return orderMapper.toDto(orderRepository.save(resources));
|
return orderMapper.toDto(orderRepository.save(resources));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public void batchCreateOrders(List<Order> orders) {
|
|
||||||
int batchSize = 50; // 根据需求调整批处理大小
|
|
||||||
for (int i = 0; i < orders.size(); i++) {
|
|
||||||
entityManager.persist(orders.get(i));
|
|
||||||
if (i % batchSize == 0 && i > 0) {
|
|
||||||
entityManager.flush(); // 批量提交
|
|
||||||
entityManager.clear(); // 清理缓存
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entityManager.flush(); // 最后确保所有剩余的实体也被flush到数据库
|
|
||||||
entityManager.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public void batchUpdateOrders(List<Order> orders) {
|
|
||||||
int batchSize = 50; // 根据需求调整批处理大小
|
|
||||||
for (int i = 0; i < orders.size(); i++) {
|
|
||||||
Order order = orders.get(i);
|
|
||||||
entityManager.merge(order); // 更新实体
|
|
||||||
if (i % batchSize == 0 && i > 0) { // 每处理50个实体执行一次flush和clear
|
|
||||||
entityManager.flush();
|
|
||||||
entityManager.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entityManager.flush(); // 确保最后的更改也被写入数据库
|
|
||||||
entityManager.clear(); // 清理持久化上下文以释放内存
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void update(Order resources) {
|
public void update(Order resources) {
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,6 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PickDetailServiceImpl implements PickDetailService {
|
public class PickDetailServiceImpl implements PickDetailService {
|
||||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
|
|
||||||
private final PickDetailRepository pickDetailRepository;
|
private final PickDetailRepository pickDetailRepository;
|
||||||
private final PickDetailMapper pickDetailMapper;
|
private final PickDetailMapper pickDetailMapper;
|
||||||
private final InventoryService inventoryService;
|
private final InventoryService inventoryService;
|
||||||
|
|
@ -76,9 +75,7 @@ public class PickDetailServiceImpl implements PickDetailService {
|
||||||
private final AgvTaskRepository agvTaskRepository;
|
private final AgvTaskRepository agvTaskRepository;
|
||||||
private final TaskRepository taskRepository;
|
private final TaskRepository taskRepository;
|
||||||
private final ItemService itemService;
|
private final ItemService itemService;
|
||||||
private final PointService pointService;
|
private final EntityManager entityManager;
|
||||||
private final StockRepository stockRepository;
|
|
||||||
private final EntityManager entityMapper;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> queryAll(PickDetailQueryCriteria criteria, Pageable pageable) {
|
public Map<String, Object> queryAll(PickDetailQueryCriteria criteria, Pageable pageable) {
|
||||||
|
|
@ -316,7 +313,7 @@ public class PickDetailServiceImpl implements PickDetailService {
|
||||||
" where DATE_FORMAT(d.createTime,'%Y-%m-%d')>='"+date+"' " +
|
" where DATE_FORMAT(d.createTime,'%Y-%m-%d')>='"+date+"' " +
|
||||||
" group by DATE_FORMAT(d.createTime,'%Y-%m-%d')";
|
" group by DATE_FORMAT(d.createTime,'%Y-%m-%d')";
|
||||||
}
|
}
|
||||||
Query query=entityMapper.createQuery(hql);
|
Query query=entityManager.createQuery(hql);
|
||||||
List<Object[]> ts=query.getResultList();
|
List<Object[]> ts=query.getResultList();
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
package com.youchain.utils;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class BatchCreateOrUpdate {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量插入实体。
|
||||||
|
*
|
||||||
|
* @param entities 要插入的实体列表
|
||||||
|
* @param <T> 实体类型
|
||||||
|
*/
|
||||||
|
@Async
|
||||||
|
@Transactional
|
||||||
|
public <T> void batchCreate(List<T> entities) {
|
||||||
|
int batchSize = 100;
|
||||||
|
if (entities == null || entities.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (T entity : entities) {
|
||||||
|
entityManager.persist(entity);
|
||||||
|
i++;
|
||||||
|
if (i % batchSize == 0) {
|
||||||
|
// 执行flush将实体状态同步到数据库,并清理持久化上下文
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保最后未达到batchSize的数据也能被flush到数据库
|
||||||
|
if (i % batchSize != 0) {
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新实体。
|
||||||
|
*
|
||||||
|
* @param entities 要更新的实体列表
|
||||||
|
* @param <T> 实体类型
|
||||||
|
*/
|
||||||
|
@Async
|
||||||
|
@Transactional
|
||||||
|
public <T> void batchUpdate(List<T> entities) {
|
||||||
|
int batchSize = 100;
|
||||||
|
if (entities == null || entities.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (T entity : entities) {
|
||||||
|
entityManager.merge(entity);
|
||||||
|
i++;
|
||||||
|
if (i % batchSize == 0) {
|
||||||
|
// 执行flush和clear以同步状态到数据库并清理上下文,减少内存使用
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i % batchSize != 0) {
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue