xiaoyi
7 天以前 481d8165f26fc7e37f0e87f6444ac0340bb563a3
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
194
195
196
197
198
199
package cn.iocoder.yudao.module.bpm.service.definition;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.category.BpmCategoryImportExcelVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.category.BpmCategoryImportReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.category.BpmCategoryImportRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.category.BpmCategoryPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.category.BpmCategorySaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmCategoryDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.category.BpmCategoryMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
 
/**
 * BPM 流程分类 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class BpmCategoryServiceImpl implements BpmCategoryService {
 
    @Resource
    private BpmCategoryMapper bpmCategoryMapper;
 
    @Resource
    private BpmModelService modelService;
 
    @Override
    public Long createCategory(BpmCategorySaveReqVO createReqVO) {
        // 校验唯一
        validateCategoryNameUnique(createReqVO);
        validateCategoryCodeUnique(createReqVO);
        // 插入
        BpmCategoryDO category = BeanUtils.toBean(createReqVO, BpmCategoryDO.class);
        bpmCategoryMapper.insert(category);
        return category.getId();
    }
 
    @Override
    public void updateCategory(BpmCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getId());
        validateCategoryNameUnique(updateReqVO);
        validateCategoryCodeUnique(updateReqVO);
        // 更新
        BpmCategoryDO updateObj = BeanUtils.toBean(updateReqVO, BpmCategoryDO.class);
        bpmCategoryMapper.updateById(updateObj);
    }
 
    private void validateCategoryNameUnique(BpmCategorySaveReqVO updateReqVO) {
        BpmCategoryDO category = bpmCategoryMapper.selectByName(updateReqVO.getName());
        if (category == null
                || ObjUtil.equal(category.getId(), updateReqVO.getId())) {
            return;
        }
        throw exception(CATEGORY_NAME_DUPLICATE, updateReqVO.getName());
    }
 
    private void validateCategoryCodeUnique(BpmCategorySaveReqVO updateReqVO) {
        BpmCategoryDO category = bpmCategoryMapper.selectByCode(updateReqVO.getCode());
        if (category == null
                || ObjUtil.equal(category.getId(), updateReqVO.getId())) {
            return;
        }
        throw exception(CATEGORY_CODE_DUPLICATE, updateReqVO.getCode());
    }
 
    @Override
    public void deleteCategory(Long id) {
        // 校验存在
        BpmCategoryDO category = validateCategoryExists(id);
        // 校验是否被流程模型使用
        Long count = modelService.getModelCountByCategory(category.getCode());
        if (count > 0) {
            throw exception(CATEGORY_DELETE_FAIL_MODEL_USED, category.getName());
        }
        // 删除
        bpmCategoryMapper.deleteById(id);
    }
 
    private BpmCategoryDO validateCategoryExists(Long id) {
        BpmCategoryDO category = bpmCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
        return category;
    }
 
    @Override
    public BpmCategoryDO getCategory(Long id) {
        return bpmCategoryMapper.selectById(id);
    }
 
    @Override
    public PageResult<BpmCategoryDO> getCategoryPage(BpmCategoryPageReqVO pageReqVO) {
        return bpmCategoryMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<BpmCategoryDO> getCategoryListByCode(Collection<String> codes) {
        if (CollUtil.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return bpmCategoryMapper.selectListByCode(codes);
    }
 
    @Override
    public List<BpmCategoryDO> getCategoryListByStatus(Integer status) {
        return bpmCategoryMapper.selectListByStatus(status);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategorySortBatch(List<Long> ids) {
        // 校验分类都存在
        List<BpmCategoryDO> categories = bpmCategoryMapper.selectByIds(ids);
        if (categories.size() != ids.size()) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
 
        // 批量更新排序
        List<BpmCategoryDO> updateList = IntStream.range(0, ids.size())
                .mapToObj(index -> new BpmCategoryDO().setId(ids.get(index)).setSort(index))
                .collect(Collectors.toList());
        bpmCategoryMapper.updateBatch(updateList);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BpmCategoryImportRespVO importCategoryList(List<BpmCategoryImportExcelVO> importCategories,
                                                      BpmCategoryImportReqVO importReqVO) {
        // 1. 校验非空
        importCategories = filterList(importCategories, item -> StrUtil.isNotBlank(item.getName()));
        if (CollUtil.isEmpty(importCategories)) {
            throw exception(CATEGORY_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐条处理
        BpmCategoryImportRespVO respVO = BpmCategoryImportRespVO.builder()
                .createCategoryNames(new ArrayList<>())
                .updateCategoryNames(new ArrayList<>())
                .failureCategoryNames(new LinkedHashMap<>())
                .build();
        importCategories.forEach(importCategory -> {
            try {
                // 2.1 校验唯一:名字与标志分别匹配,若撞到不同记录则报错
                BpmCategoryDO byName = bpmCategoryMapper.selectByName(importCategory.getName());
                BpmCategoryDO byCode = bpmCategoryMapper.selectByCode(importCategory.getCode());
                BpmCategoryDO existCategory = byCode != null ? byCode : byName;
                if (byName != null && !byName.getId().equals(existCategory.getId())) {
                    throw exception(CATEGORY_NAME_DUPLICATE, importCategory.getName());
                }
                if (byCode != null && !byCode.getId().equals(existCategory.getId())) {
                    throw exception(CATEGORY_CODE_DUPLICATE, importCategory.getCode());
                }
                // 2.2 判断是否存在
                if (existCategory == null) {
                    BpmCategoryDO category = BeanUtils.toBean(importCategory, BpmCategoryDO.class);
                    bpmCategoryMapper.insert(category);
                    respVO.getCreateCategoryNames().add(importCategory.getName());
                    return;
                }
                // 2.3 已存在,判断是否允许更新
                if (!importReqVO.getUpdateSupport()) {
                    respVO.getFailureCategoryNames().put(importCategory.getName(),
                            StrUtil.format(CATEGORY_IMPORT_EXISTS.getMsg(), importCategory.getName()));
                    return;
                }
                BpmCategoryDO updateCategory = BeanUtils.toBean(importCategory, BpmCategoryDO.class)
                        .setId(existCategory.getId());
                bpmCategoryMapper.updateById(updateCategory);
                respVO.getUpdateCategoryNames().add(importCategory.getName());
            } catch (ServiceException ex) {
                respVO.getFailureCategoryNames().put(importCategory.getName(), ex.getMessage());
            }
        });
        return respVO;
    }
 
}