feat(eqp): 新增备品备件库存变更接口

- 在控制器中添加 changeStock 接口用于库存增减操作
- 实现库存变动业务逻辑,包括参数校验和库存检查
- 增加事务管理确保数据一致性- 更新备件库存并记录变动日志
- 支持通过 BO 对象传递变动信息及操作人
- 提供详细的错误提示和操作结果反馈
This commit is contained in:
2025-10-18 14:11:56 +08:00
parent 8418454082
commit ab6dbedc8a
3 changed files with 98 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package com.klp.mes.eqp.controller;
import java.util.List;
import java.util.Arrays;
import com.klp.mes.eqp.domain.bo.EqpSparePartBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
@@ -96,4 +97,10 @@ public class EqpSparePartsChangeController extends BaseController {
@PathVariable Long[] changeIds) {
return toAjax(iEqpSparePartsChangeService.deleteWithValidByIds(Arrays.asList(changeIds), true));
}
//新增接口:库存变更接口 用来增加或者减少库存如果库存不足需要抛出异常
@PostMapping("/changeStock")
public R<Void> changeStock(@RequestBody EqpSparePartsChangeBo bo) {
return iEqpSparePartsChangeService.changeStock(bo);
}
}

View File

@@ -1,6 +1,8 @@
package com.klp.mes.eqp.service;
import com.klp.common.core.domain.R;
import com.klp.mes.eqp.domain.EqpSparePartsChange;
import com.klp.mes.eqp.domain.bo.EqpSparePartBo;
import com.klp.mes.eqp.domain.vo.EqpSparePartsChangeVo;
import com.klp.mes.eqp.domain.bo.EqpSparePartsChangeBo;
import com.klp.common.core.page.TableDataInfo;
@@ -46,4 +48,6 @@ public interface IEqpSparePartsChangeService {
* 校验并批量删除备品备件变动记录信息
*/
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
R<Void> changeStock(EqpSparePartsChangeBo bo);
}

View File

@@ -2,20 +2,30 @@ package com.klp.mes.eqp.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.klp.common.core.domain.R;
import com.klp.common.core.domain.model.LoginUser;
import com.klp.common.core.page.TableDataInfo;
import com.klp.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.common.helper.LoginHelper;
import com.klp.common.utils.StringUtils;
import com.klp.mes.eqp.domain.EqpSparePart;
import com.klp.mes.eqp.domain.bo.EqpSparePartBo;
import com.klp.mes.eqp.mapper.EqpSparePartMapper;
import liquibase.pro.packaged.A;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.klp.mes.eqp.domain.bo.EqpSparePartsChangeBo;
import com.klp.mes.eqp.domain.vo.EqpSparePartsChangeVo;
import com.klp.mes.eqp.domain.EqpSparePartsChange;
import com.klp.mes.eqp.mapper.EqpSparePartsChangeMapper;
import com.klp.mes.eqp.service.IEqpSparePartsChangeService;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
@@ -32,6 +42,9 @@ public class EqpSparePartsChangeServiceImpl implements IEqpSparePartsChangeServi
private final EqpSparePartsChangeMapper baseMapper;
@Autowired
private EqpSparePartMapper eqpSparePartMapper;
/**
* 查询备品备件变动记录
*/
@@ -124,4 +137,78 @@ public class EqpSparePartsChangeServiceImpl implements IEqpSparePartsChangeServi
}
return baseMapper.deleteBatchIds(ids) > 0;
}
@Override
@Transactional(rollbackFor = Exception.class)
public R<Void> changeStock(EqpSparePartsChangeBo bo) {
// 1. 基础参数校验基于EqpSparePartsChangeBo的字段
if (bo.getPartId() == null) {
return R.fail("备件ID不能为空");
}
if (bo.getChangeQuantity() == null || bo.getChangeQuantity() <= 0) {
return R.fail("变动数量必须为正整数");
}
if (StringUtils.isBlank(bo.getChangeType())) {
return R.fail("变动类型(增加/减少)不能为空");
}
// 校验变动类型合法性
if (!"增加".equals(bo.getChangeType()) && !"减少".equals(bo.getChangeType())) {
return R.fail("变动类型仅支持“增加”或“减少”");
}
// 2. 查询备件信息
EqpSparePart sparePart = eqpSparePartMapper.selectById(bo.getPartId());
if (sparePart == null) {
return R.fail("备品备件不存在");
}
if ("2".equals(sparePart.getDelFlag())) {
return R.fail("备品备件已删除,无法操作");
}
// 3. 库存变动逻辑
Long newQuantity;
if ("减少".equals(bo.getChangeType())) {
// 减少库存时校验库存充足性
if (sparePart.getQuantity() < bo.getChangeQuantity()) {
return R.fail("库存不足,当前库存:" + sparePart.getQuantity() + ",需减少:" + bo.getChangeQuantity());
}
newQuantity = sparePart.getQuantity() - bo.getChangeQuantity();
} else {
// 增加库存直接累加
newQuantity = sparePart.getQuantity() + bo.getChangeQuantity();
}
// 4. 更新备件库存(填充审计字段)
sparePart.setQuantity(newQuantity);
// 从Bo获取操作人优先或默认值
String operator = String.valueOf(LoginHelper.getLoginUser());
sparePart.setUpdateBy(operator);
sparePart.setUpdateTime(new Date());
boolean updateSuccess = eqpSparePartMapper.updateById(sparePart) > 0;
if (!updateSuccess) {
return R.fail("库存更新失败");
}
// 5. 插入变动记录基于EqpSparePartsChangeBo转换
EqpSparePartsChange changeLog = new EqpSparePartsChange();
// 从Bo复制核心字段
changeLog.setPartId(bo.getPartId());
changeLog.setChangeType(bo.getChangeType());
changeLog.setChangeQuantity(bo.getChangeQuantity());
changeLog.setReason(bo.getReason()); // 变动原因Bo中应包含该字段
changeLog.setRemark(bo.getRemark()); // 备注Bo中应包含该字段
// 补充审计字段
changeLog.setChangeTime(new Date());
changeLog.setCreateBy(operator);
changeLog.setUpdateBy(operator);
changeLog.setDelFlag("0"); // 未删除
// 插入变动记录需注入对应Mapper
int insertCount = baseMapper.insert(changeLog);
if (insertCount <= 0) {
return R.fail("变动记录插入失败");
}
return R.ok("库存变动成功,新库存:" + newQuantity);
}
}