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
package com.vci.web.service.impl;
 
import com.vci.corba.common.PLException;
import com.vci.corba.omd.atm.AttributeDef;
import com.vci.enumpck.UI.ItemTypeEnum;
import com.vci.pagemodel.OsAttributeVO;
import com.vci.starter.web.annotation.log.VciUnLog;
import com.vci.starter.web.enumpck.VciFieldTypeEnum;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.VciDateUtil;
import com.vci.web.service.WebAttributeServiceI;
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.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Description 属性服务
 * @Author dangsn
 * @Date 2024/11/28 14:26
 */
@Service
public class WebAttributeServiceImpl implements WebAttributeServiceI {
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 平台的调用工具类
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 使用属性编号获取对象--批量
     *
     * @param attrCodes 属性的英文名称
     * @return 属性的显示对象
     */
    @Override
    public List<OsAttributeVO> listAttrByIds(Collection<String> attrCodes) {
        if(CollectionUtils.isEmpty(attrCodes)){
            return null;
        }
        Map<String, OsAttributeVO> attributeVOMap = 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 attrCodes 属性的英文名称
     * @param attributeVOMap 属性对象
     * @return 属性的显示对象
     */
    @Override
    public List<OsAttributeVO> listAttrByIds(Collection<String> attrCodes,Map<String, OsAttributeVO> attributeVOMap) {
        if(CollectionUtils.isEmpty(attrCodes)){
            return null;
        }
        if(attributeVOMap == null){
            attributeVOMap = selectAllAttributeMap();
        }
        List<OsAttributeVO> attributeVOS = new ArrayList<>();
        Map<String, OsAttributeVO> finalAttributeVOMap = attributeVOMap;
        attrCodes.stream().forEach(attrCode->{
            OsAttributeVO attributeVO = finalAttributeVOMap.getOrDefault(attrCode.toLowerCase(),null);
            if(attributeVO!=null){
                attributeVOS.add(attributeVO);
            }
        });
        return attributeVOS;
    }
 
    /**
     * 查询所有的属性
     *
     * @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(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(new Date(attribItem.createTime));
                attributeVO.setLastModifyTime(new Date(attribItem.modifyTime));
                attributeVO.setTs(VciDateUtil.str2Date(attribItem.ts,VciDateUtil.DateTimeMillFormat));
            }catch (Throwable e){
                e.printStackTrace();
                String errorLog = "属性DO转VO时出错,原因:"+ VciBaseUtil.getExceptionMessage(e);
                logger.error(errorLog);
                throw new VciBaseException(errorLog);
            }
            attributeVO.setLastModifier(attribItem.modifier);
            attributeVO.setName(attribItem.label);
            attributeVO.setDescription(attribItem.description);
            attributeVO.setAttributeDataType(attribItem.vtDataType);
            attributeVO.setAttributeDataTypeText(VciFieldTypeEnum.getTextByValue(attribItem.vtDataType));
            //获取UI属性类型
            attributeVO.setAttributeUIType(ItemTypeEnum.convertAttributeTypeTOUITypeTextByValue(attribItem.vtDataType,false));
            //获取UI属性类型文本
            attributeVO.setAttributeUITypeText(ItemTypeEnum.convertAttributeTypeTOUITypeTextByValue(attribItem.vtDataType,true));
            attributeVO.setDefaultValue(attribItem.defValue);
            if(Func.isNotBlank(attribItem.rage)){
                attributeVO.setRange(attribItem.rage.replace("&lt;","<"));
            }else{
                attributeVO.setRange(attribItem.rage);
            }
            attributeVO.setOther(attribItem.other);
            //处理参照相关属性
            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());
                        }
                        //链接类型不支持
                        if (s.toLowerCase().contains("link") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的
                            attributeVO.setLinkTypeName(s.split("=")[1].trim());
                        }
                        if (s.toLowerCase().contains("version") && s.split("=").length > 1) {//必须要判断长度,因为枚举的时候也是包含这个btm的
                            attributeVO.setVersion(WebUtil.getInt(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 other 配置的其他
     * @return true 是参照
     */
    private boolean isReferAttr(String other){
        if(StringUtils.isNotBlank(other)
                && (other.toLowerCase().contains("btm") || other.toLowerCase().contains("link"))){
            //还不能确定,因为枚举的时候也会设置btm
            String[] temp = other.split(";");
            for(String s : temp){
                if((s.contains("btm") || s.contains("link")) && 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;
    }
}