From da78c6cd6cfa63c09fc8b41c18af6f06393847aa Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期一, 26 一月 2026 15:53:53 +0800
Subject: [PATCH] yys 修改采购审核模块

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |  191 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 144 insertions(+), 47 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 8ddfe13..c4fef55 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -1,6 +1,7 @@
 package com.ruoyi.purchase.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
@@ -10,6 +11,9 @@
 import com.ruoyi.account.pojo.AccountIncome;
 import com.ruoyi.account.service.AccountExpenseService;
 import com.ruoyi.account.service.AccountIncomeService;
+import com.ruoyi.approve.pojo.ApproveProcess;
+import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
+import com.ruoyi.approve.vo.ApproveProcessVO;
 import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
 import com.ruoyi.basic.mapper.SupplierManageMapper;
@@ -24,16 +28,16 @@
 import com.ruoyi.framework.security.LoginUser;
 import com.ruoyi.other.mapper.TempFileMapper;
 import com.ruoyi.other.pojo.TempFile;
+import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
+import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage;
 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.ProductRecordMapper;
-import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
-import com.ruoyi.purchase.mapper.TicketRegistrationMapper;
-import com.ruoyi.purchase.pojo.ProductRecord;
-import com.ruoyi.purchase.pojo.PurchaseLedger;
-import com.ruoyi.purchase.pojo.TicketRegistration;
+import com.ruoyi.purchase.mapper.*;
+import com.ruoyi.purchase.pojo.*;
 import com.ruoyi.purchase.service.IPurchaseLedgerService;
+import com.ruoyi.quality.mapper.*;
+import com.ruoyi.quality.pojo.*;
 import com.ruoyi.sales.mapper.*;
 import com.ruoyi.sales.pojo.CommonFile;
 import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
@@ -94,10 +98,24 @@
 
     private final ProductRecordMapper productRecordMapper;
 
+    private final PaymentRegistrationMapper paymentRegistrationMapper;
+
     private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
 
     private final StringRedisTemplate redisTemplate;
+    private final QualityInspectMapper qualityInspectMapper;
+    private final QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
+    private final QualityTestStandardParamMapper qualityTestStandardParamMapper;
+    private final QualityTestStandardMapper qualityTestStandardMapper;
+    private final QualityInspectParamMapper qualityInspectParamMapper;
 
+    private final ApproveProcessServiceImpl approveProcessService;
+
+    private final ProcurementRecordMapper procurementRecordStorageMapper;
+
+    private final PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
+
+    private final SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
     @Value("${file.upload-dir}")
     private String uploadDir;
 
@@ -112,7 +130,7 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws IOException {
+    public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws Exception {
 
         SalesLedger salesLedger = salesLedgerMapper.selectById(purchaseLedgerDto.getSalesLedgerId());
         //褰曞叆浜�
@@ -124,63 +142,86 @@
         PurchaseLedger purchaseLedger = new PurchaseLedger();
         BeanUtils.copyProperties(purchaseLedgerDto, purchaseLedger);
         LoginUser loginUser = SecurityUtils.getLoginUser();
-        if(ObjectUtils.isNotEmpty(loginUser) && null != loginUser.getTenantId()) {
+        if (ObjectUtils.isNotEmpty(loginUser) && null != loginUser.getTenantId()) {
             purchaseLedger.setTenantId(loginUser.getTenantId());
         }
-        purchaseLedger.setSalesContractNo(ObjectUtils.isNotEmpty(salesLedger) ? salesLedger.getSalesContractNo() : null);
+        purchaseLedger.setSalesContractNo(ObjectUtils.isNotEmpty(salesLedger) ? salesLedger.getSalesContractNo() : "");
+        purchaseLedger.setSalesLedgerId(ObjectUtils.isNotEmpty(salesLedger) ? salesLedger.getId() : -1);
         purchaseLedger.setSupplierName(supplierManage.getSupplierName());
         purchaseLedger.setRecorderId(purchaseLedgerDto.getRecorderId());
         purchaseLedger.setRecorderName(sysUser.getNickName());
         purchaseLedger.setPhoneNumber(sysUser.getPhonenumber());
-        // 2. 澶勭悊璐︽埛鏀跺叆
-        AccountExpense accountExpense = new AccountExpense();
-        accountExpense.setExpenseDate(purchaseLedger.getEntryDate());
-        accountExpense.setExpenseType("0");
-        accountExpense.setSupplierName(purchaseLedger.getSupplierName());
-        accountExpense.setExpenseMoney(purchaseLedger.getContractAmount());
-        accountExpense.setExpenseDescribed("閲囪喘鍚堝悓锛�" + purchaseLedger.getPurchaseContractNumber());
-        accountExpense.setExpenseMethod("0");
-        accountExpense.setInvoiceNumber(purchaseLedger.getPurchaseContractNumber());
-        accountExpense.setInputTime(new Date());
-        accountExpense.setInputUser(loginUser.getNickName());
-
-
         // 3. 鏂板鎴栨洿鏂颁富琛�
         if (purchaseLedger.getId() == null) {
             purchaseLedgerMapper.insert(purchaseLedger);
-//            accountIncomeService.save(accountIncome);
-            accountExpenseService.save(accountExpense);
         } else {
-            purchaseLedgerMapper.updateById(purchaseLedger);
-            PurchaseLedger purchaseLedgerDB = purchaseLedgerMapper.selectById(purchaseLedger.getId());
-            List<AccountExpense> accountExpenseDBs = accountExpenseService.getByInvoiceNumberList(purchaseLedger.getPurchaseContractNumber());
-            if (!CollectionUtils.isEmpty(accountExpenseDBs)) {
-                accountExpenseDBs.forEach(accountExpenseDB -> {
-                   accountExpenseDB.setExpenseDate(purchaseLedgerDB.getEntryDate());
-                   accountExpenseDB.setExpenseType("0");
-                   accountExpenseDB.setSupplierName(purchaseLedgerDB.getSupplierName());
-                   accountExpenseDB.setExpenseMoney(purchaseLedgerDB.getContractAmount());
-                   accountExpenseDB.setExpenseDescribed("閲囪喘鍚堝悓锛�" + purchaseLedgerDB.getPurchaseContractNumber());
-                   accountExpenseDB.setExpenseMethod("0");
-                   accountExpenseDB.setInvoiceNumber(purchaseLedgerDB.getPurchaseContractNumber());
-                   accountExpenseService.updateById(accountExpenseDB);
-               });
+            // 鍒犻櫎閲囪喘瀹℃壒锛岄噸鏂版彁浜�
+            ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                    .eq(ApproveProcess::getApproveType, 5)
+                    .eq(ApproveProcess::getApproveReason, purchaseLedger.getPurchaseContractNumber())
+                    .eq(ApproveProcess::getApproveDelete, 0)
+                    .last("limit 1"));
+            if (one != null) {
+                approveProcessService.delByIds(Collections.singletonList(one.getId()));
             }
-
+            purchaseLedgerMapper.updateById(purchaseLedger);
         }
-
+        // 6.閲囪喘瀹℃壒鏂板
+        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
+        approveProcessVO.setApproveType(5);
+        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
+        approveProcessVO.setApproveReason(purchaseLedger.getPurchaseContractNumber());
+        approveProcessVO.setApproveUserIds(purchaseLedgerDto.getApproveUserIds());
+        approveProcessVO.setApproveUser(loginUser.getUserId());
+        approveProcessVO.setApproveTime(LocalDate.now().toString());
+        approveProcessService.addApprove(approveProcessVO);
         // 4. 澶勭悊瀛愯〃鏁版嵁
         List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData();
         if (productList != null && !productList.isEmpty()) {
             handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType());
         }
-
+        //鏂板鍘熸潗鏂欐楠�
+        if (productList != null) {
+            for (SalesLedgerProduct saleProduct : productList) {
+                //鏄惁鎺ㄩ�佽川妫�锛屽鏋渢rue灏辨坊鍔�
+                if (saleProduct.getIsChecked()) {
+                    addQualityInspect(purchaseLedger, saleProduct);
+                }
+            }
+        }
         // 5. 杩佺Щ涓存椂鏂囦欢鍒版寮忕洰褰�
         if (purchaseLedgerDto.getTempFileIds() != null && !purchaseLedgerDto.getTempFileIds().isEmpty()) {
             migrateTempFilesToFormal(purchaseLedger.getId(), purchaseLedgerDto.getTempFileIds());
         }
-
         return 1;
+    }
+
+    private void addQualityInspect(PurchaseLedger purchaseLedger, SalesLedgerProduct saleProduct) {
+        QualityInspect qualityInspect = new QualityInspect();
+        qualityInspect.setInspectType(0);
+        qualityInspect.setSupplier(purchaseLedger.getSupplierName());
+        qualityInspect.setPurchaseLedgerId(purchaseLedger.getId());
+        qualityInspect.setProductId(saleProduct.getProductId());
+        qualityInspect.setProductName(saleProduct.getProductCategory());
+        qualityInspect.setModel(saleProduct.getSpecificationModel());
+        qualityInspect.setProductModelId(saleProduct.getProductModelId());
+        qualityInspect.setUnit(saleProduct.getUnit());
+        qualityInspect.setQuantity(saleProduct.getQuantity());
+        qualityInspectMapper.insert(qualityInspect);
+        List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0,null);
+        if (qualityTestStandard.size()>0){
+            qualityInspect.setTestStandardId(qualityTestStandard.get(0).getId());
+            qualityInspectMapper.updateById(qualityInspect);
+            qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery()
+                    .eq(QualityTestStandardParam::getTestStandardId,qualityTestStandard.get(0).getId()))
+                    .forEach(qualityTestStandardParam -> {
+                        QualityInspectParam param = new QualityInspectParam();
+                        com.ruoyi.common.utils.bean.BeanUtils.copyProperties(qualityTestStandardParam, param);
+                        param.setId(null);
+                        param.setInspectId(qualityInspect.getId());
+                        qualityInspectParamMapper.insert(param);
+                    });
+        }
     }
 
     private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) {
@@ -250,6 +291,9 @@
 
                 LocalDateTime localDateTime = entryDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                 salesLedgerProduct.setRegisterDate(localDateTime);
+                salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
+                salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
+                salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice());
                 salesLedgerProductMapper.insert(salesLedgerProduct);
             }
         }
@@ -342,9 +386,23 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int deletePurchaseLedgerByIds(Long[] ids) {
         if (ids == null || ids.length == 0) {
            throw new BaseException("璇烽�変腑鑷冲皯涓�鏉℃暟鎹�");
+        }
+        // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐叆搴撹褰�
+        LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new LambdaQueryWrapper<>();
+        salesLedgerProductQueryWrapper.in(SalesLedgerProduct::getSalesLedgerId, ids)
+                .eq(SalesLedgerProduct::getType, 2);
+        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductQueryWrapper);
+        if (CollectionUtils.isNotEmpty(salesLedgerProducts)) {
+            salesLedgerProducts.stream().forEach(salesLedgerProduct -> {
+                // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︿骇鍝�
+                LambdaQueryWrapper<ProcurementRecordStorage> queryWrapper = new LambdaQueryWrapper<>();
+                queryWrapper.eq(ProcurementRecordStorage::getSalesLedgerProductId, salesLedgerProduct.getId());
+                procurementRecordStorageMapper.delete(queryWrapper);
+            });
         }
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︿骇鍝�
         LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
@@ -353,12 +411,50 @@
         salesLedgerProductMapper.delete(queryWrapper);
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︾殑鏉ョエ鐧昏
         LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getSalesLedgerId,ids);
+        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getPurchaseLedgerId,ids);
         ticketRegistrationMapper.delete(ticketRegistrationLambdaQueryWrapper);
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︾殑鏉ョエ鐧昏璁板綍
         LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
         productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId,ids);
         productRecordMapper.delete(productRecordLambdaQueryWrapper);
+        // 鎵归噺鍒犻櫎浠樻鐧昏
+        LambdaQueryWrapper<PaymentRegistration> paymentRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        paymentRegistrationLambdaQueryWrapper.in(PaymentRegistration::getPurchaseLedgerId, ids);
+        paymentRegistrationMapper.delete(paymentRegistrationLambdaQueryWrapper);
+        //鎵归噺鍒犻櫎妫�楠屾爣鍑�
+        LambdaQueryWrapper<QualityInspect> materialInspectLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        materialInspectLambdaQueryWrapper.in(QualityInspect::getPurchaseLedgerId, ids);
+        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(materialInspectLambdaQueryWrapper);
+        qualityInspects.stream().forEach(qualityInspect -> {
+            if (ObjectUtils.isNotEmpty(qualityInspect.getInspectState())&&qualityInspect.getInspectState().equals(1)) {
+                throw new BaseException("宸叉彁浜ょ殑妫�楠屽崟涓嶈兘鍒犻櫎");
+            }
+        });
+        List<Long> inspectIds = qualityInspects.stream()
+                .map(QualityInspect::getId)
+                .collect(Collectors.toList());
+
+        if (inspectIds.size() > 0) {
+            LambdaQueryWrapper<QualityInspectParam> qualityStandardLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            qualityStandardLambdaQueryWrapper.in(QualityInspectParam::getInspectId, inspectIds);
+            qualityInspectParamMapper.delete(qualityStandardLambdaQueryWrapper);
+        }
+        // 鍒犻櫎閲囪喘瀹℃壒璁板綍
+        for (Long id : ids) {
+            PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
+            if(purchaseLedger != null){
+                ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                        .eq(ApproveProcess::getApproveType, 5)
+                        .eq(ApproveProcess::getApproveDelete, 0)
+                        .eq(ApproveProcess::getApproveReason, purchaseLedger.getPurchaseContractNumber())
+                        .last("limit 1"));
+                if (one != null) {
+                    approveProcessService.delByIds(Collections.singletonList(one.getId()));
+                }
+            }
+        }
+        //鎵归噺鍒犻櫎鍘熸潗鏂欐楠屾暟鎹�
+        qualityInspectMapper.delete(materialInspectLambdaQueryWrapper);
         // 鎵归噺鍒犻櫎閲囪喘鍙拌处
         return purchaseLedgerMapper.deleteBatchIds(Arrays.asList(ids));
     }
@@ -379,7 +475,8 @@
 
         // 3.鏌ヨ涓婁紶鏂囦欢
         LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
-        salesLedgerFileWrapper.eq(CommonFile::getCommonId, purchaseLedger.getId());
+        salesLedgerFileWrapper.eq(CommonFile::getCommonId, purchaseLedger.getId())
+                .eq(CommonFile::getType,FileNameType.PURCHASE.getValue());
         List<CommonFile> salesLedgerFiles = commonFileMapper.selectList(salesLedgerFileWrapper);
 
         // 4. 杞崲 DTO
@@ -475,7 +572,7 @@
     public IPage<PurchaseLedgerDto> selectPurchaseLedgerListPage(IPage ipage, PurchaseLedgerDto purchaseLedger) {
         IPage<PurchaseLedgerDto> purchaseLedgerDtoIPage = purchaseLedgerMapper.selectPurchaseLedgerListPage(ipage, purchaseLedger);
         purchaseLedgerDtoIPage.getRecords().forEach(purchaseLedgerDto -> {
-            List<CommonFile> commonFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, purchaseLedgerDto.getId()).eq(CommonFile::getType, FileNameType.PURCHASELEDGER.getValue()));
+            List<CommonFile> commonFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, purchaseLedgerDto.getId()).eq(CommonFile::getType, FileNameType.PURCHASE.getValue()));
             purchaseLedgerDto.setSalesLedgerFiles(commonFiles);
         });
         return purchaseLedgerDtoIPage;
@@ -484,8 +581,8 @@
     @Override
     public List<InvoiceRegistrationProduct> getProductBySalesNo(Long id) {
         List<InvoiceRegistrationProduct> invoiceRegistrationProducts = invoiceRegistrationProductMapper.selectList(new LambdaQueryWrapper<InvoiceRegistrationProduct>()
-                        .select(InvoiceRegistrationProduct::getId, InvoiceRegistrationProduct::getProductCategory, InvoiceRegistrationProduct::getSpecificationModel,
-                                InvoiceRegistrationProduct::getUnit, InvoiceRegistrationProduct::getQuantity)
+                .select(InvoiceRegistrationProduct::getId, InvoiceRegistrationProduct::getProductCategory, InvoiceRegistrationProduct::getSpecificationModel,
+                        InvoiceRegistrationProduct::getUnit, InvoiceRegistrationProduct::getQuantity)
                 .eq(InvoiceRegistrationProduct::getSalesLedgerId, id));
         if (invoiceRegistrationProducts.isEmpty()) {
             return new ArrayList<>();

--
Gitblit v1.9.3