no message
parent
58af5b1482
commit
62efd8ff9a
|
|
@ -0,0 +1 @@
|
|||
27324
|
||||
|
|
@ -1,21 +1,20 @@
|
|||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.youchain.basicdata.rest;
|
||||
|
||||
import com.youchain.annotation.AnonymousAccess;
|
||||
import com.youchain.annotation.Log;
|
||||
import com.youchain.basicdata.domain.Item;
|
||||
import com.youchain.basicdata.service.ItemService;
|
||||
|
|
@ -35,14 +34,13 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
|
||||
/**
|
||||
* @website https://eladmin.vip
|
||||
* @author houjianlan
|
||||
* @date 2023-08-16
|
||||
**/
|
||||
* @author houjianlan
|
||||
* @website https://eladmin.vip
|
||||
* @date 2023-08-16
|
||||
**/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "item管理")
|
||||
|
|
@ -62,13 +60,13 @@ public class ItemController {
|
|||
|
||||
@GetMapping
|
||||
@ApiOperation("查询item")
|
||||
public ResponseEntity<Object> queryItem(ItemQueryCriteria criteria, Pageable pageable){
|
||||
return new ResponseEntity<>(itemService.queryAll(criteria,pageable), HttpStatus.OK);
|
||||
public ResponseEntity<Object> queryItem(ItemQueryCriteria criteria, Pageable pageable) {
|
||||
return new ResponseEntity<>(itemService.queryAll(criteria, pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/queryItemAll")
|
||||
@ApiOperation("查询item")
|
||||
public ResponseEntity<Object> queryItemAll(ItemQueryCriteria criteria){
|
||||
public ResponseEntity<Object> queryItemAll(ItemQueryCriteria criteria) {
|
||||
return new ResponseEntity<>(itemService.queryAll(criteria), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
|
@ -76,9 +74,9 @@ public class ItemController {
|
|||
@Log("新增item")
|
||||
@ApiOperation("新增item")
|
||||
@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());
|
||||
return new ResponseEntity<>(itemService.create(resources),HttpStatus.CREATED);
|
||||
return new ResponseEntity<>(itemService.create(resources), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@Log("导入物料")
|
||||
|
|
@ -88,7 +86,7 @@ public class ItemController {
|
|||
try {
|
||||
String result = itemService.importItem(multipartFile);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -97,7 +95,7 @@ public class ItemController {
|
|||
@Log("修改item")
|
||||
@ApiOperation("修改item")
|
||||
@PreAuthorize("@el.check('item:edit')")
|
||||
public ResponseEntity<Object> updateItem(@Validated @RequestBody Item resources){
|
||||
public ResponseEntity<Object> updateItem(@Validated @RequestBody Item resources) {
|
||||
itemService.update(resources);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -166,7 +165,6 @@ public class ItemServiceImpl implements ItemService {
|
|||
throw new BadRequestException("数据为空");
|
||||
}
|
||||
|
||||
|
||||
//获取文件中所有物料编码
|
||||
List<String> codes = dataList.stream()
|
||||
.map(ItemImportReq::getCode)
|
||||
|
|
@ -182,11 +180,10 @@ public class ItemServiceImpl implements ItemService {
|
|||
|
||||
//修改物料集合
|
||||
List<Item> itemsToUpdate = new ArrayList<>();
|
||||
|
||||
for (ItemImportReq itemImportReq : dataList) {
|
||||
String code = itemImportReq.getCode();
|
||||
String specs = itemImportReq.getSpecs();
|
||||
//判断是否已存在容器
|
||||
//判断是否已存在物料
|
||||
if (existingItemMap.containsKey(code)) {
|
||||
//修改物料
|
||||
Item item = existingItemMap.get(code);
|
||||
|
|
@ -199,12 +196,12 @@ public class ItemServiceImpl implements ItemService {
|
|||
}
|
||||
}
|
||||
|
||||
//批量新增容器
|
||||
//批量新增物料
|
||||
if (!itemsToCreate.isEmpty()) {
|
||||
itemRepository.saveAll(itemsToCreate);
|
||||
}
|
||||
|
||||
//批量更新容器
|
||||
//批量更新物料
|
||||
if (!itemsToUpdate.isEmpty()) {
|
||||
itemRepository.saveAll(itemsToUpdate);
|
||||
}
|
||||
|
|
@ -228,5 +225,4 @@ public class ItemServiceImpl implements ItemService {
|
|||
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:
|
||||
db-type: com.alibaba.druid.pool.DruidDataSource
|
||||
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}
|
||||
password: ${DB_PWD:Youchain@56}
|
||||
# 初始连接数
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ spring:
|
|||
druid:
|
||||
db-type: com.alibaba.druid.pool.DruidDataSource
|
||||
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}
|
||||
password: ${DB_PWD:Youchain@56}
|
||||
# 初始连接数
|
||||
|
|
|
|||
Loading…
Reference in New Issue