liding
2 天以前 388bd216d4eb70b367ada95118d1087b45f07ae3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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.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>
 * 采购登记表 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-06-03
 */
@Service
@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);
    }
}