|
@@ -19,6 +19,7 @@ import com.iamberry.rst.util.PageUtil;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Isolation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -72,9 +73,9 @@ public class InventoryServiceImpl implements InventoryService {
|
|
|
return inventoryMapper.insert(inventoryInfo);
|
|
|
}
|
|
|
|
|
|
- @Transactional
|
|
|
@Override
|
|
|
- public synchronized Integer addInventory(InventoryInfo invent) {
|
|
|
+ @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.REPEATABLE_READ)
|
|
|
+ public Integer addInventory(InventoryInfo invent) {
|
|
|
Integer flag = 0;
|
|
|
//查询是否是组合产品
|
|
|
if(invent != null && invent.getInventoryProductColorId() != null){
|
|
@@ -98,101 +99,93 @@ public class InventoryServiceImpl implements InventoryService {
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
- public Integer addInv(InventoryInfo invent){
|
|
|
- Integer flag = 0;
|
|
|
+ private Integer addInv(InventoryInfo invent){
|
|
|
+ // 检查仓库数据
|
|
|
if(invent.getWarehouseId() == null){
|
|
|
- throw new RuntimeException("【出库】更新库存失败,出库仓库ID。");
|
|
|
+ throw new RuntimeException("【出库】更新库存失败,出库仓库ID为空。");
|
|
|
}
|
|
|
+ // 查询产品信息是否为空
|
|
|
if(invent.getInventoryProductBar() == null && invent.getInventoryProductColorId() == null){
|
|
|
throw new RuntimeException("【出库】更新库存失败,出库需要产品SKU或颜色ID。");
|
|
|
+ }
|
|
|
+ ProductColor productColor = null;
|
|
|
+ if(!StringUtils.isEmpty(invent.getInventoryProductBar())){
|
|
|
+ productColor = productColorMapper.getProductColorByBar(invent.getInventoryProductBar());
|
|
|
}else{
|
|
|
- ProductColor productColor = null;
|
|
|
- if(!StringUtils.isEmpty(invent.getInventoryProductBar())){
|
|
|
- productColor = productColorMapper.getProductColorByBar(invent.getInventoryProductBar());
|
|
|
- }else{
|
|
|
- productColor = productColorMapper.getProductColorById(invent.getInventoryProductColorId());
|
|
|
- }
|
|
|
- if(productColor != null){
|
|
|
- invent.setInventoryProductBar(productColor.getColorBar());
|
|
|
- }else{
|
|
|
- throw new RuntimeException("【出库】更新库存失败,产品SKU或颜色ID出错。");
|
|
|
- }
|
|
|
+ productColor = productColorMapper.getProductColorById(invent.getInventoryProductColorId());
|
|
|
}
|
|
|
-
|
|
|
+ if(productColor == null){
|
|
|
+ throw new RuntimeException("【出库】更新库存失败,产品SKU或颜色ID出错。");
|
|
|
+ }
|
|
|
+ // 查询仓库信息
|
|
|
OrderWarehouse orderWarehouse = orderWarehouseMapper.getOrderWarehouseById(invent.getWarehouseId());
|
|
|
if(orderWarehouse == null){
|
|
|
throw new RuntimeException("【出库】更新库存失败,查询仓库信息失败。");
|
|
|
}
|
|
|
- String warehouseName = orderWarehouse.getWarehouseName();
|
|
|
- InventoryInfo info = inventoryMapper.getByInventoryByBar(invent.getInventoryProductBar(),invent.getWarehouseId());
|
|
|
- if(info != null){
|
|
|
- boolean isNumUpadate = false;
|
|
|
- Integer logType = 0;
|
|
|
- Integer num = 0; //数量
|
|
|
- Integer logQualityType=0;//1.良品 2不良品
|
|
|
- InventoryInfo newInent = new InventoryInfo();
|
|
|
- newInent.setInventoryId(info.getInventoryId());
|
|
|
+ // 查询当前仓库管理的库存信息
|
|
|
+ InventoryInfo info = inventoryMapper.getByInventoryByBar(productColor.getColorBar(),invent.getWarehouseId());
|
|
|
+ if (info == null) {
|
|
|
+ throw new RuntimeException("【入库】更新库存失败,未查询到库存信息,请确认“"+ orderWarehouse.getWarehouseName() +"”是否有该产品["+ invent.getInventoryProductBar()+ "]的库存信息。");
|
|
|
+ }
|
|
|
|
|
|
- String record = "";
|
|
|
- if(!StringUtils.isEmpty(invent.getInventoryRecentRecord())){
|
|
|
- record = invent.getInventoryRecentRecord();
|
|
|
- }
|
|
|
- //日志记录与 库存最后记录保持一致
|
|
|
- String logRecord = "";
|
|
|
+ // 获取当前仓库管理的数据
|
|
|
+ InventoryInfo newInent = new InventoryInfo();
|
|
|
+ newInent.setInventoryId(info.getInventoryId());
|
|
|
+ newInent.setWarehouseId(invent.getWarehouseId());
|
|
|
|
|
|
- //inventoryGoodProductNum;//良品数量
|
|
|
- if(invent.getInventoryGoodProductNum() != null && invent.getInventoryGoodProductNum() != 0 ){
|
|
|
- logRecord = "[良品]入库"+invent.getInventoryGoodProductNum()+"件,"+record;
|
|
|
- newInent.setInventoryGoodProductNum(info.getInventoryGoodProductNum() + invent.getInventoryGoodProductNum());
|
|
|
- newInent.setInventoryRecentRecord(logRecord);
|
|
|
- isNumUpadate = true;
|
|
|
- num = invent.getInventoryGoodProductNum();
|
|
|
- logQualityType = 1;
|
|
|
- }else if(invent.getInventoryDefectiveProductNum() != null && invent.getInventoryDefectiveProductNum() != 0 ){
|
|
|
- //inventoryDefectiveProductNum;//不良品数量
|
|
|
- logRecord = "[不良品]入库"+invent.getInventoryDefectiveProductNum()+"件,"+record;
|
|
|
- newInent.setInventoryDefectiveProductNum(info.getInventoryDefectiveProductNum() + invent.getInventoryDefectiveProductNum());
|
|
|
- newInent.setInventoryRecentRecord(logRecord);
|
|
|
- isNumUpadate = true;
|
|
|
- num = invent.getInventoryDefectiveProductNum();
|
|
|
- logQualityType = 2;
|
|
|
- }
|
|
|
-// else if(invent.getInventoryRemainingNum() != null && invent.getInventoryRemainingNum() != 0 ){
|
|
|
-// // inventoryRemainingNum;//库存剩余数量
|
|
|
-// newInent.setInventoryRemainingNum(info.getInventoryRemainingNum() - invent.getInventoryRemainingNum());
|
|
|
-// info.setInventoryRecentRecord("[剩余库存]出库:"+invent.getInventoryRemainingNum()+"件");
|
|
|
-// isNumUpadate = true;
|
|
|
-// num = invent.getInventoryRemainingNum();
|
|
|
-// }
|
|
|
- if(isNumUpadate && logQualityType != 0 && num != 0){
|
|
|
- flag = inventoryMapper.updateById(newInent);
|
|
|
- if(flag < 1){
|
|
|
- throw new RuntimeException("【入库】更新库存失败,库存修改失败。");
|
|
|
- }
|
|
|
- InventoryLog inventoryLog = new InventoryLog();
|
|
|
- inventoryLog.setLogProductName(info.getInventoryProductName());
|
|
|
- inventoryLog.setLogProductBar(info.getInventoryProductBar());
|
|
|
- inventoryLog.setLogRemainingNum(num);
|
|
|
- if(invent.getLogType() != null && invent.getLogType()!=0){
|
|
|
- inventoryLog.setLogType(invent.getLogType()); //类型1.采购 2.发货 3.次品
|
|
|
- }else{
|
|
|
- inventoryLog.setLogType(1); //类型1.采购 2.发货 3.次品
|
|
|
- }
|
|
|
- inventoryLog.setLogOperationType(1); //增加
|
|
|
- inventoryLog.setLogDesc(logRecord);
|
|
|
- inventoryLog.setLogQualityType(logQualityType);
|
|
|
- inventoryLog.setLogWarehouseId(invent.getWarehouseId());
|
|
|
- flag = inventoryLogMapper.insert(inventoryLog);
|
|
|
- if(flag < 1){
|
|
|
- throw new RuntimeException("【入库】更新库存失败,增加库存记录失败。");
|
|
|
- }
|
|
|
- }else{
|
|
|
- throw new RuntimeException("【入库】更新库存失败,数量未变动或数据出错。");
|
|
|
- }
|
|
|
+ synchronized (this) {
|
|
|
+ // 此处使用空修改,将多个线程阻塞,只允许一个线程通过,使用mysql的行级锁进行排他
|
|
|
+ inventoryMapper.updateById(newInent);
|
|
|
+ }
|
|
|
+ // 备注信息
|
|
|
+ String record = invent.getInventoryRecentRecord() == null ? "" : invent.getInventoryRecentRecord();
|
|
|
+ //日志记录与 库存最后记录保持一致
|
|
|
+ String logRecord = "";
|
|
|
+
|
|
|
+ // logQualityType: 1.良品 2不良品 num:本次变动的数量
|
|
|
+ int logQualityType=0, num = 0;;
|
|
|
+ if(invent.getInventoryGoodProductNum() != null && invent.getInventoryGoodProductNum() != 0 ){
|
|
|
+ //良品数量
|
|
|
+ logRecord = "[良品]入库"+invent.getInventoryGoodProductNum()+"件,"+record;
|
|
|
+ newInent.setInventoryGoodProductNum(info.getInventoryGoodProductNum() + invent.getInventoryGoodProductNum());
|
|
|
+ newInent.setInventoryRecentRecord(logRecord);
|
|
|
+ num = invent.getInventoryGoodProductNum();
|
|
|
+ logQualityType = 1;
|
|
|
+ }else if(invent.getInventoryDefectiveProductNum() != null && invent.getInventoryDefectiveProductNum() != 0 ){
|
|
|
+ //不良品数量
|
|
|
+ logRecord = "[不良品]入库"+invent.getInventoryDefectiveProductNum()+"件,"+record;
|
|
|
+ newInent.setInventoryDefectiveProductNum(info.getInventoryDefectiveProductNum() + invent.getInventoryDefectiveProductNum());
|
|
|
+ newInent.setInventoryRecentRecord(logRecord);
|
|
|
+ num = invent.getInventoryDefectiveProductNum();
|
|
|
+ logQualityType = 2;
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("【入库】更新库存失败,数量未变动或数据出错。");
|
|
|
+ }
|
|
|
+ if(inventoryMapper.updateById(newInent) <= 0){
|
|
|
+ throw new RuntimeException("【入库】更新库存失败,库存修改失败。");
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * 添加库存日志信息
|
|
|
+ */
|
|
|
+ InventoryLog inventoryLog = new InventoryLog();
|
|
|
+ inventoryLog.setLogProductName(info.getInventoryProductName());
|
|
|
+ inventoryLog.setLogProductBar(info.getInventoryProductBar());
|
|
|
+ inventoryLog.setLogRemainingNum(num);
|
|
|
+ if(invent.getLogType() != null && invent.getLogType()!=0){
|
|
|
+ //类型1.采购 2.发货 3.次品
|
|
|
+ inventoryLog.setLogType(invent.getLogType());
|
|
|
}else{
|
|
|
- throw new RuntimeException("【入库】更新库存失败,未查询到库存信息,请确认“"+ warehouseName +"”是否有该产品["+ invent.getInventoryProductBar()+ "]的库存信息。");
|
|
|
+ //类型1.采购 2.发货 3.次品
|
|
|
+ inventoryLog.setLogType(1);
|
|
|
}
|
|
|
- return flag;
|
|
|
+ inventoryLog.setLogOperationType(1);
|
|
|
+ inventoryLog.setLogDesc(logRecord);
|
|
|
+ inventoryLog.setLogQualityType(logQualityType);
|
|
|
+ inventoryLog.setLogWarehouseId(invent.getWarehouseId());
|
|
|
+ if (inventoryLogMapper.insert(inventoryLog) <= 0) {
|
|
|
+ throw new RuntimeException("【入库】添加库存日志失败。");
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
@Transactional
|