Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
M
mini-wms
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Members
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
周海峰
mini-wms
Commits
90af9a4c
Commit
90af9a4c
authored
Dec 22, 2025
by
yubin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
导入修改
parent
f97e11c7
隐藏空白字符变更
内嵌
并排
正在显示
13 个修改的文件
包含
330 行增加
和
55 行删除
+330
-55
ruoyi-admin-vue/src/views/inventory/warehouses/index.vue
+4
-4
ruoyi-inventory/src/main/java/com/ruoyi/inventory/controller/WarehousesController.java
+21
-0
ruoyi-inventory/src/main/java/com/ruoyi/inventory/domain/Warehouses.java
+8
-8
ruoyi-inventory/src/main/java/com/ruoyi/inventory/domain/vo/OwnerTemplateVO.java
+3
-0
ruoyi-inventory/src/main/java/com/ruoyi/inventory/domain/vo/StorageLocationsLocationTemplateVO.java
+2
-0
ruoyi-inventory/src/main/java/com/ruoyi/inventory/mapper/WarehousesMapper.java
+3
-0
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/IWarehousesService.java
+3
-0
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/InventoryServiceImpl.java
+2
-2
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/OutboundOrdersServiceImpl.java
+11
-0
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/OwnersServiceImpl.java
+67
-21
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/StorageLocationsServiceImpl.java
+56
-15
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/WarehousesServiceImpl.java
+129
-4
ruoyi-inventory/src/main/resources/mapper/inventory/WarehousesMapper.xml
+21
-1
没有找到文件。
ruoyi-admin-vue/src/views/inventory/warehouses/index.vue
View file @
90af9a4c
...
...
@@ -96,7 +96,7 @@
clearable
class=
"w20"
>
<el-option
label=
"
启用
"
value=
"1"
/>
<el-option
label=
"
正常
"
value=
"1"
/>
<el-option
label=
"停用"
value=
"0"
/>
</el-select>
</el-form-item>
...
...
@@ -128,7 +128,7 @@
:type=
"scope.row.isEnabled === 1 ? 'success' : 'danger'"
size=
"small"
>
{{
scope
.
row
.
isEnabled
===
1
?
'
启用
'
:
'停用'
}}
{{
scope
.
row
.
isEnabled
===
1
?
'
正常
'
:
'停用'
}}
</el-tag>
</
template
>
</el-table-column>
...
...
@@ -198,8 +198,8 @@
<el-col
:span=
"12"
>
<el-form-item
label=
"应用状态"
prop=
"isEnabled"
>
<el-radio-group
v-model=
"form.isEnabled"
style=
"width: 100%"
>
<el-radio-button
label=
"1"
>
启用
</el-radio-button>
<el-radio-button
label=
"0"
>
禁
用
</el-radio-button>
<el-radio-button
label=
"1"
>
正常
</el-radio-button>
<el-radio-button
label=
"0"
>
停
用
</el-radio-button>
</el-radio-group>
</el-form-item>
</el-col>
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/controller/WarehousesController.java
View file @
90af9a4c
...
...
@@ -6,6 +6,7 @@ import java.util.Map;
import
javax.servlet.http.HttpServletResponse
;
import
com.ruoyi.common.config.WarehouseConfig
;
import
com.ruoyi.inventory.domain.vo.StorageLocationsLocationTemplateVO
;
import
org.springframework.security.access.prepost.PreAuthorize
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
...
...
@@ -17,6 +18,7 @@ import com.ruoyi.inventory.domain.Warehouses;
import
com.ruoyi.inventory.service.IWarehousesService
;
import
com.ruoyi.common.utils.poi.ExcelUtil
;
import
com.ruoyi.common.core.page.TableDataInfo
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* 仓库Controller
...
...
@@ -129,4 +131,23 @@ public class WarehousesController extends BaseController
List
<
Warehouses
>
list
=
warehousesService
.
selectWarehousesList
(
warehouses
);
return
getDataTable
(
list
);
}
@PreAuthorize
(
"@ss.hasPermi('inventory:warehouses:add')"
)
@Log
(
title
=
"货主信息"
,
businessType
=
BusinessType
.
IMPORT
)
@PostMapping
(
"/import"
)
public
AjaxResult
importTemplate
(
MultipartFile
file
,
boolean
updateSupport
)
throws
Exception
{
ExcelUtil
<
Warehouses
>
util
=
new
ExcelUtil
<
Warehouses
>(
Warehouses
.
class
);
List
<
Warehouses
>
warehouses
=
util
.
importExcel
(
file
.
getInputStream
());
String
operName
=
getUsername
();
String
message
=
warehousesService
.
importWarehouses
(
warehouses
,
updateSupport
,
operName
);
return
success
(
message
);
}
@PostMapping
(
"/importTemplate"
)
public
void
importTemplate
(
HttpServletResponse
response
)
{
ExcelUtil
<
Warehouses
>
util
=
new
ExcelUtil
<
Warehouses
>(
Warehouses
.
class
);
util
.
importTemplateExcel
(
response
,
"库位数据"
);
}
}
ruoyi-inventory/src/main/java/com/ruoyi/inventory/domain/Warehouses.java
View file @
90af9a4c
package
com
.
ruoyi
.
inventory
.
domain
;
import
com.ruoyi.common.config.WarehouseConfig
;
import
lombok.Data
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
import
org.apache.commons.lang3.builder.ToStringStyle
;
...
...
@@ -21,19 +22,19 @@ public class Warehouses extends BaseEntity
private
String
id
;
/** 仓库编码 检索条件 */
@Excel
(
name
=
"仓库编码
检索条件
"
)
@Excel
(
name
=
"仓库编码"
)
private
String
warehousesCode
;
/** 仓库名称 检索条件 */
@Excel
(
name
=
"仓库名称
检索条件
"
)
@Excel
(
name
=
"仓库名称"
)
private
String
warehousesName
;
/** 仓库类型1-普通仓 2-危险品仓 3-冷藏仓 字典设置,检索条件 */
@Excel
(
name
=
"仓库类型
1-普通仓 2-危险品仓 3-冷藏仓 字典设置,检索条件
"
)
@Excel
(
name
=
"仓库类型
"
,
dictType
=
"warehouse_type
"
)
private
Long
warehouseType
;
/** 仓库地点 检索条件 */
@Excel
(
name
=
"仓库地点
检索条件
"
)
@Excel
(
name
=
"仓库地点"
)
private
String
address
;
/** 仓库区域 */
...
...
@@ -45,7 +46,7 @@ public class Warehouses extends BaseEntity
private
Long
capacity
;
/** 仓库管理员 文字,检索条件 */
@Excel
(
name
=
"仓库管理员
文字,检索条件
"
)
@Excel
(
name
=
"仓库管理员"
)
private
String
manager
;
/** 联系电话 */
...
...
@@ -53,11 +54,10 @@ public class Warehouses extends BaseEntity
private
String
contactPhone
;
/** 应用状态1使用0未使用 */
@Excel
(
name
=
"应用状态
1使用0未使用
"
)
@Excel
(
name
=
"应用状态
"
,
dictType
=
"is_enabled
"
)
private
Long
isEnabled
;
/** 应用数据1使用0删除 删除用 */
@Excel
(
name
=
"应用数据1使用0删除 删除用"
)
private
Long
isUsed
;
/** 排序 */
...
...
@@ -70,7 +70,7 @@ public class Warehouses extends BaseEntity
/** 排序号 */
private
String
updateUserCode
;
private
String
localId
;
private
String
localId
=
WarehouseConfig
.
DEFAULT_WAREHOUSE_ID
;
public
void
setId
(
String
id
)
{
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/domain/vo/OwnerTemplateVO.java
View file @
90af9a4c
...
...
@@ -12,6 +12,9 @@ import lombok.Data;
@Data
public
class
OwnerTemplateVO
{
@Excel
(
name
=
"NO."
)
private
String
no
;
/**
* 客户编码/客户名称(根据业务场景调整,若为编码建议命名为 customerCode)
*/
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/domain/vo/StorageLocationsLocationTemplateVO.java
View file @
90af9a4c
...
...
@@ -13,6 +13,8 @@ import lombok.Data;
@Data
public
class
StorageLocationsLocationTemplateVO
{
@Excel
(
name
=
"NO."
)
private
String
no
;
@Excel
(
name
=
"库位编码"
)
private
String
locationCode
;
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/mapper/WarehousesMapper.java
View file @
90af9a4c
...
...
@@ -4,6 +4,7 @@ import java.util.List;
import
java.util.Map
;
import
com.ruoyi.inventory.domain.Warehouses
;
import
org.apache.ibatis.annotations.Mapper
;
import
org.apache.ibatis.annotations.Update
;
/**
...
...
@@ -12,6 +13,7 @@ import org.apache.ibatis.annotations.Update;
* @author ruoyi
* @date 2025-12-01
*/
@Mapper
public
interface
WarehousesMapper
{
/**
...
...
@@ -86,4 +88,5 @@ public interface WarehousesMapper
*/
Warehouses
selectWarehousesByName
(
String
name
);
public
int
batchInsertWarehouses
(
List
<
Warehouses
>
warehousesList
);
}
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/IWarehousesService.java
View file @
90af9a4c
...
...
@@ -4,6 +4,7 @@ import java.util.List;
import
java.util.Map
;
import
com.ruoyi.inventory.domain.Warehouses
;
import
com.ruoyi.inventory.domain.vo.StorageLocationsLocationTemplateVO
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
...
...
@@ -73,4 +74,6 @@ public interface IWarehousesService
*/
public
List
<
Map
<
String
,
Object
>>
getMapList
();
public
String
importWarehouses
(
List
<
Warehouses
>
WarehousesList
,
Boolean
isUpdateSupport
,
String
operName
);
}
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/InventoryServiceImpl.java
View file @
90af9a4c
...
...
@@ -175,8 +175,8 @@ public class InventoryServiceImpl implements IInventoryService
return
1
;
}
p
rivate
void
createInventoryOutboundLog
(
Inventory
inventory
,
OutboundOrderItems
outboundOrderItem
,
Long
deductQty
,
String
updateUser
,
Date
updateTime
)
{
p
ublic
void
createInventoryOutboundLog
(
Inventory
inventory
,
OutboundOrderItems
outboundOrderItem
,
Long
deductQty
,
String
updateUser
,
Date
updateTime
)
{
InventoryTransactions
transactions
=
new
InventoryTransactions
();
transactions
.
setId
(
IdUtils
.
simpleUUID
());
// 确保IdUtils工具类存在,若无则替换为UUID.randomUUID().toString()
transactions
.
setTransactionType
(
2L
);
// 事务类型-出库
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/OutboundOrdersServiceImpl.java
View file @
90af9a4c
...
...
@@ -343,6 +343,17 @@ public class OutboundOrdersServiceImpl implements IOutboundOrdersService {
newItem
.
setActualQuantity
(
deductQty
);
// 仅当前库存ID的扣减数量
newItem
.
setUpdateBy
(
updateUser
);
newItem
.
setUpdateTime
(
updateTime
);
// 记录出库事务日志:获取对应库存并调用库存服务的日志方法
try
{
Inventory
inv
=
inventoryMapper
.
selectInventoryById
(
inventoryId
);
if
(
inv
!=
null
)
{
inventoryService
.
createInventoryOutboundLog
(
inv
,
newItem
,
deductQty
,
updateUser
,
updateTime
);
}
}
catch
(
Exception
ignore
)
{
// 保持原有流程稳定性,记录日志失败不应阻塞扣减流程
}
newSingleItems
.
add
(
newItem
);
}
}
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/OwnersServiceImpl.java
View file @
90af9a4c
package
com
.
ruoyi
.
inventory
.
service
.
impl
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
com.ruoyi.common.exception.ServiceException
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.DictUtils
;
import
com.ruoyi.common.utils.SecurityUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.inventory.domain.StorageLocations
;
import
com.ruoyi.inventory.domain.vo.OwnerTemplateVO
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.beans.BeanUtils
;
...
...
@@ -85,6 +84,7 @@ public class OwnersServiceImpl implements IOwnersService {
@Override
public
String
importOwners
(
List
<
OwnerTemplateVO
>
ownersList
,
Boolean
isUpdateSupport
,
String
operName
)
{
// 空数据校验
if
(
CollectionUtils
.
isEmpty
(
ownersList
))
{
throw
new
ServiceException
(
"导入用户数据不能为空!"
);
}
...
...
@@ -101,16 +101,33 @@ public class OwnersServiceImpl implements IOwnersService {
// 批量插入的有效数据集合
List
<
Owners
>
batchInsertList
=
new
ArrayList
<>();
// ========== 核心优化:提前加载所有已存在的用户编码映射 ==========
Map
<
String
,
String
>
ownerCodeToIdMap
=
loadOwnerCodeToIdMap
();
for
(
OwnerTemplateVO
ownerTemp
:
ownersList
)
{
try
{
Owners
owners
=
new
Owners
();
// 核心校验:用户编码(ownerCode)不能为空
String
ownerCode
=
ownerTemp
.
getOwnerCode
();
if
(
ownerCode
==
null
||
ownerCode
.
trim
().
isEmpty
())
{
throw
new
ServiceException
(
"【"
+
ownerTemp
.
getNo
()+
"】用户编码不能为空"
);
}
// 去除首尾空格,避免因空格导致的重复判断错误
String
cleanOwnerCode
=
ownerCode
.
trim
();
// 核心优化:从Map中校验用户编码是否已存在(替代数据库查询)
if
(
ownerCodeToIdMap
.
containsKey
(
cleanOwnerCode
))
{
throw
new
ServiceException
(
"用户【"
+
cleanOwnerCode
+
"】已存在"
);
}
// 处理激活状态转换
BeanUtils
.
copyProperties
(
ownerTemp
,
owners
);
if
(
ownerTemp
.
getIsActive
()
!=
null
)
{
if
(
ownerTemp
.
getIsActive
()
!=
null
)
{
owners
.
setIsActive
(
Long
.
valueOf
(
ownerTemp
.
getIsActive
()));
}
// 类型转换
if
(
ownerTemp
.
getOwnerType
()
!=
null
)
{
if
(
ownerTemp
.
getOwnerType
()
!=
null
)
{
owners
.
setOwnerType
(
Long
.
valueOf
(
ownerTemp
.
getOwnerType
()));
}
// 生成UUID主键
...
...
@@ -135,13 +152,14 @@ public class OwnersServiceImpl implements IOwnersService {
// 加入批量集合
batchInsertList
.
add
(
owners
);
successNum
++;
successMsg
.
append
(
"<br/>"
+
successNum
+
"、用户 "
+
owners
.
getOwnerName
()
+
" 导入成功"
);
String
ownerName
=
owners
.
getOwnerName
()
!=
null
?
owners
.
getOwnerName
()
:
cleanOwnerCode
;
successMsg
.
append
(
"<br/>"
+
successNum
+
"、用户 "
+
ownerName
+
" 导入成功"
);
}
catch
(
Exception
e
)
{
failureNum
++;
String
owner
Name
=
ownerTemp
.
getOwnerCode
()
!=
null
?
ownerTemp
.
getOwnerCode
()
:
"未知名称
"
;
String
msg
=
"<br/>"
+
failureNum
+
"、
账号 "
+
ownerNam
e
+
" 导入失败:"
;
String
owner
Code
=
ownerTemp
.
getOwnerCode
()
!=
null
?
ownerTemp
.
getOwnerCode
()
:
"未知编码
"
;
String
msg
=
"<br/>"
+
failureNum
+
"、
用户编码 "
+
ownerCod
e
+
" 导入失败:"
;
failureMsg
.
append
(
msg
+
e
.
getMessage
());
// 异常数据
不加入批量集合
,继续处理下一条
// 异常数据
跳过
,继续处理下一条
continue
;
}
}
...
...
@@ -151,22 +169,50 @@ public class OwnersServiceImpl implements IOwnersService {
try
{
ownersMapper
.
batchInsertOwners
(
batchInsertList
);
}
catch
(
Exception
e
)
{
// 批量插入失败时,统计失败数量并抛出异常
failureNum
+=
batchInsertList
.
size
();
successNum
-=
batchInsertList
.
size
();
failureMsg
.
insert
(
0
,
String
.
format
(
"<br/>批量插入失败:%s,已失败数据条数追加 %d 条"
,
e
.
getMessage
(),
batchInsertList
.
size
()));
// 批量插入失败时,统计失败数量(不抛异常,仅记录)
int
batchFailNum
=
batchInsertList
.
size
();
failureNum
+=
batchFailNum
;
successNum
-=
batchFailNum
;
failureMsg
.
insert
(
0
,
String
.
format
(
"<br/>批量插入失败:%s,失败条数追加 %d 条"
,
e
.
getMessage
(),
batchFailNum
));
}
}
// 处理结果反馈
// 结果反馈处理
StringBuilder
resultMsg
=
new
StringBuilder
();
if
(
successNum
>
0
)
{
resultMsg
.
append
(
"数据导入完成!成功导入 "
+
successNum
+
" 条,成功列表如下:"
);
resultMsg
.
append
(
successMsg
);
}
if
(
failureNum
>
0
)
{
failureMsg
.
insert
(
0
,
"很抱歉,导入失败!共 "
+
failureNum
+
" 条数据格式不正确"
);
throw
new
ServiceException
(
failureMsg
.
toString
());
}
else
{
successMsg
.
insert
(
0
,
"恭喜您,数据已全部导入成功!共 "
+
successNum
+
" 条,数据如下:"
);
resultMsg
.
append
(
"<br/><br/>失败导入 "
+
failureNum
+
" 条,失败原因如下:"
);
resultMsg
.
append
(
failureMsg
);
}
// 全部失败的友好提示
if
(
successNum
==
0
&&
failureNum
>
0
)
{
resultMsg
.
insert
(
0
,
"很抱歉,所有数据均导入失败!"
);
}
return
resultMsg
.
toString
();
}
private
Map
<
String
,
String
>
loadOwnerCodeToIdMap
()
{
// 查询条件补充:仅查询未删除的用户(符合业务逻辑)
Owners
query
=
new
Owners
();
query
.
setIsUsed
(
1L
);
List
<
Owners
>
ownerList
=
ownersMapper
.
selectOwnersList
(
query
);
if
(
org
.
springframework
.
util
.
CollectionUtils
.
isEmpty
(
ownerList
))
{
return
Collections
.
emptyMap
();
}
return
successMsg
.
toString
();
return
ownerList
.
stream
()
.
filter
(
o
->
StringUtils
.
isNotBlank
(
o
.
getOwnerCode
()))
// 核心优化:对编码去空格后作为Key,避免空格导致的匹配错误
.
collect
(
Collectors
.
toMap
(
o
->
o
.
getOwnerCode
().
trim
(),
Owners:
:
getId
,
(
k1
,
k2
)
->
k1
// 存在重复编码时保留第一条(避免Map键冲突)
));
}
/**
...
...
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/StorageLocationsServiceImpl.java
View file @
90af9a4c
package
com
.
ruoyi
.
inventory
.
service
.
impl
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
com.ruoyi.common.config.WarehouseConfig
;
import
com.ruoyi.common.core.domain.entity.Materials
;
...
...
@@ -266,12 +264,28 @@ public class StorageLocationsServiceImpl implements IStorageLocationsService
// 批量插入数据集合
List
<
StorageLocations
>
batchInsertList
=
new
ArrayList
<>();
// ========== 核心优化:提前加载所有已存在的库位编码映射 ==========
Map
<
String
,
String
>
locationCodeToIdMap
=
loadLocationCodeToIdMap
();
for
(
StorageLocationsLocationTemplateVO
templateVO
:
storageLocationsLocationTemplateVOS
)
{
try
{
StorageLocations
storageLocations
=
new
StorageLocations
();
// 1. 库位编码为空校验
String
locationCode
=
templateVO
.
getLocationCode
();
if
(
locationCode
==
null
||
locationCode
.
trim
().
isEmpty
())
{
throw
new
ServiceException
(
"【"
+
templateVO
.
getNo
()+
"】库位编码不能为空"
);
}
// 去除首尾空格,避免因空格导致的重复判断错误
String
cleanLocationCode
=
locationCode
.
trim
();
// 2. 库位编码已存在校验(优化:从Map中查询,无需数据库交互)
if
(
locationCodeToIdMap
.
containsKey
(
cleanLocationCode
))
{
throw
new
ServiceException
(
"库位【"
+
cleanLocationCode
+
"】已存在"
);
}
// 拷贝基础属性
BeanUtils
.
copyProperties
(
templateVO
,
storageLocations
);
storageLocations
.
setLocationName
(
templateVO
.
getLocationCode
());
storageLocations
.
setLocationName
(
cleanLocationCode
);
// 使用去空格后的编码
// 字段类型转换与赋值
storageLocations
.
setId
(
UUID
.
randomUUID
().
toString
());
...
...
@@ -304,8 +318,8 @@ public class StorageLocationsServiceImpl implements IStorageLocationsService
successMsg
.
append
(
"<br/>"
+
successNum
+
"、库位 "
+
locationName
+
" 导入成功"
);
}
catch
(
Exception
e
)
{
failureNum
++;
String
location
Name
=
templateVO
.
getLocationCode
()
!=
null
?
templateVO
.
getLocationCode
()
:
"未知名称
"
;
String
msg
=
"<br/>"
+
failureNum
+
"、库位
"
+
locationNam
e
+
" 导入失败:"
;
String
location
Code
=
templateVO
.
getLocationCode
()
!=
null
?
templateVO
.
getLocationCode
()
:
"未知编码
"
;
String
msg
=
"<br/>"
+
failureNum
+
"、库位
编码 "
+
locationCod
e
+
" 导入失败:"
;
failureMsg
.
append
(
msg
+
e
.
getMessage
());
// 异常数据跳过,继续处理下一条
continue
;
...
...
@@ -318,20 +332,46 @@ public class StorageLocationsServiceImpl implements IStorageLocationsService
storageLocationsMapper
.
batchInsertStorageLocations
(
batchInsertList
);
}
catch
(
Exception
e
)
{
// 批量插入失败,更新失败统计
failureNum
+=
batchInsertList
.
size
();
successNum
-=
batchInsertList
.
size
();
int
batchFailNum
=
batchInsertList
.
size
();
failureNum
+=
batchFailNum
;
successNum
-=
batchFailNum
;
failureMsg
.
insert
(
0
,
String
.
format
(
"<br/>批量插入失败:%s,失败条数追加 %d 条"
,
e
.
getMessage
(),
batch
InsertList
.
size
()
));
e
.
getMessage
(),
batch
FailNum
));
}
}
// 结果反馈处理
StringBuilder
resultMsg
=
new
StringBuilder
();
if
(
successNum
>
0
)
{
resultMsg
.
append
(
"数据导入完成!成功导入 "
+
successNum
+
" 条,成功列表如下:"
);
resultMsg
.
append
(
successMsg
);
}
if
(
failureNum
>
0
)
{
failureMsg
.
insert
(
0
,
"很抱歉,导入失败!共 "
+
failureNum
+
" 条数据格式不正确"
);
throw
new
ServiceException
(
failureMsg
.
toString
());
}
else
{
successMsg
.
insert
(
0
,
"恭喜您,数据已全部导入成功!共 "
+
successNum
+
" 条,数据如下:"
);
resultMsg
.
append
(
"<br/><br/>失败导入 "
+
failureNum
+
" 条,失败原因如下:"
);
resultMsg
.
append
(
failureMsg
);
}
// 如果全部失败,保留友好提示
if
(
successNum
==
0
&&
failureNum
>
0
)
{
resultMsg
.
insert
(
0
,
"很抱歉,所有数据均导入失败!"
);
}
return
resultMsg
.
toString
();
}
private
Map
<
String
,
String
>
loadLocationCodeToIdMap
()
{
StorageLocations
query
=
new
StorageLocations
();
query
.
setIsUsed
(
1L
);
List
<
StorageLocations
>
locationList
=
storageLocationsMapper
.
selectStorageLocationsList
(
query
);
if
(
org
.
springframework
.
util
.
CollectionUtils
.
isEmpty
(
locationList
))
{
return
Collections
.
emptyMap
();
}
return
successMsg
.
toString
();
return
locationList
.
stream
()
.
filter
(
l
->
StringUtils
.
isNotBlank
(
l
.
getLocationCode
()))
// 核心优化:对编码去空格后作为Key,避免空格导致的匹配错误
.
collect
(
Collectors
.
toMap
(
l
->
l
.
getLocationCode
().
trim
(),
StorageLocations:
:
getId
,
(
k1
,
k2
)
->
k1
// 存在重复编码时保留第一条
));
}
}
\ No newline at end of file
ruoyi-inventory/src/main/java/com/ruoyi/inventory/service/impl/WarehousesServiceImpl.java
View file @
90af9a4c
package
com
.
ruoyi
.
inventory
.
service
.
impl
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
com.ruoyi.common.exception.ServiceException
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.SecurityUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.inventory.domain.StorageLocations
;
import
com.ruoyi.inventory.service.IWarehousesService
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
com.ruoyi.inventory.mapper.WarehousesMapper
;
...
...
@@ -142,4 +143,128 @@ public class WarehousesServiceImpl implements IWarehousesService
public
List
<
Map
<
String
,
Object
>>
getMapList
(){
return
warehousesMapper
.
getMapList
();
}
@Override
public
String
importWarehouses
(
List
<
Warehouses
>
warehousesList
,
Boolean
isUpdateSupport
,
String
operName
)
{
// 空数据校验
if
(
CollectionUtils
.
isEmpty
(
warehousesList
))
{
throw
new
ServiceException
(
"导入仓库数据不能为空!"
);
}
int
successNum
=
0
;
int
failureNum
=
0
;
StringBuilder
successMsg
=
new
StringBuilder
();
StringBuilder
failureMsg
=
new
StringBuilder
();
Date
now
=
DateUtils
.
getNowDate
();
// 获取当前登录用户ID
Long
userId
=
SecurityUtils
.
getUserId
();
String
operId
=
userId
.
toString
();
// 批量插入的有效数据集合
List
<
Warehouses
>
batchInsertList
=
new
ArrayList
<>();
// ========== 核心优化:提前加载所有已存在的仓库编码映射 ==========
Map
<
String
,
String
>
warehouseCodeToIdMap
=
warehousesCodeToIdMap
();
for
(
Warehouses
warehouse
:
warehousesList
)
{
// ========== 关键修复:先校验warehouse对象是否为null ==========
if
(
warehouse
==
null
)
{
failureNum
++;
failureMsg
.
append
(
"<br/>"
+
failureNum
+
"、导入失败:数据行不能为空(存在空对象)"
);
continue
;
// 跳过空对象,继续处理下一条
}
try
{
// 核心校验:仓库编码不能为空
String
warehouseCode
=
warehouse
.
getWarehousesCode
();
if
(
warehouseCode
==
null
||
warehouseCode
.
trim
().
isEmpty
())
{
// 修复原逻辑错误:当编码为空时,提示中用"未知编码"替代空值,避免展示【null】或空字符串
throw
new
ServiceException
(
"【未知编码】仓库编码不能为空"
);
}
// 去除首尾空格,避免因空格导致的重复判断错误
String
cleanWarehouseCode
=
warehouseCode
.
trim
();
// 核心优化:从Map中校验仓库编码是否已存在(替代数据库查询)
if
(
warehouseCodeToIdMap
.
containsKey
(
cleanWarehouseCode
))
{
throw
new
ServiceException
(
"仓库【"
+
cleanWarehouseCode
+
"】已存在"
);
}
// 生成UUID主键(根据实际主键策略调整,若数据库自增可删除)
warehouse
.
setId
(
UUID
.
randomUUID
().
toString
());
// 填充公共字段
warehouse
.
setCreateBy
(
operId
);
warehouse
.
setCreateTime
(
now
);
warehouse
.
setCreateUserCode
(
operId
);
if
(
warehouse
.
getIsUsed
()
==
null
)
{
warehouse
.
setIsUsed
(
1L
);
// 默认未删除
}
if
(
warehouse
.
getSortNo
()
==
null
)
{
warehouse
.
setSortNo
(
0L
);
// 默认排序号
}
// 加入批量集合
batchInsertList
.
add
(
warehouse
);
successNum
++;
String
warehouseName
=
warehouse
.
getWarehousesName
()
!=
null
?
warehouse
.
getWarehousesName
()
:
cleanWarehouseCode
;
successMsg
.
append
(
"<br/>"
+
successNum
+
"、仓库 "
+
warehouseName
+
" 导入成功"
);
}
catch
(
Exception
e
)
{
failureNum
++;
String
warehouseCode
=
warehouse
.
getWarehousesCode
()
!=
null
?
warehouse
.
getWarehousesCode
()
:
"未知编码"
;
String
msg
=
"<br/>"
+
failureNum
+
"、仓库编码 "
+
warehouseCode
+
" 导入失败:"
;
failureMsg
.
append
(
msg
+
e
.
getMessage
());
// 异常数据跳过,继续处理下一条
continue
;
}
}
// 批量插入有效数据(如果有)
if
(!
CollectionUtils
.
isEmpty
(
batchInsertList
))
{
try
{
warehousesMapper
.
batchInsertWarehouses
(
batchInsertList
);
}
catch
(
Exception
e
)
{
// 批量插入失败时,统计失败数量(不抛异常,仅记录)
int
batchFailNum
=
batchInsertList
.
size
();
failureNum
+=
batchFailNum
;
successNum
-=
batchFailNum
;
failureMsg
.
insert
(
0
,
String
.
format
(
"<br/>批量插入失败:%s,失败条数追加 %d 条"
,
e
.
getMessage
(),
batchFailNum
));
}
}
// 结果反馈处理
StringBuilder
resultMsg
=
new
StringBuilder
();
if
(
successNum
>
0
)
{
resultMsg
.
append
(
"数据导入完成!成功导入 "
+
successNum
+
" 条,成功列表如下:"
);
resultMsg
.
append
(
successMsg
);
}
if
(
failureNum
>
0
)
{
resultMsg
.
append
(
"<br/><br/>失败导入 "
+
failureNum
+
" 条,失败原因如下:"
);
resultMsg
.
append
(
failureMsg
);
}
// 全部失败的友好提示
if
(
successNum
==
0
&&
failureNum
>
0
)
{
resultMsg
.
insert
(
0
,
"很抱歉,所有数据均导入失败!"
);
}
return
resultMsg
.
toString
();
}
private
Map
<
String
,
String
>
warehousesCodeToIdMap
()
{
Warehouses
query
=
new
Warehouses
();
query
.
setIsUsed
(
1L
);
// 仅查询未删除的仓库数据
List
<
Warehouses
>
warehouseList
=
warehousesMapper
.
selectWarehousesList
(
query
);
if
(
org
.
springframework
.
util
.
CollectionUtils
.
isEmpty
(
warehouseList
))
{
return
Collections
.
emptyMap
();
}
return
warehouseList
.
stream
()
.
filter
(
w
->
StringUtils
.
isNotBlank
(
w
.
getWarehousesCode
()))
// 核心优化:对编码去空格后作为Key,避免空格导致的匹配错误
.
collect
(
Collectors
.
toMap
(
w
->
w
.
getWarehousesCode
().
trim
(),
Warehouses:
:
getId
,
(
k1
,
k2
)
->
k1
// 存在重复编码时保留第一条(避免Map键冲突)
));
}
}
ruoyi-inventory/src/main/resources/mapper/inventory/WarehousesMapper.xml
View file @
90af9a4c
...
...
@@ -39,7 +39,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if
test=
"manager != null and manager != ''"
>
and manager like concat('%', #{manager}, '%')
</if>
<if
test=
"contactPhone != null and contactPhone != ''"
>
and contact_phone = #{contactPhone}
</if>
<if
test=
"isEnabled != null "
>
and is_enabled = #{isEnabled}
</if>
<if
test=
"isUsed != null "
>
and is_used = #{isUsed}
</if>
<if
test=
"sortNo != null "
>
and sort_no = #{sortNo}
</if>
<if
test=
"createUserCode != null and createUserCode != ''"
>
and create_user_code = #{createUserCode}
</if>
<if
test=
"updateUserCode != null and updateUserCode != ''"
>
and update_user_code = #{updateUserCode}
</if>
...
...
@@ -133,4 +132,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{id}
</foreach>
</delete>
<insert
id=
"batchInsertWarehouses"
parameterType=
"java.util.List"
>
insert into warehouses
(
id, warehouses_code, warehouses_name, warehouse_type, address, area,
capacity, manager, contact_phone, is_enabled, is_used, sort_no,
create_time, create_user_code, update_time, update_user_code
)
values
<foreach
collection=
"list"
item=
"item"
separator=
","
>
(
#{item.id}, #{item.warehousesCode}, #{item.warehousesName},
#{item.warehouseType}, #{item.address}, #{item.area},
#{item.capacity}, #{item.manager}, #{item.contactPhone},
#{item.isEnabled}, #{item.isUsed}, #{item.sortNo},
#{item.createTime}, #{item.createUserCode}, #{item.updateTime},
#{item.updateUserCode}
)
</foreach>
</insert>
</mapper>
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论