云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cn.iocoder.yudao.module.mes.service.wm.barcode;
 
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.wm.barcode.vo.config.MesWmBarcodeConfigImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.barcode.vo.config.MesWmBarcodeConfigImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.barcode.vo.config.MesWmBarcodeConfigPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.barcode.vo.config.MesWmBarcodeConfigSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.barcode.MesWmBarcodeConfigDO;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.barcode.MesWmBarcodeConfigMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 条码配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesWmBarcodeConfigServiceImpl implements MesWmBarcodeConfigService {
 
    @Resource
    private MesWmBarcodeConfigMapper barcodeConfigMapper;
 
    @Resource
    @Lazy
    private MesWmBarcodeService barcodeService;
 
    @Override
    public Long createBarcodeConfig(MesWmBarcodeConfigSaveReqVO createReqVO) {
        // 校验业务类型唯一
        validateBarcodeConfigBizTypeUnique(null, createReqVO.getBizType());
 
        // 插入
        MesWmBarcodeConfigDO config = BeanUtils.toBean(createReqVO, MesWmBarcodeConfigDO.class);
        barcodeConfigMapper.insert(config);
        return config.getId();
    }
 
    @Override
    public void updateBarcodeConfig(MesWmBarcodeConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateBarcodeConfigExists(updateReqVO.getId());
        // 校验业务类型唯一
        validateBarcodeConfigBizTypeUnique(updateReqVO.getId(), updateReqVO.getBizType());
 
        // 更新
        MesWmBarcodeConfigDO updateObj = BeanUtils.toBean(updateReqVO, MesWmBarcodeConfigDO.class);
        barcodeConfigMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteBarcodeConfig(Long id) {
        // 校验存在
        validateBarcodeConfigExists(id);
        // 校验是否有关联的条码记录
        if (barcodeService.getBarcodeCountByConfigId(id) > 0) {
            throw exception(WM_BARCODE_CONFIG_HAS_BARCODE);
        }
 
        // 删除
        barcodeConfigMapper.deleteById(id);
    }
 
    private MesWmBarcodeConfigDO validateBarcodeConfigExists(Long id) {
        MesWmBarcodeConfigDO config = barcodeConfigMapper.selectById(id);
        if (config == null) {
            throw exception(WM_BARCODE_CONFIG_NOT_EXISTS);
        }
        return config;
    }
 
    private void validateBarcodeConfigBizTypeUnique(Long id, Integer bizType) {
        MesWmBarcodeConfigDO config = barcodeConfigMapper.selectByBizType(bizType);
        if (config == null) {
            return;
        }
        if (ObjUtil.notEqual(config.getId(), id)) {
            throw exception(WM_BARCODE_CONFIG_BIZ_TYPE_DUPLICATE);
        }
    }
 
    @Override
    public MesWmBarcodeConfigDO getBarcodeConfig(Long id) {
        return barcodeConfigMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesWmBarcodeConfigDO> getBarcodeConfigPage(MesWmBarcodeConfigPageReqVO pageReqVO) {
        return barcodeConfigMapper.selectPage(pageReqVO);
    }
 
    @Override
    public MesWmBarcodeConfigDO getBarcodeConfigByBizType(Integer bizType) {
        return barcodeConfigMapper.selectByBizType(bizType);
    }
 
    @Override
    public MesWmBarcodeConfigDO validateBarcodeConfigByBizType(Integer bizType) {
        MesWmBarcodeConfigDO config = barcodeConfigMapper.selectByBizType(bizType);
        if (config == null) {
            throw exception(WM_BARCODE_CONFIG_NOT_EXISTS);
        }
        return config;
    }
 
    @Override
    public List<MesWmBarcodeConfigDO> getEnableBarcodeConfigList() {
        return barcodeConfigMapper.selectEnableList();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesWmBarcodeConfigImportRespVO importBarcodeConfigList(List<MesWmBarcodeConfigImportExcelVO> importList, boolean updateSupport) {
        // 校验导入数据不能为空
        if (cn.hutool.core.collection.CollUtil.isEmpty(importList)) {
            throw exception(WM_BARCODE_CONFIG_IMPORT_LIST_IS_EMPTY);
        }
        MesWmBarcodeConfigImportRespVO respVO = MesWmBarcodeConfigImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        // 索引,用于错误提示
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            Integer bizType = importItem.getBizType();
            String key = bizType != null ? String.valueOf(bizType) : "第 " + currentIndex + " 行";
            // 校验业务类型必填
            if (bizType == null) {
                respVO.getFailureCodes().put(key, "业务类型不能为空");
                return;
            }
            // 校验条码格式必填
            if (importItem.getFormat() == null) {
                respVO.getFailureCodes().put(key, "条码格式不能为空");
                return;
            }
            // 校验内容格式模板必填
            if (StrUtil.isBlank(importItem.getContentFormat())) {
                respVO.getFailureCodes().put(key, "内容格式模板不能为空");
                return;
            }
            MesWmBarcodeConfigDO existing = barcodeConfigMapper.selectByBizType(bizType);
            if (existing != null && updateSupport) {
                // 更新
                MesWmBarcodeConfigDO updateObj = new MesWmBarcodeConfigDO();
                updateObj.setId(existing.getId());
                updateObj.setFormat(importItem.getFormat());
                updateObj.setContentFormat(importItem.getContentFormat());
                updateObj.setContentExample(importItem.getContentExample());
                updateObj.setAutoGenerateFlag(importItem.getAutoGenerateFlag());
                updateObj.setDefaultTemplate(importItem.getDefaultTemplate());
                updateObj.setStatus(importItem.getStatus());
                updateObj.setRemark(importItem.getRemark());
                barcodeConfigMapper.updateById(updateObj);
                respVO.getUpdateCodes().add(String.valueOf(bizType));
            } else if (existing != null) {
                respVO.getFailureCodes().put(key, "该业务类型的条码配置已存在:" + bizType);
            } else {
                // 创建
                MesWmBarcodeConfigDO createObj = new MesWmBarcodeConfigDO();
                createObj.setId(null);
                createObj.setBizType(bizType);
                createObj.setFormat(importItem.getFormat());
                createObj.setContentFormat(importItem.getContentFormat());
                createObj.setContentExample(importItem.getContentExample());
                createObj.setAutoGenerateFlag(ObjUtil.defaultIfNull(importItem.getAutoGenerateFlag(), Boolean.TRUE));
                createObj.setDefaultTemplate(importItem.getDefaultTemplate());
                createObj.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
                createObj.setRemark(importItem.getRemark());
                barcodeConfigMapper.insert(createObj);
                respVO.getCreateCodes().add(String.valueOf(bizType));
            }
        });
        return respVO;
    }
 
}