package com.ruoyi.sales.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.approve.bean.vo.ApproveProcessVO; import com.ruoyi.approve.pojo.ApproveProcess; import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; import com.ruoyi.common.enums.FileNameType; import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; import com.ruoyi.common.utils.OrderUtils; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.framework.security.LoginUser; import com.ruoyi.other.service.impl.TempFileServiceImpl; import com.ruoyi.procurementrecord.utils.StockUtils; import com.ruoyi.sales.dto.SalesLedgerProductDto; import com.ruoyi.sales.dto.ShippingInfoDto; import com.ruoyi.sales.mapper.SalesLedgerProductMapper; import com.ruoyi.sales.mapper.ShippingInfoMapper; import com.ruoyi.sales.pojo.SalesLedgerProduct; import com.ruoyi.sales.pojo.ShippingInfo; import com.ruoyi.sales.service.ShippingInfoService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.math.BigDecimal; import java.time.LocalDate; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; /** * @author :yys * @date : 2025/10/22 9:33 */ @Service @Slf4j public class ShippingInfoServiceImpl extends ServiceImpl implements ShippingInfoService { @Autowired private ShippingInfoMapper shippingInfoMapper; @Autowired private TempFileServiceImpl tempFileService; @Autowired private SalesLedgerProductMapper salesLedgerProductMapper; @Autowired private StockUtils stockUtils; @Autowired private CommonFileServiceImpl commonFileService; @Autowired private ApproveProcessServiceImpl approveProcessService; @Override public IPage listPage(Page page, ShippingInfo req) { IPage listPage = shippingInfoMapper.listPage(page, req); listPage.getRecords().forEach(item ->{ item.setCommonFileList(commonFileService.getFileListByBusinessId(item.getId(), FileNameType.SHIP.getValue())); }); return listPage; } @Override public boolean deductStock(ShippingInfoDto req) throws IOException { ShippingInfo byId = this.getById(req.getId()); if (byId == null) { throw new RuntimeException("发货信息不存在"); } //扣减库存(按本次发货数量,支持部分发货) if(!"已发货".equals(byId.getStatus())){ SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(byId.getSalesLedgerProductId()); BigDecimal shipQuantity = byId.getQuantity() != null ? byId.getQuantity() : salesLedgerProduct.getQuantity(); stockUtils.substractStock(salesLedgerProduct.getProductModelId(), shipQuantity, StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId()); } byId.setExpressNumber(req.getExpressNumber()); byId.setExpressCompany(req.getExpressCompany()); byId.setStatus("已发货"); byId.setShippingCarNumber(req.getShippingCarNumber()); byId.setShippingDate(req.getShippingDate()); boolean update = this.updateById(byId); // 迁移文件 if(CollectionUtils.isNotEmpty(req.getTempFileIds())){ tempFileService.migrateTempFilesToFormal(req.getId(), req.getTempFileIds(), FileNameType.SHIP.getValue()); } return update ; } @Override @Transactional(rollbackFor = Exception.class) public boolean addShipping(ShippingInfoDto req) throws Exception { // 校验产品行 SalesLedgerProduct product = salesLedgerProductMapper.selectById(req.getSalesLedgerProductId()); if (product == null) { throw new RuntimeException("产品明细不存在"); } if (product.getQuantity() == null) { throw new RuntimeException("产品明细数量为空,无法发货"); } // 未传本次发货数量时默认取产品行全量 if (req.getQuantity() == null) { req.setQuantity(product.getQuantity()); } validateQuantity(req.getSalesLedgerProductId(), req.getQuantity()); // 发货审批 LoginUser loginUser = SecurityUtils.getLoginUser(); String sh = OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SH"); ApproveProcessVO approveProcessVO = new ApproveProcessVO(); approveProcessVO.setApproveType(7); approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId()); approveProcessVO.setApproveReason(req.getType() + ":" + sh); List approveUserIds = req.getApproveUserIds(); approveProcessVO.setApproveUserIds( approveUserIds == null || approveUserIds.isEmpty() ? "" : String.join(",", approveUserIds)); approveProcessVO.setApproveUser(loginUser.getUserId()); approveProcessVO.setApproveTime(LocalDate.now().toString()); approveProcessService.addApprove(approveProcessVO); // 添加发货单 req.setShippingNo(sh); req.setStatus("待审核"); return this.save(req); } /** * 剩余可发数量 = 产品行数量 - 状态为"审核通过/已发货"的发货数量之和 */ public BigDecimal getUnshippedQuantity(Long salesLedgerProductId) { SalesLedgerProduct product = salesLedgerProductMapper.selectById(salesLedgerProductId); if (product == null || product.getQuantity() == null) { return BigDecimal.ZERO; } List shippedList = shippingInfoMapper.selectList(new LambdaQueryWrapper() .eq(ShippingInfo::getSalesLedgerProductId, salesLedgerProductId) .in(ShippingInfo::getStatus, Arrays.asList("审核通过", "已发货"))); BigDecimal shipped = shippedList.stream() .map(ShippingInfo::getQuantity) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal remaining = product.getQuantity().subtract(shipped); return remaining.compareTo(BigDecimal.ZERO) > 0 ? remaining : BigDecimal.ZERO; } /** * 校验本次发货数量:必须大于0且不超过剩余可发数量 */ private void validateQuantity(Long salesLedgerProductId, BigDecimal quantity) { if (quantity == null || quantity.compareTo(BigDecimal.ZERO) <= 0) { throw new RuntimeException("本次发货数量必须大于0"); } BigDecimal remaining = getUnshippedQuantity(salesLedgerProductId); if (quantity.compareTo(remaining) > 0) { throw new RuntimeException("本次发货数量超过剩余可发数量(剩余可发:" + remaining + ")"); } } @Override public boolean delete(List ids) { List shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper() .in(ShippingInfo::getId, ids)); if(CollectionUtils.isEmpty(shippingInfos)) return false; // 删除附件 commonFileService.deleteByBusinessIds(ids, FileNameType.SHIP.getValue()); // 扣已发货库存 for (ShippingInfo shippingInfo : shippingInfos) { if("已发货".equals(shippingInfo.getStatus())) { stockUtils.deleteStockOutRecord(shippingInfo.getId(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode()); } } // 删除发货审批 if(CollectionUtils.isNotEmpty(shippingInfos)){ for (ShippingInfo shippingInfo : shippingInfos){ ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper() .like(ApproveProcess::getApproveReason, shippingInfo.getShippingNo())); if(one != null){ approveProcessService.delByIds(Collections.singletonList(one.getId())); } } } return this.removeBatchByIds(ids); } @Override public List getReturnManagementDtoById(Long shippingId) { return shippingInfoMapper.getReturnManagementDtoById(shippingId ); } @Override public List getShippingInfoByCustomerName(String customerName) { return shippingInfoMapper.getShippingInfoByCustomerName(customerName); } }