| | |
| | | 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.mapper.ApproveProcessMapper; |
| | | import com.ruoyi.approve.pojo.ApproveProcess; |
| | | import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.enums.StockQualifiedRecordTypeEnum; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; |
| | | 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.project.system.domain.SysDept; |
| | | import com.ruoyi.project.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import com.ruoyi.sales.dto.SalesLedgerProductDto; |
| | | import com.ruoyi.sales.dto.ShippingInfoDto; |
| | | import com.ruoyi.sales.mapper.SalesLedgerMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.mapper.ShipmentApprovalMapper; |
| | | import com.ruoyi.sales.mapper.ShippingInfoMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import com.ruoyi.sales.pojo.ShipmentApproval; |
| | | import com.ruoyi.sales.pojo.ShippingInfo; |
| | | import com.ruoyi.sales.service.ICommonFileService; |
| | | 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.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | @Autowired |
| | | private CommonFileServiceImpl commonFileService; |
| | | |
| | | @Override |
| | | public IPage<ShippingInfo> listPage(Page page, ShippingInfo req) { |
| | | IPage<ShippingInfo> listPage = shippingInfoMapper.listPage(page, req); |
| | | @Autowired |
| | | private ApproveProcessServiceImpl approveProcessService; |
| | | |
| | | @Autowired |
| | | private ApproveProcessMapper approveProcessMapper; |
| | | |
| | | @Autowired |
| | | private ShipmentApprovalMapper shipmentApprovalMapper; |
| | | |
| | | @Autowired |
| | | private SalesLedgerMapper salesLedgerMapper; |
| | | |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | |
| | | @Override |
| | | public IPage<ShippingInfoDto> listPage(Page page, ShippingInfo req) { |
| | | IPage<ShippingInfoDto> listPage = shippingInfoMapper.listPage(page, req); |
| | | listPage.getRecords().forEach(item ->{ |
| | | item.setCommonFileList(commonFileService.getFileListByBusinessId(item.getId(), FileNameType.SHIP.getValue())); |
| | | }); |
| | | return listPage; |
| | | } |
| | | |
| | |
| | | if (byId == null) { |
| | | throw new RuntimeException("发货信息不存在"); |
| | | } |
| | | //扣减库存 |
| | | if(!"已发货".equals(byId.getStatus())){ |
| | | SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(byId.getSalesLedgerProductId()); |
| | | stockUtils.substractStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId()); |
| | | } |
| | | byId.setExpressNumber(req.getExpressNumber()); |
| | | byId.setExpressCompany(req.getExpressCompany()); |
| | | byId.setStatus("已发货"); |
| | | byId.setShippingCarNumber(req.getShippingCarNumber()); |
| | | boolean update = this.updateById(req); |
| | | //扣减库存 |
| | | SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(req.getSalesLedgerProductId()); |
| | | stockUtils.substractStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId()); |
| | | byId.setShippingDate(req.getShippingDate()); |
| | | boolean update = this.updateById(byId); |
| | | // 迁移文件 |
| | | if(CollectionUtils.isNotEmpty(req.getTempFileIds())){ |
| | | tempFileService.migrateTempFilesToFormal(req.getId(), req.getTempFileIds(), FileNameType.SHIP.getValue()); |
| | |
| | | |
| | | @Override |
| | | public boolean delete(List<Long> ids) { |
| | | List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>() |
| | | .in(ShippingInfo::getId, ids)); |
| | | if(CollectionUtils.isEmpty(shippingInfos)) return false; |
| | | // 删除附件 |
| | | commonFileService.deleteByBusinessIds(ids, FileNameType.SHIP.getValue()); |
| | | for (Long id : ids) { |
| | | stockUtils.deleteStockRecord(id, StockQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode()); |
| | | // 扣已发货库存 |
| | | for (ShippingInfo shippingInfo : shippingInfos) { |
| | | if("已发货".equals(shippingInfo.getStatus())) { |
| | | stockUtils.deleteStockOutRecord(shippingInfo.getId(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode()); |
| | | } |
| | | } |
| | | boolean delete = this.removeBatchByIds(ids); |
| | | return delete; |
| | | // 删除发货审批 |
| | | if(CollectionUtils.isNotEmpty(shippingInfos)){ |
| | | for (ShippingInfo shippingInfo : shippingInfos){ |
| | | ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>() |
| | | .like(ApproveProcess::getApproveReason, shippingInfo.getShippingNo())); |
| | | if(one != null){ |
| | | approveProcessService.delByIds(Collections.singletonList(one.getId())); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return this.removeBatchByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public List<SalesLedgerProductDto> getReturnManagementDtoById(Long shippingId) { |
| | | return shippingInfoMapper.getReturnManagementDtoById(shippingId ); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public List<ShippingInfo> getShippingInfoByCustomerName(String customerName) { |
| | | return shippingInfoMapper.getShippingInfoByCustomerName(customerName); |
| | | } |
| | | |
| | | /** |
| | | * 一键发货 - 自动审批通过并出库 |
| | | * 创建审批记录,批准人为"月光" |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public boolean oneClickShipping(ShippingInfoDto req) throws IOException { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | |
| | | // 获取销售台账产品信息 |
| | | SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(req.getSalesLedgerProductId()); |
| | | if (salesLedgerProduct == null) { |
| | | throw new RuntimeException("销售产品不存在"); |
| | | } |
| | | |
| | | // 获取销售台账信息 |
| | | SalesLedger salesLedger = salesLedgerMapper.selectById(req.getSalesLedgerId()); |
| | | |
| | | // 查询"月光"用户 |
| | | com.ruoyi.project.system.domain.SysUser moonlightUser = sysUserMapper.selectOne( |
| | | new LambdaQueryWrapper<com.ruoyi.project.system.domain.SysUser>() |
| | | .eq(com.ruoyi.project.system.domain.SysUser::getNickName, "月光") |
| | | .last("limit 1") |
| | | ); |
| | | |
| | | // 生成发货单号 |
| | | String shippingNo = com.ruoyi.common.utils.OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SH", "shipping_no"); |
| | | |
| | | // 创建发货记录,状态直接为"已发货" |
| | | ShippingInfo shippingInfo = new ShippingInfo(); |
| | | shippingInfo.setShippingNo(shippingNo); |
| | | shippingInfo.setSalesLedgerId(req.getSalesLedgerId()); |
| | | shippingInfo.setSalesLedgerProductId(req.getSalesLedgerProductId()); |
| | | shippingInfo.setType(req.getType()); |
| | | shippingInfo.setShippingDate(req.getShippingDate()); |
| | | shippingInfo.setShippingCarNumber(req.getShippingCarNumber()); |
| | | shippingInfo.setExpressCompany(req.getExpressCompany()); |
| | | shippingInfo.setExpressNumber(req.getExpressNumber()); |
| | | shippingInfo.setStatus("已发货"); |
| | | boolean save = this.save(shippingInfo); |
| | | |
| | | if (save) { |
| | | // 创建审批流程记录,状态为审核完成(2),批准人为"月光" |
| | | String salesContractNo = salesLedger != null ? salesLedger.getSalesContractNo() : ""; |
| | | StringBuilder approveReason = new StringBuilder(); |
| | | approveReason.append("发货单号:").append(shippingNo); |
| | | if (salesContractNo != null && !salesContractNo.isEmpty()) { |
| | | approveReason.append("\n销售单号:").append(salesContractNo); |
| | | } |
| | | approveReason.append("\n").append(req.getType()); |
| | | if ("货车".equals(req.getType()) && req.getShippingCarNumber() != null && !req.getShippingCarNumber().isEmpty()) { |
| | | approveReason.append(":").append(req.getShippingCarNumber()); |
| | | } else if ("快递".equals(req.getType()) && req.getExpressCompany() != null && !req.getExpressCompany().isEmpty()) { |
| | | approveReason.append(":").append(req.getExpressCompany()); |
| | | } |
| | | |
| | | ApproveProcess approveProcess = new ApproveProcess(); |
| | | String approveId = com.ruoyi.common.utils.OrderUtils.countTodayByCreateTime(approveProcessMapper, "", "approve_id"); |
| | | approveProcess.setApproveId(approveId); |
| | | approveProcess.setApproveUser(loginUser.getUserId()); |
| | | approveProcess.setApproveUserName(loginUser.getNickName()); |
| | | approveProcess.setApproveDeptId(loginUser.getCurrentDeptId()); |
| | | |
| | | SysDept sysDept = sysDeptMapper.selectDeptById(loginUser.getCurrentDeptId()); |
| | | |
| | | approveProcess.setApproveDeptName(sysDept.getDeptName()); |
| | | approveProcess.setApproveReason(approveReason.toString()); |
| | | approveProcess.setApproveUserIds(moonlightUser != null ? String.valueOf(moonlightUser.getUserId()) : ""); |
| | | approveProcess.setApproveUserNames(moonlightUser != null ? moonlightUser.getNickName() : "月光"); |
| | | approveProcess.setApproveUserCurrentId(moonlightUser != null ? moonlightUser.getUserId() : null); |
| | | approveProcess.setApproveUserCurrentName(moonlightUser != null ? moonlightUser.getNickName() : "月光"); |
| | | approveProcess.setApproveStatus(2); // 审核完成 |
| | | approveProcess.setApproveType(7); // 发货审批 |
| | | approveProcess.setApproveDelete(0); |
| | | approveProcess.setApproveTime(new Date()); // 申请时间 |
| | | approveProcess.setTenantId(loginUser.getCurrentDeptId()); |
| | | approveProcess.setCreateTime(LocalDateTime.now()); |
| | | approveProcess.setEndDate(new Date()); |
| | | approveProcessService.save(approveProcess); |
| | | |
| | | // 直接扣减库存 |
| | | stockUtils.substractStock( |
| | | salesLedgerProduct.getProductModelId(), |
| | | salesLedgerProduct.getQuantity(), |
| | | StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), |
| | | shippingInfo.getId() |
| | | ); |
| | | |
| | | // 迁移附件 |
| | | if (CollectionUtils.isNotEmpty(req.getTempFileIds())) { |
| | | tempFileService.migrateTempFilesToFormal(shippingInfo.getId(), req.getTempFileIds(), FileNameType.SHIP.getValue()); |
| | | } |
| | | } |
| | | |
| | | return save; |
| | | } |
| | | |
| | | /** |
| | | * 批量一键发货 - 将销售台账下所有未发货的产品全部发货 |
| | | * 创建审批记录,批准人为"月光" |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public boolean batchOneClickShipping(Long salesLedgerId, ShippingInfoDto req) throws IOException { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | |
| | | // 查询"月光"用户 |
| | | com.ruoyi.project.system.domain.SysUser moonlightUser = sysUserMapper.selectOne( |
| | | new LambdaQueryWrapper<com.ruoyi.project.system.domain.SysUser>() |
| | | .eq(com.ruoyi.project.system.domain.SysUser::getNickName, "月光") |
| | | .last("limit 1") |
| | | ); |
| | | |
| | | // 获取销售台账信息 |
| | | SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerId); |
| | | String salesContractNo = salesLedger != null ? salesLedger.getSalesContractNo() : ""; |
| | | |
| | | // 查询该销售台账下所有产品 |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList( |
| | | new LambdaQueryWrapper<SalesLedgerProduct>() |
| | | .eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId) |
| | | .eq(SalesLedgerProduct::getType, 1) // 销售类型 |
| | | ); |
| | | |
| | | if (CollectionUtils.isEmpty(products)) { |
| | | throw new RuntimeException("该销售台账下没有产品"); |
| | | } |
| | | |
| | | // 过滤出未发货的产品(没有发货记录或发货状态不是"已发货") |
| | | List<SalesLedgerProduct> unshippedProducts = new ArrayList<>(); |
| | | for (SalesLedgerProduct product : products) { |
| | | ShippingInfo existingShipping = shippingInfoMapper.selectOne( |
| | | new LambdaQueryWrapper<ShippingInfo>() |
| | | .eq(ShippingInfo::getSalesLedgerProductId, product.getId()) |
| | | .eq(ShippingInfo::getStatus, "已发货") |
| | | .last("limit 1") |
| | | ); |
| | | if (existingShipping == null) { |
| | | unshippedProducts.add(product); |
| | | } |
| | | } |
| | | |
| | | if (CollectionUtils.isEmpty(unshippedProducts)) { |
| | | throw new RuntimeException("该销售台账下所有产品已发货"); |
| | | } |
| | | |
| | | // 收集所有发货单号用于合并审批 |
| | | StringBuilder allShippingNos = new StringBuilder(); |
| | | for (SalesLedgerProduct product : unshippedProducts) { |
| | | // 生成发货单号 |
| | | String shippingNo = com.ruoyi.common.utils.OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SH", "shipping_no"); |
| | | |
| | | // 创建发货记录 |
| | | ShippingInfo shippingInfo = new ShippingInfo(); |
| | | shippingInfo.setShippingNo(shippingNo); |
| | | shippingInfo.setSalesLedgerId(salesLedgerId); |
| | | shippingInfo.setSalesLedgerProductId(product.getId()); |
| | | shippingInfo.setType(req.getType()); |
| | | shippingInfo.setShippingDate(req.getShippingDate()); |
| | | shippingInfo.setShippingCarNumber(req.getShippingCarNumber()); |
| | | shippingInfo.setExpressCompany(req.getExpressCompany()); |
| | | shippingInfo.setExpressNumber(req.getExpressNumber()); |
| | | shippingInfo.setStatus("已发货"); |
| | | |
| | | boolean save = this.save(shippingInfo); |
| | | if (save) { |
| | | // 收集发货单号 |
| | | if (allShippingNos.length() > 0) { |
| | | allShippingNos.append("\n"); |
| | | } |
| | | allShippingNos.append("发货单号:").append(shippingNo); |
| | | |
| | | // 扣减库存 |
| | | stockUtils.substractStock( |
| | | product.getProductModelId(), |
| | | product.getQuantity(), |
| | | StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), |
| | | shippingInfo.getId() |
| | | ); |
| | | } |
| | | } |
| | | |
| | | // 创建合并的审批流程记录 |
| | | if (allShippingNos.length() > 0) { |
| | | StringBuilder approveReason = new StringBuilder(); |
| | | approveReason.append(allShippingNos.toString()); |
| | | if (salesContractNo != null && !salesContractNo.isEmpty()) { |
| | | approveReason.append("\n销售单号:").append(salesContractNo); |
| | | } |
| | | approveReason.append("\n").append(req.getType()); |
| | | if ("货车".equals(req.getType()) && req.getShippingCarNumber() != null && !req.getShippingCarNumber().isEmpty()) { |
| | | approveReason.append(":").append(req.getShippingCarNumber()); |
| | | } else if ("快递".equals(req.getType()) && req.getExpressCompany() != null && !req.getExpressCompany().isEmpty()) { |
| | | approveReason.append(":").append(req.getExpressCompany()); |
| | | } |
| | | |
| | | ApproveProcess approveProcess = new ApproveProcess(); |
| | | String approveId = com.ruoyi.common.utils.OrderUtils.countTodayByCreateTime(approveProcessMapper, "", "approve_id"); |
| | | approveProcess.setApproveId(approveId); |
| | | approveProcess.setApproveUser(loginUser.getUserId()); |
| | | approveProcess.setApproveUserName(loginUser.getNickName()); |
| | | approveProcess.setApproveDeptId(loginUser.getCurrentDeptId()); |
| | | SysDept sysDept = sysDeptMapper.selectDeptById(loginUser.getCurrentDeptId()); |
| | | approveProcess.setApproveDeptName(sysDept.getDeptName()); |
| | | approveProcess.setApproveReason(approveReason.toString()); |
| | | approveProcess.setApproveUserIds(moonlightUser != null ? String.valueOf(moonlightUser.getUserId()) : ""); |
| | | approveProcess.setApproveUserNames(moonlightUser != null ? moonlightUser.getNickName() : "月光"); |
| | | approveProcess.setApproveUserCurrentId(moonlightUser != null ? moonlightUser.getUserId() : null); |
| | | approveProcess.setApproveUserCurrentName(moonlightUser != null ? moonlightUser.getNickName() : "月光"); |
| | | approveProcess.setApproveStatus(2); // 审核完成 |
| | | approveProcess.setApproveType(7); // 发货审批 |
| | | approveProcess.setApproveDelete(0); |
| | | approveProcess.setApproveTime(new Date()); // 申请时间 |
| | | approveProcess.setTenantId(loginUser.getCurrentDeptId()); |
| | | approveProcess.setCreateTime(LocalDateTime.now()); |
| | | approveProcess.setEndDate(new Date()); |
| | | approveProcessService.save(approveProcess); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | } |