Browse Source

更新拆单库存查询、拆单进制计算算法

lwx_dev
erdanergou 3 years ago
parent
commit
65cd73d772
  1. 3
      src/main/java/com/dreamchaser/depository_manage/controller/MaterialController.java
  2. 2
      src/main/java/com/dreamchaser/depository_manage/controller/SplitController.java
  3. 6
      src/main/java/com/dreamchaser/depository_manage/mapper/SplitUnitMapper.xml
  4. 20
      src/main/java/com/dreamchaser/depository_manage/service/SplitUnitService.java
  5. 447
      src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java
  6. 229
      src/main/java/com/dreamchaser/depository_manage/service/impl/SplitUnitServiceImpl.java
  7. 56
      src/test/java/com/dreamchaser/depository_manage/TestForOther.java
  8. 6
      target/classes/com/dreamchaser/depository_manage/mapper/SplitUnitMapper.xml

3
src/main/java/com/dreamchaser/depository_manage/controller/MaterialController.java

@ -611,7 +611,7 @@ public class MaterialController {
SplitInfo splitInfo = splitUnitService.findSplitInfoByMidAndUnit(paramForSplitInfo);
// 获取最高级对应当前单位的数量
int saveQuantity = inventoryP.getQuantity() * splitUnitService.findSplitInfoQuantity(splitInfo);
int saveQuantity = inventoryP.getQuantity() * splitUnitService.findSplitInfoScaleQuantity(splitInfo,-1);
SplitInventory splitInventory = splitUnitService.findSplitInventoryByIidAndSid(inventoryP.getId(), splitInfo.getId());
saveQuantity += splitInventory.getSaveQuantity();
@ -623,7 +623,6 @@ public class MaterialController {
saveQuantity += splitInventory.getSaveQuantity() * splitInfo.getQuantity();
parentSplitInfo = splitUnitService.findSplitInfoById(parentSplitInfo.getParentId());
}
if (quantity <= saveQuantity) { // 如果当前数量合适则跳出循环
flag = true;
break;

2
src/main/java/com/dreamchaser/depository_manage/controller/SplitController.java

@ -123,7 +123,7 @@ public class SplitController {
List<Inventory> inventoryByMid = materialService.findInventoryByMid(splitInfoByMidAndUnit.getMid());
if(inventoryByMid.size() > 0){
Inventory inventory = inventoryByMid.get(0);
return new RestResponse(inventory.getPrice() / splitUnitService.findSplitInfoQuantity(splitInfoByMidAndUnit));
return new RestResponse(inventory.getPrice() / splitUnitService.findSplitInfoScaleQuantity(splitInfoByMidAndUnit,-1));
}
}else{
Integer mid = ObjectFormatUtil.toInteger(map.get("mid"));

6
src/main/java/com/dreamchaser/depository_manage/mapper/SplitUnitMapper.xml

@ -151,13 +151,13 @@
<if test="sid != null and sid != ''">
sid = #{sid},
</if>
<if test="outQuantity != null and outQuantity != ''">
<if test="outQuantity != null">
outQuantity = #{outQuantity},
</if>
<if test="inQuantity != null and inQuantity != ''">
<if test="inQuantity != null">
inQuantity = #{inQuantity},
</if>
<if test="saveQuantity != null and saveQuantity != ''">
<if test="saveQuantity != null">
saveQuantity = #{saveQuantity},
</if>
</set>

20
src/main/java/com/dreamchaser/depository_manage/service/SplitUnitService.java

@ -1,5 +1,6 @@
package com.dreamchaser.depository_manage.service;
import com.dreamchaser.depository_manage.entity.Inventory;
import com.dreamchaser.depository_manage.entity.SplitInfo;
import com.dreamchaser.depository_manage.entity.SplitInventory;
import com.dreamchaser.depository_manage.pojo.SplitInfoP;
@ -101,12 +102,25 @@ public interface SplitUnitService {
List<SplitInventory> findSplitInventoryByIid(Integer Iid);
/**
* 用于计算当前拆单单位与基础单位之间的进率
*
* 用于计算当前拆单单位与目标拆单单位之间的进率
* @param splitInfo 待查询拆单记录
* @param parentSplitInfoId 目标拆单单位
* @return
*/
int findSplitInfoScaleQuantity(SplitInfo splitInfo, Integer parentSplitInfoId);
/**
* 获取当前拆单与基础拆单之间的库存总额(当前拆单记录为-1是全部库存是使用)
*
* @param splitInfoId 当前拆单记录Id
* @param inventory 库存记录
* @param baseSplitInfoId 基础拆单记录Id
* @param saveQuantity 最终返回值
* @param allQuantityFlag 用于标志是否获取全部库存
* @return
*/
int findSplitInfoQuantity(SplitInfo splitInfo);
int findAllInventoryForSplitInfo(Integer splitInfoId, Inventory inventory, Integer baseSplitInfoId, int saveQuantity, boolean allQuantityFlag);
/**
* 根据库存id与拆单记录id获取对应拆单库存处理记录

447
src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java

@ -11,6 +11,7 @@ import com.dreamchaser.depository_manage.pojo.callBackXml.approvalCallBackXml.Ap
import com.dreamchaser.depository_manage.pojo.callBackXml.callBackXml_button_templatecard.TemplateCard;
import com.dreamchaser.depository_manage.service.DepositoryRecordService;
import com.dreamchaser.depository_manage.service.RoleService;
import com.dreamchaser.depository_manage.service.SplitUnitService;
import com.dreamchaser.depository_manage.utils.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
@ -61,6 +62,9 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
@Autowired
private SplitUnitMapper splitUnitMapper;
@Autowired
private SplitUnitService splitUnitService;
/**
* 提交申请插入一条仓库调度记录
@ -889,9 +893,9 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "出库失败,库存不足"));
}
} else {
// 如果是拆单后的单位
}
else {
// 如果是拆单后的出库
// 用于获取对应的拆单记录
Map<String, Object> paramForSplitInfo = new HashMap<>();
@ -943,12 +947,36 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
// 获取剩余不足的数量
int surplus = trueOut - splitInventory.getSaveQuantity();
if (surplus < splitInfoById.getQuantity()) {
if (splitInfoById.getParentId() != null) {
// 如果当前拆单有父级
// 设置剩余拆单库存处理数量
// 设置当前拆单库存出库数量
splitInventory.setOutQuantity(splitInventory.getOutQuantity() + splitInventory.getSaveQuantity());
// 将当前库存设置为0
splitInventory.setSaveQuantity(0);
// 修改库存数量
splitUnitMapper.updateSplitInventory(splitInventory);
if (splitInfoById.getParentId() != null) {
// 如果当前拆单没有父级
// 如果剩余不足的数量小于拆单单位对应的数量
// 获取当前父级拆单记录
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(splitInfoById.getParentId());
restResponse = updateSplitInventoryInfo(parentSplitInfo, inventory, trueOut, surplus, splitInfo);
updateApplicationMinOutInfo(id, applicationOutMinById, record, trueOut, userByPort, placeId, userAgent);
} else {
// 如果当前拆单没有父级
if (surplus < splitInfoById.getQuantity()) {
// 如果当前未处理的数量小于当前拆单库存数量
Map<String, Object> params = new HashMap<>();
params.put("mid", inventory.getId());
@ -976,13 +1004,16 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
// 令库存-1
inventory.setQuantity(inventory.getQuantity() - 1);
// 重新计算总额
inventory.setAmounts(inventory.getQuantity() * inventory.getPrice());
// 重新计算单价
BigDecimal decimal = new BigDecimal(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
if (Integer.compare(inventory.getQuantity(), 0) != 0) {
// 重新计算单价
BigDecimal decimal = BigDecimal.valueOf(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
}
// 修改库存记录
materialMapper.updateInventory(inventory);
@ -1023,100 +1054,95 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "出库失败,库存不足"));
}
}else{
//如果有父级
} else {
// 如果大于
// 获取库存重要减少的数量
int surplus_redundant = surplus / splitInfoById.getQuantity();
// 获取拆单库存处理中要处理数量
int saveQuantity = surplus - surplus_redundant * splitInfoById.getQuantity();
if (saveQuantity > splitInventory.getSaveQuantity()) {
// 如果要处理的数量大于剩余数量
}
} else {
// 如果大于
// 获取库存重要减少的数量
int surplus_redundant = surplus / splitInfoById.getQuantity();
// 获取拆单库存处理中要处理数量
int saveQuantity = surplus - surplus_redundant * splitInfoById.getQuantity();
// 获取一个数量
surplus_redundant += 1;
splitInventory.setSaveQuantity(splitInfoById.getQuantity() + splitInventory.getSaveQuantity() - saveQuantity);
} else {
// 如果不大于
splitInventory.setSaveQuantity(splitInventory.getSaveQuantity() - saveQuantity);
}
splitInventory.setOutQuantity(splitInventory.getOutQuantity() + saveQuantity);
Map<String, Object> params = new HashMap<>();
params.put("mid", inventory.getId());
params.put("pid", placeId);
if (saveQuantity > splitInventory.getSaveQuantity()) {
// 如果要处理的数量大于剩余数量
// 获取当前物料在库位中的数量
placeAndMaterialByMidAndPid = placeMapper.findPlaceAndMaterialByMidAndPid(params);
// 获取一个数量
surplus_redundant += 1;
splitInventory.setSaveQuantity(splitInfoById.getQuantity() + splitInventory.getSaveQuantity() - saveQuantity);
} else {
// 如果不大于
splitInventory.setSaveQuantity(splitInventory.getSaveQuantity() - saveQuantity);
}
splitInventory.setOutQuantity(splitInventory.getOutQuantity() + saveQuantity);
Map<String, Object> params = new HashMap<>();
params.put("mid", inventory.getId());
params.put("pid", placeId);
// 获取当前物料在库位中的数量
placeAndMaterialByMidAndPid = placeMapper.findPlaceAndMaterialByMidAndPid(params);
if (placeAndMaterialByMidAndPid != null) {
// 如果当前库位存在该物料
if (placeAndMaterialByMidAndPid.getQuantity() < surplus_redundant) {
// 如果当前库存中的物料库存不足以出库
flag = false;
if (placeAndMaterialByMidAndPid != null) {
// 如果当前库位存在该物料
if (placeAndMaterialByMidAndPid.getQuantity() < surplus_redundant) {
// 如果当前库存中的物料库存不足以出库
flag = false;
}
} else {
restResponse.setStatus(566);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "当前位置未发现该物料"));
return restResponse;
}
} else {
restResponse.setStatus(566);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "当前位置未发现该物料"));
return restResponse;
}
if (inventory.getQuantity() >= surplus_redundant && flag) {
// 如果能够出库
if (inventory.getQuantity() >= surplus_redundant && flag) {
// 如果能够出库
// 令库存-1
inventory.setQuantity(inventory.getQuantity() - surplus_redundant);
// 重新计算总额
inventory.setAmounts(inventory.getQuantity() * inventory.getPrice());
// 令库存-1
inventory.setQuantity(inventory.getQuantity() - surplus_redundant);
// 重新计算单价
BigDecimal decimal = new BigDecimal(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
// 重新计算总额
inventory.setAmounts(inventory.getQuantity() * inventory.getPrice());
// 修改库存记录
materialMapper.updateInventory(inventory);
if (Integer.compare(inventory.getQuantity(), 0) != 0) {
// 重新计算单价
BigDecimal decimal = BigDecimal.valueOf(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
}
// 修改库存记录
materialMapper.updateInventory(inventory);
// 修改当前库位存放物料的数量
placeAndMaterialByMidAndPid.setQuantity(placeAndMaterialByMidAndPid.getQuantity() - surplus_redundant);
placeMapper.updateMaterialAndPlace(placeAndMaterialByMidAndPid);
// 修改当前库位存放物料的数量
placeAndMaterialByMidAndPid.setQuantity(placeAndMaterialByMidAndPid.getQuantity() - surplus_redundant);
placeMapper.updateMaterialAndPlace(placeAndMaterialByMidAndPid);
// 修改库位数量
Place placeById = placeMapper.findPlaceById(placeId);
placeById.setQuantity(placeById.getQuantity() - surplus_redundant);
placeMapper.UpdatePlace(placeById);
// 修改库位数量
Place placeById = placeMapper.findPlaceById(placeId);
placeById.setQuantity(placeById.getQuantity() - surplus_redundant);
placeMapper.UpdatePlace(placeById);
// 如果是库存转移订单
Map<String, Object> map = new HashMap<>();
if (record.getIstransfer() == 1) {
map.put("quantity", String.valueOf(surplus_redundant));
map.put("applicantId", record.getApplicantId());
map.put("minRecordId", applicationOutMinById.getId()); // 出库订单编号
transferMaterial(map);
}
// 如果是库存转移订单
Map<String, Object> map = new HashMap<>();
if (record.getIstransfer() == 1) {
map.put("quantity", String.valueOf(surplus_redundant));
map.put("applicantId", record.getApplicantId());
map.put("minRecordId", applicationOutMinById.getId()); // 出库订单编号
transferMaterial(map);
}
updateApplicationMinOutInfo(id, applicationOutMinById, record, trueOut, userByPort, placeId, userAgent);
updateApplicationMinOutInfo(id, applicationOutMinById, record, trueOut, userByPort, placeId, userAgent);
restResponse.setStatus(200);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库成功", "出库成功"));
} else {
restResponse.setStatus(508);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "出库失败,库存不足"));
restResponse.setStatus(200);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库成功", "出库成功"));
} else {
restResponse.setStatus(508);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "出库失败,库存不足"));
}
}
}
}
} else {
@ -1136,11 +1162,12 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
// 设置新库存
inventory.setQuantity(inventory.getQuantity() - residue - 1);
// 重新计算单价
BigDecimal decimal = BigDecimal.valueOf(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
if (Integer.compare(inventory.getQuantity(), 0) != 0) {
// 重新计算单价
BigDecimal decimal = BigDecimal.valueOf(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
}
//更新库存
materialMapper.updateInventory(inventory);
@ -1191,23 +1218,237 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
return restResponse;
}
/**
* 用于有父级拆单记录的出库处理
* @param splitInfo 当前拆单记录
* @param inventory 当前库存记录
*
* @param splitInfo 父级拆单记录
* @param inventory 当前库存记录
* @param quantity 当前出库总量
* @param nowQuantity 当前未出库数量
* @param realSplitInfo 实际出库的拆单记录
* @return
*/
RestResponse updateSplitInventoryInfo(SplitInfo splitInfo,Inventory inventory){
// 获取当前拆单的父级
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
//
Map<String,Object> paramForSplitInventory = new HashMap<>();
paramForSplitInventory.put("sid",splitInfo.getId());
paramForSplitInventory.put("iid",inventory.getId());
splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
return null;
}
RestResponse updateSplitInventoryInfo(SplitInfo splitInfo, Inventory inventory, int quantity, int nowQuantity, SplitInfo realSplitInfo) {
// 定义返回值
RestResponse restResponse = new RestResponse();
// 用于获取当前拆单记录对应的拆单库存记录
Map<String, Object> paramForSplitInventory = new HashMap<>();
paramForSplitInventory.put("sid", splitInfo.getId());
paramForSplitInventory.put("iid", inventory.getId());
SplitInventory splitInventoryForSplitInfo = splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
// 获取当前拆单记录与实际出库拆单之间的进制
int parentScale = splitUnitService.findSplitInfoScaleQuantity(realSplitInfo, splitInfo.getId());
if (splitInventoryForSplitInfo.getSaveQuantity() * parentScale >= nowQuantity) {
// 如果当前库存拆单记录的库存可以满足需要
// 设置当前拆单库存
splitInventoryForSplitInfo.setSaveQuantity(splitInventoryForSplitInfo.getSaveQuantity() - nowQuantity);
// 设置当前拆单出库库存数
splitInventoryForSplitInfo.setOutQuantity(splitInventoryForSplitInfo.getOutQuantity() + nowQuantity);
// 修改库存记录
splitUnitMapper.updateSplitInventory(splitInventoryForSplitInfo);
restResponse.setStatus(200);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库成功", "出库成功"));
} else {
// 如果当前库存拆单记录的库存不能满足需要
// 获取当前物料的库存
int allInventoryForSplitInfo = splitUnitService.findAllInventoryForSplitInfo(-1, inventory, realSplitInfo.getId(), 0, true);
// 判断当前所有库存是否可以满足需求
if (allInventoryForSplitInfo >= nowQuantity) {
// 如果当前库存数可以满足需求
if (splitInfo.getParentId() != null) {
// 如果还有父级
// 获取当前拆单的父级
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
paramForSplitInventory.put("sid", parentSplitInfo.getId());
SplitInventory parentSplitInventoryForSplitInfo = splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
// 定义当前库存下的库存数量
// 获取当前物料的进制
int scaleQuantity = splitUnitService.findSplitInfoScaleQuantity(parentSplitInfo, realSplitInfo.getId());
int realQuantity = scaleQuantity * parentSplitInventoryForSplitInfo.getSaveQuantity();
if (realQuantity >= nowQuantity) {
// 如果当前库存容量可以满足需求
if (nowQuantity >= splitInfo.getQuantity()) {
//如果当前要出库的数量大于等于进制数量
// 当前库存不足的数量
Double remainderQuantity = (double) (nowQuantity - splitInventoryForSplitInfo.getSaveQuantity());
// 获取需要借位的数量
int scale = (int) Math.ceil(remainderQuantity / splitInfo.getQuantity());
// 令父级拆单库存减少对应数量
parentSplitInventoryForSplitInfo.setSaveQuantity(parentSplitInventoryForSplitInfo.getSaveQuantity() - scale);
// 令父级拆单出库量添加对应数量
parentSplitInventoryForSplitInfo.setOutQuantity(parentSplitInventoryForSplitInfo.getOutQuantity() + scale);
// 修改父级拆单库存处理记录
splitUnitMapper.updateSplitInventory(parentSplitInventoryForSplitInfo);
// 设置当前库存数量
splitInventoryForSplitInfo.setSaveQuantity(scale * splitInfo.getQuantity() - splitInventoryForSplitInfo.getSaveQuantity());
// 设置当前出库数量
splitInventoryForSplitInfo.setOutQuantity(splitInventoryForSplitInfo.getOutQuantity() + ((quantity - splitInventoryForSplitInfo.getSaveQuantity())));
// 修改当前拆单库存处理记录
} else {
// 如果当前出库数量不大于进制数量
// 令父级拆单库存减一
parentSplitInventoryForSplitInfo.setSaveQuantity(parentSplitInventoryForSplitInfo.getSaveQuantity() - 1);
// 令父级拆单出库量加一
parentSplitInventoryForSplitInfo.setOutQuantity(parentSplitInventoryForSplitInfo.getOutQuantity() + 1);
// 修改父级拆单库存处理记录
splitUnitMapper.updateSplitInventory(parentSplitInventoryForSplitInfo);
// 设置当前拆单库存数
splitInventoryForSplitInfo.setSaveQuantity(splitInventoryForSplitInfo.getSaveQuantity() - quantity);
// 设置当前拆单库存出库数
splitInventoryForSplitInfo.setOutQuantity(splitInventoryForSplitInfo.getOutQuantity() + quantity);
// 修改当前拆单库存处理记录
}
splitUnitMapper.updateSplitInventory(splitInventoryForSplitInfo);
restResponse.setStatus(200);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库成功", "出库成功"));
} else {
// 如果当前当前父级与其的库存数量不大于要出库的数量
if (parentSplitInfo.getParentId() != null) {
nowQuantity = nowQuantity - realQuantity;
parentSplitInventoryForSplitInfo.setOutQuantity(parentSplitInventoryForSplitInfo.getOutQuantity() + parentSplitInventoryForSplitInfo.getSaveQuantity());
parentSplitInventoryForSplitInfo.setSaveQuantity(0);
splitUnitMapper.updateSplitInventory(parentSplitInventoryForSplitInfo);
// 如果当前还有父级
// 进行递归
RestResponse restResponse1 = updateSplitInventoryInfo(parentSplitInfo, inventory, quantity, nowQuantity, splitInfo);
// 获取其父级减少与实际出库的拆单记录的数量
Object data = restResponse1.getData();
// 计算当前与最底层的进制
double splitInfoScaleQuantity = 1;
if (Integer.compare(realSplitInfo.getId(), parentSplitInfo.getId()) != 0) {
// 如果当前不是最底层
splitInfoScaleQuantity *= splitUnitService.findSplitInfoScaleQuantity(realSplitInfo, parentSplitInfo.getId());
}
if (data != null && !"".equals(data.toString())) {
int surplus_redundant = ObjectFormatUtil.toInteger(data) * (int) splitInfoScaleQuantity;
if (surplus_redundant - nowQuantity == 0) {
// 如果刚好完成出库且没有剩余
restResponse.setStatus(200);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库成功", "出库成功"));
} else {
// 如果还有剩余
// 计算当前针对于最底层的数量
surplus_redundant = surplus_redundant - nowQuantity;
// 获取当前拆单单位要添加的库存
double floor = Math.floor(surplus_redundant / splitInfoScaleQuantity);
parentSplitInventoryForSplitInfo.setSaveQuantity((int) floor);
if (surplus_redundant - floor == 0) {
restResponse.setStatus(200);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库成功", "出库成功"));
} else {
restResponse.setData(surplus_redundant - floor);
}
}
} else {
return restResponse1;
}
} else {
// 如果没有父级
// 获取当前拆单单位与顶级之间的进制
int splitInfoQuantity = splitUnitService.findSplitInfoScaleQuantity(splitInfo, -1);
// 获取所有当前单位的拆单库存
realQuantity = inventory.getQuantity() * splitInfoQuantity + realQuantity;
if (realQuantity > nowQuantity) {
// 如果可以出库
// 计算当前不足的数量
int residue_quantity = realQuantity - nowQuantity;
// 计算库存需要减少的数量
int inventory_quantity = (int) Math.ceil((double) residue_quantity / splitInfoQuantity);
// 重新设置库存数量
inventory.setQuantity(inventory.getQuantity() - inventory_quantity);
// 重新设置总额
inventory.setAmounts(inventory.getAmounts() - inventory_quantity * inventory.getPrice());
if (Integer.compare(inventory.getQuantity(), 0) != 0) {
// 重新计算单价
BigDecimal decimal = BigDecimal.valueOf(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
}
// 修改库存数量
materialMapper.updateInventory(inventory);
} else {
// 如果不可出库
restResponse.setStatus(508);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "出库失败,库存不足"));
}
}
}
} else {
// 如果已经没有父级
// 获取顶级与当前单位之间的进制
double splitInfoScaleQuantityForAll = splitUnitService.findSplitInfoScaleQuantity(splitInfo, -1);
// 计算库存减少的数目
int surplus_redundant = (int) Math.ceil(nowQuantity / splitInfoScaleQuantityForAll);
// 设置当前库存数量
inventory.setQuantity(inventory.getQuantity() - surplus_redundant);
// 设置当前总额
inventory.setAmounts(inventory.getPrice() * inventory.getQuantity());
if(Integer.compare(inventory.getQuantity(),0) != 0) {
// 设置当前单价
BigDecimal decimal = BigDecimal.valueOf(inventory.getAmounts() / inventory.getQuantity());
Double price = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
inventory.setPrice(price);
}
// 修改库存数量
materialMapper.updateInventory(inventory);
restResponse.setData(surplus_redundant);
}
} else {
// 如果不能满足需求
restResponse.setStatus(508);
restResponse.setData("");
restResponse.setStatusInfo(new StatusInfo("出库失败", "出库失败,库存不足"));
}
}
return restResponse;
}
/**

229
src/main/java/com/dreamchaser/depository_manage/service/impl/SplitUnitServiceImpl.java

@ -147,7 +147,7 @@ public class SplitUnitServiceImpl implements SplitUnitService {
// 如果当前入库的数量大于拆单规定的进制数量
// 计算要进的数目
int quantity_in = (realQuantity / scale) ;
int quantity_in = (realQuantity / scale);
// 更新处理数量
disposeQuantity += quantity_in;
// 拆单库存实际入库的数量
@ -176,17 +176,16 @@ public class SplitUnitServiceImpl implements SplitUnitService {
map.put("realQuantity", quantity.toString());
if (splitInfoForUnit.getParentId() != null) {
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(splitInfoForUnit.getParentId());
updateSplitInfoSaveQuantity(parentSplitInfo, disposeQuantity, inventory.getId(), map,quantity.intValue());
updateSplitInfoSaveQuantity(parentSplitInfo, disposeQuantity, inventory.getId(), map, quantity.intValue());
} else {
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfoForUnit);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfoForUnit, -1);
map.put("price", price.toString());
depositoryRecordService.applicationInPlace(map);
}
}
else {
} else {
// 如果没有记录
// 获取当前拆单记录的父级
@ -211,18 +210,18 @@ public class SplitUnitServiceImpl implements SplitUnitService {
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(parentId);
if (inventory != null) {
updateSplitInfoSaveQuantity(parentSplitInfo, ObjectFormatUtil.toInteger(String.valueOf(saveQuantity)), inventory.getId(), map,quantity.intValue());
updateSplitInfoSaveQuantity(parentSplitInfo, ObjectFormatUtil.toInteger(String.valueOf(saveQuantity)), inventory.getId(), map, quantity.intValue());
} else {
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfoForUnit);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfoForUnit, -1);
map.put("price", price.toString());
result += depositoryRecordService.applicationInPlace(map);
updateSplitInfoSaveQuantity(parentSplitInfo, ObjectFormatUtil.toInteger(String.valueOf(saveQuantity)), ObjectFormatUtil.toInteger(map.get("newInMid")), map,quantity.intValue());
updateSplitInfoSaveQuantity(parentSplitInfo, ObjectFormatUtil.toInteger(String.valueOf(saveQuantity)), ObjectFormatUtil.toInteger(map.get("newInMid")), map, quantity.intValue());
}
} else {
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfoForUnit);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfoForUnit, -1);
map.put("price", price.toString());
result += depositoryRecordService.applicationInPlace(map);
}
} else {
@ -240,31 +239,30 @@ public class SplitUnitServiceImpl implements SplitUnitService {
// 插入一条库存记录
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfoForUnit);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfoForUnit, -1);
map.put("price", price.toString());
if(parentId != null){
if (parentId != null) {
// 如果有父级
// 先插入一条库存记录用于后续操作
map.put("quantity","0");
result += depositoryRecordService.applicationInPlace(map);
map.put("quantity", "0");
result += depositoryRecordService.applicationInPlace(map);
}else{
} else {
// 如果没有父级
result += depositoryRecordService.applicationInPlace(map);
result += depositoryRecordService.applicationInPlace(map);
}
paramForInsertSplitInventory.put("iid", map.get("newInMid"));
}
splitUnitMapper.addSplitInventory(paramForInsertSplitInventory);
if(parentId != null){
if (parentId != null) {
// 获取父级
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(splitInfoForUnit.getParentId());
updateSplitInfoSaveQuantity(parentSplitInfo,disposeQuantity,ObjectFormatUtil.toInteger(map.get("newInMid")),map,quantity.intValue());
updateSplitInfoSaveQuantity(parentSplitInfo, disposeQuantity, ObjectFormatUtil.toInteger(map.get("newInMid")), map, quantity.intValue());
}
}
@ -278,39 +276,171 @@ public class SplitUnitServiceImpl implements SplitUnitService {
}
/**
* 用于计算当前拆单单位与基础单位之间的进率
*
* 用于计算当前拆单单位与目标拆单单位之间的进率
* @param splitInfo 待查询拆单记录
* @param parentSplitInfoId 目标拆单单位
* @return
*/
public int findSplitInfoQuantity(SplitInfo splitInfo) {
int quantity = 1;
public int findSplitInfoScaleQuantity(SplitInfo splitInfo, Integer parentSplitInfoId) {
int quantity = 1; // 定义进制
quantity *= splitInfo.getQuantity();
if (splitInfo.getParentId() != null) {
SplitInfo splitInfoById = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
return quantity * findSplitInfoQuantity(splitInfoById);
} else {
return quantity;
if(parentSplitInfoId != null && Integer.compare(-1,parentSplitInfoId) != 0){
// 如果待查询的父级不是顶级
if(Integer.compare(splitInfo.getId(),parentSplitInfoId) == 0){
// 如果当前拆单记录是目标拆单单位
return quantity;
}else{
SplitInfo splitInfoById = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
return quantity * findSplitInfoScaleQuantity(splitInfoById,parentSplitInfoId);
}
}else {
if (splitInfo.getParentId() != null) {
SplitInfo splitInfoById = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
return quantity * findSplitInfoScaleQuantity(splitInfoById, parentSplitInfoId);
} else {
return quantity;
}
}
}
/**
* 获取当前拆单与基础拆单之间的库存总额(当前拆单记录为-1是全部库存是使用)
*
* @param splitInfoId 当前拆单记录Id
* @param inventory 库存记录
* @param baseSplitInfoId 基础拆单记录Id
* @param saveQuantity 最终返回值
* @param allQuantityFlag 用于标志是否获取全部库存
* @return
*/
public int findAllInventoryForSplitInfo(Integer splitInfoId, Inventory inventory, Integer baseSplitInfoId, int saveQuantity, boolean allQuantityFlag) {
if (allQuantityFlag) {
// 用于标志是否第一次计算
boolean flag = false;
// 获取当前基础单位
SplitInfo baseSplitInfo = splitUnitMapper.findSplitInfoById(baseSplitInfoId);
// 获取当前基础单位的库存
Map<String, Object> paramForSplitInventory = new HashMap<>();
paramForSplitInventory.put("iid", inventory.getId());
// 如果当前获取的为对应基础单位的所有库存
// 如果是第一次计算
if (Integer.compare(saveQuantity, 0) == 0) {
// 如果是第一次计算
paramForSplitInventory.put("sid", baseSplitInfoId);
// 当前基础单位的库存
SplitInventory baseSplitInventory = splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
if(baseSplitInventory != null) {
saveQuantity += baseSplitInventory.getSaveQuantity();
}
}
if(Integer.compare(-1,splitInfoId) != 0){
flag = true;
// 获取当前记录的子记录
SplitInfo splitInfoByParentId = splitUnitMapper.findSplitInfoByParentId(splitInfoId);
paramForSplitInventory.put("sid",splitInfoId);
SplitInventory splitInventoryByIidAndSid = splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
if(splitInventoryByIidAndSid != null){
saveQuantity += splitInventoryByIidAndSid.getSaveQuantity() * splitInfoByParentId.getQuantity();
}
}
if(baseSplitInfo.getParentId() != null && !flag){
// 如果有父级且是第一次
// 获取其父级
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(baseSplitInfo.getParentId());
return findAllInventoryForSplitInfo(parentSplitInfo.getId(),inventory,baseSplitInfoId,saveQuantity,allQuantityFlag);
}else{
if(flag){
// 如果不是第一次
// 获取当前的拆单记录
SplitInfo splitInfo = splitUnitMapper.findSplitInfoById(splitInfoId);
if(splitInfo.getParentId() != null){
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
return findAllInventoryForSplitInfo(parentSplitInfo.getId(),inventory,baseSplitInfoId,saveQuantity,allQuantityFlag);
}else{
int splitInfoScaleQuantity = findSplitInfoScaleQuantity(baseSplitInfo, -1);
return saveQuantity + inventory.getQuantity() * splitInfoScaleQuantity;
}
}else{
int splitInfoScaleQuantity = findSplitInfoScaleQuantity(baseSplitInfo, -1);
return saveQuantity + inventory.getQuantity() * splitInfoScaleQuantity;
}
}
}
else {
// 如果不是获取所有库存
// 获取当前基础单位
SplitInfo baseSplitInfo = splitUnitMapper.findSplitInfoById(baseSplitInfoId);
// 获取当前基础单位的库存
Map<String, Object> paramForSplitInventory = new HashMap<>();
paramForSplitInventory.put("iid", inventory.getId());
// 如果当前获取的为对应基础单位的所有库存
if (Integer.compare(saveQuantity, 0) == 0) {
// 如果是第一次计算
paramForSplitInventory.put("sid", baseSplitInfoId);
// 当前基础单位的库存
SplitInventory baseSplitInventory = splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
saveQuantity += baseSplitInventory.getSaveQuantity();
}
// 获取当前拆单记录与基础拆单记录之间的进制
int splitInfoScaleQuantity = findSplitInfoScaleQuantity(baseSplitInfo, splitInfoId);
paramForSplitInventory.put("sid", splitInfoId);
// 获取当前拆单记录的库存
SplitInventory splitInventory = splitUnitMapper.findSplitInventoryByIidAndSid(paramForSplitInventory);
saveQuantity += splitInventory.getSaveQuantity() * splitInfoScaleQuantity;
// 获取当前的子类
SplitInfo splitInfoByParentId = splitUnitMapper.findSplitInfoByParentId(splitInfoId);
if (splitInfoByParentId != null && Integer.compare(splitInfoByParentId.getId(),baseSplitInfoId) != 0) {
// 如果有子类
return findAllInventoryForSplitInfo(splitInfoByParentId.getId(), inventory, baseSplitInfoId, saveQuantity, allQuantityFlag);
} else {
// 如果没有
return saveQuantity;
}
}
}
/**
* 根据库存id与拆单记录id获取对应拆单库存处理记录
*
* @param Iid 待查询库存id
* @param sid 待查询拆单id
* @return
*/
@Override
public SplitInventory findSplitInventoryByIidAndSid(Integer Iid, Integer sid) {
Map<String,Object> param = new HashMap<>();
param.put("iid",Iid);
param.put("sid",sid);
Map<String, Object> param = new HashMap<>();
param.put("iid", Iid);
param.put("sid", sid);
return splitUnitMapper.findSplitInventoryByIidAndSid(param);
}
/**
* 根据父级查询拆单记录
*
* @param parentId 待查询父级
* @return
*/
@ -321,12 +451,13 @@ public class SplitUnitServiceImpl implements SplitUnitService {
/**
* 用于拆单库存处理记录的修改
* @param splitInfo 对应拆单记录
* @param quantity 修改数目
* @param map 入库条件
*
* @param splitInfo 对应拆单记录
* @param quantity 修改数目
* @param map 入库条件
* @param inQuantity 最终入库数量
*/
private void updateSplitInfoSaveQuantity(SplitInfo splitInfo, int quantity, int iid, Map<String, Object> map,int inQuantity) {
private void updateSplitInfoSaveQuantity(SplitInfo splitInfo, int quantity, int iid, Map<String, Object> map, int inQuantity) {
int result = 0;
@ -374,33 +505,31 @@ public class SplitUnitServiceImpl implements SplitUnitService {
// 获取具体父级
SplitInfo splitInfoForParent = splitUnitMapper.findSplitInfoById(parentId);
updateSplitInfoSaveQuantity(splitInfoForParent, disposeQuantity, splitInventory.getIid(), map,inQuantity);
updateSplitInfoSaveQuantity(splitInfoForParent, disposeQuantity, splitInventory.getIid(), map, inQuantity);
} else {
// 如果为空
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfo);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfo, -1);
map.put("price", price.toString());
map.put("quantity", String.valueOf(disposeQuantity));
map.put("realQuantity", String.valueOf(inQuantity));
depositoryRecordService.applicationInPlace(map);
}
}
else {
} else {
// 当前库存拆单记录与新入库的数目不大于预设的进制
splitInventory.setSaveQuantity(splitInventory.getSaveQuantity() + disposeQuantity);
splitInventory.setInQuantity(splitInventory.getInQuantity() + disposeQuantity);
splitUnitMapper.updateSplitInventory(splitInventory);
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfo);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfo, -1);
map.put("price", price.toString());
map.put("quantity", "0");
map.put("realQuantity", String.valueOf(inQuantity));
depositoryRecordService.applicationInPlace(map);
}
}
else {
} else {
// 如果为空
Map<String, Object> paramForInsertSplitInventory = new HashMap<>();
@ -425,11 +554,11 @@ public class SplitUnitServiceImpl implements SplitUnitService {
// 获取当前拆单记录的父级
SplitInfo parentSplitInfo = splitUnitMapper.findSplitInfoById(parentId);
updateSplitInfoSaveQuantity(parentSplitInfo, quantity_scale, iid, map,inQuantity);
updateSplitInfoSaveQuantity(parentSplitInfo, quantity_scale, iid, map, inQuantity);
} else {
Double price = Double.parseDouble((String) map.get("price"));
price = price * findSplitInfoQuantity(splitInfo);
map.put("price",price.toString());
price = price * findSplitInfoScaleQuantity(splitInfo, -1);
map.put("price", price.toString());
map.put("quantity", String.valueOf(quantity_scale));
map.put("realQuantity", String.valueOf(inQuantity));
depositoryRecordService.applicationInPlace(map);

56
src/test/java/com/dreamchaser/depository_manage/TestForOther.java

@ -1,23 +1,20 @@
package com.dreamchaser.depository_manage;
import com.dreamchaser.depository_manage.entity.Inventory;
import com.dreamchaser.depository_manage.entity.SplitInfo;
import com.dreamchaser.depository_manage.entity.SplitInventory;
import com.dreamchaser.depository_manage.mapper.MaterialMapper;
import com.dreamchaser.depository_manage.mapper.SplitUnitMapper;
import com.dreamchaser.depository_manage.pojo.InventoryP;
import com.dreamchaser.depository_manage.service.SplitUnitService;
import com.dreamchaser.depository_manage.utils.CreateQrCodeUtil;
import com.dreamchaser.depository_manage.utils.ObjectFormatUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import sun.misc.BASE64Decoder;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
@ -28,13 +25,48 @@ import java.io.OutputStream;
public class TestForOther {
@Test
public void Test() throws IOException {
Double a = 24.0;
System.out.println(a.intValue());
}
@Autowired
SplitUnitService splitUnitService;
@Autowired
SplitUnitMapper splitUnitMapper;
@Autowired
MaterialMapper materialMapper;
@Test
public void Test() throws IOException {
SplitInfo splitInfoById = splitUnitMapper.findSplitInfoById(7);
double splitInfoScaleQuantity = findSplitInfoScaleQuantity(splitInfoById, 7);
System.out.println(splitInfoScaleQuantity);
}
/**
* 用于计算当前拆单单位与目标拆单单位之间的进率
* @param splitInfo 待查询拆单记录
* @param parentSplitInfoId 目标拆单单位
* @return
*/
public int findSplitInfoScaleQuantity(SplitInfo splitInfo, Integer parentSplitInfoId) {
int quantity = 1; // 定义进制
quantity *= splitInfo.getQuantity();
if(parentSplitInfoId != null && Integer.compare(-1,parentSplitInfoId) != 0){
// 如果待查询的父级不是顶级
if(Integer.compare(splitInfo.getId(),parentSplitInfoId) == 0){
// 如果当前拆单记录是目标拆单单位
return quantity;
}else{
SplitInfo splitInfoById = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
return quantity * findSplitInfoScaleQuantity(splitInfoById,parentSplitInfoId);
}
}else {
if (splitInfo.getParentId() != null) {
SplitInfo splitInfoById = splitUnitMapper.findSplitInfoById(splitInfo.getParentId());
return quantity * findSplitInfoScaleQuantity(splitInfoById, parentSplitInfoId);
} else {
return quantity;
}
}
}
}

6
target/classes/com/dreamchaser/depository_manage/mapper/SplitUnitMapper.xml

@ -151,13 +151,13 @@
<if test="sid != null and sid != ''">
sid = #{sid},
</if>
<if test="outQuantity != null and outQuantity != ''">
<if test="outQuantity != null">
outQuantity = #{outQuantity},
</if>
<if test="inQuantity != null and inQuantity != ''">
<if test="inQuantity != null">
inQuantity = #{inQuantity},
</if>
<if test="saveQuantity != null and saveQuantity != ''">
<if test="saveQuantity != null">
saveQuantity = #{saveQuantity},
</if>
</set>

Loading…
Cancel
Save