2 天以前 111270df037596a04df97f787ca8b9199dd99866
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
102
package cn.iocoder.yudao.module.mdm.api.item;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemSyncReqDTO;
import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO;
import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO;
import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemBatchConfigMapper;
import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
/**
 * MDM 物料同步 API 实现类
 *
 * @author 芋道源码
 */
@RestController
@Validated
public class MdmItemSyncApiImpl implements MdmItemSyncApi {
 
    @Resource
    private MdmItemMapper itemMapper;
 
    @Resource
    private MdmItemBatchConfigMapper itemBatchConfigMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> syncItemToMdm(MdmItemSyncReqDTO syncReqDTO) {
        // 1. 按 code 查询是否已存在
        MdmItemDO existingItem = itemMapper.selectByCode(syncReqDTO.getCode());
 
        // 2. 保存或更新物料主数据
        MdmItemDO item = BeanUtils.toBean(syncReqDTO, MdmItemDO.class);
        Long itemId;
        if (existingItem != null) {
            // 更新
            item.setId(existingItem.getId());
            itemMapper.updateById(item);
            itemId = existingItem.getId();
        } else {
            // 新增
            itemMapper.insert(item);
            itemId = item.getId();
        }
 
        // 3. 同步批次属性配置
        syncBatchConfig(itemId, syncReqDTO);
 
        return success(itemId);
    }
 
    /**
     * 同步批次属性配置
     */
    private void syncBatchConfig(Long itemId, MdmItemSyncReqDTO syncReqDTO) {
        // 检查是否有批次配置数据
        if (syncReqDTO.getProduceDateFlag() == null
                && syncReqDTO.getExpireDateFlag() == null
                && syncReqDTO.getReceiptDateFlag() == null
                && syncReqDTO.getVendorFlag() == null
                && syncReqDTO.getPurchaseOrderCodeFlag() == null
                && syncReqDTO.getLotNumberFlag() == null
                && syncReqDTO.getQualityStatusFlag() == null) {
            return;
        }
 
        // 查询已有配置
        MdmItemBatchConfigDO existing = itemBatchConfigMapper.selectOne(MdmItemBatchConfigDO::getItemId, itemId);
 
        MdmItemBatchConfigDO config = new MdmItemBatchConfigDO();
        config.setItemId(itemId);
        config.setProduceDateFlag(Boolean.TRUE.equals(syncReqDTO.getProduceDateFlag()));
        config.setExpireDateFlag(Boolean.TRUE.equals(syncReqDTO.getExpireDateFlag()));
        config.setReceiptDateFlag(Boolean.TRUE.equals(syncReqDTO.getReceiptDateFlag()));
        config.setVendorFlag(Boolean.TRUE.equals(syncReqDTO.getVendorFlag()));
        config.setPurchaseOrderCodeFlag(Boolean.TRUE.equals(syncReqDTO.getPurchaseOrderCodeFlag()));
        config.setLotNumberFlag(Boolean.TRUE.equals(syncReqDTO.getLotNumberFlag()));
        config.setQualityStatusFlag(Boolean.TRUE.equals(syncReqDTO.getQualityStatusFlag()));
        // 清空不需要的字段
        config.setClientFlag(false);
        config.setSalesOrderCodeFlag(false);
        config.setWorkOrderFlag(false);
        config.setTaskFlag(false);
        config.setWorkstationFlag(false);
        config.setToolFlag(false);
        config.setMoldFlag(false);
 
        if (existing != null) {
            config.setId(existing.getId());
            itemBatchConfigMapper.updateById(config);
        } else {
            itemBatchConfigMapper.insert(config);
        }
    }
 
}