ludc
2024-07-31 79120a1740872fbb20a79d0cde0a3fa9f55ec285
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.vci.web.service.impl;
 
import com.vci.client.mw.ClientSessionUtility;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.vrm.VersionRule;
import com.vci.dto.OsRevisionRuleDTO;
import com.vci.starter.web.annotation.log.VciUnLog;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.VciDateUtil;
import com.vci.pagemodel.OsRevisionRuleVO;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.web.service.OsRevisionRuleServiceI;
import com.vci.web.util.Func;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 版本规则的服务
 * @author weidy
 * @date 2021-2-15
 */
@Service
public class OsRevisionRuleServiceImpl implements OsRevisionRuleServiceI {
 
    /**
     * 平台调用客户端
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 加载自身
     */
    @Autowired(required = false)
    @Lazy
    private OsRevisionRuleServiceI self;
 
    /**
     * 查询所有的版本规则
     *
     * @return 版本对象
     */
    @Override
    public List<OsRevisionRuleVO> selectAllRevision() {
        try {
            return revisionRuleDO2VOs(Arrays.stream(platformClientUtil.getVersionService().getVersionRules()).collect(Collectors.toList()));
        } catch (PLException e) {
            throw WebUtil.getVciBaseException(e);
        }
    }
 
    /**
     * 查询所有的版本规则映射
     *
     * @return key 是版本的英文名称
     */
    @Override
    @VciUnLog
    public Map<String, OsRevisionRuleVO> selectAllRevisionMap() {
        return Optional.ofNullable(self.selectAllRevision()).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.toMap(s->s.getId().toLowerCase(),t->t,(o1,o2)->o1));
    }
 
    /**
     * 创建版本规则
     * @param osRevisionRuleDTO
     * @return
     */
    @Override
    public boolean addVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) throws PLException {
        //判空
        VciBaseUtil.alertNotNull(osRevisionRuleDTO,"版本规则对象",osRevisionRuleDTO.getId(),"版本规则名称");
        //版本规则合规检验
        this.checkVersionRule(osRevisionRuleDTO);
        //查重
        VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getName());
        //name不为空
        if(Func.isNotEmpty(vr) && !"".equals(vr.name)){
            throw new PLException("500",new String[]{"名称重复请更换名称!"});
        }
        return platformClientUtil.getVersionService().addVersionRule(this.dto2VersionRule(osRevisionRuleDTO));
    }
 
    /**
     * 修改版本规则
     * @param osRevisionRuleDTO
     * @return
     */
    @Override
    public boolean updateVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) throws PLException {
        //判空
        VciBaseUtil.alertNotNull(osRevisionRuleDTO,"版本规则对象",osRevisionRuleDTO.getId(),"版本规则名称");
        //判断是否在系统中存在
        VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getName());
        //版本规则合规检验
        this.checkVersionRule(osRevisionRuleDTO);
        //name不为空
        if(Func.isEmpty(vr) && !"".equals(vr.name)){
            throw new PLException("500",new String[]{"修改的版本规则在系统中不存在!"});
        }
        return platformClientUtil.getVersionService().modifyVersionRule(this.dto2VersionRule(osRevisionRuleDTO));
    }
 
    /**
     * 检查版本规则设置的是否合理
     * @param dto
     */
    private void checkVersionRule(OsRevisionRuleDTO dto) throws PLException {
        //版本规则名称只能为英文字母
        String regex = "[a-z A-Z]*";
        if (!dto.getId().matches(regex)) {
            throw new PLException("500",new String[]{"名称只能为英文!"});
        }
        //跳跃字符只能为数字或者字母
        if(Func.isNotBlank(dto.getJumpCharacter()) && (!(dto.getJumpCharacter().matches(regex)))){
            throw new PLException("500",new String[]{"跳跃字符只能为数字或者字母!"});
        }
        //初始值不能为空且只能为数字或者字母或英文状态下的符号
        String regex1 = "[A-Za-z0-9!@#$%^&*()-_=+{}':|;,.?/]+$";
        if(Func.isBlank(dto.getInitialValue()) || !dto.getInitialValue().matches(regex1)){
            throw new PLException("500",new String[]{"初始值不能为空且只能为数字或者字母或英文状态下的符号!"});
        }
        if(dto.getInitialValue().length() + dto.getInitialValue().length() > 32) {
            throw new PLException("500",new String[]{"初始值不能超过32个字符!"});
        }
        //步长不能为空且必须为1-9的正整数
        String regex2 = "[1-9]";
        if(Func.isBlank(dto.getStepLength()) || (!dto.getStepLength().matches(regex2))){
            throw new PLException("500",new String[]{"步长不能为空且必须为1-9的正整数"});
        }
        //前缀相关判断
        String regex3 = "^\\s+.*";
        if(Func.isNotBlank(dto.getPrefixion()) && (dto.getPrefixion().matches(regex3))){
            throw new PLException("500",new String[]{"前缀不能以空格开头"});
        }
        if (dto.getPrefixion().length() + dto.getPrefixion().length() > 32) {
            throw new PLException("500",new String[]{"前缀不能超过32个字符"});
        }
        //后缀相关判断
        String regex4 = "^*.\\s+$";
        if(Func.isNotBlank(dto.getSuffix()) && (dto.getSuffix().matches(regex4))){
            throw new PLException("500",new String[]{"后缀不能以空格结尾"});
        }
        if (dto.getSuffix().length() + dto.getSuffix().length() > 32) {
            throw new PLException("500",new String[]{"后缀不能超过32个字符"});
        }
        if (dto.getId().length() > 255) {
            throw new PLException("500",new String[]{"名称不能超过255个字符"});
        }
    }
 
    /**
     * dto对象转换为VersionRule对象
     * @return
     */
    private VersionRule dto2VersionRule(OsRevisionRuleDTO osRevisionRuleDTO){
        VersionRule newVR = new VersionRule();
        newVR.name = osRevisionRuleDTO.getId();
        newVR.tag = osRevisionRuleDTO.getName();
        newVR.description = osRevisionRuleDTO.getDescription();
        newVR.jumpCharacter = osRevisionRuleDTO.getJumpCharacter();
        newVR.initialValue = osRevisionRuleDTO.getInitialValue();
        newVR.stepLength = osRevisionRuleDTO.getStepLength();
        newVR.prefixion = osRevisionRuleDTO.getPrefixion();
        newVR.suffix = osRevisionRuleDTO.getSuffix();
        String userName = "developer";//WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();
        long timeMillis = System.currentTimeMillis();
        newVR.creator = Func.isBlank(osRevisionRuleDTO.getCreator()) ? userName:osRevisionRuleDTO.getCreator();
        newVR.createTime = Func.isEmpty(osRevisionRuleDTO.getCreateTime()) ? timeMillis:osRevisionRuleDTO.getCreateTime().getTime();
        newVR.modifier = userName;
        newVR.modifyTime = timeMillis;
        return newVR;
    }
 
    /**
     * 数据对象转换为显示对象
     *
     * @param versionRules 数据对象
     * @return 显示对象
     */
    @Override
    public List<OsRevisionRuleVO> revisionRuleDO2VOs(Collection<VersionRule> versionRules) {
        List<OsRevisionRuleVO> ruleVOS = new ArrayList<>();
        Optional.ofNullable(versionRules).orElseGet(()->new ArrayList<>()).stream().forEach(versionRule -> {
            OsRevisionRuleVO ruleVO = revisionRuleDO2VO(versionRule);
            ruleVOS.add(ruleVO);
        });
        return ruleVOS;
    }
 
    /**
     * 数据对象转换为显示对象
     *
     * @param versionRule 数据对象
     * @return 显示对象
     */
    @Override
    public OsRevisionRuleVO revisionRuleDO2VO(VersionRule versionRule) {
        OsRevisionRuleVO ruleVO = new OsRevisionRuleVO();
        if(versionRule !=null){
            ruleVO.setOid(versionRule.oid);
            ruleVO.setCreator(versionRule.creator);
            ruleVO.setLastModifier(versionRule.modifier);
            try {
                ruleVO.setCreateTime(VciDateUtil.long2Date(versionRule.createTime));
                ruleVO.setLastModifyTime(VciDateUtil.long2Date(versionRule.modifyTime));
                ruleVO.setTs(VciDateUtil.str2Date(versionRule.ts,VciDateUtil.DateTimeFormat));
            } catch (Exception e) {
                e.printStackTrace();
            }
            ruleVO.setDescription(versionRule.description);
            ruleVO.setId(versionRule.name);
            ruleVO.setName(versionRule.tag);
            ruleVO.setStepLength(WebUtil.getInt(versionRule.stepLength));
            ruleVO.setJumpCharacter(versionRule.jumpCharacter);
            ruleVO.setPrefixion(versionRule.prefixion);
            ruleVO.setSuffix(versionRule.suffix);
            ruleVO.setInitialValue(versionRule.initialValue);
            //associated暂时没有使用
        }
        return ruleVO;
    }
 
    /**
     * 使用编号获取规则的值
     *
     * @param id 编号
     * @return 显示对象
     */
    @Override
    public OsRevisionRuleVO getRevisionRuleById(String id) {
        if(StringUtils.isNotBlank(id)){
            return self.selectAllRevisionMap().getOrDefault(id.toLowerCase().trim(),null);
        }
        return null;
    }
 
    /**
     * 查询应用范围
     * @param vrName 版本规则英文名称
     * @return
     */
    @Override
    public List<Map<String, String>> getUsedVersionRuleList(String vrName) throws PLException {
        if(Func.isBlank(vrName)){
            throw new PLException("500",new String[]{"请选择要查询应用范围的属性!"});
        }
        String[] btNames = platformClientUtil.getBtmService().getBTNamesByVerName(vrName);
        if(Func.isEmpty(btNames)){
            return new ArrayList<>();
        }
        List<Map<String,String>> btmNameMapList = new ArrayList<>();
        Arrays.stream(btNames).forEach(btName->{
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("versionRuleName",vrName);
            itemMap.put("source",btName);
            btmNameMapList.add(itemMap);
        });
        return btmNameMapList;
    }
 
    /**
     * 清除缓存
     */
    @Override
    public void clearCache() {
 
    }
 
}