From 49e1bc66ebaf696ebd3fc3ed33d65c8795fd3cde Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期二, 01 七月 2025 18:05:12 +0800
Subject: [PATCH] 1.巡检定时任务 2.设备管理 3.文档查询

---
 main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java |  107 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 96 insertions(+), 11 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java
index 2e87119..bbae14c 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java
@@ -1,19 +1,32 @@
 package com.ruoyi.business.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 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.basic.entity.CoalInfo;
+import com.ruoyi.basic.entity.Supply;
+import com.ruoyi.basic.mapper.CoalInfoMapper;
+import com.ruoyi.basic.mapper.SupplyMapper;
 import com.ruoyi.business.dto.PurchaseRegistrationDto;
+import com.ruoyi.business.entity.PendingInventory;
 import com.ruoyi.business.entity.PurchaseRegistration;
+import com.ruoyi.business.mapper.PendingInventoryMapper;
 import com.ruoyi.business.mapper.PurchaseRegistrationMapper;
 import com.ruoyi.business.service.PurchaseRegistrationService;
+import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
 
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.List;
 import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -30,35 +43,107 @@
 
     private final PurchaseRegistrationMapper purchaseRegistrationMapper;
 
+    private final PendingInventoryMapper pendingInventoryMapper;
+
+    private final CoalInfoMapper coalInfoMapper;
+
+    private final SupplyMapper supplyMapper;
+
     @Override
-    public IPage<PurchaseRegistration> selectPurchaseRegistrationList(Page page, PurchaseRegistrationDto purchaseRegistrationDto) {
+    public IPage<PurchaseRegistration> selectPurchaseRegistrationList(Page<PurchaseRegistration> page, PurchaseRegistrationDto dto) {
         LambdaQueryWrapper<PurchaseRegistration> queryWrapper = new LambdaQueryWrapper<>();
+
+        String keyword = dto.getSearchAll();
+        if (StringUtils.isNotBlank(keyword)) {
+            // 鏌ヨ鐓ょ鍚嶇О涓ā绯婂尮閰嶇殑coalId鍒楄〃
+            List<Long> matchedCoalIds = coalInfoMapper.selectList(
+                            new LambdaQueryWrapper<CoalInfo>().like(CoalInfo::getCoal, keyword)
+                    ).stream()
+                    .map(CoalInfo::getId)
+                    .collect(Collectors.toList());
+
+            // 缁勮鏌ヨ鏉′欢锛氱叅绉岻D鍦ㄥ尮閰嶇殑鍒楄〃涓�
+            queryWrapper.and(w -> {
+                if (!matchedCoalIds.isEmpty()) {
+                    w.in(PurchaseRegistration::getCoalId, matchedCoalIds).or();
+                }
+                w.like(PurchaseRegistration::getSupplierName, keyword);
+            });
+        }
+
         queryWrapper.orderByDesc(PurchaseRegistration::getCreateTime);
+
         return purchaseRegistrationMapper.selectPage(page, queryWrapper);
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int addOrEditPR(PurchaseRegistrationDto purchaseRegistrationDto) {
+        // 鍙傛暟鏍¢獙
+        Assert.notNull(purchaseRegistrationDto, "閲囪喘鐧昏淇℃伅涓嶈兘涓虹┖");
+
+        // 鍒涘缓閲囪喘鐧昏瀹炰綋骞跺鍒跺睘鎬�
         PurchaseRegistration purchaseRegistration = new PurchaseRegistration();
-        BeanUtils.copyProperties(purchaseRegistrationDto,purchaseRegistration);
+        BeanUtils.copyProperties(purchaseRegistrationDto, purchaseRegistration);
+        CoalInfo coalInfo = coalInfoMapper.selectById(purchaseRegistrationDto.getCoalId());
+        if (coalInfo == null) {
+            throw new BaseException("鐓ょ淇℃伅涓嶅瓨鍦�");
+        }
+        Supply supply = supplyMapper.selectById(purchaseRegistrationDto.getSupplierId());
+        if (supply == null) {
+            throw new BaseException("渚涘簲鍟嗕俊鎭笉瀛樺湪");
+        }
+        purchaseRegistration.setSupplierName(supply.getSupplierName());
         if (Objects.isNull(purchaseRegistrationDto.getId())) {
-            return purchaseRegistrationMapper.insert(purchaseRegistration);
+            // 鏂板閲囪喘鐧昏
+            purchaseRegistration.setRegistrationDate(LocalDate.now());
+            int insertCount = purchaseRegistrationMapper.insert(purchaseRegistration);
+            if (insertCount > 0) {
+                // 閲囪喘鐧昏鎴愬姛锛屽悓姝ュ垱寤哄緟鍏ュ簱璁板綍
+                PendingInventory pendingInventory = createPendingInventory(purchaseRegistration);
+                pendingInventory.setSupplierName(supply.getSupplierName());
+                return pendingInventoryMapper.insert(pendingInventory);
+            }
+            return insertCount;
         } else {
+            // 鏇存柊閲囪喘鐧昏
             return purchaseRegistrationMapper.updateById(purchaseRegistration);
         }
     }
 
+    /**
+     * 鏍规嵁閲囪喘鐧昏淇℃伅鍒涘缓寰呭叆搴撹褰�
+     *
+     * @param purchaseRegistration 閲囪喘鐧昏瀹炰綋
+     * @return 寰呭叆搴撳疄浣�
+     */
+    private PendingInventory createPendingInventory(PurchaseRegistration purchaseRegistration) {
+        PendingInventory pendingInventory = new PendingInventory();
+        // 澶嶅埗鍩烘湰灞炴��
+        BeanUtils.copyProperties(purchaseRegistration, pendingInventory);
+
+        // 璁剧疆寰呭叆搴撹褰曠壒鏈夌殑灞炴�э紙濡傛灉鏈夛級
+        pendingInventory.setId(null);
+        pendingInventory.setPurchaseId(purchaseRegistration.getId());
+        pendingInventory.setCoalId(purchaseRegistration.getCoalId());
+        pendingInventory.setInventoryQuantity(purchaseRegistration.getPurchaseQuantity());
+        return pendingInventory;
+    }
+
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int delByIds(Long[] ids) {
-        // 妫�鏌ュ弬鏁�
         if (ids == null || ids.length == 0) {
             return 0;
         }
-        // 鏋勯�犳洿鏂版潯浠�
-        UpdateWrapper<PurchaseRegistration> updateWrapper = new UpdateWrapper<>();
-        updateWrapper.in("id", ids)
-                .set("deleted", 1);  // 璁剧疆 deleted 涓� 1 琛ㄧず宸插垹闄�
-        // 鎵ц鎵归噺閫昏緫鍒犻櫎
-        return purchaseRegistrationMapper.update(null, updateWrapper);
+
+        // 1. 鍒犻櫎鍏宠仈鐨凱endingInventory璁板綍
+        LambdaQueryWrapper<PendingInventory> wrapper = new LambdaQueryWrapper<>();
+        wrapper.in(PendingInventory::getPurchaseId, Arrays.asList(ids));
+        pendingInventoryMapper.delete(wrapper);  // 鏀逛负delete鎿嶄綔
+
+        // 2. 鎵归噺鍒犻櫎閲囪喘娉ㄥ唽璁板綍
+        return purchaseRegistrationMapper.deleteByIds(Arrays.asList(ids));
     }
+
 }

--
Gitblit v1.9.3