9 天以前 67c5d3ea86cfaad45c0150b96949dbcb6729b4d0
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
package cn.iocoder.yudao.module.mes.service.md.item;
 
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.batchconfig.MesMdItemBatchConfigSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemBatchConfigDO;
import cn.iocoder.yudao.module.mes.dal.mysql.md.item.MesMdItemBatchConfigMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MD_ITEM_BATCH_CONFIG_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MD_ITEM_NOT_EXISTS;
 
/**
 * MES 物料批次属性配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesMdItemBatchConfigServiceImpl implements MesMdItemBatchConfigService {
 
    @Resource
    private MesMdItemBatchConfigMapper itemBatchConfigMapper;
 
    @Resource
    @Lazy // 避免循环依赖(MesMdItemServiceImpl 也会注入本 Service)
    private MesMdItemService itemService;
 
    @Override
    public MesMdItemBatchConfigDO getItemBatchConfigByItemId(Long itemId) {
        return itemBatchConfigMapper.selectByItemId(itemId);
    }
 
    @Override
    public MesMdItemBatchConfigDO validateItemBatchConfigExists(Long itemId) {
        MesMdItemBatchConfigDO config = getItemBatchConfigByItemId(itemId);
        if (config == null) {
            throw exception(MD_ITEM_BATCH_CONFIG_NOT_EXISTS);
        }
        return config;
    }
 
    @Override
    public Long saveItemBatchConfig(MesMdItemBatchConfigSaveReqVO saveReqVO) {
        // 1. 校验物料存在
        if (itemService.getItem(saveReqVO.getItemId()) == null) {
            throw exception(MD_ITEM_NOT_EXISTS);
        }
 
        // 2. 查询已有配置,决定新增还是更新
        MesMdItemBatchConfigDO existing = itemBatchConfigMapper.selectByItemId(saveReqVO.getItemId());
        if (existing != null) {
            // 更新
            MesMdItemBatchConfigDO updateObj = BeanUtils.toBean(saveReqVO, MesMdItemBatchConfigDO.class);
            updateObj.setId(existing.getId());
            itemBatchConfigMapper.updateById(updateObj);
            return existing.getId();
        }
 
        // 新增
        MesMdItemBatchConfigDO insertObj = BeanUtils.toBean(saveReqVO, MesMdItemBatchConfigDO.class);
        itemBatchConfigMapper.insert(insertObj);
        return insertObj.getId();
    }
 
    @Override
    public void deleteItemBatchConfigByItemId(Long itemId) {
        itemBatchConfigMapper.deleteByItemId(itemId);
    }
 
}