田源
2024-12-03 e973fb747f0d1a382fb9e4cdb20383a0a546ef67
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
package com.vci.web.service.impl;
 
import com.vci.corba.common.PLException;
import com.vci.corba.omd.btm.BizType;
import com.vci.pagemodel.*;
import com.vci.starter.web.annotation.log.VciUnLog;
import com.vci.starter.web.enumpck.BooleanEnum;
import com.vci.starter.web.util.BeanUtilForVCI;
import com.vci.starter.web.util.Lcm.ConcurrentDateFormat;
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.service.WebBtmServiceI;
import com.vci.web.service.WebEnumServiceI;
import com.vci.web.service.WebLinkTypeServiceI;
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.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Description 业务类型服务
 * @Author dangsn
 * @Date 2024/11/28 11:32
 */
@Service
public class WebBtmServiceImpl implements WebBtmServiceI {
 
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 平台的调用工具类
     */
    @Resource
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 枚举的服务
     */
    @Resource
    private WebEnumServiceI enumService;
 
    /**
     * 链接类型的服务
     */
    @Resource
    private WebLinkTypeServiceI linkTypeService;
 
    /**
     * 属性的服务
     */
    @Resource
    private WebAttributeServiceI attributeService;
 
    /**
     * 使用编号获取业务类型
     *
     * @param id 编号
     * @return 业务类型
     */
    @Override
    public OsBtmTypeVO getBtmById(String id) throws PLException {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        BizType bizType = platformClientUtil.getBtmService().getBizTypeByName(id);
        return btmDO2VO(bizType,null);
    }
 
    /**
     * 数据对象转换为显示对象
     *
     * @param btmItem 数据对象
     * @return 显示对象
     */
    @Override
    public OsBtmTypeVO btmDO2VO(BizType btmItem, Map<String, OsAttributeVO> attributeVOMap) {
        OsBtmTypeVO vo = new OsBtmTypeVO();
        if (btmItem != null) {
            vo.setOid(btmItem.oid);
            vo.setCreator(btmItem.creator);
            vo.setLastModifier(btmItem.modifier);
            try {
                // btmItem.createTime拿到的是时间戳,但是这儿要的是例如2017-12-27 09:32:20.034这样的格式,所以不调用这个方法,改调用其他方法
                //vo.setCreateTime(VciDateUtil.str2Date(String.valueOf(btmItem.createTime),VciDateUtil.DateTimeFormat));
                vo.setCreateTime(Func.parse(btmItem.createTime, ConcurrentDateFormat.of(VciDateUtil.DateTimeFormat)));
                vo.setLastModifyTime(Func.parse(btmItem.modifyTime, ConcurrentDateFormat.of(VciDateUtil.DateTimeFormat)));
                vo.setTs(Func.parse(btmItem.ts, ConcurrentDateFormat.of(VciDateUtil.DateTimeMillFormat)));
            } catch (Exception e) {
                e.printStackTrace();
            }
            vo.setDescription(btmItem.description);
            vo.setId(btmItem.name);
            vo.setName(btmItem.label);
            vo.setLifeCycleId(btmItem.lifeCycle);
            vo.setAbstractFlag(btmItem.isAbstract);
            vo.setImplClass(btmItem.implClass);
            vo.setShape(btmItem.shape);
            vo.setRevLevel(btmItem.revLevel);
            vo.setRevisionRuleId(btmItem.revRuleName);
            vo.setTableName(VciBaseUtil.getTableName(vo.getId()));
            vo.setInputRevisionFlag(btmItem.revInput);
            vo.setDelimiter(btmItem.delimiter);
            vo.setfName(btmItem.fName);
            vo.setVersionRule(String.valueOf(btmItem.verRuleName));
            if (StringUtils.isNotBlank(vo.getRevisionRuleId()) || vo.isInputRevisionFlag()) {
                vo.setRevisionFlag(true);
            }
            vo.setLifeCycleIds(Arrays.stream(btmItem.lifeCycles).collect(Collectors.joining(",")));
            vo.setApNameArray(btmItem.apNameArray);
            List<OsAttributeVO> attributeVOS = attributeService.listAttrByIds(Arrays.stream(btmItem.apNameArray).collect(Collectors.toList()),attributeVOMap);
            List<OsBtmTypeAttributeVO> btmTypeAttributeVOS = new ArrayList<>();
            Optional.ofNullable(attributeVOS).orElseGet(() -> new ArrayList<>()).stream().forEach(attributeVO -> {
                OsBtmTypeAttributeVO btmTypeAttributeVO = new OsBtmTypeAttributeVO();
                BeanUtilForVCI.convert(attributeVO, btmTypeAttributeVO);
                btmTypeAttributeVO.setPkBtmType(vo.getOid());
                btmTypeAttributeVO.setBtmTypeId(vo.getId());
                btmTypeAttributeVO.setAttributeDataType(attributeVO.getAttributeDataType());
                btmTypeAttributeVO.setAttributeLength(attributeVO.getAttrLength());
                btmTypeAttributeVO.setDefaultValue(attributeVO.getDefaultValue());
                if ("secretgrade".equalsIgnoreCase(attributeVO.getId())) {
                    vo.setSecretFlag(true);
                }
                if (StringUtils.isNotBlank(attributeVO.getBtmTypeId())) {
                    btmTypeAttributeVO.setReferFlag(true);
                    btmTypeAttributeVO.setReferBtmTypeId(attributeVO.getBtmTypeId());
                }
                if (StringUtils.isNotBlank(attributeVO.getEnumId())) {
                    btmTypeAttributeVO.setEnumFlag(true);
                    btmTypeAttributeVO.setEnumItemMap(enumService.getEnumValueMap(btmTypeAttributeVO.getEnumId()));
                }
                btmTypeAttributeVOS.add(btmTypeAttributeVO);
            });
            vo.setAttributes(btmTypeAttributeVOS);
        }
        return vo;
    }
 
    /**
     * 获取业务类型的在哪个属性中使用
     *
     * @param btmName 业务类型
     * @return 引用的信息
     */
    @Override
    public List<OsUsedAttributeVO> listBtmUsedInfo(String btmName) {
        return listBtmUsedInfo(btmName, false);
    }
 
    /**
     * 获取业务类型的在哪个属性中使用
     *
     * @param btmId   业务类型
     * @param hasLink 是否包含链接类型中
     * @return 引用的信息
     */
    @Override
    public List<OsUsedAttributeVO> listBtmUsedInfo(String btmId, boolean hasLink) {
        VciBaseUtil.alertNotNull(btmId, "业务类型的名称");
        List<OsAttributeVO> allReferThisBtmAttributes = Optional.ofNullable(attributeService.selectAllAttribute()).orElseGet(() -> new ArrayList<>()).stream().filter(s -> btmId.equalsIgnoreCase(s.getBtmTypeId())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(allReferThisBtmAttributes)) {
            return null;
        }
        //根据这些属性,找它使用的业务类型
        List<OsUsedAttributeVO> usedInfos = new ArrayList<>();
        List<OsBtmTypeVO> btmTypeVOMap = selectAllBtmMap().values().stream().collect(Collectors.toList());
        List<OsLinkTypeVO> linkTypeVOS = linkTypeService.selectAllLinkMap().values().stream().collect(Collectors.toList());
        allReferThisBtmAttributes.stream().forEach(attribute -> {
            //其他的业务类型包含了这个属性的
            List<OsBtmTypeVO> usedBtms = btmTypeVOMap.stream().filter(btmTypeVO -> !CollectionUtils.isEmpty(btmTypeVO.getAttributes()) && btmTypeVO.getAttributes().stream().anyMatch(s -> attribute.getId().equalsIgnoreCase(s.getId()))).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(usedBtms)) {
                usedBtms.stream().forEach(btm -> {
                    btm.getAttributes().stream().filter(s -> attribute.getId().equalsIgnoreCase(s.getId())).forEach(attrInBtm -> {
                        OsUsedAttributeVO usedAttributeVO = new OsUsedAttributeVO();
                        usedAttributeVO.setId(attrInBtm.getId());
                        usedAttributeVO.setName(attrInBtm.getName());
                        usedAttributeVO.setPkBtmType(btm.getId());
                        usedAttributeVO.setBusinessType(BooleanEnum.TRUE.getValue());
                        usedInfos.add(usedAttributeVO);
                    });
                });
            }
            if (hasLink) {
                List<OsLinkTypeVO> usedLinks = linkTypeVOS.stream().filter(linkTypeVO -> !CollectionUtils.isEmpty(linkTypeVO.getAttributes()) && linkTypeVO.getAttributes().stream().anyMatch(s -> attribute.getId().equalsIgnoreCase(s.getId()))).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(usedLinks)) {
                    usedLinks.stream().forEach(link -> {
                        link.getAttributes().stream().filter(s -> attribute.getId().equalsIgnoreCase(s.getId())).forEach(attrInBtm -> {
                            OsUsedAttributeVO usedAttributeVO = new OsUsedAttributeVO();
                            usedAttributeVO.setId(attrInBtm.getId());
                            usedAttributeVO.setName(attrInBtm.getName());
                            usedAttributeVO.setPkBtmType(link.getId());
                            usedAttributeVO.setBusinessType(BooleanEnum.FASLE.getValue());
                            usedInfos.add(usedAttributeVO);
                        });
                    });
                }
            }
        });
        return usedInfos;
    }
 
    /**
     * 查询所有的业务类型映射
     *
     * @return key 是业务的英文名称的小写
     */
    @Override
    @VciUnLog
    public Map<String, OsBtmTypeVO> selectAllBtmMap() {
        return Optional.ofNullable(selectAllBtm()).orElseGet(() -> new ArrayList<OsBtmTypeVO>()).stream().collect(Collectors.toMap(s -> s.getId().toLowerCase(), t -> t, (o1, o2) -> o1));
    }
 
    /**
     * 查询所有的业务类型映射
     *
     * @return key 是业务的英文名称的小写
     */
    @Override
    @VciUnLog
    public Map<String, OsBtmTypeVO> selectAllBtmMap(Map<String, OsAttributeVO> attributeVOMap) {
        return Optional.ofNullable(selectAllBtm(attributeVOMap)).orElseGet(() -> new ArrayList<OsBtmTypeVO>()).stream().collect(Collectors.toMap(s -> s.getId().toLowerCase(), t -> t, (o1, o2) -> o1));
    }
 
    /**
     * 查询所有的业务类型
     *
     * @return 业务类型对象
     */
    @Override
    @VciUnLog
    public List<OsBtmTypeVO> selectAllBtm() {
        try {
            return btmDO2VOs(Arrays.stream(platformClientUtil.getBtmService().getBizTypes("")).collect(Collectors.toList()),null);
        } catch (PLException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 查询所有的业务类型
     *
     * @return 业务类型对象
     */
    @Override
    @VciUnLog
    public List<OsBtmTypeVO> selectAllBtm(Map<String, OsAttributeVO> attributeVOMap) {
        try {
            return btmDO2VOs(Arrays.stream(platformClientUtil.getBtmService().getBizTypes("")).collect(Collectors.toList()),attributeVOMap);
        } catch (PLException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 数据对象转换为显示对象
     *
     * @param btmItems 数据对象
     * @return 显示对象
     */
    @Override
    public List<OsBtmTypeVO> btmDO2VOs(Collection<BizType> btmItems,Map<String, OsAttributeVO> attributeVOMap) {
        List<OsBtmTypeVO> VOS = new ArrayList<>();
        Optional.ofNullable(btmItems).orElseGet(() -> new ArrayList<>()).parallelStream().forEach(btmItem -> {
            OsBtmTypeVO vo = btmDO2VO(btmItem,attributeVOMap);
            VOS.add(vo);
        });
        return VOS;
    }
 
    /**
     * 使用编号获取业务类型
     *
     * @param btmIds 编号
     * @return 业务类型
     */
    @Override
    public List<OsBtmTypeVO> listBtmByIds(Collection<String> btmIds) {
        if (CollectionUtils.isEmpty(btmIds)) {
            return null;
        }
        BtmDataFetcher btmDataFetcher = new BtmDataFetcher();
        List<OsBtmTypeVO> btmTypeVOS = new ArrayList<>();
        btmIds.stream().forEach(id -> {
            OsBtmTypeVO btmType = btmDataFetcher.getBtmType(id);
            btmTypeVOS.add(btmType);
        });
        return btmTypeVOS;
    }
 
    private class BtmDataFetcher {
 
        //业务类型
        private Map<String, BizType> btmTypeVOMap = new HashMap<String, BizType>();
 
        public BtmDataFetcher() {
            initBtmDataFetcher();
        }
 
        private void initBtmDataFetcher(){
            try {
                BizType[] bizTypes = platformClientUtil.getBtmService().getBizTypes("");
                //List<OsBtmTypeVO> osBtmTypeVOS = btmDO2VOs(Arrays.asList(bizTypes), null);
                if(Func.isNotEmpty(bizTypes)){
                    btmTypeVOMap = Arrays.stream(bizTypes).collect(Collectors.toMap(btm -> btm.name, btm -> btm));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
 
        /**
         * 获取业务类型
         * @param id
         * @return
         */
        public OsBtmTypeVO getBtmType(String id) {
            BizType bizType = btmTypeVOMap.get(id);
            if(Func.isEmpty(bizType)){
                return null;
            }
            //获取的时候才赚VO对象,这样避免一次性全部Btm转VO太慢的问题
            return btmDO2VO(bizType,null);
        }
    }
 
    /**
     * 根据业务类型获取包含的属性--不包含基础属性
     *
     * @param btmId 业务类型的编号
     * @return 属性的内容
     */
    @Override
    public List<OsBtmTypeAttributeVO> listAttributeByBtmId(String btmId) {
        VciBaseUtil.alertNotNull(btmId, "业务类型的编号");
        BizType[] bizTypes = new BizType[0];
        try {
            bizTypes = platformClientUtil.getBtmService().getBizTypes(btmId);
        } catch (PLException e) {
            throw WebUtil.getVciBaseException(e);
        }
        if (Func.isEmpty(bizTypes)) {
            return new ArrayList<>();
        }
        BizType bizType = bizTypes[0];
        OsBtmTypeVO btmTypeVO = btmDO2VO(bizType,null);
        List<OsBtmTypeAttributeVO> attributes = btmTypeVO.getAttributes();
        if (attributes == null) {
            attributes = new ArrayList<>();
        }
        return attributes.stream().sorted(((o1, o2) -> o1.getId().toLowerCase(Locale.ROOT).compareTo(o2.getId().toLowerCase(Locale.ROOT)))).collect(Collectors.toList());
    }
}