yangys
2025-11-21 e8ed1a91c77ab62a924f12acd55777f227bacd7e
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.qianwen.smartman.modules.smis.service.impl;
 
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.base.BaseServiceImpl;
import com.qianwen.core.tool.node.ForestNodeMerger;
import com.qianwen.core.tool.utils.CollectionUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.smis.convert.BomMaterialConvert;
import com.qianwen.smartman.modules.smis.entity.BomMaterial;
import com.qianwen.smartman.modules.smis.entity.BomVersion;
import com.qianwen.smartman.modules.smis.enums.BomVersionStatusEnum;
import com.qianwen.smartman.modules.smis.mapper.BomMaterialMapper;
import com.qianwen.smartman.modules.smis.service.IBomMaterialService;
import com.qianwen.smartman.modules.smis.service.IBomProcessService;
import com.qianwen.smartman.modules.smis.service.IBomVersionService;
import com.qianwen.smartman.modules.smis.vo.BomMaterialDetailVO;
import com.qianwen.smartman.modules.smis.vo.BomMaterialVO;
 
@Service
public class BomMaterialServiceImpl extends BaseServiceImpl<BomMaterialMapper, BomMaterial> implements IBomMaterialService {
    @Autowired
    private IBomProcessService bomProcessService;
    @Autowired
    @Lazy
    private IBomVersionService bomVersionService;
 
    @Override 
    @Transactional(rollbackFor = {Exception.class})
    public Boolean saveBomMaterial(Long bomId, String allPath, Long parentId, List<BomMaterialVO> bomMaterialVOList) {
        bomMaterialVOList.forEach(i -> {
            BomMaterial bomMaterial = new BomMaterial();
            bomMaterial.setId(Long.valueOf(IdWorker.getId()));
            bomMaterial.setVersionId(bomId);
            bomMaterial.setParentId(parentId == null ? Long.valueOf("0") : parentId);
            bomMaterial.setMaterialId(i.getMaterialId());
            bomMaterial.setAllPath(Func.isBlank(allPath) ? "/" + bomMaterial.getId() : allPath + "/" + bomMaterial.getId());
            save(bomMaterial);
        });
        return true;
    }
 
    @Override 
    @Transactional(rollbackFor = {Exception.class})
    public Boolean deleteBomMaterial(Long bomId) {
        List<BomMaterial> list = list(Wrappers.<BomMaterial>query().lambda()
                    .in(BomMaterial::getVersionId, new Object[] { bomId }));
        if (CollectionUtil.isEmpty(list))
          return true; 
        
        remove(Wrappers.<BomMaterial>update().lambda()
            .eq(BomMaterial::getVersionId, bomId));
        List<Long> bomMaterialIdList = list.stream().map(BomMaterial::getId).distinct().collect(Collectors.toList());
        this.bomProcessService.deleteBomProcess(bomMaterialIdList);
        return true;
    }
 
    @Override 
    public List<BomMaterialVO> getBomMaterialVOListByBomId(Long bomId) {
        return BomMaterialConvert.INSTANCE.convert(list(Wrappers.<BomMaterial>query().lambda()
                  .in(BomMaterial::getVersionId, new Object[] { bomId })));
    }
 
    @Override 
    public BomMaterialDetailVO detail(Long id) {
        return ((BomMaterialMapper) this.baseMapper).detail(id);
    }
 
    @Override 
    public List<BomMaterialDetailVO> list(Map<String, Object> params) {
        return ((BomMaterialMapper) this.baseMapper).list(params);
    }
 
    @Override 
    public IPage<BomMaterialDetailVO> page(IPage<BomMaterialDetailVO> page, Map<String, Object> params) {
        return page.setRecords(((BomMaterialMapper) this.baseMapper).page(page, params));
    }
 
    @Override 
    public Boolean insert(BomMaterialVO bomMaterialVO) {
        if (Func.isEmpty(bomMaterialVO.getVersionId())) {
            throw new ServiceException(MessageUtils.message("cps.production.bom.version.id.is.empty", new Object[0]));
        }
        BomVersion bomVersion = (BomVersion) this.bomVersionService.getById(bomMaterialVO.getVersionId());
        if (BomVersionStatusEnum.FINALIZED.getStatus().equals(bomVersion.getVersionStatus())) {
            throw new ServiceException(MessageUtils.message("cps.production.bom.version.has.been.finalized.can.not.add.bom.material", new Object[0]));
        }
        List<BomMaterial> list = list(Wrappers.<BomMaterial>query().lambda()
                .eq(BomMaterial::getVersionId, bomMaterialVO.getVersionId())
                .eq(BomMaterial::getMaterialId, bomMaterialVO.getMaterialId()));
        
        if (CollectionUtil.isNotEmpty(list)) {
            throw new ServiceException(MessageUtils.message("cps.production.bom.version.has.same.bom.material", new Object[0]));
        }
        String allPath = "";
        if (Func.isNotEmpty(bomMaterialVO.getParentId()) && bomMaterialVO.getParentId().compareTo((Long) 0L) > 0) {
            allPath = Optional.ofNullable(getById(bomMaterialVO.getParentId()).getAllPath()).orElse(null);
        }
        saveBomMaterial(bomMaterialVO.getVersionId(), allPath, bomMaterialVO.getParentId(), Arrays.asList(bomMaterialVO));
        return true;
    }
 
    @Override 
    @Transactional(rollbackFor = {Exception.class})
    public Boolean removeBomMaterial(Long id) {
        BomMaterial bomMaterial = (BomMaterial) getById(id);
        BomVersion bomVersion = (BomVersion) this.bomVersionService.getById(bomMaterial.getVersionId());
        if (BomVersionStatusEnum.FINALIZED.getStatus().equals(bomVersion.getVersionStatus())) {
            throw new ServiceException(MessageUtils.message("cps.production.bom.version.has.been.finalized.can.not.delete.bom.material", new Object[0]));
        }
        List<BomMaterial> list = list(Wrappers.<BomMaterial>query().lambda()
                .eq(BomMaterial::getParentId, bomMaterial.getId()));
        if (CollectionUtil.isNotEmpty(list)) {
            throw new ServiceException(MessageUtils.message("cps.production.bom.material.has.children.can.not.delete", new Object[0]));
        }
        removeById(id);
        this.bomProcessService.deleteBomProcess(Arrays.asList(id));
        return true;
    }
 
    @Override 
    public List<BomMaterialDetailVO> tree(Map<String, Object> params) {
        return ForestNodeMerger.merge(((BomMaterialMapper) this.baseMapper).list(params));
    }
}