yuxc
2024-06-19 663878fdcd0d44c105888014f4cdf7fc926f609f
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package com.vci.web.service.impl;
 
import com.vci.corba.common.PLException;
import com.vci.corba.omd.atm.AttributeDef;
import com.vci.starter.web.annotation.log.VciUnLog;
import com.vci.starter.web.enumpck.VciFieldTypeEnum;
import com.vci.starter.web.pagemodel.BaseQueryObject;
import com.vci.starter.web.pagemodel.DataGrid;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.VciDateUtil;
import com.vci.web.model.OsAttributeDO;
import com.vci.web.pageModel.OsAttributeVO;
import com.vci.web.service.OsAttributeServiceI;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
//import static com.vci.client.omd.attribpool.ui.VTDataTypePanel.*;
import static com.vci.omd.constants.AttributeConstants.*;
 
/**
 * 属性池服务  --已经调用了平台的服务,因此不在提供Dao层
 * @author weidy
 * @date 2021-2-15
 */
@Service
public class OsAttributeServiceImpl implements OsAttributeServiceI {
 
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 平台的调用工具类
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 加载自身
     */
    @Autowired(required = false)
    @Lazy
    private OsAttributeServiceI self;
 
    /**
     * 默认的属性
     */
    private static List<OsAttributeVO> defaultAttributeVOs = new ArrayList<>();
 
    /**
     * 默认属性的映射,key是小写
     */
    private static Map<String,OsAttributeVO> defaultAttributeVOMap = new HashMap<>();
 
    /**
     * 获取默认的属性
     * @return 默认的属性列表
     */
    @Override
    public List<OsAttributeVO> getDefaultAttributeVOs() {
        return OsAttributeServiceImpl.defaultAttributeVOs;
    }
 
    /**
     * 设置默认的属性
     * @param defaultAttributeVOs 默认的属性列表
     */
    @Override
    public void setDefaultAttributeVOs(List<OsAttributeVO> defaultAttributeVOs) {
        OsAttributeServiceImpl.defaultAttributeVOs = defaultAttributeVOs;
    }
 
    /**
     * 获取的默认属性的映射
     * @return 默认的属性映射
     */
    @Override
    public Map<String, OsAttributeVO> getDefaultAttributeVOMap() {
        return OsAttributeServiceImpl.defaultAttributeVOMap;
    }
 
    /**
     * 设置默认的属性的映射
     * @param defaultAttributeVOMap 默认的属性映射
     */
    @Override
    public void setDefaultAttributeVOMap(Map<String, OsAttributeVO> defaultAttributeVOMap) {
        OsAttributeServiceImpl.defaultAttributeVOMap = defaultAttributeVOMap;
    }
 
    /**
     * 查询所有的属性
     *
     * @return 属性的显示对象
     */
    @Override
    public List<OsAttributeVO> selectAllAttribute() {
        //后面两个分页数,完全没有用
        try {
            return attributeDO2VOs(Arrays.stream(platformClientUtil.getAttributeService().getAttributeDefs("",1,1)).collect(Collectors.toList()));
        } catch (PLException vciError) {
            throw WebUtil.getVciBaseException(vciError);
        }
    }
 
    /**
     * 查询所有的属性映射
     *
     * @return key是属性的英文名称小写,value是属性的显示对象
     */
    @Override
    @VciUnLog
    public Map<String, OsAttributeVO> selectAllAttributeMap() {
        return Optional.ofNullable(self.selectAllAttribute()).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.toMap(s->s.getId().toLowerCase(),t->t,(o1,o2)->o1));
    }
 
    /**
     * 属性的数据对象转换为显示对象
     *
     * @param attribItems 数据对象
     * @return 显示对象
     */
    @Override
    public List<OsAttributeVO> attributeDO2VOs(Collection<AttributeDef> attribItems) {
        List<OsAttributeVO> vos = new ArrayList<>();
        Optional.ofNullable(attribItems).orElseGet(()->new ArrayList<>()).stream().forEach(attribItem -> {
            vos.add(attributeDO2VO(attribItem));
        });
        return vos;
    }
 
    /**
     * 属性的数据对象转换为显示对象
     *
     * @param attribItem 数据对象
     * @return 显示对象
     */
    @Override
    public OsAttributeVO attributeDO2VO(AttributeDef attribItem) {
        OsAttributeVO attributeVO = new OsAttributeVO();
        if(attribItem!=null){
            attributeVO.setOid(attribItem.oid);
            attributeVO.setId(attribItem.name);
            attributeVO.setCreator(attribItem.creator);
            try {
                attributeVO.setCreateTime(VciDateUtil.str2Date(String.valueOf(attribItem.createTime),VciDateUtil.DateTimeFormat));
                attributeVO.setLastModifyTime(VciDateUtil.str2Date(String.valueOf(attribItem.modifyTime),VciDateUtil.DateTimeFormat));
                attributeVO.setTs(VciDateUtil.str2Date(attribItem.ts,VciDateUtil.DateTimeMillFormat));
            }catch (Throwable e){
 
            }
            attributeVO.setLastModifier(attribItem.modifier);
            attributeVO.setName(attribItem.label);
            attributeVO.setDescription(attribItem.description);
            attributeVO.setAttributeDataType(attribItem.vtDataType);
            attributeVO.setAttributeDataTypeText(VciFieldTypeEnum.getTextByValue(attribItem.vtDataType));
            attributeVO.setDefaultValue(attribItem.defValue);
            attributeVO.setRange(attribItem.rage);
            if(StringUtils.isNotBlank(attribItem.other)) {
                if (isReferAttr(attribItem.other)) {
                    //说明这个的确是参照字段
                    String[] others = attribItem.other.split(";");
                    for (String s : others) {
                        if (s.toLowerCase().contains("btm") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的
                            attributeVO.setBtmTypeId(s.split("=")[1].trim());
                        }
                        //链接类型不支持
                    }
                }
                //必输和长度
                String[] others = attribItem.other.split(";");
                for (String s : others) {
                    if (s.toLowerCase().contains("allownull") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的
                        boolean allownull = false;
                        if (s.split("=")[1].trim().toLowerCase().equals("yes")) {
                            allownull = true;
                        }
                        attributeVO.setNullableFlag(allownull);
                    }
                    if (s.toLowerCase().indexOf("length") > -1 && s.split("=").length > 1) {
                        int length = WebUtil.getInt(s.split("=")[1].trim());
                        if (length > 0) {
                            attributeVO.setAttrLength(length);
                        }
                    }
                }
                //枚举
                if(isEnumAttr(attribItem.other)){
                    for (String s : others) {
                        if(s.contains("enumName") && s.split("=").length>1) {
                            attributeVO.setEnumId(s.split("=")[1].trim());
                        }
                    }
                }
            }
        }
        return attributeVO;
    }
 
    /**
     * 属性的显示对象转换为数据对象
     *
     * @param attributeVO 显示对象
     * @return 数据对象
     */
    @Override
    public AttributeDef attributeVO2DO(OsAttributeVO attributeVO) {
        AttributeDef attribItem = new AttributeDef();
        attribItem.oid = attributeVO.getOid();
        attribItem.ts = VciDateUtil.date2Str(attributeVO.getTs(),VciDateUtil.DateTimeFormat);
        attribItem.creator = attributeVO.getCreator();
        attribItem.createTime = attributeVO.getCreateTime() != null?attributeVO.getCreateTime().getTime():null;
        attribItem.modifier = attributeVO.getLastModifier();
        attribItem.modifyTime = attributeVO.getLastModifyTime() != null ? attributeVO.getLastModifyTime().getTime():null;
        attribItem.name = attributeVO.getId();
        attribItem.label = attributeVO.getName();
        attribItem.description = attributeVO.getDescription() == null ?"":attributeVO.getDescription();
        attribItem.vtDataType = attributeVO.getAttributeDataType() == null ? VciFieldTypeEnum.VTString.name() : attributeVO.getAttributeDataType();
        attribItem.defValue = attributeVO.getDefaultValue() == null?"":attributeVO.getDefaultValue();
        attribItem.rage = attributeVO.getRange() == null ? "" : attributeVO.getRange();
        //other需要自行处理
        StringBuffer sb = new StringBuffer();
        sb.append(ALLOWNULL).append(" = ").append(attributeVO.isNullableFlag() ? "yes" : "no").append(";");
        VciFieldTypeEnum fieldTypeEnum = VciFieldTypeEnum.valueOf(attributeVO.getAttributeDataType());
        String[] otherInfos = attribItem.other.split(";");
        int length = 0;
        if(otherInfos!=null&& otherInfos.length > 0){
            for(String s : otherInfos){
                if(s.contains(LENGTH+" =") || s.contains(LENGTH+"=")){
                    length = VciBaseUtil.getInt(s.split("=")[1]);
                    break;
                }
            }
        }
        switch (fieldTypeEnum) {
            case VTDouble:
                if(attributeVO.getAttrLength() == null){
                    attributeVO.setAttrLength(20);
                }
                if(attributeVO.getPrecisionLength() == null){
                    attributeVO.setPrecisionLength(2);
                }
                sb.append(ACCURACY).append(" = ").append(attributeVO.getPrecisionLength()).append(";");
                sb.append(LENGTH).append(" = ").append(length > attributeVO.getAttrLength()?length:attributeVO.getAttrLength()).append(";");
 
                break;
            case VTInteger:
                if (StringUtils.isNotBlank(attributeVO.getEnumId())) {
                    sb.append(ENUMNAME).append(" = ").append(attributeVO.getEnumId()).append(";");
                }
                break;
            case VTString:
                if (StringUtils.isNotBlank(attributeVO.getBtmTypeId())) {
                    //参照
                    sb.append(BTM).append(" = ").append(attributeVO.getBtmTypeId()).append(";");
                    //链接类型暂时不支持
                }
                sb.append(LENGTH).append(" = ").append(length > attributeVO.getAttrLength()?length:attributeVO.getAttrLength()).append(";");
                if (StringUtils.isNotBlank(attributeVO.getEnumId())) {
                    sb.append(ENUMNAME).append(" = ").append(attributeVO.getEnumId()).append(";");
                }
                break;
            default:
                //不需要处理
                break;
        }
        attribItem.other = sb.toString();
        if (attribItem.other.endsWith(";")) {
            attribItem.other = attribItem.other.substring(0, attribItem.other.length() - 1);
        }
        return attribItem;
    }
 
    /**
     * 根据属性的名称获取属性对象
     * @param attrCode 属性英文名称
     * @return 属性的基本信息
     */
    @Override
    public OsAttributeVO getAttr(String attrCode) {
        if(StringUtils.isBlank(attrCode)){
            return null;
        }
        return self.selectAllAttributeMap().getOrDefault(attrCode.toLowerCase(),null);
    }
 
    /**
     * 使用属性编号获取对象--批量
     *
     * @param attrCodes 属性的英文名称
     * @return 属性的显示对象
     */
    @Override
    public List<OsAttributeVO> listAttrByIds(Collection<String> attrCodes) {
        if(CollectionUtils.isEmpty(attrCodes)){
            return null;
        }
        Map<String, OsAttributeVO> attributeVOMap = self.selectAllAttributeMap();
        List<OsAttributeVO> attributeVOS = new ArrayList<>();
        attrCodes.stream().forEach(attrCode->{
            OsAttributeVO attributeVO = attributeVOMap.getOrDefault(attrCode.toLowerCase(),null);
            if(attributeVO!=null){
                attributeVOS.add(attributeVO);
            }
        });
        return attributeVOS;
    }
 
    /**
     * 批量添加属性
     *
     * @param attribItemList 属性的列表
     */
    @Override
    public void batchAddAttribute(List<AttributeDef> attribItemList) {
        if(!CollectionUtils.isEmpty(attribItemList)){
            attribItemList.stream().forEach(attribItem -> {
                try {
                    platformClientUtil.getAttributeService().addAttributeDef(attribItem);
                } catch (PLException e) {
                    throw WebUtil.getVciBaseException(e);
                }
            });
        }
    }
 
    /**
     * 批量编辑属性
     * @param editAttrList 属性的列表
     */
    @Override
    public void batchEditAttribute(List<AttributeDef> editAttrList) {
        if(!CollectionUtils.isEmpty(editAttrList)){
            editAttrList.stream().forEach(attribItem -> {
                try {
                    platformClientUtil.getAttributeService().modifyAttributeDef(attribItem);
                } catch (PLException e) {
                    throw WebUtil.getVciBaseException(e);
                }
            });
        }
    }
 
    /**
     * 属性列表
     *
     * @param baseQueryObject 查询对象
     * @return 属性的显示对象
     */
    @Override
    public DataGrid<OsAttributeVO> gridAttribute(BaseQueryObject baseQueryObject) {
        return gridObject(baseQueryObject, OsAttributeDO.class,self.selectAllAttributeMap(),OsAttributeVO.class);
    }
 
    /**
     * 是否默认的属性
     *
     * @param attr 属性编号
     * @return true表示是默认属性
     */
    @Override
    public boolean isDefaultAttr(String attr) {
        if(StringUtils.isBlank(attr)){
            return  false;
        }
        return getDefaultAttributeVOMap().containsKey(attr.toLowerCase(Locale.ROOT));
    }
 
    /**
     * 是否为参照属性
     * @param other 配置的其他
     * @return true 是参照
     */
    private boolean isReferAttr(String other){
        if(StringUtils.isNotBlank(other)
                && (other.toLowerCase().contains("btm") || other.toLowerCase().contains("ltm"))){
            //还不能确定,因为枚举的时候也会设置btm
            String[] temp = other.split(";");
            for(String s : temp){
                if((s.contains("btm") || s.contains("ltm")) && s.split("=").length>1){
                    return true;
                }
            }
        }
        return false;
    }
 
 
    /**
     * 是否为枚举的属性
     * @param other 配置的内容
     * @return true 是枚举
     */
    private boolean isEnumAttr(String other){
        if(StringUtils.isNotBlank(other)
                && other.contains("enumName")){
            //还不能确定,因为枚举的时候也会设置btm
            String[] temp = other.split(";");
            for(String s : temp){
                if(s.contains("enumName")&& s.split("=").length>1){
                    return true;
                }
            }
        }
        return false;
    }
 
 
    /**
     * 清除缓存
     */
    @Override
    public void clearCache() {
 
    }
}