田源
2024-08-23 9e20b9fb77a41cb5b4a6eb6213fc51cab1f0bb91
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
package com.vci.web.service.impl;
import cn.hutool.core.io.FileUtil;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.corba.omd.qtm.QTInfo;
import com.vci.corba.portal.data.Constraint;
import com.vci.corba.portal.data.PLAction;
import com.vci.corba.portal.data.PLActionCls;
import com.vci.corba.portal.data.PLActionParam;
import com.vci.dto.PLActionClsDTO;
import com.vci.dto.PLActionDTO;
import com.vci.dto.PLActionQueryDTO;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.util.ControllerUtil;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.web.other.ExportActionLogBean;
import com.vci.web.other.ExportBeans;
import com.vci.web.other.LinkQTExportData;
import com.vci.web.service.*;
import com.vci.web.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Action管理的服务实现类
 * @author yuxc
 * @date 2024-8-16
 */
@Service
public class OsActionServiceImpl implements OsActionServiceI {
 
    @Autowired
    private PlatformClientUtil platformClientUtil;
    /**
     * 保存Action分类信息
     * @param pLActionCls Action分类信息
     * @return 保存结果
     */
    @Override
    public BaseResult saveActionCls(PLActionClsDTO pLActionCls) throws PLException {
 
        if (pLActionCls.getName() == null || pLActionCls.getName().trim().equals("")) {
            throw new PLException("500", new String[]{"分类名称不能为空!"});
        }
        if (pLActionCls.getName().equals("未分类")) {
            throw new PLException("500", new String[]{"未分类节点已经存在!"});
        }
        PLActionCls pac = new PLActionCls();
        pac.id = WebUtil.getSnowflakePk();
        pac.name = pLActionCls.getName();
        pac.pid = pLActionCls.getPid();
        pac.description = pLActionCls.getDescription() == null ? "" : pLActionCls.getDescription();
        pac.creator = WebUtil.getCurrentUserId();
        pac.createTime = System.currentTimeMillis();
        pac.serialno = pLActionCls.getSerialno();
        // 保存分类信息
        String message = platformClientUtil.getUIService().creaetePLActionCls(pac);
        if (message.startsWith("0")) {
            if (message.equals("01")) {
                message = "分类" + pac.name + "已经存在!";
            } else if (message.equals("02")) {
                message = "同一分类下序号不能重复!";
            } else {
                message = "保存分类时发生异常!" + message.substring(1);
            }
            throw new PLException("500", new String[]{message});
        }
        return BaseResult.success("分类创建成功!");
    }
 
    /**
     * 修改Action分类信息
     * @param pLActionCls Action分类信息
     * @return 保存结果
     */
    @Override
    public BaseResult updateActionCls(PLActionClsDTO pLActionCls) throws PLException {
 
        if (pLActionCls.getName() == null || pLActionCls.getName().trim().equals("")) {
            throw new PLException("500", new String[]{"分类名称不能为空!"});
        }
        if (pLActionCls.getName().equals("未分类")) {
            throw new PLException("500", new String[]{"未分类节点已经存在!"});
        }
        if (pLActionCls.getId() == null || pLActionCls.getId().trim().equals("")) {
            throw new PLException("500", new String[]{"主键不能为空!"});
        }
        PLActionCls pac = new PLActionCls();
        pac.name = pLActionCls.getName();
        pac.pid = pLActionCls.getPid();
        pac.description = pLActionCls.getDescription() == null ? "" : pLActionCls.getDescription();
        pac.creator = WebUtil.getCurrentUserId();
        pac.createTime = System.currentTimeMillis();
        pac.serialno = pLActionCls.getSerialno();
        // 修改分类信息
        String message = platformClientUtil.getUIService().editPLActionCls(pac);
        if (message.startsWith("0")) {
            if (message.equals("01")) {
                message = "分类" + pac.name + "已经存在!";
            } else if (message.equals("02")) {
                message = "同一分类下序号不能重复!";
            } else {
                message = "修改分类时发生异常!" + message.substring(1);
            }
            throw new PLException("500", new String[]{message});
        }
        return BaseResult.success("分类修改成功!");
    }
 
    /**
     * 获取Action分类树
     * isExp 是否用户导出 true是,false否
     * @return 查询结果
     */
    @Override
    public BaseResult getActionTree(boolean isExp) throws PLException {
        PLActionCls[] clses = platformClientUtil.getUIService().getPLActionClsArray();
        PLActionClsDTO treDto = new PLActionClsDTO();
        treDto.setName("Action分类");
        Map<String, List<PLActionCls>> allDataMap = Arrays.stream(clses).collect(Collectors.groupingBy(pl -> pl.pid));
 
        for (PLActionCls cls : clses) {
            if (StringUtils.isBlank(cls.pid)) {
                PLActionClsDTO parentDto = new PLActionClsDTO();
                parentDto.setId(cls.id);
                parentDto.setName(cls.name);
                parentDto.setPid(cls.pid);
                parentDto.setDescription(cls.description);
                parentDto.setCreator(cls.creator);
                parentDto.setCreateTime(cls.createTime);
                parentDto.setSerialno(cls.serialno);
                //这里处理导出树的逻辑
                if(isExp){
                    Constraint[] consArray = new Constraint[1];
                    consArray[0] =  new Constraint("plactioncls", cls.id);
                    PLAction[] plActionsByConsArray = platformClientUtil.getUIService().getPLActionsByConsArray(consArray);
                    if(parentDto.getChilds().isEmpty() && plActionsByConsArray.length == 0){
                        continue;
                    }
                    for (PLAction plAction : plActionsByConsArray) {
                        PLActionDTO plActionDTO = new PLActionDTO();
                        plActionDTO.setPlName(plAction.plCode + "/" + plAction.plName);
                        plActionDTO.setPlCode(plAction.plCode);
                        plActionDTO.setPlOId(plAction.plOId);
                        parentDto.getActionChilds().add(plActionDTO);
                    }
                }
                addClsTreeNode(parentDto, allDataMap, isExp);
                treDto.getChilds().add(parentDto);
            }
        }
        PLActionClsDTO plac = new PLActionClsDTO();
        plac.setName("未分类");
        if(isExp){
            Constraint[] consArray = new Constraint[1];
            consArray[0] =  new Constraint("plactioncls", "");
            PLAction[] plActionsByConsArray = platformClientUtil.getUIService().getPLActionsByConsArray(consArray);
            for (PLAction plAction : plActionsByConsArray) {
                PLActionDTO plActionDTO = new PLActionDTO();
                plActionDTO.setPlName(plAction.plCode + "/" + plAction.plName);
                plActionDTO.setPlCode(plAction.plCode);
                plActionDTO.setPlOId(plAction.plOId);
                plac.getActionChilds().add(plActionDTO);
            }
        }
        treDto.getChilds().add(plac);
        return BaseResult.success(treDto);
    }
    /**
     * 获取Action表格数据
     * dto 查询条件
     * @return 查询结果
     */
    @Override
    public BaseResult getActionTableData(PLActionQueryDTO dto) throws PLException {
        Constraint[] consArray ;
        if(StringUtils.isNotBlank(dto.getPlactioncls())){
            consArray = new Constraint[7];
            consArray[6] = new Constraint("plactioncls", dto.getPlactioncls());
        }else {
            consArray = new Constraint[6];
        }
        consArray[0] = new Constraint("plcode", dto.getPlcode());
        consArray[1] = new Constraint("plname", dto.getPlname());
        consArray[2] = new Constraint("plbsurl", dto.getPlbsurl());
        consArray[3] = new Constraint("plcsclass", dto.getPlcsclass());
        consArray[4] = new Constraint("pltypetype", dto.getPltypetype());
        consArray[5] = new Constraint("pldesc", dto.getPldesc());
        PLAction[] plActionsByConsArray = platformClientUtil.getUIService().getPLActionsByConsArray(consArray);
        Arrays.sort(plActionsByConsArray, new Comparator<PLAction>() {
            @Override
            public int compare(PLAction o1, PLAction o2) {
                String py1 = PinyinCommon.getPingYin(o1.plCode);
                String py2 = PinyinCommon.getPingYin(o2.plCode);
                return py1.compareTo(py2);
            }
        });
        return BaseResult.dataList(Arrays.asList(plActionsByConsArray));
    }
    /**
     * 保存Action数据
     * dto action传输对象
     * @return 保存结果
     */
    @Override
    public BaseResult saveAction(PLActionDTO dto) throws PLException {
        if(StringUtils.isBlank(dto.getPlCode())){
            throw new PLException("500", new String[]{"请输入编号"});
        }
        PLAction[] actionsInDB= platformClientUtil.getUIService().getAllPLAction();
        for(int i =0;i<actionsInDB.length;i++){
            if (dto.getPlCode().equals(actionsInDB[i].plCode)) {
                throw new PLException("500", new String[]{"新建Action编号重复,请重新输入编号"});
            }
        }
        PLAction plAction = new PLAction();
        plAction.plOId = WebUtil.getSnowflakePk();
        plAction.plCode = StringUtils.defaultString(dto.getPlCode());
        plAction.plName = StringUtils.defaultString(dto.getPlName());
        plAction.plCSClass = StringUtils.defaultString(dto.getPlCSClass());
        plAction.plBSUrl = StringUtils.defaultString(dto.getPlBSUrl());
        plAction.plDesc = StringUtils.defaultString(dto.getPlDesc());
        plAction.plCreateUser = WebUtil.getCurrentUserId();
        plAction.plModifyUser = WebUtil.getCurrentUserId();
        plAction.plActionCls = StringUtils.defaultString(dto.getPlActionCls());
        plAction.plTypeType = StringUtils.defaultString(dto.getPlTypeType());
        boolean b = platformClientUtil.getUIService().savePLAction(plAction);
        if(!b){
            throw new PLException("500", new String[]{"保存失败!!"});
        }
        return BaseResult.success("操作成功!");
    }
    /**
     * 修改Action数据
     * dto action传输对象
     * @return 修改结果
     */
    @Override
    public BaseResult updateAction(PLActionDTO dto) throws PLException {
        if(StringUtils.isBlank(dto.getPlCode())){
            throw new PLException("500", new String[]{"编号不能为空"});
        }
        PLAction[] actionsInDB= platformClientUtil.getUIService().getAllPLAction();
        for(int i =0;i<actionsInDB.length;i++){
            if (dto.getPlCode().equals(actionsInDB[i].plCode) && !dto.getPlOId().equals(actionsInDB[i].plOId)) {
                throw new PLException("500", new String[]{"修改Action编号重复,请确认编号"});
            }
        }
        PLAction plAction = new PLAction();
        plAction.plOId = StringUtils.defaultString(dto.getPlOId());
        plAction.plCode = StringUtils.defaultString(dto.getPlCode());
        plAction.plName = StringUtils.defaultString(dto.getPlName());
        plAction.plCSClass = StringUtils.defaultString(dto.getPlCSClass());
        plAction.plBSUrl = StringUtils.defaultString(dto.getPlBSUrl());
        plAction.plDesc = StringUtils.defaultString(dto.getPlDesc());
        plAction.plCreateUser = dto.getPlCreateUser();
        plAction.plModifyUser = WebUtil.getCurrentUserId();
        plAction.plActionCls = StringUtils.defaultString(dto.getPlActionCls());
        plAction.plTypeType = StringUtils.defaultString(dto.getPlTypeType());
        boolean b = platformClientUtil.getUIService().updatePLAction(plAction);
        if(!b){
            throw new PLException("500", new String[]{"修改失败!!"});
        }
        return BaseResult.success("修改成功!");
    }
 
    /**
     * 删除Action数据
     * dto action传输对象
     * @return 删除结果
     */
    @Override
    public BaseResult deleteAction(PLActionDTO dto) throws PLException {
        PLAction plAction = new PLAction();
        plAction.plOId = StringUtils.defaultString(dto.getPlOId());
        plAction.plCode = StringUtils.defaultString(dto.getPlCode());
        plAction.plName = StringUtils.defaultString(dto.getPlName());
        plAction.plCSClass = StringUtils.defaultString(dto.getPlCSClass());
        plAction.plBSUrl = StringUtils.defaultString(dto.getPlBSUrl());
        plAction.plDesc = StringUtils.defaultString(dto.getPlDesc());
        plAction.plCreateUser = dto.getPlCreateUser();
        plAction.plModifyUser = WebUtil.getCurrentUserId();
        plAction.plActionCls = StringUtils.defaultString(dto.getPlActionCls());
        plAction.plTypeType = StringUtils.defaultString(dto.getPlTypeType());
        boolean b = platformClientUtil.getUIService().deletePLAction(plAction);
        if(!b){
            throw new PLException("500", new String[]{"删除失败!!"});
        }
        return BaseResult.success("删除成功!");
    }
    /**
     * 导出Action
     * @return
     */
    @Override
    public void exportBeans(List<String> actionOid, HttpServletResponse response) throws PLException, IOException {
        String defaultTempFolder = LocalFileUtil.getDefaultTempFolder();
        String vciqtmfFileName = defaultTempFolder + File.separator + "actionTemplateExp" + new Date().getTime() + ".vciamf";
        HashMap exportBeans = new HashMap<String, Object>();
        getExportBeans(actionOid, exportBeans);// 获得导出Bean同时,记录log
 
        ObjectOutputStream vciamfFileStream = null;
        try {
            File vciqtmfFile = new File(vciqtmfFileName);
            vciamfFileStream = new ObjectOutputStream(new FileOutputStream(vciqtmfFile));
            vciamfFileStream.writeObject(exportBeans);
        }finally {
            try {
                if (vciamfFileStream != null) {
                    vciamfFileStream.flush();
                    vciamfFileStream.close();
                }
            } catch (Exception e) {
                throw new PLException("500",new String[]{"导出流关闭异常!"});
            }
        }
        ControllerUtil.writeFileToResponse(response,vciqtmfFileName);
        FileUtil.del(defaultTempFolder + File.separator);
    }
 
    /**
     * 处理导出的对象
     * @param actionOid 界面选择的action列表数据
     * @param exportBeansMap 导出对象
     * @return
     * @throws PLException
     */
    private void getExportBeans(List<String> actionOid, HashMap exportBeansMap) throws PLException {
        PLActionCls[] plActionClsArray = platformClientUtil.getUIService().getPLActionClsArray();
        Map<String, PLActionCls> clsMap = Arrays.stream(plActionClsArray).collect(Collectors.toMap(e -> e.id, e -> e));
        ExportBeans exportBeans = new ExportBeans();
        for (String oid : actionOid) {
            PLAction plAction = platformClientUtil.getUIService().getPLActionById(oid);
            //有父节点则进行处理
            if(StringUtils.isNotBlank(plAction.plActionCls)){
                allPLActionClsParent(exportBeans, clsMap.get(plAction.plActionCls), clsMap);
            }
            exportBeans.getPLActions().put(plAction.plOId, plAction);
            PLActionParam[] params = platformClientUtil.getUIService().getPLActionParamArrayByActionId(plAction.plOId);
            if(params != null && params.length > 0){//如果参数存在
                for (PLActionParam plActionParam : params) {//添加action参数
                    exportBeans.addPLActionParamBean(plActionParam);
                }
            }
            String category = clsMap.containsKey(plAction.plActionCls) ? clsMap.get(plAction.plActionCls).name : "";
            exportBeans.setLogBean(new ExportActionLogBean(ExportActionLogBean.RIGHT_STATE,
                    plAction.plCode,plAction.plName,plAction.plCSClass,plAction.plBSUrl,
                    plAction.plTypeType,plAction.plDesc,category));
        }
        exportBeansMap.put("exportBeans", exportBeans);
    }
    //增加父类数据
    private void allPLActionClsParent(ExportBeans exportBeans, PLActionCls cls, Map<String, PLActionCls> clsMap) {
        if(cls.pid != ""){
            allPLActionClsParent(exportBeans, clsMap.get(cls.pid), clsMap);
        }
        exportBeans.addPLActionClsBean(cls);
    }
 
    /**
     * 添加子节点
     * @param parentDto 父节点对象
     * @param allDataMap 所有分组对象
     * @param isExp true为导出功能的树,false为界面分类树
     */
    private void addClsTreeNode(PLActionClsDTO parentDto, Map<String, List<PLActionCls>> allDataMap, Boolean isExp) throws PLException {
        if(allDataMap.containsKey(parentDto.id)){
            for (PLActionCls cls : allDataMap.get(parentDto.id)) {
                PLActionClsDTO childDto = new PLActionClsDTO();
                childDto.setId(cls.id);
                childDto.setName(cls.name);
                childDto.setPid(cls.pid);
                childDto.setDescription(cls.description);
                childDto.setCreator(cls.creator);
                childDto.setCreateTime(cls.createTime);
                childDto.setSerialno(cls.serialno);
                if(isExp){
                    Constraint[] consArray = new Constraint[1];
                    consArray[0] =  new Constraint("plactioncls", cls.id);
                    PLAction[] plActionsByConsArray = platformClientUtil.getUIService().getPLActionsByConsArray(consArray);
                    if(parentDto.getChilds().isEmpty() && plActionsByConsArray.length == 0){
                        continue;
                    }
                    for (PLAction plAction : plActionsByConsArray) {
                        PLActionDTO plActionDTO = new PLActionDTO();
                        plActionDTO.setPlName(plAction.plCode + "/" + plAction.plName);
                        plActionDTO.setPlCode(plAction.plCode);
                        plActionDTO.setPlOId(plAction.plOId);
                        parentDto.getActionChilds().add(plActionDTO);
                    }
                }
                addClsTreeNode(childDto, allDataMap, isExp);
                parentDto.getChilds().add(childDto);
            }
        }
    }
 
}