liding
3 天以前 49e1bc66ebaf696ebd3fc3ed33d65c8795fd3cde
main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java
@@ -1,24 +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.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
 * <p>
@@ -37,10 +45,34 @@
    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());
            // 组装查询条件:煤种ID在匹配的列表中
            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);
    }
@@ -53,12 +85,23 @@
        // 创建采购登记实体并复制属性
        PurchaseRegistration purchaseRegistration = new 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())) {
            // 新增采购登记
            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;
@@ -70,6 +113,7 @@
    /**
     * 根据采购登记信息创建待入库记录
     *
     * @param purchaseRegistration 采购登记实体
     * @return 待入库实体
     */
@@ -79,22 +123,27 @@
        BeanUtils.copyProperties(purchaseRegistration, pendingInventory);
        // 设置待入库记录特有的属性(如果有)
         pendingInventory.setInventoryQuantity(purchaseRegistration.getPurchaseQuantity());
         pendingInventory.setCostPerUnit(new BigDecimal(22));
        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. 删除关联的PendingInventory记录
        LambdaQueryWrapper<PendingInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PendingInventory::getPurchaseId, Arrays.asList(ids));
        pendingInventoryMapper.delete(wrapper);  // 改为delete操作
        // 2. 批量删除采购注册记录
        return purchaseRegistrationMapper.deleteByIds(Arrays.asList(ids));
    }
}