From a62b1426d01ccba34a9cd92c41e392db6fa59d90 Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期一, 30 三月 2026 09:40:41 +0800
Subject: [PATCH] 销售审批合同字段修改

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |  189 ++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 177 insertions(+), 12 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 46a5033..abcf72b 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -6,6 +6,13 @@
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.account.pojo.AccountExpense;
+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.IApproveProcessService;
+import com.ruoyi.approve.vo.ApproveProcessVO;
 import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
 import com.ruoyi.basic.mapper.SupplierManageMapper;
@@ -20,6 +27,8 @@
 import com.ruoyi.framework.security.LoginUser;
 import com.ruoyi.other.mapper.TempFileMapper;
 import com.ruoyi.other.pojo.TempFile;
+import com.ruoyi.procurementrecord.dto.SimplePP;
+import com.ruoyi.procurementrecord.service.ProcurementPriceManagementService;
 import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.mapper.SysUserMapper;
 import com.ruoyi.purchase.dto.PurchaseLedgerDto;
@@ -42,15 +51,18 @@
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 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.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
@@ -66,8 +78,9 @@
 @RequiredArgsConstructor
 @Slf4j
 public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService {
-
+    private final AccountExpenseService accountExpenseService;
     private final PurchaseLedgerMapper purchaseLedgerMapper;
+    private final IApproveProcessService approveProcessService;
 
     private final SalesLedgerMapper salesLedgerMapper;
     private final SalesLedgerProductMapper salesLedgerProductMapper;
@@ -92,6 +105,8 @@
 
     private final StringRedisTemplate redisTemplate;
 
+    private final ProcurementPriceManagementService procurementPriceManagementService;
+
     @Value("${file.upload-dir}")
     private String uploadDir;
 
@@ -103,8 +118,42 @@
         }
         return purchaseLedgerMapper.selectList(queryWrapper);
     }
-
     @Override
+    @Transactional(rollbackFor = Exception.class)
+    public int addPurchaseTemplate(PurchaseLedgerDto purchaseLedgerDto) {
+        //褰曞叆浜�
+        SysUser sysUser = userMapper.selectUserById(purchaseLedgerDto.getRecorderId());
+
+        SupplierManage supplierManage = supplierManageMapper.selectById(purchaseLedgerDto.getSupplierId());
+
+        PurchaseLedger purchaseLedger = new PurchaseLedger();
+        purchaseLedgerDto.setApprovalStatus(3);
+        BeanUtils.copyProperties(purchaseLedgerDto, purchaseLedger);
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        if(ObjectUtils.isNotEmpty(loginUser) && null != loginUser.getTenantId()) {
+            purchaseLedger.setTenantId(loginUser.getTenantId());
+        }
+        purchaseLedger.setSupplierName(supplierManage.getSupplierName());
+        purchaseLedger.setRecorderName(sysUser.getNickName());
+        purchaseLedger.setPhoneNumber(sysUser.getPhonenumber());
+        purchaseLedger.setPurchaseContractNumber("11111");
+        purchaseLedger.setEntryDate(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()));
+        purchaseLedgerMapper.insert(purchaseLedger);
+
+        // 4. 澶勭悊瀛愯〃鏁版嵁
+        List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData();
+        if (productList != null && !productList.isEmpty()) {
+            productList.forEach(product -> {
+                product.setSalesLedgerId(purchaseLedger.getId());
+                product.setType(purchaseLedgerDto.getType());
+            });
+            productList.forEach(salesLedgerProductMapper::insert);
+        }
+
+        return 1;
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class)
     public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws IOException {
 
         SalesLedger salesLedger = salesLedgerMapper.selectById(purchaseLedgerDto.getSalesLedgerId());
@@ -125,12 +174,41 @@
         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);
+                });
+            }
+
         }
 
         // 4. 澶勭悊瀛愯〃鏁版嵁
@@ -144,6 +222,26 @@
             migrateTempFilesToFormal(purchaseLedger.getId(), purchaseLedgerDto.getTempFileIds());
         }
 
+        //6.閲囪喘闇�瑕佸鎵�
+        //鏂板瀹℃壒鏁版嵁
+        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
+        approveProcessVO.setApproveDeptId(purchaseLedger.getTenantId());
+        //瀹℃壒鐞嗙敱鏄噰璐悎鍚屽彿鐢ㄨ繖涓潵鍏宠仈
+        approveProcessVO.setApproveReason(purchaseLedger.getPurchaseContractNumber());
+        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        approveProcessVO.setApproveTime(LocalDate.now().format(dateFormat));
+        //5鏄噰璐敵璇�
+        approveProcessVO.setApproveType(5);
+        //褰曞叆浜�=鐢宠浜�
+        approveProcessVO.setApproveUser(purchaseLedger.getRecorderId());
+        //瀹℃壒浜�
+        approveProcessVO.setApproveUserIds(purchaseLedgerDto.getApproveUserIds());
+        try {
+            approveProcessService.addApprove(approveProcessVO);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
         return 1;
     }
 
@@ -151,7 +249,7 @@
         if (products == null || products.isEmpty()) {
             throw new BaseException("浜у搧淇℃伅涓嶅瓨鍦�");
         }
-
+        PurchaseLedger ledger = purchaseLedgerMapper.selectById(salesLedgerId);
         // 鎻愬墠鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑ID
         Set<Long> productIds = products.stream()
                 .map(SalesLedgerProduct::getProductId)
@@ -197,7 +295,7 @@
 
         List<SalesLedgerProduct> updateList = partitionedProducts.get(true);
         List<SalesLedgerProduct> insertList = partitionedProducts.get(false);
-        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerId);
+        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(salesLedgerId);
 
         // 鎵ц鏇存柊鎿嶄綔
         if (!updateList.isEmpty()) {
@@ -210,10 +308,35 @@
         if (!insertList.isEmpty()) {
             for (SalesLedgerProduct salesLedgerProduct : insertList) {
                 salesLedgerProduct.setType(type);
-                salesLedgerProduct.setRegisterDate(salesLedger.getEntryDate().toString());
+                Date entryDate = purchaseLedger.getEntryDate();
+
+                LocalDateTime localDateTime = entryDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
+                salesLedgerProduct.setRegisterDate(localDateTime);
                 salesLedgerProductMapper.insert(salesLedgerProduct);
             }
         }
+
+        updateList.addAll(insertList);
+        updateList.forEach(it->{
+            SimplePP simplePP = new SimplePP();
+            simplePP.setProductId(it.getProductId());
+            simplePP.setSupplierName(ledger.getSupplierName());
+            simplePP.setSupplierId(ledger.getSupplierId());
+            simplePP.setUnit(it.getUnit());
+            simplePP.setProductName(it.getProductCategory());
+            simplePP.setSpecification(it.getSpecificationModel());
+            simplePP.setFinalPrice(it.getTaxInclusiveUnitPrice().divide(
+                    BigDecimal.ONE.add(it.getTaxRate().divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP)),
+                    2,  // 淇濈暀4浣嶏紙鍙牴鎹笟鍔¤皟鏁达級
+                    RoundingMode.HALF_UP
+            ));
+            simplePP.setRemark(
+                    "绯荤粺鏍规嵁閲囪喘鎻愪氦鑷姩鐢熸垚銆傞噰璐悎鍚屽彿涓�:" + purchaseLedger.getPurchaseContractNumber() + "銆�"
+            );
+            procurementPriceManagementService.autoCreateRecord(simplePP);
+        });
+
+
 
         // 璁$畻鎬诲惈绋庨噾棰�
         BigDecimal totalTaxInclusiveAmount = products.stream()
@@ -270,12 +393,15 @@
 
             try {
                 // 鎵ц鏂囦欢杩佺Щ锛堜娇鐢ㄥ師瀛愭搷浣滅‘淇濆畨鍏ㄦ�э級
-                Files.move(
-                        Paths.get(tempFile.getTempPath()),
-                        formalFilePath,
-                        StandardCopyOption.REPLACE_EXISTING,
-                        StandardCopyOption.ATOMIC_MOVE
-                );
+//                Files.move(
+//                        Paths.get(tempFile.getTempPath()),
+//                        formalFilePath,
+//                        StandardCopyOption.REPLACE_EXISTING,
+//                        StandardCopyOption.ATOMIC_MOVE
+//                );
+                // 鍘熷瓙绉诲姩澶辫触锛屼娇鐢ㄥ鍒�+鍒犻櫎
+                Files.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING);
+                Files.deleteIfExists(Paths.get(tempFile.getTempPath()));
                 log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath);
 
                 // 鏇存柊鏂囦欢璁板綍锛堝叧鑱斿埌涓氬姟ID锛�
@@ -317,6 +443,23 @@
         LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
         productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId,ids);
         productRecordMapper.delete(productRecordLambdaQueryWrapper);
+        // 鍒犻櫎鍏宠仈鐨勫鎵硅〃
+        List<String> strings = purchaseLedgerMapper.selectBatchIds(Arrays.asList(ids)).stream()
+                .map(PurchaseLedger::getPurchaseContractNumber)
+                .distinct()
+                .collect(Collectors.toList());
+        List<ApproveProcess> list = approveProcessService.list(Wrappers.<ApproveProcess>lambdaQuery().eq(ApproveProcess::getApproveType, 5)                                         .in(ApproveProcess::getApproveReason, strings));
+        if (CollectionUtils.isNotEmpty(list)){
+            List<String> approveIds = list.stream()
+                    .map(ApproveProcess::getApproveId)
+                    .distinct()
+                    .collect(Collectors.toList());
+            Long[] ides = approveIds.stream()
+                    .filter(s -> s != null && !s.isEmpty())
+                    .map(Long::valueOf)
+                    .toArray(Long[]::new);
+            approveProcessService.delApprove(ides);
+        }
         // 鎵归噺鍒犻櫎閲囪喘鍙拌处
         return purchaseLedgerMapper.deleteBatchIds(Arrays.asList(ids));
     }
@@ -337,7 +480,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
@@ -399,6 +543,27 @@
     }
 
     @Override
+    public PurchaseLedgerDto getPurchaseByCode(PurchaseLedgerDto purchaseLedgerDto) {
+        // 1. 鏌ヨ涓昏〃
+        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>().eq(PurchaseLedger::getPurchaseContractNumber, purchaseLedgerDto.getPurchaseContractNumber())            .last("LIMIT 1"));
+        if (purchaseLedger == null) {
+            throw new BaseException("閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        // 2. 鏌ヨ瀛愯〃
+        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
+        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId()).eq(SalesLedgerProduct::getType, 2);
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
+        // 4. 杞崲 DTO
+        PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
+        BeanUtils.copyProperties(purchaseLedger, resultDto);
+        if (!products.isEmpty()) {
+            resultDto.setHasChildren(true);
+            resultDto.setProductData(products);
+        }
+        return resultDto;
+    }
+
+    @Override
     public List getPurchasesNo() {
         LambdaQueryWrapper<PurchaseLedger> queryWrapper = Wrappers.lambdaQuery();
         queryWrapper.select(PurchaseLedger::getId, PurchaseLedger::getPurchaseContractNumber, PurchaseLedger::getSupplierId);

--
Gitblit v1.9.3