| | |
| | | |
| | | @Override |
| | | public void borrowSubstance(FeStandardSubstanceRecord feStandardSubstanceRecord) { |
| | | FeStandardSubstance standardSubstance = feStandardSubstanceMapper.selectOne(Wrappers.<FeStandardSubstance>lambdaQuery() |
| | | .eq(FeStandardSubstance::getId, feStandardSubstanceRecord.getSubstanceId()) |
| | | .eq(FeStandardSubstance::getState, 0) |
| | | .last("limit 1")); |
| | | if (ObjectUtils.isEmpty(standardSubstance)){ |
| | | throw new ErrorException("该物质已被借出!"); |
| | | Long borrowQuantity = feStandardSubstanceRecord.getBorrowQuantity(); |
| | | if (borrowQuantity == null || borrowQuantity <= 0) { |
| | | throw new ErrorException("请填写正确的领用数量!"); |
| | | } |
| | | FeStandardSubstance standardSubstance = feStandardSubstanceMapper.selectById(feStandardSubstanceRecord.getSubstanceId()); |
| | | if (ObjectUtils.isEmpty(standardSubstance)){ |
| | | throw new ErrorException("该标准物质不存在!"); |
| | | } |
| | | long remaining = calcRemaining(standardSubstance); |
| | | if (remaining <= 0) { |
| | | throw new ErrorException("该标准物质已全部领用,剩余数量为0!"); |
| | | } |
| | | if (borrowQuantity > remaining) { |
| | | throw new ErrorException("领用数量不能超过剩余数量(当前剩余" + remaining + ")!"); |
| | | } |
| | | long remainingAfter = remaining - borrowQuantity; |
| | | feStandardSubstanceRecord.setRemainingQuantity(remainingAfter); |
| | | // 记录领用当时的原始数量快照,之后清单里再改数量不影响历史领用记录 |
| | | feStandardSubstanceRecord.setOriginalQuantity(standardSubstance.getQuantity()); |
| | | this.baseMapper.insert(feStandardSubstanceRecord); |
| | | standardSubstance.setState(1); |
| | | standardSubstance.setState(remainingAfter == 0 ? 1 : 0); |
| | | feStandardSubstanceMapper.updateById(standardSubstance); |
| | | } |
| | | |
| | | @Override |
| | | public void returnSubstance(FeStandardSubstanceRecord feStandardSubstanceRecord) { |
| | | FeStandardSubstance standardSubstance = feStandardSubstanceMapper.selectOne(Wrappers.<FeStandardSubstance>lambdaQuery() |
| | | .eq(FeStandardSubstance::getId, feStandardSubstanceRecord.getSubstanceId()) |
| | | .eq(FeStandardSubstance::getState, 1)); |
| | | public Long getRemainingQuantity(Integer substanceId) { |
| | | FeStandardSubstance standardSubstance = feStandardSubstanceMapper.selectById(substanceId); |
| | | if (ObjectUtils.isEmpty(standardSubstance)){ |
| | | throw new ErrorException("该物质未被领用,无需归还!"); |
| | | return 0L; |
| | | } |
| | | standardSubstance.setState(0); |
| | | feStandardSubstanceMapper.updateById(standardSubstance); |
| | | baseMapper.update(feStandardSubstanceRecord, Wrappers.<FeStandardSubstanceRecord>lambdaUpdate() |
| | | .eq(FeStandardSubstanceRecord::getSubstanceId, feStandardSubstanceRecord.getSubstanceId()) |
| | | .eq(FeStandardSubstanceRecord::getStatus, 0)); |
| | | return Math.max(calcRemaining(standardSubstance), 0L); |
| | | } |
| | | |
| | | private long calcRemaining(FeStandardSubstance substance) { |
| | | long total = substance.getQuantity() == null ? 0L : substance.getQuantity(); |
| | | long borrowed = 0L; |
| | | List<FeStandardSubstanceRecord> records = this.baseMapper.selectList(Wrappers.<FeStandardSubstanceRecord>lambdaQuery() |
| | | .eq(FeStandardSubstanceRecord::getSubstanceId, substance.getId())); |
| | | for (FeStandardSubstanceRecord record : records) { |
| | | if ("1".equals(record.getStatus())) { // 历史归还记录不计入已领用 |
| | | continue; |
| | | } |
| | | borrowed += record.getBorrowQuantity() == null ? total : record.getBorrowQuantity(); |
| | | } |
| | | return total - borrowed; |
| | | } |
| | | |
| | | @Override |