no message
							parent
							
								
									58af5b1482
								
							
						
					
					
						commit
						62efd8ff9a
					
				| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					27324
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,6 @@
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
package com.youchain.basicdata.rest;
 | 
					package com.youchain.basicdata.rest;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.youchain.annotation.AnonymousAccess;
 | 
					 | 
				
			||||||
import com.youchain.annotation.Log;
 | 
					import com.youchain.annotation.Log;
 | 
				
			||||||
import com.youchain.basicdata.domain.Item;
 | 
					import com.youchain.basicdata.domain.Item;
 | 
				
			||||||
import com.youchain.basicdata.service.ItemService;
 | 
					import com.youchain.basicdata.service.ItemService;
 | 
				
			||||||
| 
						 | 
					@ -35,12 +34,11 @@ import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import javax.servlet.http.HttpServletResponse;
 | 
					import javax.servlet.http.HttpServletResponse;
 | 
				
			||||||
import javax.transaction.Transactional;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
* @website https://eladmin.vip
 | 
					 | 
				
			||||||
 * @author houjianlan
 | 
					 * @author houjianlan
 | 
				
			||||||
 | 
					 * @website https://eladmin.vip
 | 
				
			||||||
 * @date 2023-08-16
 | 
					 * @date 2023-08-16
 | 
				
			||||||
 **/
 | 
					 **/
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,7 +36,6 @@ import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.*;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import javax.persistence.EntityManager;
 | 
					 | 
				
			||||||
import javax.servlet.http.HttpServletResponse;
 | 
					import javax.servlet.http.HttpServletResponse;
 | 
				
			||||||
import java.util.stream.Collectors;
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -166,7 +165,6 @@ public class ItemServiceImpl implements ItemService {
 | 
				
			||||||
            throw new BadRequestException("数据为空");
 | 
					            throw new BadRequestException("数据为空");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
        //获取文件中所有物料编码
 | 
					        //获取文件中所有物料编码
 | 
				
			||||||
        List<String> codes = dataList.stream()
 | 
					        List<String> codes = dataList.stream()
 | 
				
			||||||
                .map(ItemImportReq::getCode)
 | 
					                .map(ItemImportReq::getCode)
 | 
				
			||||||
| 
						 | 
					@ -182,11 +180,10 @@ public class ItemServiceImpl implements ItemService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //修改物料集合
 | 
					        //修改物料集合
 | 
				
			||||||
        List<Item> itemsToUpdate = new ArrayList<>();
 | 
					        List<Item> itemsToUpdate = new ArrayList<>();
 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (ItemImportReq itemImportReq : dataList) {
 | 
					        for (ItemImportReq itemImportReq : dataList) {
 | 
				
			||||||
            String code = itemImportReq.getCode();
 | 
					            String code = itemImportReq.getCode();
 | 
				
			||||||
            String specs = itemImportReq.getSpecs();
 | 
					            String specs = itemImportReq.getSpecs();
 | 
				
			||||||
            //判断是否已存在容器
 | 
					            //判断是否已存在物料
 | 
				
			||||||
            if (existingItemMap.containsKey(code)) {
 | 
					            if (existingItemMap.containsKey(code)) {
 | 
				
			||||||
                //修改物料
 | 
					                //修改物料
 | 
				
			||||||
                Item item = existingItemMap.get(code);
 | 
					                Item item = existingItemMap.get(code);
 | 
				
			||||||
| 
						 | 
					@ -199,12 +196,12 @@ public class ItemServiceImpl implements ItemService {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //批量新增容器
 | 
					        //批量新增物料
 | 
				
			||||||
        if (!itemsToCreate.isEmpty()) {
 | 
					        if (!itemsToCreate.isEmpty()) {
 | 
				
			||||||
            itemRepository.saveAll(itemsToCreate);
 | 
					            itemRepository.saveAll(itemsToCreate);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //批量更新容器
 | 
					        //批量更新物料
 | 
				
			||||||
        if (!itemsToUpdate.isEmpty()) {
 | 
					        if (!itemsToUpdate.isEmpty()) {
 | 
				
			||||||
            itemRepository.saveAll(itemsToUpdate);
 | 
					            itemRepository.saveAll(itemsToUpdate);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -228,5 +225,4 @@ public class ItemServiceImpl implements ItemService {
 | 
				
			||||||
        return item;
 | 
					        return item;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,78 +0,0 @@
 | 
				
			||||||
package com.youchain.utils;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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>      实体类型
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @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>      实体类型
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @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();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,81 @@
 | 
				
			||||||
 | 
					package com.youchain.utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Component;
 | 
				
			||||||
 | 
					import org.springframework.transaction.annotation.Transactional;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.persistence.EntityManager;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Component
 | 
				
			||||||
 | 
					public class BatchUtils {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private EntityManager entityManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 批量插入实体。
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param entities 要插入的实体列表
 | 
				
			||||||
 | 
					     * @param <T>      实体类型
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Transactional
 | 
				
			||||||
 | 
					    public <T> void batchInsert(List<T> entities, int batchSize) {
 | 
				
			||||||
 | 
					        if (entities.isEmpty()) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 0; i < entities.size(); i++) {
 | 
				
			||||||
 | 
					            entityManager.persist(entities.get(i));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if ((i + 1) % batchSize == 0) {
 | 
				
			||||||
 | 
					                entityManager.flush();
 | 
				
			||||||
 | 
					                entityManager.clear();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 处理剩余记录
 | 
				
			||||||
 | 
					        entityManager.flush();
 | 
				
			||||||
 | 
					        entityManager.clear();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 重载方法,使用默认批量大小
 | 
				
			||||||
 | 
					    @Transactional
 | 
				
			||||||
 | 
					    public <T> void batchInsert(List<T> entities) {
 | 
				
			||||||
 | 
					        batchInsert(entities, 500);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 批量更新实体。
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param entities 要更新的实体列表
 | 
				
			||||||
 | 
					     * @param <T>      实体类型
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Transactional
 | 
				
			||||||
 | 
					    public <T> void batchUpdate(List<T> entities, int batchSize) {
 | 
				
			||||||
 | 
					        if (entities.isEmpty()) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 0; i < entities.size(); i++) {
 | 
				
			||||||
 | 
					            entityManager.merge(entities.get(i));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if ((i + 1) % batchSize == 0) {
 | 
				
			||||||
 | 
					                entityManager.flush();
 | 
				
			||||||
 | 
					                entityManager.clear();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 处理剩余记录
 | 
				
			||||||
 | 
					        entityManager.flush();
 | 
				
			||||||
 | 
					        entityManager.clear();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Transactional
 | 
				
			||||||
 | 
					    public <T> void batchUpdate(List<T> entities) {
 | 
				
			||||||
 | 
					        batchUpdate(entities, 500);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -4,7 +4,7 @@ spring:
 | 
				
			||||||
    druid:
 | 
					    druid:
 | 
				
			||||||
      db-type: com.alibaba.druid.pool.DruidDataSource
 | 
					      db-type: com.alibaba.druid.pool.DruidDataSource
 | 
				
			||||||
      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
 | 
					      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
 | 
				
			||||||
      url: jdbc:log4jdbc:mysql://${DB_HOST:47.103.100.52}:${DB_PORT:53306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
 | 
					      url: jdbc:log4jdbc:mysql://${DB_HOST:47.103.100.52}:${DB_PORT:53306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true
 | 
				
			||||||
      username: ${DB_USER:root}
 | 
					      username: ${DB_USER:root}
 | 
				
			||||||
      password: ${DB_PWD:Youchain@56}
 | 
					      password: ${DB_PWD:Youchain@56}
 | 
				
			||||||
      # 初始连接数
 | 
					      # 初始连接数
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,7 +4,7 @@ spring:
 | 
				
			||||||
    druid:
 | 
					    druid:
 | 
				
			||||||
      db-type: com.alibaba.druid.pool.DruidDataSource
 | 
					      db-type: com.alibaba.druid.pool.DruidDataSource
 | 
				
			||||||
      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
 | 
					      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
 | 
				
			||||||
      url: jdbc:log4jdbc:mysql://${DB_HOST:10.172.64.114}:${DB_PORT:3306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
 | 
					      url: jdbc:log4jdbc:mysql://${DB_HOST:10.172.64.114}:${DB_PORT:3306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true
 | 
				
			||||||
      username: ${DB_USER:root}
 | 
					      username: ${DB_USER:root}
 | 
				
			||||||
      password: ${DB_PWD:Youchain@56}
 | 
					      password: ${DB_PWD:Youchain@56}
 | 
				
			||||||
      # 初始连接数
 | 
					      # 初始连接数
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue