| | |
| | | package com.ruoyi.stock.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; |
| | | import com.ruoyi.approve.vo.ApproveProcessVO; |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.security.LoginUser; |
| | |
| | | import com.ruoyi.stock.service.StockInRecordService; |
| | | import com.ruoyi.stock.service.StockOutRecordService; |
| | | import com.ruoyi.stock.service.StockUninventoryService; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2026-01-22 10:17:45 |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class StockUninventoryServiceImpl extends ServiceImpl<StockUninventoryMapper, StockUninventory> implements StockUninventoryService { |
| | | |
| | | @Autowired |
| | | private StockUninventoryMapper stockUninventoryMapper; |
| | | @Autowired |
| | | private StockOutRecordService stockOutRecordService; |
| | | @Autowired |
| | | private StockInRecordService stockInRecordService; |
| | | @Autowired |
| | | private ApproveProcessServiceImpl approveProcessService; |
| | | @Autowired |
| | | private ProductModelMapper productModelMapper; |
| | | |
| | | @Override |
| | | public IPage<StockUninventoryDto> pageStockUninventory(Page page, StockUninventoryDto stockUninventoryDto) { |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Integer addStockUninventory(StockUninventoryDto stockUninventoryDto) { |
| | | List<StockUninventory> stockUninventoryList = stockUninventoryMapper.selectList(null); |
| | | //新增入库记录再添加库存 |
| | | StockInRecordDto stockInRecordDto = new StockInRecordDto(); |
| | | stockInRecordDto.setRecordId(stockUninventoryDto.getRecordId()); |
| | |
| | | stockInRecordDto.setRemark(stockUninventoryDto.getRemark()); |
| | | stockInRecordDto.setApproveStatus(0); |
| | | stockInRecordDto.setType("1"); |
| | | if (stockUninventoryDto.getBatchNo() == null || stockUninventoryDto.getBatchNo().isEmpty()) { |
| | | String batchNo; |
| | | // 获取当前月份(两位) |
| | | LocalDate now = LocalDate.now(); |
| | | String monthFlag = now.format(DateTimeFormatter.ofPattern("MM")); |
| | | |
| | | // 获取当前月份的最大流水号 |
| | | int maxSeq = getCurrentMonthMaxSeq(stockUninventoryDto, monthFlag, stockUninventoryList); |
| | | |
| | | // 新流水号 = 最大流水号 + 1 |
| | | int newSeq = maxSeq + 1; |
| | | String seqStr = String.format("%03d", newSeq); |
| | | |
| | | // 组装batchNo |
| | | ProductModel productModel = productModelMapper.selectById(stockUninventoryDto.getProductModelId()); |
| | | batchNo = stockUninventoryDto.getMaterialCode() + productModel.getModel() + "P" + monthFlag + seqStr; |
| | | stockInRecordDto.setBatchNo(batchNo); |
| | | } else { |
| | | stockInRecordDto.setBatchNo(stockUninventoryDto.getBatchNo()); |
| | | } |
| | | Long id = stockInRecordService.add(stockInRecordDto); |
| | | |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | |
| | | } |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | /** |
| | | * 查询当前月份已存在的最大流水号 |
| | | */ |
| | | private static int getCurrentMonthMaxSeq(StockUninventoryDto dto, String monthFlag, List<StockUninventory> existingList) { |
| | | int maxSeq = 0; |
| | | |
| | | String prefix = dto.getMaterialCode() + dto.getModel() + "P" + monthFlag; |
| | | |
| | | // 正则匹配:前缀 + 3位数字 |
| | | Pattern pattern = Pattern.compile(Pattern.quote(prefix) + "(\\d{3})"); |
| | | |
| | | for (StockUninventory item : existingList) { |
| | | String batchNo = item.getBatchNo(); |
| | | if (batchNo == null) continue; |
| | | |
| | | Matcher matcher = pattern.matcher(batchNo); |
| | | if (matcher.find()) { |
| | | int seq = Integer.parseInt(matcher.group(1)); |
| | | if (seq > maxSeq) { |
| | | maxSeq = seq; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return maxSeq; |
| | | } |
| | | |
| | | public void addApproveByPurchase(LoginUser loginUser, StockInRecordDto stockInRecordDto,Long id) throws Exception { |
| | |
| | | approveProcessService.addApprove(approveProcessVO); |
| | | } |
| | | |
| | | @Override |
| | | public Integer addStockUninventoryNoReview(StockUninventoryDto stockUninventoryDto) { |
| | | //新增入库记录再添加库存 |
| | | StockInRecordDto stockInRecordDto = new StockInRecordDto(); |
| | | stockInRecordDto.setRecordId(stockUninventoryDto.getRecordId()); |
| | | stockInRecordDto.setRecordType(stockUninventoryDto.getRecordType()); |
| | | stockInRecordDto.setStockInNum(stockUninventoryDto.getQualitity()); |
| | | stockInRecordDto.setProductModelId(stockUninventoryDto.getProductModelId()); |
| | | stockInRecordDto.setType("1"); |
| | | stockInRecordService.add(stockInRecordDto); |
| | | //再进行新增库存数量库存 |
| | | //先查询库存表中的产品是否存在,不存在新增,存在更新 |
| | | StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId())); |
| | | if (ObjectUtils.isEmpty(oldStockUnInventory)) { |
| | | StockUninventory newStockUnInventory = new StockUninventory(); |
| | | newStockUnInventory.setProductModelId(stockUninventoryDto.getProductModelId()); |
| | | newStockUnInventory.setQualitity(stockUninventoryDto.getQualitity()); |
| | | newStockUnInventory.setVersion(1); |
| | | newStockUnInventory.setRemark(stockUninventoryDto.getRemark()); |
| | | stockUninventoryMapper.insert(newStockUnInventory); |
| | | }else { |
| | | stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto); |
| | | } |
| | | return 1; |
| | | } |
| | | |
| | | /** |
| | | * 更新或创建非良品库存信息 |
| | | * |
| | |
| | | */ |
| | | public void updateOrCreateStockUninventory(StockInRecord stockInRecord) { |
| | | // 先查询库存表中的产品是否存在 |
| | | StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne( |
| | | new QueryWrapper<StockUninventory>().lambda() |
| | | .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()) |
| | | ); |
| | | LambdaQueryWrapper<StockUninventory> queryWrapper = new QueryWrapper<StockUninventory>().lambda() |
| | | .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()); |
| | | |
| | | // 根据 batchNo 是否为空构建不同的查询条件 |
| | | if (stockInRecord.getBatchNo() != null && !stockInRecord.getBatchNo().isEmpty()) { |
| | | queryWrapper.eq(StockUninventory::getBatchNo, stockInRecord.getBatchNo()); |
| | | } else { |
| | | queryWrapper.isNull(StockUninventory::getBatchNo); |
| | | } |
| | | |
| | | StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(queryWrapper); |
| | | |
| | | if (ObjectUtils.isEmpty(oldStockUnInventory)) { |
| | | // 不存在则新增 |
| | |
| | | newStockUnInventory.setQualitity(stockInRecord.getStockInNum()); |
| | | newStockUnInventory.setVersion(1); |
| | | newStockUnInventory.setRemark(stockInRecord.getRemark()); |
| | | newStockUnInventory.setLockedQuantity(stockInRecord.getLockedQuantity()); |
| | | newStockUnInventory.setBatchNo(stockInRecord.getBatchNo()); |
| | | stockUninventoryMapper.insert(newStockUnInventory); |
| | | } else { |
| | | // 存在则更新 |
| | | LambdaUpdateWrapper<StockUninventory> updateWrapper = new LambdaUpdateWrapper<>(); |
| | | if (stockInRecord.getStockInNum() != null) { |
| | | updateWrapper.setSql("qualitity = qualitity + " + stockInRecord.getStockInNum()); |
| | | updateWrapper |
| | | .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()); |
| | | |
| | | // 根据 batchNo 是否为空构建不同的更新条件 |
| | | if (stockInRecord.getBatchNo() != null && !stockInRecord.getBatchNo().isEmpty()) { |
| | | updateWrapper.eq(StockUninventory::getBatchNo, stockInRecord.getBatchNo()); |
| | | } else { |
| | | updateWrapper.isNull(StockUninventory::getBatchNo); |
| | | } |
| | | updateWrapper.setSql("version = version + 1"); |
| | | String remark = stockInRecord.getRemark(); |
| | | if (remark != null && !remark.isEmpty()) { |
| | | updateWrapper.set(StockUninventory::getRemark, remark); |
| | | } |
| | | updateWrapper.set(StockUninventory::getUpdateTime, new Date()); |
| | | updateWrapper.eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()); |
| | | |
| | | updateWrapper |
| | | .setSql(stockInRecord.getStockInNum() != null, |
| | | "qualitity = qualitity + " + stockInRecord.getStockInNum()) |
| | | .setSql(true, "version = version + 1") |
| | | .set(stockInRecord.getRemark() != null && !stockInRecord.getRemark().isEmpty(), |
| | | StockUninventory::getRemark, stockInRecord.getRemark()) |
| | | .setSql(stockInRecord.getLockedQuantity() != null, |
| | | "locked_quantity = locked_quantity + " + stockInRecord.getLockedQuantity()) |
| | | .set(StockUninventory::getUpdateTime, new Date()); |
| | | |
| | | stockUninventoryMapper.update(null, updateWrapper); |
| | | } |
| | | } |
| | |
| | | stockUninventory.setLockedQuantity(stockUninventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity())); |
| | | return this.updateById(stockUninventory); |
| | | } |
| | | } |
| | | } |