liding
3 天以前 388bd216d4eb70b367ada95118d1087b45f07ae3
main-business/src/main/java/com/ruoyi/business/service/impl/PurchaseRegistrationServiceImpl.java
@@ -1,11 +1,23 @@
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.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
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.util.Objects;
/**
 * <p>
@@ -19,4 +31,71 @@
@RequiredArgsConstructor
public class PurchaseRegistrationServiceImpl extends ServiceImpl<PurchaseRegistrationMapper, PurchaseRegistration> implements PurchaseRegistrationService {
    private final PurchaseRegistrationMapper purchaseRegistrationMapper;
    private final PendingInventoryMapper pendingInventoryMapper;
    @Override
    public IPage<PurchaseRegistration> selectPurchaseRegistrationList(Page page, PurchaseRegistrationDto purchaseRegistrationDto) {
        LambdaQueryWrapper<PurchaseRegistration> queryWrapper = new LambdaQueryWrapper<>();
        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);
        if (Objects.isNull(purchaseRegistrationDto.getId())) {
            // 新增采购登记
            int insertCount = purchaseRegistrationMapper.insert(purchaseRegistration);
            if (insertCount > 0) {
                // 采购登记成功,同步创建待入库记录
                PendingInventory pendingInventory = createPendingInventory(purchaseRegistration);
                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.setStatus(InventoryStatus.PENDING);
        // pendingInventory.setCreateTime(LocalDateTime.now());
        return pendingInventory;
    }
    @Override
    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);
    }
}