|
|
|
|
@ -1,5 +1,4 @@ |
|
|
|
|
package com.nov.KgLowDurable.service.Impl; |
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import com.github.pagehelper.PageHelper; |
|
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
|
@ -20,7 +19,6 @@ import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
import java.math.RoundingMode; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
@ -49,6 +47,9 @@ public class LdTwoPutStorageServiceImpl extends ServiceImpl<LdTwoPutStorageMappe |
|
|
|
|
@Autowired |
|
|
|
|
ILdDurableFormService durableFormService; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
ILdTwoInventoryRecordService twoInventoryRecordService; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public PageInfo<LdTwoPutStorageVO> getTwoPutStorageList(Date startTime, Date endTime, String inOperator, String demandDepartment, Integer pageNum, Integer pageSize) { |
|
|
|
|
|
|
|
|
|
@ -66,126 +67,374 @@ public class LdTwoPutStorageServiceImpl extends ServiceImpl<LdTwoPutStorageMappe |
|
|
|
|
return new LdTwoPutStorageInfoVO().setLdTwoPutStorage(ldTwoPutStorage).setLdTwoPutStorageDetails(ldTwoPutStorageDetailList); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
// @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
// public boolean submit(LdTwoPutStorageDto dto) {
|
|
|
|
|
// LdTwoPutStorage ldTwoPutStorage = twoPutStorageMapper.selectById(dto.getLdTwoPutStorage().getId());
|
|
|
|
|
// if(null == ldTwoPutStorage){
|
|
|
|
|
// throw new CustomerException("数据为空");
|
|
|
|
|
// }
|
|
|
|
|
// ldTwoPutStorage.setStatus(BatchConstant.ABOVE_WAREHOUSE);
|
|
|
|
|
// ldTwoPutStorage.setInOperatorId(dto.getUserInfoVO().getUserId());
|
|
|
|
|
// ldTwoPutStorage.setInOperatorName(dto.getUserInfoVO().getName());
|
|
|
|
|
// ldTwoPutStorage.setInDate(new Date());
|
|
|
|
|
// int updateReturt = twoPutStorageMapper.updateById(ldTwoPutStorage);
|
|
|
|
|
// if(updateReturt<=0){
|
|
|
|
|
// throw new CustomerException("提交失败");
|
|
|
|
|
// }
|
|
|
|
|
// //明细入库 加库存 做记录
|
|
|
|
|
// List<LdTwoPutStorageDetail> ldTwoPutStorageDetailList = twoPutStorageDetailService.getByTwoPutStorageId(dto.getLdTwoPutStorage().getId());
|
|
|
|
|
// for (LdTwoPutStorageDetail ldTwoPutStorageDetail : ldTwoPutStorageDetailList) {
|
|
|
|
|
// ldTwoPutStorageDetail.setInboundStatus(BatchConstant.ABOVE_WAREHOUSE).setInboundPerson(dto.getUserInfoVO().getName()).setInboundTime(new Date());
|
|
|
|
|
// boolean update = twoPutStorageDetailService.updateById(ldTwoPutStorageDetail);
|
|
|
|
|
// if(!update){
|
|
|
|
|
// throw new CustomerException("提交失败");
|
|
|
|
|
// }
|
|
|
|
|
// if(BatchConstant.CONSUMER.equals(ldTwoPutStorageDetail.getType())){
|
|
|
|
|
// // 查询易耗库存
|
|
|
|
|
// LdConsumerForm ldConsumerForm = consumerFormService.getByMaterialId(ldTwoPutStorageDetail.getMaterialId());
|
|
|
|
|
// if(null == ldConsumerForm){
|
|
|
|
|
// ldConsumerForm = new LdConsumerForm()
|
|
|
|
|
// .setMaterialId(ldTwoPutStorageDetail.getMaterialId())
|
|
|
|
|
// .setMaterialName(ldTwoPutStorageDetail.getMaterialName())
|
|
|
|
|
// .setMaterialCode(ldTwoPutStorageDetail.getMaterialCode())
|
|
|
|
|
// .setOnePutMaterialCode(ldTwoPutStorageDetail.getOnePutMaterialCode())
|
|
|
|
|
// .setModel(ldTwoPutStorageDetail.getModel())
|
|
|
|
|
// .setType(ldTwoPutStorageDetail.getType())
|
|
|
|
|
// .setUnit(ldTwoPutStorageDetail.getUnit())
|
|
|
|
|
// .setNum(ldTwoPutStorageDetail.getInboundQuantity())
|
|
|
|
|
// .setUnitPrice(ldTwoPutStorageDetail.getUnitPrice())
|
|
|
|
|
// .setAmount(ldTwoPutStorageDetail.getInboundQuantity()
|
|
|
|
|
// .multiply(ldTwoPutStorageDetail.getUnitPrice()))
|
|
|
|
|
// .setDepartmentId(ldTwoPutStorageDetail.getDemandDepartment())
|
|
|
|
|
// .setDepartment(ldTwoPutStorageDetail.getDemandDepartmentName());
|
|
|
|
|
// consumerFormService.save(ldConsumerForm);
|
|
|
|
|
// }else {
|
|
|
|
|
// // 数量 = 原数量 + 入库数量
|
|
|
|
|
// BigDecimal originalNum = ldConsumerForm.getNum() != null ? ldConsumerForm.getNum() : BigDecimal.ZERO;
|
|
|
|
|
// BigDecimal inboundQuantity = ldTwoPutStorageDetail.getInboundQuantity() != null ? ldTwoPutStorageDetail.getInboundQuantity() : BigDecimal.ZERO;
|
|
|
|
|
// BigDecimal newNum = originalNum.add(inboundQuantity);
|
|
|
|
|
// ldConsumerForm.setNum(newNum);
|
|
|
|
|
//
|
|
|
|
|
// // 金额 = 原金额 + 入库金额
|
|
|
|
|
// BigDecimal originalAmount = ldConsumerForm.getAmount() != null ? ldConsumerForm.getAmount() : BigDecimal.ZERO;
|
|
|
|
|
// BigDecimal inboundAmount = ldTwoPutStorageDetail.getInboundQuantity().multiply(ldTwoPutStorageDetail.getUnitPrice());
|
|
|
|
|
// BigDecimal newAmount = originalAmount.add(inboundAmount);
|
|
|
|
|
// ldConsumerForm.setAmount(newAmount);
|
|
|
|
|
//
|
|
|
|
|
// // 单价 = 新金额 / 新数量
|
|
|
|
|
// BigDecimal newUnitPrice = BigDecimal.ZERO;
|
|
|
|
|
// if (newNum.compareTo(BigDecimal.ZERO) != 0) {
|
|
|
|
|
// // 保留6位小数,四舍五入
|
|
|
|
|
// newUnitPrice = newAmount.divide(newNum, 4, RoundingMode.HALF_UP);
|
|
|
|
|
// }
|
|
|
|
|
// ldConsumerForm.setUnitPrice(newUnitPrice);
|
|
|
|
|
// consumerFormService.updateById(ldConsumerForm);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// LdTwoInventoryRecord ldTwoInventoryRecord = new LdTwoInventoryRecord();
|
|
|
|
|
// ldTwoInventoryRecord.setConsumerFormId(ldConsumerForm.getId().longValue());
|
|
|
|
|
// ldTwoInventoryRecord.setTransactionType(BatchConstant.PUT_WAREHOUSE);
|
|
|
|
|
// ldTwoInventoryRecord.setQuantity(ldTwoPutStorageDetail.getInboundQuantity());
|
|
|
|
|
// ldTwoInventoryRecord.setMoney(ldTwoPutStorageDetail.getUnitPrice());
|
|
|
|
|
// ldTwoInventoryRecord.setOperatorId(dto.getUserInfoVO().getUserId());
|
|
|
|
|
// ldTwoInventoryRecord.setOperatorName(dto.getUserInfoVO().getName());
|
|
|
|
|
// ldTwoInventoryRecord.setOperationTime(new Date());
|
|
|
|
|
// ldTwoInventoryRecord.setDepartmentId(dto.getUserInfoVO().getMainErDepartment());
|
|
|
|
|
// ldTwoInventoryRecord.setDepartmentName(dto.getUserInfoVO().getMainErDepartmentName());
|
|
|
|
|
// boolean saveTwoInventoryRecord = twoInventoryRecordService.save(ldTwoInventoryRecord);
|
|
|
|
|
// if(!saveTwoInventoryRecord){
|
|
|
|
|
// throw new IllegalArgumentException("入库记录保存失败");
|
|
|
|
|
// }
|
|
|
|
|
// }else {
|
|
|
|
|
// // 耐用品 获取入库数量
|
|
|
|
|
// BigDecimal inboundQuantity = ldTwoPutStorageDetail.getInboundQuantity();
|
|
|
|
|
// // 验证数量
|
|
|
|
|
// if (inboundQuantity == null) {
|
|
|
|
|
// throw new IllegalArgumentException("入库数量不能为空");
|
|
|
|
|
// }
|
|
|
|
|
// // 验证是否为整数
|
|
|
|
|
// if (!isInteger(inboundQuantity)) {
|
|
|
|
|
// throw new IllegalArgumentException("耐用品入库数量必须是整数");
|
|
|
|
|
// }
|
|
|
|
|
// // 验证是否为正数
|
|
|
|
|
// if (inboundQuantity.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
|
|
// throw new IllegalArgumentException("入库数量必须大于0");
|
|
|
|
|
// }
|
|
|
|
|
// // 转换为整数
|
|
|
|
|
// int count = inboundQuantity.intValue();
|
|
|
|
|
//
|
|
|
|
|
// System.out.println("开始处理 " + count + " 个耐用品");
|
|
|
|
|
//
|
|
|
|
|
// List<LdDurableForm> durableList = new ArrayList<>();
|
|
|
|
|
// // 循环处理每个物品
|
|
|
|
|
// for (int i = 0; i < count; i++) {
|
|
|
|
|
// LdDurableForm ldDurableForm = new LdDurableForm()
|
|
|
|
|
// .setMaterialId(ldTwoPutStorageDetail.getMaterialId())
|
|
|
|
|
// .setMaterialName(ldTwoPutStorageDetail.getMaterialName())
|
|
|
|
|
// .setMaterialCode(ldTwoPutStorageDetail.getMaterialCode())
|
|
|
|
|
// .setOnePutMaterialCode(ldTwoPutStorageDetail.getOnePutMaterialCode())
|
|
|
|
|
// .setModel(ldTwoPutStorageDetail.getModel())
|
|
|
|
|
// .setType(ldTwoPutStorageDetail.getType())
|
|
|
|
|
// .setUnit(ldTwoPutStorageDetail.getUnit())
|
|
|
|
|
// .setNum(new BigDecimal(1))
|
|
|
|
|
// .setUnitPrice(ldTwoPutStorageDetail.getUnitPrice())
|
|
|
|
|
// .setAmount(ldTwoPutStorageDetail.getUnitPrice())
|
|
|
|
|
// .setDepartmentId(ldTwoPutStorageDetail.getDemandDepartment())
|
|
|
|
|
// .setDepartment(ldTwoPutStorageDetail.getDemandDepartmentName());
|
|
|
|
|
// durableList.add(ldDurableForm);
|
|
|
|
|
// }
|
|
|
|
|
// durableFormService.saveBatch(durableList);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// /**
|
|
|
|
|
// * 判断BigDecimal是否为整数
|
|
|
|
|
// */
|
|
|
|
|
// private boolean isInteger(BigDecimal value) {
|
|
|
|
|
// try {
|
|
|
|
|
// value.toBigIntegerExact();
|
|
|
|
|
// return true;
|
|
|
|
|
// } catch (ArithmeticException e) {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public boolean submit(LdTwoPutStorageDto dto) { |
|
|
|
|
// 参数校验
|
|
|
|
|
if (dto == null || dto.getLdTwoPutStorage() == null || dto.getUserInfoVO() == null) { |
|
|
|
|
throw new CustomerException("参数不能为空"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 1. 更新主表状态
|
|
|
|
|
LdTwoPutStorage ldTwoPutStorage = twoPutStorageMapper.selectById(dto.getLdTwoPutStorage().getId()); |
|
|
|
|
if(null == ldTwoPutStorage){ |
|
|
|
|
throw new CustomerException("数据为空"); |
|
|
|
|
} |
|
|
|
|
ldTwoPutStorage.setStatus(BatchConstant.ABOVE_WAREHOUSE); |
|
|
|
|
ldTwoPutStorage.setInOperatorId(dto.getUserInfoVO().getUserId()); |
|
|
|
|
ldTwoPutStorage.setInOperatorName(dto.getUserInfoVO().getName()); |
|
|
|
|
ldTwoPutStorage.setInDate(new Date()); |
|
|
|
|
int updateReturt = twoPutStorageMapper.updateById(ldTwoPutStorage); |
|
|
|
|
if(updateReturt<=0){ |
|
|
|
|
throw new CustomerException("提交失败"); |
|
|
|
|
} |
|
|
|
|
//明细入库 加库存
|
|
|
|
|
List<LdTwoPutStorageDetail> ldTwoPutStorageDetailList = twoPutStorageDetailService.getByTwoPutStorageId(dto.getLdTwoPutStorage().getId()); |
|
|
|
|
for (LdTwoPutStorageDetail ldTwoPutStorageDetail : ldTwoPutStorageDetailList) { |
|
|
|
|
ldTwoPutStorageDetail.setInboundStatus(BatchConstant.ABOVE_WAREHOUSE).setInboundPerson(dto.getUserInfoVO().getName()).setInboundTime(new Date()); |
|
|
|
|
boolean update = twoPutStorageDetailService.updateById(ldTwoPutStorageDetail); |
|
|
|
|
if(!update){ |
|
|
|
|
throw new CustomerException("提交失败"); |
|
|
|
|
if (null == ldTwoPutStorage) { |
|
|
|
|
throw new CustomerException("入库单不存在"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 状态检查(防止重复提交)
|
|
|
|
|
if (BatchConstant.ABOVE_WAREHOUSE.equals(ldTwoPutStorage.getStatus())) { |
|
|
|
|
throw new CustomerException("该入库单已提交"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
UserInfoVO userInfo = dto.getUserInfoVO(); |
|
|
|
|
Date now = new Date(); |
|
|
|
|
|
|
|
|
|
ldTwoPutStorage.setStatus(BatchConstant.ABOVE_WAREHOUSE) |
|
|
|
|
.setInOperatorId(userInfo.getUserId()) |
|
|
|
|
.setInOperatorName(userInfo.getName()) |
|
|
|
|
.setInDate(now); |
|
|
|
|
|
|
|
|
|
int updateResult = twoPutStorageMapper.updateById(ldTwoPutStorage); |
|
|
|
|
if (updateResult <= 0) { |
|
|
|
|
throw new CustomerException("入库单提交失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 2. 批量处理明细
|
|
|
|
|
List<LdTwoPutStorageDetail> details = twoPutStorageDetailService.getByTwoPutStorageId(dto.getLdTwoPutStorage().getId()); |
|
|
|
|
if (CollectionUtils.isEmpty(details)) { |
|
|
|
|
throw new CustomerException("入库明细为空"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 批量更新明细状态
|
|
|
|
|
List<LdTwoPutStorageDetail> updatedDetails = new ArrayList<>(); |
|
|
|
|
for (LdTwoPutStorageDetail detail : details) { |
|
|
|
|
detail.setInboundStatus(BatchConstant.ABOVE_WAREHOUSE) |
|
|
|
|
.setInboundPerson(userInfo.getName()) |
|
|
|
|
.setInboundTime(now); |
|
|
|
|
updatedDetails.add(detail); |
|
|
|
|
} |
|
|
|
|
boolean updateBatchResult = twoPutStorageDetailService.updateBatchById(updatedDetails); |
|
|
|
|
if (!updateBatchResult) { |
|
|
|
|
throw new CustomerException("明细更新失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3. 按类型分别处理
|
|
|
|
|
processConsumerGoods(details, userInfo); // 易耗品
|
|
|
|
|
processDurableGoods(details, userInfo); // 耐用品
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 处理易耗品 |
|
|
|
|
*/ |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
private void processConsumerGoods(List<LdTwoPutStorageDetail> details, UserInfoVO userInfo) { |
|
|
|
|
Date now = new Date(); |
|
|
|
|
|
|
|
|
|
for (LdTwoPutStorageDetail detail : details) { |
|
|
|
|
if (!BatchConstant.CONSUMER.equals(detail.getType())) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(BatchConstant.CONSUMER.equals(ldTwoPutStorageDetail.getType())){ |
|
|
|
|
// 查询易耗库存
|
|
|
|
|
LdConsumerForm ldConsumerForm = consumerFormService.getByMaterialId(ldTwoPutStorageDetail.getMaterialId()); |
|
|
|
|
if(null == ldConsumerForm){ |
|
|
|
|
ldConsumerForm = new LdConsumerForm() |
|
|
|
|
.setMaterialId(ldTwoPutStorageDetail.getMaterialId()) |
|
|
|
|
.setMaterialName(ldTwoPutStorageDetail.getMaterialName()) |
|
|
|
|
.setMaterialCode(ldTwoPutStorageDetail.getMaterialCode()) |
|
|
|
|
.setOnePutMaterialCode(ldTwoPutStorageDetail.getOnePutMaterialCode()) |
|
|
|
|
.setModel(ldTwoPutStorageDetail.getModel()) |
|
|
|
|
.setType(ldTwoPutStorageDetail.getType()) |
|
|
|
|
.setUnit(ldTwoPutStorageDetail.getUnit()) |
|
|
|
|
.setNum(ldTwoPutStorageDetail.getInboundQuantity()) |
|
|
|
|
.setUnitPrice(ldTwoPutStorageDetail.getUnitPrice()) |
|
|
|
|
.setAmount(ldTwoPutStorageDetail.getInboundQuantity() |
|
|
|
|
.multiply(ldTwoPutStorageDetail.getUnitPrice())) |
|
|
|
|
.setDepartmentId(ldTwoPutStorageDetail.getDemandDepartment()) |
|
|
|
|
.setDepartment(ldTwoPutStorageDetail.getDemandDepartmentName()); |
|
|
|
|
consumerFormService.save(ldConsumerForm); |
|
|
|
|
}else { |
|
|
|
|
// 数量 = 原数量 + 入库数量
|
|
|
|
|
BigDecimal originalNum = ldConsumerForm.getNum() != null ? ldConsumerForm.getNum() : BigDecimal.ZERO; |
|
|
|
|
BigDecimal inboundQuantity = ldTwoPutStorageDetail.getInboundQuantity() != null ? ldTwoPutStorageDetail.getInboundQuantity() : BigDecimal.ZERO; |
|
|
|
|
BigDecimal newNum = originalNum.add(inboundQuantity); |
|
|
|
|
ldConsumerForm.setNum(newNum); |
|
|
|
|
|
|
|
|
|
// 金额 = 原金额 + 入库金额
|
|
|
|
|
BigDecimal originalAmount = ldConsumerForm.getAmount() != null ? ldConsumerForm.getAmount() : BigDecimal.ZERO; |
|
|
|
|
BigDecimal inboundAmount = ldTwoPutStorageDetail.getInboundQuantity().multiply(ldTwoPutStorageDetail.getUnitPrice()); |
|
|
|
|
BigDecimal newAmount = originalAmount.add(inboundAmount); |
|
|
|
|
ldConsumerForm.setAmount(newAmount); |
|
|
|
|
|
|
|
|
|
// 单价 = 新金额 / 新数量
|
|
|
|
|
BigDecimal newUnitPrice = BigDecimal.ZERO; |
|
|
|
|
if (newNum.compareTo(BigDecimal.ZERO) != 0) { |
|
|
|
|
// 保留6位小数,四舍五入
|
|
|
|
|
newUnitPrice = newAmount.divide(newNum, 4, RoundingMode.HALF_UP); |
|
|
|
|
} |
|
|
|
|
ldConsumerForm.setUnitPrice(newUnitPrice); |
|
|
|
|
consumerFormService.updateById(ldConsumerForm); |
|
|
|
|
} |
|
|
|
|
}else { |
|
|
|
|
// 耐用品 获取入库数量
|
|
|
|
|
BigDecimal inboundQuantity = ldTwoPutStorageDetail.getInboundQuantity(); |
|
|
|
|
// 验证数量
|
|
|
|
|
if (inboundQuantity == null) { |
|
|
|
|
throw new IllegalArgumentException("入库数量不能为空"); |
|
|
|
|
} |
|
|
|
|
// 验证是否为整数
|
|
|
|
|
if (!isInteger(inboundQuantity)) { |
|
|
|
|
throw new IllegalArgumentException("耐用品入库数量必须是整数"); |
|
|
|
|
} |
|
|
|
|
// 验证是否为正数
|
|
|
|
|
if (inboundQuantity.compareTo(BigDecimal.ZERO) <= 0) { |
|
|
|
|
throw new IllegalArgumentException("入库数量必须大于0"); |
|
|
|
|
} |
|
|
|
|
// 转换为整数
|
|
|
|
|
int count = inboundQuantity.intValue(); |
|
|
|
|
|
|
|
|
|
System.out.println("开始处理 " + count + " 个耐用品"); |
|
|
|
|
|
|
|
|
|
List<LdDurableForm> durableList = new ArrayList<>(); |
|
|
|
|
// 循环处理每个物品
|
|
|
|
|
for (int i = 0; i < count; i++) { |
|
|
|
|
LdDurableForm ldDurableForm = new LdDurableForm() |
|
|
|
|
.setMaterialId(ldTwoPutStorageDetail.getMaterialId()) |
|
|
|
|
.setMaterialName(ldTwoPutStorageDetail.getMaterialName()) |
|
|
|
|
.setMaterialCode(ldTwoPutStorageDetail.getMaterialCode()) |
|
|
|
|
.setOnePutMaterialCode(ldTwoPutStorageDetail.getOnePutMaterialCode()) |
|
|
|
|
.setModel(ldTwoPutStorageDetail.getModel()) |
|
|
|
|
.setType(ldTwoPutStorageDetail.getType()) |
|
|
|
|
.setUnit(ldTwoPutStorageDetail.getUnit()) |
|
|
|
|
.setNum(new BigDecimal(1)) |
|
|
|
|
.setUnitPrice(ldTwoPutStorageDetail.getUnitPrice()) |
|
|
|
|
.setAmount(ldTwoPutStorageDetail.getInboundQuantity() |
|
|
|
|
.multiply(ldTwoPutStorageDetail.getUnitPrice())) |
|
|
|
|
.setDepartmentId(ldTwoPutStorageDetail.getDemandDepartment()) |
|
|
|
|
.setDepartment(ldTwoPutStorageDetail.getDemandDepartmentName()); |
|
|
|
|
durableList.add(ldDurableForm); |
|
|
|
|
} |
|
|
|
|
durableFormService.saveBatch(durableList); |
|
|
|
|
// 参数校验
|
|
|
|
|
validateDetailForConsumer(detail); |
|
|
|
|
|
|
|
|
|
// 处理库存
|
|
|
|
|
LdConsumerForm inventory = consumerFormService.getByMaterialId(detail.getMaterialId()); |
|
|
|
|
|
|
|
|
|
if (inventory == null) { |
|
|
|
|
// 新增库存
|
|
|
|
|
inventory = buildNewConsumerInventory(detail); |
|
|
|
|
consumerFormService.save(inventory); |
|
|
|
|
} else { |
|
|
|
|
// 更新库存
|
|
|
|
|
updateConsumerInventory(inventory, detail); |
|
|
|
|
consumerFormService.updateById(inventory); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 保存入库记录
|
|
|
|
|
saveInventoryRecord(inventory, detail, userInfo, now, BatchConstant.PUT_WAREHOUSE); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
/** |
|
|
|
|
* 处理耐用品 |
|
|
|
|
*/ |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
private void processDurableGoods(List<LdTwoPutStorageDetail> details, UserInfoVO userInfo) { |
|
|
|
|
List<LdDurableForm> durableList = new ArrayList<>(); |
|
|
|
|
Date now = new Date(); |
|
|
|
|
|
|
|
|
|
for (LdTwoPutStorageDetail detail : details) { |
|
|
|
|
if (!BatchConstant.DURABLE.equals(detail.getType())) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 参数校验
|
|
|
|
|
validateDetailForDurable(detail); |
|
|
|
|
|
|
|
|
|
BigDecimal inboundQuantity = detail.getInboundQuantity(); |
|
|
|
|
int count = inboundQuantity.intValue(); |
|
|
|
|
|
|
|
|
|
// 批量创建耐用品记录
|
|
|
|
|
for (int i = 0; i < count; i++) { |
|
|
|
|
LdDurableForm durable = new LdDurableForm() |
|
|
|
|
.setMaterialId(detail.getMaterialId()) |
|
|
|
|
.setMaterialName(detail.getMaterialName()) |
|
|
|
|
.setMaterialCode(detail.getMaterialCode()) |
|
|
|
|
.setOnePutMaterialCode(detail.getOnePutMaterialCode()) |
|
|
|
|
.setModel(detail.getModel()) |
|
|
|
|
.setType(detail.getType()) |
|
|
|
|
.setUnit(detail.getUnit()) |
|
|
|
|
.setNum(BigDecimal.ONE) |
|
|
|
|
.setUnitPrice(detail.getUnitPrice()) |
|
|
|
|
.setAmount(detail.getUnitPrice()) |
|
|
|
|
.setDepartmentId(detail.getDemandDepartment()) |
|
|
|
|
.setDepartment(detail.getDemandDepartmentName()); |
|
|
|
|
|
|
|
|
|
durableList.add(durable); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!durableList.isEmpty()) { |
|
|
|
|
durableFormService.saveBatch(durableList); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 判断BigDecimal是否为整数 |
|
|
|
|
* 校验易耗品明细 |
|
|
|
|
*/ |
|
|
|
|
private boolean isInteger(BigDecimal value) { |
|
|
|
|
private void validateDetailForConsumer(LdTwoPutStorageDetail detail) { |
|
|
|
|
if (detail.getInboundQuantity() == null || detail.getInboundQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
|
|
|
|
throw new CustomerException("易耗品入库数量必须大于0"); |
|
|
|
|
} |
|
|
|
|
if (detail.getUnitPrice() == null || detail.getUnitPrice().compareTo(BigDecimal.ZERO) < 0) { |
|
|
|
|
throw new CustomerException("单价不能为负数"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 校验耐用品明细 |
|
|
|
|
*/ |
|
|
|
|
private void validateDetailForDurable(LdTwoPutStorageDetail detail) { |
|
|
|
|
if (detail.getInboundQuantity() == null) { |
|
|
|
|
throw new CustomerException("入库数量不能为空"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 验证是否为整数
|
|
|
|
|
try { |
|
|
|
|
value.toBigIntegerExact(); |
|
|
|
|
return true; |
|
|
|
|
detail.getInboundQuantity().intValueExact(); |
|
|
|
|
} catch (ArithmeticException e) { |
|
|
|
|
return false; |
|
|
|
|
throw new CustomerException("耐用品入库数量必须是整数"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (detail.getInboundQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
|
|
|
|
throw new CustomerException("入库数量必须大于0"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建新的易耗品库存 |
|
|
|
|
*/ |
|
|
|
|
private LdConsumerForm buildNewConsumerInventory(LdTwoPutStorageDetail detail) { |
|
|
|
|
BigDecimal amount = detail.getInboundQuantity().multiply(detail.getUnitPrice()); |
|
|
|
|
|
|
|
|
|
return new LdConsumerForm() |
|
|
|
|
.setMaterialId(detail.getMaterialId()) |
|
|
|
|
.setMaterialName(detail.getMaterialName()) |
|
|
|
|
.setMaterialCode(detail.getMaterialCode()) |
|
|
|
|
.setOnePutMaterialCode(detail.getOnePutMaterialCode()) |
|
|
|
|
.setModel(detail.getModel()) |
|
|
|
|
.setType(detail.getType()) |
|
|
|
|
.setUnit(detail.getUnit()) |
|
|
|
|
.setNum(detail.getInboundQuantity()) |
|
|
|
|
.setUnitPrice(detail.getUnitPrice()) |
|
|
|
|
.setAmount(amount) |
|
|
|
|
.setDepartmentId(detail.getDemandDepartment()) |
|
|
|
|
.setDepartment(detail.getDemandDepartmentName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 更新易耗品库存 |
|
|
|
|
*/ |
|
|
|
|
private void updateConsumerInventory(LdConsumerForm inventory, LdTwoPutStorageDetail detail) { |
|
|
|
|
BigDecimal originalNum = inventory.getNum() != null ? inventory.getNum() : BigDecimal.ZERO; |
|
|
|
|
BigDecimal inboundQuantity = detail.getInboundQuantity() != null ? detail.getInboundQuantity() : BigDecimal.ZERO; |
|
|
|
|
|
|
|
|
|
// 更新数量
|
|
|
|
|
BigDecimal newNum = originalNum.add(inboundQuantity); |
|
|
|
|
inventory.setNum(newNum); |
|
|
|
|
|
|
|
|
|
// 更新金额
|
|
|
|
|
BigDecimal originalAmount = inventory.getAmount() != null ? inventory.getAmount() : BigDecimal.ZERO; |
|
|
|
|
BigDecimal inboundAmount = inboundQuantity.multiply(detail.getUnitPrice()); |
|
|
|
|
BigDecimal newAmount = originalAmount.add(inboundAmount); |
|
|
|
|
inventory.setAmount(newAmount); |
|
|
|
|
|
|
|
|
|
// 重新计算单价(加权平均)
|
|
|
|
|
if (newNum.compareTo(BigDecimal.ZERO) > 0) { |
|
|
|
|
BigDecimal newUnitPrice = newAmount.divide(newNum, 6, RoundingMode.HALF_UP); |
|
|
|
|
inventory.setUnitPrice(newUnitPrice); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 保存出入库记录 |
|
|
|
|
*/ |
|
|
|
|
private void saveInventoryRecord(LdConsumerForm inventory, LdTwoPutStorageDetail detail, |
|
|
|
|
UserInfoVO userInfo, Date operationTime, String transactionType) { |
|
|
|
|
LdTwoInventoryRecord record = new LdTwoInventoryRecord() |
|
|
|
|
.setConsumerFormId(inventory.getId().longValue()) |
|
|
|
|
.setTransactionType(transactionType) |
|
|
|
|
.setQuantity(detail.getInboundQuantity()) |
|
|
|
|
.setMoney(detail.getUnitPrice()) |
|
|
|
|
.setOperatorId(userInfo.getUserId()) |
|
|
|
|
.setOperatorName(userInfo.getName()) |
|
|
|
|
.setOperationTime(operationTime) |
|
|
|
|
.setDepartmentId(userInfo.getMainErDepartment()) |
|
|
|
|
.setDepartmentName(userInfo.getMainErDepartmentName()); |
|
|
|
|
|
|
|
|
|
boolean saved = twoInventoryRecordService.save(record); |
|
|
|
|
if (!saved) { |
|
|
|
|
throw new CustomerException("出入库记录保存失败"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|