kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package com.highdatas.mdm.controller;
 
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.highdatas.mdm.entity.Flows;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.mapper.MaintainDetailMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.ActivitiStatus;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Operate;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  前端控制器
 * </p>
 * @description 数据版本接口
 * @author kimi
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/maintain")
public class MaintainController {
    @Autowired
    IMaintainService maintainService;
 
    @Autowired
    IMaintainDetailService maintainDetailService;
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    IFlowsService  flowsService;
    @Autowired
    IMaintainFieldService maintainFieldService;
    @Autowired
    IMasterAuthorService masterAuthorService;
    @Autowired
    MaintainDetailMapper maintainDetailMapper;
 
    /**
     *
     * @description:  通过id获取版本信息
     * @param id maintainId
     * @return: maintain信息
     *
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable String id) {
        //通过id获取版本信息
        Maintain maintain = maintainService.selectById(id);
        if (maintain == null) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
        return Result.success(maintain);
    }
 
    /**
     *
     * @description:  通过id 获取上个版本的相关数据
     * @param maintainId 版本id
     * @param id 数据id
     * @return: 上个版本的数据信息
     *
     */
    @RequestMapping(value = "/getPreInfo/{id}", method = RequestMethod.GET)
    public Result getPreInfo(@PathVariable String id, @RequestParam String maintainId) {
        //通过id获取版本信息
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
        //通过id获取上个版本的数据信息
        Map<String, Object> preInfo = maintainService.getPreInfo(maintain, id);
        return Result.success(preInfo);
    }
    /**
     *
     * @description:  获取未提交的数据列表
     * @param tableName 表名
     * @param pageNo 页数
     * @return: 未提交数据
     *
     */
    @RequestMapping(value = "{tableName}/unSubmit/{pageNo}", method = RequestMethod.GET)
    public Result unSubmit(@PathVariable String tableName, @PathVariable Integer pageNo, HttpServletRequest request) throws UnsupportedEncodingException {
        //pageSize 每页数据量 , whereSegment筛选条件
        String pageSize = request.getParameter("pageSize");
        String whereSegment = request.getParameter("whereSegment");
        //请求头里获取用户信息
        TUser user = DbUtils.getUser(request);
        //获取筛选
        if (StringUtils.isEmpty(whereSegment)) {
            whereSegment = Constant.WHERE_DEFAULT;
        }else  {
            whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
        }
        //获取未提交的数据
        return maintainService.getUnSubmitData(user, tableName, pageNo, pageSize, whereSegment);
    }
    /**
     *
     * @description:  获取未提交的数据量
     * @param tableName 表名
     * @return: 未提交数据量
     *
     */
    @RequestMapping(value = "{tableName}/unSubmitCnt", method = RequestMethod.GET)
    public Result unSubmitCnt(@PathVariable String tableName,HttpServletRequest request) throws UnsupportedEncodingException {
        //whereSegment 筛选条件
        String whereSegment = request.getParameter("whereSegment");
        TUser user = DbUtils.getUser(request);
        if (StringUtils.isEmpty(whereSegment)) {
            whereSegment = Constant.WHERE_DEFAULT;
        }else  {
            whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
        }
        //获取未提交的数据量
        Long unSubmitDataCnt = maintainService.getUnSubmitDataCnt(user, tableName, whereSegment);
        return Result.success(unSubmitDataCnt);
    }
 
    /**
     *
     * @description:  获取未审批的数据量
     * @param tableName 表名
     * @return: 未审批数据量
     *
     */
    @RequestMapping(value = "{tableName}/unflowCnt", method = RequestMethod.GET)
    public Result unflow(@PathVariable String tableName,  HttpServletRequest request) throws UnsupportedEncodingException {
        //whereSegment 筛选条件
        String whereSegment = request.getParameter("whereSegment");
        TUser user = DbUtils.getUser(request);
        //组合筛选条件
        if (StringUtils.isEmpty(whereSegment)) {
            whereSegment = Constant.WHERE_DEFAULT;
        }else  {
            whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
        }
        //获取未审批的数据条数
        Long invalidVerionDataCnt = maintainService.getInvalidVerionDataCnt(user, tableName, whereSegment);
        return Result.success(invalidVerionDataCnt);
    }
 
    /**
     *
     * @description:  获取未提交的数据列表
     * @param tableName 表名
     * @param pageNo 页数
     * @return: 未提交数据
     *
     */
    @RequestMapping(value = "{tableName}/unflow/{pageNo}", method = RequestMethod.GET)
    public Result unflow(@PathVariable String tableName, @PathVariable Integer pageNo, HttpServletRequest request) throws UnsupportedEncodingException {
        //pageSize 每页数据量 , whereSegment筛选条件
        String pageSize = request.getParameter("pageSize");
        String whereSegment = request.getParameter("whereSegment");
        //请求头里获取用户信息
        TUser user = DbUtils.getUser(request);
        //组装筛选条件
        if (StringUtils.isEmpty(whereSegment)) {
            whereSegment = Constant.WHERE_DEFAULT;
        }else  {
            whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
        }
        //获取未审批通过的数据
        if (StringUtils.isEmpty(pageSize)) {
            return maintainService.getInvalidVerionData(user, tableName, whereSegment, pageNo, null);
        } else {
            return maintainService.getInvalidVerionData(user, tableName, whereSegment, pageNo, Integer.valueOf(pageSize));
        }
    }
 
    /**
     *
     * @description:  获取主题的版本list
     * @param tableName 表名
     * @return: 主题的版本list
     *
     */
    @RequestMapping(value = "/version/{tableName}", method = RequestMethod.GET)
    public Result getHistory(@PathVariable String tableName, HttpServletRequest request) {
        //获取请求头里的用户信息
        TUser user = DbUtils.getUser(request);
        String userId = user.getUserId();
        //获取最大版本
        Maintain maxVersion = maintainService.getMaxVersion(tableName);
        if (maxVersion == null) {
            return Result.error(new CodeMsg(100001,"无当前版本"));
        }
        //获取当前版本
        Maintain nowVersion = maintainService.getNowVersion(tableName);
        //获取版本list
        List<Maintain> maintainList = maintainService.selectList(new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no",false));
        List<Maintain> result = new ArrayList<>();
        int count = 0;
        for (Maintain maintain : maintainList) {
            String flowId = maintain.getFlowId();
            //去除未生效的版本
            if (StringUtils.isEmpty(flowId)) {
                continue;
            }
            //校验是否有权限
            boolean author = masterAuthorService.checkMaintainAuthor(user, maintain.getId());
            if (!author) {
                continue;
            }
            Flows flows = flowsService.selectById(maintain.getFlowId());
            if (flows.getStatus().equals(ActivitiStatus.close)) {
                continue;
            }
            //校验是否为下一节点审批人
            if (flows.getStatus().equals(ActivitiStatus.working) || flows.getStatus().equals(ActivitiStatus.refuse)) {
                boolean nextAudit = flowsService.isNextAudit(flows, userId);
                if (nextAudit) {
                    count++;
                }
//                count++;
                continue;
            }else if(flows.getStatus().equals(ActivitiStatus.open)){
                //历史i版本状态为1
                maintain.setMaintainType(1);
            }
            if (nowVersion != null  && maintain.getId().equalsIgnoreCase(nowVersion.getId())) {
                //当前版本状态为0
                maintain.setMaintainType(0);
            }
            result.add(maintain);
        }
        //
        int cnt = maintainFieldService.getUnFlowCount(tableName, userId);
        count += cnt;
        if (count > 0) {
            //未生效版本状态为-1
            Maintain maintain = new Maintain();
            maintain.setMaintainType(-1);
            maintain.setVersion("待审核");
            maintain.setTableName(tableName);
            maintain.setRecordCount(count);
            result.add(maintain);
        }
        //list 倒叙
        Collections.sort(result, new Comparator<Maintain>() {
            @Override
            public int compare(Maintain o1, Maintain o2) {
                return o1.getMaintainType().compareTo(o2.getMaintainType());
            }
        });
        return Result.success(result);
}
//    /**
//     *
//     * @description:  获取主题的版本list
//     * @param tableName 表名
//     * @return: 主题的版本list
//     *
//     */
//    @RequestMapping(value = "/detail/{id}/{pageNo}", method = RequestMethod.GET)
//    public Result detail(@PathVariable String id, @PathVariable int pageNo) {
//        Maintain maintain = maintainService.selectById(id);
//        String tableName = maintain.getTableName();
//        String tableTempName = tableName + Constant.RECORD;
//
//        List<MaintainDetail> maintainDetailList = maintainDetailService.selectList(new EntityWrapper<MaintainDetail>().eq("parent_id", id));
//
//        return Result.success(maintain);
//    }
 
    /**
      *
      * @description:  获取所有版本的list
      * @param pageno yeshu
      * @return: 所有版本的list
      *
      */
    @RequestMapping(value = "/all/{pageno}", method = RequestMethod.GET)
    public Result all(@PathVariable int pageno, HttpServletRequest request) {
        //表名
        String tableName = request.getParameter("tableName");
        //检验表名是否存在
        String s = tableInfoMapper.selectTableByName(tableName);
 
        Page<Maintain> result;
        //pageSize 每页数据量
        String pageSize = request.getParameter("pageSize");
        Page<Maintain> maintainPage;
        if (StringUtils.isEmpty(pageSize)) {
            maintainPage = new Page<Maintain>(pageno, 20);
 
        } else {
            maintainPage = new Page<Maintain>(pageno, Integer.valueOf(pageSize));
        }
        //筛选--表名
        if (StringUtils.isEmpty(tableName)) {
            result = maintainService.selectPage(maintainPage);
        } else {
            Wrapper<Maintain> tableNameWrapper = new EntityWrapper<Maintain>().eq("table_name", tableName);
            tableNameWrapper.orderBy("order_no desc");
            result = maintainService.selectPage(maintainPage, tableNameWrapper);
        }
        List<Maintain> records = result.getRecords();
        List<Maintain> resultObj = new ArrayList<>();
 
        for (Maintain record : records) {
            //校验是否审批结束
            String flowId = record.getFlowId();
            if (StringUtils.isEmpty(flowId)) {
                continue;
            }
            Flows flows = flowsService.selectById(flowId);
            if (flows == null || !flows.getStatus().equals(ActivitiStatus.open)) {
                continue;
            }
            //校验用户是否存在
            String chargeId = record.getChargeId();
            TUser user = DbUtils.getUserById(chargeId);
            if (user == null) {
                record.setChargeId("用户已删除");
            }else {
                record.setChargeId(user.getUserName());
            }
 
            resultObj.add(record);
        }
        result.setRecords(resultObj);
        return  Result.success(result);
    }
 
    /**
     *
     * @description:  获取允许比较的版本list
     * @param pageNo 页数
     * @return: 允许比较的版本list
     *
     */
    @RequestMapping(value = "/compareList/{pageNo}", method = RequestMethod.GET)
    public Result compare(@PathVariable Integer pageNo, @RequestParam String tableName, HttpServletRequest request) {
        //pageSize 每页数据量
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize = 15;
        if (!StringUtils.isEmpty(pageSizeStr)) {
            pageSize = Integer.valueOf(pageSizeStr);
        }
        //筛选掉不含流程信息后的版本list
        List<Maintain> maintainList = maintainService.selectList(new EntityWrapper<Maintain>().eq("table_name", tableName).isNotNull("flow_id").orderBy("order_no desc"));
        com.highdatas.mdm.pojo.Page page = new com.highdatas.mdm.pojo.Page(maintainList.size());
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        //筛选审批成功的版本list,即当前生效的版本
        List<Maintain> collect = maintainList.stream().filter(maintain -> ActivitiStatus.open.equals(flowsService.getStatusByBusinessId(maintain.getId())))
                .skip(page.getBeginRecordNo_1()).limit(pageSize).collect(Collectors.toList());
 
        List<HashMap<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < collect.size(); i++) {
            Maintain maintain = collect.get(i);
            String version = maintain.getVersion();
            Date createTime = maintain.getCreateTime();
            String chargeId = maintain.getChargeId();
            TUser user = DbUtils.getUserById(chargeId);
            HashMap<String, Object> oneResult = new HashMap<>();
            oneResult.put("version", version);
            oneResult.put("time", createTime);
            oneResult.put("userName", chargeId);
            oneResult.put("id", maintain.getId());
            if (user != null) {
                //添加用户信息
                oneResult.put("userName", user.getUserName());
            }
 
            if (i < collect.size() - 1) {
                //获取上个版本的版本信息
                Maintain preMaintain = collect.get(i + 1);
                //BY操作类型获取上个版本的数据量
                List<Map<String, Object>> maps = maintainDetailMapper.selectOperateCnt(maintain.getTableName(), preMaintain.getOrderNo(), maintain.getOrderNo());
                oneResult.put("content", maps);
 
                oneResult.put("preMaintain", preMaintain);
            } else {
                List<Map<String, Object>> maps = maintainDetailMapper.selectOperateFirstCnt(maintain.getTableName(), maintain.getOrderNo());
                oneResult.put("content", maps);
            }
 
            result.add(oneResult);
        }
 
        //组装返回值
        JSONObject object = new JSONObject();
        object.fluentPut("total", page.getRecordCount());
        object.fluentPut("size", page.getPageSize());
        object.fluentPut("pages", page.getPageCount());
        object.fluentPut("current", page.getPageNo());
        object.fluentPut("records", result);
 
        return Result.success(object);
    }
 
 
    /**
     *
     * @description:  BY 操作类型获取比较两个版本的数据
     * @param pageNo 页数
     * @param operate 操作类型
     * @param maintainId1 第一个版本
     * @param maintainId2 第二个版本
     * @return: BY 操作类型返回数据
     *
     */
    @RequestMapping(value = "/compare/{maintainId1}/{maintainId2}", method = RequestMethod.GET)
    public Result compare(@PathVariable String maintainId1, @PathVariable String maintainId2, @RequestParam Operate operate, @RequestParam int pageNo, HttpServletRequest request) {
        //pageSize 每页数据量
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize = 15;
        if (!StringUtils.isEmpty(pageSizeStr)) {
            pageSize = Integer.valueOf(pageSizeStr);
        }
        //比较两个版本
        JSONObject object = maintainService.compare(maintainId1, maintainId2, operate, pageNo, pageSize);
 
        return Result.success(object);
    }
 
    /**
     *
     * @description:  获取比较两个版本的字段
     * @param maintainId1 第一个版本
     * @param maintainId2 第二个版本
     * @return: 比较两个版本的字段
     *
     */
    @RequestMapping(value = "/compareField/{maintainId1}/{maintainId2}", method = RequestMethod.GET)
    public Result compareField(@PathVariable String maintainId1, @PathVariable String maintainId2) {
        //比较两个版本的字段
        JSONObject object = maintainService.compareField(maintainId1, maintainId2);
 
        return Result.success(object);
    }
 
    /**
     *
     * @description:  判断是否能继续上传表结构
     * @param tableName 表名
     * @return: 是否能继续上传表结构
     *
     */
    @RequestMapping(value = "/canUpload/{tableName}", method = RequestMethod.GET)
    public Result canUpload(@PathVariable String tableName) {
        //获取当前版本
        Maintain nowVersion = maintainService.getNowVersion(tableName);
        if (nowVersion == null) {
            return Result.success(true);
        }else {
            return Result.success(false);
        }
    }
 
    /**
     *
     * @description:  获取某个版本下的记录
     * @param pageNo 页数
     * @return: 某个版本下的记录
     *
     */
    @RequestMapping(value = "/detail/{pageNo}", method = RequestMethod.GET)
    public Result setFlowId(@RequestParam String flowId,@PathVariable Integer pageNo, HttpServletRequest request) {
        //whereSegment 筛选条件
        String whereSegment = request.getParameter("whereSegment");
        //pageSize 每页数据量
        String pageSize = request.getParameter("pageSize");
        //获取请求头中的用户信息
        TUser user = DbUtils.getUser(request);
        if (StringUtils.isEmpty(whereSegment)) {
            whereSegment = Constant.WHERE_DEFAULT;
        }
        //通过筛选调教获取记录表中的数据
        if (StringUtils.isEmpty(pageSize)) {
            return maintainService.tempDataByVersionByFlow(user, flowId, whereSegment, pageNo, null);
        }
        return maintainService.tempDataByVersionByFlow(user, flowId, whereSegment, pageNo, Integer.valueOf(pageSize));
    }
 
    /**
     *
     * @description:  更新流程实例id
     * @param id 版本id
     * @param flowid 流程实例id
     * @return: 是否更新成功
     *
     */
    @RequestMapping(value = "/set/{id}", method = RequestMethod.GET)
    public Result setFlowId(@PathVariable String id, @RequestParam String flowid) {
        //通过id获取版本信息
        Maintain maintain = maintainService.selectById(id);
        if (maintain == null) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
        //更新流程实例id
        maintain.setFlowId(flowid);
        maintain.updateById();
        return Result.success(maintain);
    }
}