ludc
2023-04-07 7df1d61319149a666e8b2801a3c89c1d80900d2e
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.vci.ubcs.code.service.impl;
 
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.code.dto.CodeRuleDTO;
import com.vci.ubcs.code.entity.CodeAllcode;
import com.vci.ubcs.code.entity.CodeClassify;
import com.vci.ubcs.code.entity.CodeRule;
import com.vci.ubcs.code.lifecycle.CodeRuleLC;
import com.vci.ubcs.code.mapper.CodeRuleMapper;
import com.vci.ubcs.code.service.ICodeAllcodeService;
import com.vci.ubcs.code.service.ICodeBasicSecService;
import com.vci.ubcs.code.service.ICodeClassifyService;
import com.vci.ubcs.code.service.ICodeRuleService;
import com.vci.ubcs.code.vo.pagemodel.CodeClassifyVO;
import com.vci.ubcs.code.vo.pagemodel.CodeRuleVO;
import com.vci.ubcs.code.wrapper.CodeClassifyWrapper;
import com.vci.ubcs.code.wrapper.CodeRuleWrapper;
import com.vci.ubcs.com.vci.starter.exception.VciBaseException;
import com.vci.ubcs.com.vci.starter.revision.service.RevisionModelUtil;
import com.vci.ubcs.com.vci.starter.web.util.VciBaseUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
import static com.vci.ubcs.code.constant.FrameWorkDefaultValueConstant.FRAMEWORK_RELEASE_EDITING;
import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.DATA_OID_NOT_EXIST;
import static com.vci.ubcs.code.constant.MdmLifeCycleConstant.CODE_RULE_LC;
 
/**
 * 编码规则 服务实现类
 *
 * @author ludc
 * @since 2023-04-03
 */
@Service
public class CodeRuleServiceImpl extends ServiceImpl<CodeRuleMapper, CodeRule> implements ICodeRuleService {
 
    /**
     * 数据操作层
     */
    @Resource
    private CodeRuleMapper codeRuleMapper;
 
    @Resource
    private ICodeClassifyService codeClassifyServcie;
 
    @Resource
    private ICodeAllcodeService codeAllcodeService;
 
    @Resource
    private ICodeBasicSecService codeBasicSecService;
 
    /**
     * 对象的操作
     */
    @Resource
    private RevisionModelUtil revisionModelUtil;
 
    @Override
    public IPage<CodeRuleVO> selectPlCodeRulePage(IPage<CodeRuleVO> page, CodeRuleVO codeRule) {
        //对生命周期的枚举进行转换
        if(!StringUtils.isEmpty(codeRule.getLcStatusText())){
            codeRule.setLcStatus(CodeRuleLC.getValueByText(codeRule.getLcStatusText()));
        }
        List<CodeRule> codeRulePage = codeRuleMapper.selectCodeRulePage(page, codeRule);
        //do转vo同时setLcStatusText生命周期值,并包装成分页对象返回
        return page.setRecords(CodeRuleWrapper.build().listVO(codeRulePage));
    }
 
    /**
     * 增加主数据编码规则
     *
     * @param codeRuleDTO 主数据编码规则数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public Boolean addSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeRuleDTO, "需要添加的数据对象");
        //将DTO转换为DO
        CodeRule codeRule = Objects.requireNonNull(BeanUtil.copy(codeRuleDTO, CodeRule.class));
        String userId = AuthUtil.getUserId().toString();
        codeRule.setOid(VciBaseUtil.getPk());
        codeRule.setRevisionOid(VciBaseUtil.getPk());
        codeRule.setNameOid(VciBaseUtil.getPk());
        codeRule.setBtmname("coderule");
        codeRule.setLastR("1");
        codeRule.setLastV("1");
        codeRule.setFirstR("1");
        codeRule.setFirstV("1");
        codeRule.setCreator(userId);
        codeRule.setCreateTime(new Date());
        codeRule.setLastModifier("1");
        codeRule.setLastModifyTime(new Date());
        codeRule.setVersionRule("0");
        codeRule.setVersionSeq(1);
        codeRule.setLctid(CODE_RULE_LC);
        codeRule.setLcStatus(FRAMEWORK_RELEASE_EDITING);
        codeRule.setOwner("1");
        codeRule.setCreator(userId);
        codeRule.setLastModifier(userId);
        return codeRuleMapper.insert(codeRule)>0;
    }
 
    /**
     * 校验编码规则的状态是否可以编辑或删除
     *
     * @param lcStatus 编码规则
     * @return true表示可以编辑或删除,false表示不可以
     */
    @Override
    public boolean checkEditDelStatus(String lcStatus) {
        if (CodeRuleLC.RELEASED.getValue().equals(lcStatus) || CodeRuleLC.DISABLED.getValue().equals(lcStatus)) {
            return false;
        }
        return true;
    }
 
    /**
     * 修改主数据编码规则
     *
     * @param codeRuleDTO 主数据编码规则数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public Boolean editSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeRuleDTO, "数据对象", codeRuleDTO.getOid(), "主数据编码规则主键");
        if (!checkEditDelStatus(codeRuleDTO.getLcStatus())) {
            throw new VciBaseException("编码规则已发布,不允许编辑或删除");
        }
        //将DTO转换为DO
        CodeRule codeRule = selectByOid(codeRuleDTO.getOid());
        revisionModelUtil.copyFromDTOIgnore(codeRuleDTO, codeRule);
        return codeRuleMapper.updateById(codeRule)>0;
    }
 
    /**
     * 删除主数据编码规则
     *
     * @param codeRuleDTO 主数据编码规则数据传输对象,oid和ts需要传输
     * @return 删除结果反馈::success:成功,fail:失败
     * @throws VciBaseException 参数为空,被引用时抛出异常
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteCodeRule(CodeRuleDTO codeRuleDTO) throws VciBaseException {
        VciBaseUtil.alertNotNull(codeRuleDTO, "主数据编码规则数据对象", codeRuleDTO.getOid(), "主数据编码规则的主键");
        CodeRule codeRule = selectByOid(codeRuleDTO.getOid());
        if (!checkEditDelStatus(codeRule.getLcStatus())) {
            return R.fail("编码规则已发布,不允许编辑或删除");
        } else {
            if (isAlreadyInUse(codeRule.getOid())) {
                return R.fail("编码规则已被引用,不允许编辑或删除!");
            }
        }
 
        List<CodeAllcode> codeDOList = codeAllcodeService.selectByWrapper(Wrappers.<CodeAllcode>query().eq("codeRuleOid", codeRuleDTO.getOid()));
        if (!CollectionUtils.isEmpty(codeDOList)) {
            return R.fail("编码规则已生成编码,不允许删除");
        }
        //执行删除操作
        //WebUtil.setPersistence(false);
        boolean resBoolean = codeBasicSecService.batchDeleteSecByCodeRuleOid(codeRule.getOid());
        resBoolean = codeRuleMapper.deleteById(codeRule.getOid())>0;
        //WebUtil.setPersistence(true);
        return R.status(resBoolean);
    }
 
    /**
     * 主键查询数据对象
     *
     * @param oid 主键
     * @return 数据对象
     * @throws VciBaseException 参数为空,并且数据不存在的时候会抛出异常
     */
    private CodeRule selectByOid(String oid) throws VciBaseException {
        VciBaseUtil.alertNotNull(oid, "主键");
        CodeRule codeRuleDO = codeRuleMapper.selectById(oid.trim());
        if (codeRuleDO == null || StringUtils.isBlank(codeRuleDO.getOid())) {
            throw new VciBaseException(DATA_OID_NOT_EXIST);
        }
        return codeRuleDO;
    }
 
    /**
     * 检验编码规则是否已经被使用
     *
     * @param oid 编码规则主键
     * @return true表示已经使用,false表示未被使用
     */
    @Override
    public boolean isAlreadyInUse(String oid) {
        Collection<CodeClassifyVO> codeClassifyVOS = listUseRangeInCodeClassify(oid);
        if (codeClassifyVOS.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 查看主数据编码规则的使用范围
     *
     * @param oid 编码规则主键
     * @return 主题库分类使用到该编码规则的所有集合
     */
    @Override
    public Collection<CodeClassifyVO> listUseRangeInCodeClassify(String oid) {
        List<CodeClassify> codeClassifies = codeClassifyServcie.selectByWrapper(Wrappers.<CodeClassify>query().eq("codeRuleOid", oid));
        return CodeClassifyWrapper.build().listVO(codeClassifies);
    }
 
}