no message
parent
58af5b1482
commit
62efd8ff9a
|
|
@ -0,0 +1 @@
|
||||||
|
27324
|
||||||
|
|
@ -1,21 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2019-2020 Zheng Jie
|
* Copyright 2019-2020 Zheng Jie
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
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,14 +34,13 @@ 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
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Api(tags = "item管理")
|
@Api(tags = "item管理")
|
||||||
|
|
@ -62,13 +60,13 @@ public class ItemController {
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ApiOperation("查询item")
|
@ApiOperation("查询item")
|
||||||
public ResponseEntity<Object> queryItem(ItemQueryCriteria criteria, Pageable pageable){
|
public ResponseEntity<Object> queryItem(ItemQueryCriteria criteria, Pageable pageable) {
|
||||||
return new ResponseEntity<>(itemService.queryAll(criteria,pageable), HttpStatus.OK);
|
return new ResponseEntity<>(itemService.queryAll(criteria, pageable), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/queryItemAll")
|
@GetMapping(value = "/queryItemAll")
|
||||||
@ApiOperation("查询item")
|
@ApiOperation("查询item")
|
||||||
public ResponseEntity<Object> queryItemAll(ItemQueryCriteria criteria){
|
public ResponseEntity<Object> queryItemAll(ItemQueryCriteria criteria) {
|
||||||
return new ResponseEntity<>(itemService.queryAll(criteria), HttpStatus.OK);
|
return new ResponseEntity<>(itemService.queryAll(criteria), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,9 +74,9 @@ public class ItemController {
|
||||||
@Log("新增item")
|
@Log("新增item")
|
||||||
@ApiOperation("新增item")
|
@ApiOperation("新增item")
|
||||||
@PreAuthorize("@el.check('item:add')")
|
@PreAuthorize("@el.check('item:add')")
|
||||||
public ResponseEntity<Object> createItem(@Validated @RequestBody Item resources){
|
public ResponseEntity<Object> createItem(@Validated @RequestBody Item resources) {
|
||||||
resources.setDept(UserUtils.getDept());
|
resources.setDept(UserUtils.getDept());
|
||||||
return new ResponseEntity<>(itemService.create(resources),HttpStatus.CREATED);
|
return new ResponseEntity<>(itemService.create(resources), HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Log("导入物料")
|
@Log("导入物料")
|
||||||
|
|
@ -88,7 +86,7 @@ public class ItemController {
|
||||||
try {
|
try {
|
||||||
String result = itemService.importItem(multipartFile);
|
String result = itemService.importItem(multipartFile);
|
||||||
return new ResponseEntity<>(ApiResult.success(result, null), HttpStatus.OK);
|
return new ResponseEntity<>(ApiResult.success(result, null), HttpStatus.OK);
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +95,7 @@ public class ItemController {
|
||||||
@Log("修改item")
|
@Log("修改item")
|
||||||
@ApiOperation("修改item")
|
@ApiOperation("修改item")
|
||||||
@PreAuthorize("@el.check('item:edit')")
|
@PreAuthorize("@el.check('item:edit')")
|
||||||
public ResponseEntity<Object> updateItem(@Validated @RequestBody Item resources){
|
public ResponseEntity<Object> updateItem(@Validated @RequestBody Item resources) {
|
||||||
itemService.update(resources);
|
itemService.update(resources);
|
||||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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