From 08c0e7417807e0e172640835d21f873157b03650 Mon Sep 17 00:00:00 2001 From: liding <756868258@qq.com> Date: 星期二, 13 五月 2025 16:48:51 +0800 Subject: [PATCH] 采购台账联调优化 --- src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 204 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java index a209ccb..c8ef0fc 100644 --- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java +++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java @@ -1,15 +1,44 @@ package com.ruoyi.purchase.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.ruoyi.common.exception.base.BaseException; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.other.mapper.TempFileMapper; +import com.ruoyi.other.pojo.TempFile; +import com.ruoyi.project.system.domain.SysUser; +import com.ruoyi.project.system.mapper.SysUserMapper; +import com.ruoyi.purchase.dto.PurchaseLedgerDto; import com.ruoyi.purchase.mapper.PurchaseLedgerMapper; import com.ruoyi.purchase.pojo.PurchaseLedger; import com.ruoyi.purchase.service.IPurchaseLedgerService; -import lombok.AllArgsConstructor; +import com.ruoyi.sales.mapper.SalesLedgerFileMapper; +import com.ruoyi.sales.mapper.SalesLedgerMapper; +import com.ruoyi.sales.mapper.SalesLedgerProductMapper; +import com.ruoyi.sales.pojo.SalesLedger; +import com.ruoyi.sales.pojo.SalesLedgerFile; +import com.ruoyi.sales.pojo.SalesLedgerProduct; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FilenameUtils; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; /** * 閲囪喘鍙拌处Service涓氬姟灞傚鐞� @@ -18,10 +47,23 @@ * @date 2025-05-09 */ @Service -@AllArgsConstructor +@RequiredArgsConstructor +@Slf4j public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService { - private PurchaseLedgerMapper purchaseLedgerMapper; + private final PurchaseLedgerMapper purchaseLedgerMapper; + + private final SalesLedgerMapper salesLedgerMapper; + private final SalesLedgerProductMapper salesLedgerProductMapper; + + private final SysUserMapper userMapper; + + private final TempFileMapper tempFileMapper; + + private final SalesLedgerFileMapper salesLedgerFileMapper; + + @Value("${file.upload-dir}") + private String uploadDir; @Override public List<PurchaseLedger> selectPurchaseLedgerList(PurchaseLedger purchaseLedger) { @@ -29,11 +71,137 @@ } @Override - public int addOrEditPurchase(PurchaseLedger purchaseLedger) { + public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws IOException { + + SalesLedger salesLedger = salesLedgerMapper.selectById(purchaseLedgerDto.getSalesLedgerId()); + + if (salesLedger == null) { + throw new BaseException("閿�鍞彴璐︿笉瀛樺湪"); + } + SysUser sysUser = userMapper.selectUserById(purchaseLedgerDto.getRecorderId()); + + // DTO杞珽ntity + PurchaseLedger purchaseLedger = new PurchaseLedger(); + BeanUtils.copyProperties(purchaseLedgerDto, purchaseLedger); + purchaseLedger.setTenantId(salesLedger.getTenantId()); + purchaseLedger.setSalesContractNo(salesLedger.getSalesContractNo()); + purchaseLedger.setRecorderId(purchaseLedgerDto.getRecorderId()); + purchaseLedger.setRecorderName(sysUser.getNickName()); + + // 3. 鏂板鎴栨洿鏂颁富琛� if (purchaseLedger.getId() == null) { - return purchaseLedgerMapper.insert(purchaseLedger); + purchaseLedgerMapper.insert(purchaseLedger); } else { - return purchaseLedgerMapper.updateById(purchaseLedger); + purchaseLedgerMapper.updateById(purchaseLedger); + } + + // 4. 澶勭悊瀛愯〃鏁版嵁 + List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData(); + if (productList != null && !productList.isEmpty()) { + handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType()); + + } + + // 5. 杩佺Щ涓存椂鏂囦欢鍒版寮忕洰褰� + if (purchaseLedgerDto.getTempFileIds() != null && !purchaseLedgerDto.getTempFileIds().isEmpty()) { + migrateTempFilesToFormal(purchaseLedger.getId(), purchaseLedgerDto.getTempFileIds()); + } + + return 1; + } + + private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) { + // 鎸塈D鍒嗙粍锛屽尯鍒嗘柊澧炲拰鏇存柊鐨勮褰� + Map<Boolean, List<SalesLedgerProduct>> partitionedProducts = products.stream() + .peek(p -> p.setSalesLedgerId(salesLedgerId)) + .collect(Collectors.partitioningBy(p -> p.getId() != null)); + + List<SalesLedgerProduct> updateList = partitionedProducts.get(true); + List<SalesLedgerProduct> insertList = partitionedProducts.get(false); + + // 鎵ц鏇存柊鎿嶄綔 + if (!updateList.isEmpty()) { + for (SalesLedgerProduct product : updateList) { + product.setType(type); + salesLedgerProductMapper.updateById(product); + } + } + // 鎵ц鎻掑叆鎿嶄綔 + if (!insertList.isEmpty()) { + for (SalesLedgerProduct salesLedgerProduct : insertList) { + salesLedgerProduct.setType(type); + salesLedgerProductMapper.insert(salesLedgerProduct); + } + } + } + + /** + * 灏嗕复鏃舵枃浠惰縼绉诲埌姝e紡鐩綍 + * + * @param businessId 涓氬姟ID锛堥攢鍞彴璐D锛� + * @param tempFileIds 涓存椂鏂囦欢ID鍒楄〃 + * @throws IOException 鏂囦欢鎿嶄綔寮傚父 + */ + private void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds) throws IOException { + if (CollectionUtils.isEmpty(tempFileIds)) { + return; + } + + // 鏋勫缓姝e紡鐩綍璺緞锛堟寜涓氬姟绫诲瀷鍜屾棩鏈熷垎缁勶級 + String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE); + + Path formalDirPath = Paths.get(formalDir); + + // 纭繚姝e紡鐩綍瀛樺湪锛堥�掑綊鍒涘缓锛� + if (!Files.exists(formalDirPath)) { + Files.createDirectories(formalDirPath); + } + + for (String tempFileId : tempFileIds) { + // 鏌ヨ涓存椂鏂囦欢璁板綍 + TempFile tempFile = tempFileMapper.selectById(tempFileId); + if (tempFile == null) { + log.warn("涓存椂鏂囦欢涓嶅瓨鍦紝璺宠繃澶勭悊: {}", tempFileId); + continue; + } + + // 鏋勫缓姝e紡鏂囦欢鍚嶏紙鍖呭惈涓氬姟ID鍜屾椂闂存埑锛岄伩鍏嶅啿绐侊級 + String originalFilename = tempFile.getOriginalName(); + String fileExtension = FilenameUtils.getExtension(originalFilename); + String formalFilename = businessId + "_" + + System.currentTimeMillis() + "_" + + UUID.randomUUID().toString().substring(0, 8) + + (StringUtils.hasText(fileExtension) ? "." + fileExtension : ""); + + Path formalFilePath = formalDirPath.resolve(formalFilename); + + try { + // 鎵ц鏂囦欢杩佺Щ锛堜娇鐢ㄥ師瀛愭搷浣滅‘淇濆畨鍏ㄦ�э級 + Files.move( + Paths.get(tempFile.getTempPath()), + formalFilePath, + StandardCopyOption.REPLACE_EXISTING, + StandardCopyOption.ATOMIC_MOVE + ); + log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath); + + // 鏇存柊鏂囦欢璁板綍锛堝叧鑱斿埌涓氬姟ID锛� + SalesLedgerFile fileRecord = new SalesLedgerFile(); + fileRecord.setLedgerId(businessId); + fileRecord.setName(originalFilename); + fileRecord.setUrl(formalFilePath.toString()); + fileRecord.setCreateTime(LocalDateTime.now()); + salesLedgerFileMapper.insert(fileRecord); + + // 鍒犻櫎涓存椂鏂囦欢璁板綍 + tempFileMapper.deleteById(tempFile); + + log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath); + } catch (IOException e) { + log.error("鏂囦欢杩佺Щ澶辫触: {}", tempFile.getTempPath(), e); + // 鍙�夋嫨鍥炴粴浜嬪姟鎴栬褰曞け璐ユ枃浠� + throw new IOException("鏂囦欢杩佺Щ寮傚父", e); + } } } @@ -41,4 +209,34 @@ public int deletePurchaseLedgerByIds(Long[] ids) { return purchaseLedgerMapper.deleteBatchIds(Arrays.asList(ids)); } + + @Override + public PurchaseLedgerDto getPurchaseById(PurchaseLedgerDto purchaseLedgerDto) { + // 1. 鏌ヨ涓昏〃 + PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(purchaseLedgerDto.getId()); + if (purchaseLedger == null) { + throw new BaseException("閲囪喘鍙拌处涓嶅瓨鍦�"); + } + + // 2. 鏌ヨ瀛愯〃 + LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>(); + productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId()) + .eq(SalesLedgerProduct::getType, purchaseLedgerDto.getType()); + List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper); + + // 3.鏌ヨ涓婁紶鏂囦欢 + LambdaQueryWrapper<SalesLedgerFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>(); + salesLedgerFileWrapper.eq(SalesLedgerFile::getLedgerId, purchaseLedger.getId()); + List<SalesLedgerFile> salesLedgerFiles = salesLedgerFileMapper.selectList(salesLedgerFileWrapper); + + // 4. 杞崲 DTO + PurchaseLedgerDto resultDto = new PurchaseLedgerDto(); + BeanUtils.copyProperties(purchaseLedger, resultDto); + if (!products.isEmpty()) { + resultDto.setHasChildren(true); + resultDto.setProductData(products); + resultDto.setSalesLedgerFiles(salesLedgerFiles); + } + return resultDto; + } } -- Gitblit v1.9.3