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
package com.vci.web.controller;
 
import cn.hutool.core.io.FileUtil;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.qtm.QTD;
import com.vci.dto.QTInfoDTO;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.util.ControllerUtil;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.web.service.OsQuereyTemplateServiceI;
import com.vci.web.util.Func;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * 查询模板的控制器
 * @author weidy
 * @date 2022-3-26
 */
@RequestMapping("/templateController")
@RestController
public class OsQueryTemplateController {
 
    /**
     * 查询模板服务
     */
    @Autowired
    private OsQuereyTemplateServiceI quereyTemplateServiceI;
 
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 查询模板的列表
     * @param btmName 类型
     * @param linkFlag 是否链接类型 :true 链接类型 ,false 业务类型
     * @return 查询模板的列表
     */
    @GetMapping("/queryTemplateList")
    public BaseResult queryTemplateList(String btmName,Boolean linkFlag){
        try {
            return quereyTemplateServiceI.queryTemplateList(btmName,linkFlag);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
 
    /**
     * 查询模板的列表添加了字段的相关属性
     * @param btmName 类型
     * @param linkFlag 是否链接类型 :true 链接类型 ,false 业务类型
     * @param direction 正反方向
     * @return 查询模板的列表
     */
    @GetMapping("/queryTemplateListByAttr")
    public BaseResult queryTemplateListByAttr(String btmName,Boolean linkFlag, String direction){
        try {
            return quereyTemplateServiceI.queryTemplateListByAttr(btmName,linkFlag,direction);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 保存查询模板
     * @param qtd 查询模板实体类
     * @return 保存结果
     */
    @PostMapping("/saveTemplate")
    public BaseResult saveTemplate(@RequestBody QTD qtd){
        try {
            return quereyTemplateServiceI.saveTemplate(qtd);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 修改查询模板
     * @param qtd 查询模板实体类
     * @return 保存结果
     */
    @PostMapping("/updateTemplate")
    public BaseResult updateTemplate(@RequestBody QTD qtd){
        try {
            return quereyTemplateServiceI.updateTemplate(qtd);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 删除查询模板
     * @param name 查询模板名
     * @return 保存结果
     */
    @DeleteMapping("/deleteTemplate")
    public BaseResult deleteTemplate(String name){
        try {
            return quereyTemplateServiceI.deleteTemplate(name);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 查询条件的查询按钮查询lis列表
     * @param qtInfoDTO 传输的数据对象:
     *                linkTypeName 链接类型名称、
     *                rdPositive 方向,true正向,false反向
     *                btmName 业务类型名称
     *                combRelaType 业务类型选择值
     *                versionValue 版本班次值
     *                isQueryIsLeaf 是否选择下级
     *                level 子节点层数
     * @return 查询结果
     */
    @PostMapping("/getCriteria")
    public BaseResult getCriteria(@RequestBody QTInfoDTO qtInfoDTO){
        try {
            return quereyTemplateServiceI.getCriteria(qtInfoDTO);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 业务类型查询模板下的查询条件的查询按钮查询list列表
     * @param qtInfoDTO
     * @return 查询结果
     */
    @PostMapping("/getCriteriaBtm")
    public BaseResult getCriteriaBtm(@RequestBody QTInfoDTO qtInfoDTO){
        try {
            return quereyTemplateServiceI.getCriteriaBtm(qtInfoDTO);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 查询条件下的选择查询模板对话框(查询全部模板和所对应的业务类型或链接类型名)
     * @return
     */
    @GetMapping("/getAllQTs")
    public BaseResult getAllQTs(){
        try {
            return quereyTemplateServiceI.getAllQTs();
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 查询模板列表
     * @param btName 类型名称
     * @return 查询结果
     */
    @GetMapping("/getObjTypeQTs")
    public BaseResult getObjTypeQTs(String btName){
        try {
            return quereyTemplateServiceI.getObjTypeQTs(btName);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        } catch (DocumentException e) {
            e.printStackTrace();
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(500);
            objectBaseResult.setMsg(Arrays.toString(e.getMessage().toCharArray()));
            return objectBaseResult;
        }
    }
 
    /**
     * 检查查询模板名字是否存在
     * @param name 查询模板名字
     * @return 查询结果
     */
    @GetMapping("/isExistsQT")
    public BaseResult isExistsQT(String name){
        try {
            return quereyTemplateServiceI.isExistsQT(name);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 链接类型查询模板保存
     * @param qtInfoDTO 保存传输对象
     * @return 保存结果
     */
    @PostMapping("/linkSave")
    public BaseResult linkSave(@RequestBody QTInfoDTO qtInfoDTO){
        try {
            return quereyTemplateServiceI.linkSave(qtInfoDTO);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 链接类型查询模板树查询,用于界面的导出功能
     * @return 查询结果
     */
    @GetMapping("/getLinkTree")
    public BaseResult getLinkTree(){
        try {
            return quereyTemplateServiceI.getLinkTree();
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 导出链接类型查询模板
     * names 查询模板名
     * @return
     */
    @PostMapping("/expLinkTemplate")
    public void expLinkTemplate(@RequestBody Map qtNames, HttpServletResponse response) throws PLException, IOException {
        quereyTemplateServiceI.expLinkTemplate(String.valueOf(qtNames.get("qtNames")), response);
    }
 
    /**
     * 导入链接类型查询模板
     * @param file 上传的文件
     * @return 导入结果
     */
    @PostMapping("/impLinkTemplate")
    public BaseResult impLinkTemplate(MultipartFile file){
        try {
            return quereyTemplateServiceI.impLinkTemplate(file);
        }catch (Throwable e) {
            throw new VciBaseException(VciBaseUtil.getExceptionMessage(e),new String[0],e);
        }
    }
 
    /**
     * 查询方案删除
     * @param names 查询方案名
     * @return 操作结果
     */
    @DeleteMapping("/deleteLinkTemplate")
    public BaseResult deleteLinkTemplate(String names){
        try {
            return quereyTemplateServiceI.deleteLinkTemplate(names);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 业务类型查询模板保存
     * @param qtInfoDTO 保存传输对象
     * @return 保存结果
     */
    @PostMapping("/btmSave")
    public BaseResult btmSave(@RequestBody QTInfoDTO qtInfoDTO){
        try {
            return quereyTemplateServiceI.btmSave(qtInfoDTO);
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 更新数据库结构
     * @return 更新结果
     */
    @PostMapping("/updateDBStructure")
    public BaseResult btmSave(){
        try {
            return quereyTemplateServiceI.updateDBStructure();
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 业务类型查询模板树查询,用于界面的导出功能
     * @return 查询结果
     */
    @GetMapping("/getBtmQtTree")
    public BaseResult getBtmQtTree(){
        try {
            return quereyTemplateServiceI.getBtmQtTree();
        } catch (PLException e) {
            BaseResult objectBaseResult = new BaseResult<>();
            objectBaseResult.setCode(Integer.parseInt(e.code));
            objectBaseResult.setMsg(Arrays.toString(e.messages));
            return objectBaseResult;
        }
    }
 
    /**
     * 导出业务类型查询模板
     * names 查询模板名
     * @return
     */
    @PostMapping("/expBtmQTTemplate")
    public void expBtmQTTemplate(@RequestBody Map qtNames, HttpServletResponse response) throws PLException, IOException {
        try {
            String excelPath = quereyTemplateServiceI.expBtmQTTemplate(String.valueOf(qtNames.get("qtNames")));
            ControllerUtil.writeFileToResponse(response,excelPath);
            FileUtil.del(LocalFileUtil.getDefaultTempFolder() + File.separator);
        } catch (Exception e) {
            String msg = "导出业务类型查询模板时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e);
            try {
                //出错时
                e.printStackTrace();
                ControllerUtil.writeDataToResponse(response,"error_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss") + ".txt", StringUtils.isNotBlank(msg)?msg.getBytes():new byte[0],null);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
 
    /**
     * 导入业务类型查询模板
     * @param file 上传的文件
     * @return 导入结果
     */
    @PostMapping("/impBtmTemplate")
    public BaseResult impBtmTemplate(MultipartFile file){
        try {
            return quereyTemplateServiceI.impBtmTemplate(file);
        }catch (Throwable e) {
            throw new VciBaseException(VciBaseUtil.getExceptionMessage(e),new String[0],e);
        }
    }
 
}