kimi
2020-04-15 5d15287b2a06f978485ac6af71e33e1a82b43a65
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
package com.highdatas.mdm.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.generator.config.querys.AbstractDbQuery;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.highdatas.mdm.entity.Flows;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.MaintainDetail;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.mapper.MaintainMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.*;
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.stereotype.Service;
 
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kimi
 * @since 2019-12-16
 */
@Service
public class MaintainServiceImpl extends ServiceImpl<MaintainMapper, Maintain> implements IMaintainService {
    @Autowired
    IMaintainDetailService maintainDetailService;
 
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    MasterDataService masterDataService;
    @Autowired
    ISysFieldService fieldService;
 
    @Autowired
    MaintainMapper maintainMapper;
 
    @Autowired
    IFlowsService flowsService;
 
    @Override
    public HashMap<Operate, Object> compare(String maintainId, String maintainId2) {
        Maintain baseMaintain = selectById(maintainId);
        Maintain compareMaintain = selectById(maintainId2);
 
        HashMap<Operate, Object> operateIntegerMap = new HashMap<Operate, Object>();
 
        if (DbUtils.compareVersion(baseMaintain, compareMaintain) > 0) {
            addCompareInfo(baseMaintain, compareMaintain, operateIntegerMap);
            operateIntegerMap.put(Operate.common, MessageFormat.format("{0} 对比 {1}", baseMaintain.getVersion(), compareMaintain.getVersion()));
        }else {
            addCompareInfo(compareMaintain, baseMaintain, operateIntegerMap);
            operateIntegerMap.put(Operate.common, MessageFormat.format("{0} 对比 {1}", compareMaintain.getVersion(), baseMaintain.getVersion()));
        }
 
        return operateIntegerMap;
 
    }
 
    private void addCompareInfo(Maintain baseMaintain, Maintain compareMaintain, HashMap<Operate, Object> operateIntegerMap) {
        Integer fromNo = compareMaintain.getOrderNo();
        Integer toNo = baseMaintain.getOrderNo();
        for (int i = fromNo + 1; i <= toNo; i++) {
            Maintain maintain = selectOne(new EntityWrapper<Maintain>().eq("table_name", baseMaintain.getTableName()).eq("order_no", i));
            List<MaintainDetail> maintainDetailList = maintainDetailService.selectList(new EntityWrapper<MaintainDetail>().eq("parent_id", maintain.getId()));
            for (MaintainDetail maintainDetail : maintainDetailList) {
                Operate operate = maintainDetail.getOperate();
                Integer integer = (Integer) operateIntegerMap.get(operate);
                if (integer == null) {
                    integer = 1;
                } else {
                    integer++;
                }
                operateIntegerMap.put(operate, integer);
            }
        }
    }
 
    @Override
    public void dealFlow(String maintainId, ActivitiStatus status) {
        Maintain maintain = selectById(maintainId);
        String tableName = maintain.getTableName();
        List<MaintainDetail> maintainDetailList = maintainDetailService.selectList(new EntityWrapper<MaintainDetail>().eq("parent_id", maintainId));
        if (status.equals(ActivitiStatus.close)) {
             String collect = maintainDetailList.stream().map(detail -> DbUtils.quotedStr(detail.getPreMergeId())).collect(Collectors.joining(Constant.COMMA));
            //delete temp
            if (!StringUtils.isEmpty(collect)) {
                masterDataService.delete(tableName + Constant.RECORD, "id in (" +collect+ ")");
            }
 
            //delete detail
            for (MaintainDetail maintainDetail : maintainDetailList) {
                maintainDetail.deleteById();
            }
            //delete maintain
            maintain.deleteById();
            return;
        }
        String fields  = masterDataService.getTempFields(null, tableName);
        String tempFields = masterDataService.getFields(tableName + Constant.RECORD);
 
        String tableTempName = tableName + Constant.RECORD;
        // insert 2 std
        tableInfoMapper.tempCreate2std(tableName, fields, tableTempName, DbUtils.quotedStr(maintainId));
 
 
        // update
        List<Map<String, Object>> updeateDatas = tableInfoMapper.tempByOperate(DbUtils.quotedStr(Operate.update.name()), tempFields, tableTempName, DbUtils.quotedStr(maintainId));
        for (Map<String, Object> updeateData : updeateDatas) {
            String stdId = updeateData.get(Constant.STD_ID).toString();
            String id = updeateData.get(Constant.ID).toString();
            Result result = masterDataService.selectById(tableName, stdId);
            Object tempData = result.getData();
            String datas = JSON.toJSONString(tempData);
            JSONObject object = JSON.parseObject(datas);
            Object grid = object.get("grid");
            object = JSON.parseObject(JSON.toJSONString(grid));
 
            object.put(Constant.STD_ID, stdId);
            object.put(Constant.ID, id);
 
            datas = JSON.toJSONString(object);
 
            masterDataService.updateById(tableTempName,datas, id,true);
 
            updeateData.put(Constant.STD_ID, null);
            updeateData.put(Constant.ID, stdId);
 
            masterDataService.updateById(tableName,JSON.toJSONString(updeateData), stdId,true);
        }
        //delete
        List<Map<String, Object>> delDatas = tableInfoMapper.tempByOperate(DbUtils.quotedStr(Operate.delete.name()), tempFields, tableTempName, DbUtils.quotedStr(maintainId));
        for (Map<String, Object> delData : delDatas) {
            Object id = delData.get(Constant.STD_ID);
            masterDataService.deleteById(tableName, id.toString());
        }
        // update deal
        tableInfoMapper.tempDeal(tableTempName, DbUtils.quotedStr(maintainId));
 
    }
    @Override
    public boolean checkNowVersion(String maintainId) {
        Maintain maintain = selectById(maintainId);
        Maintain maxVersion = getNowVersion(maintain.getTableName());
        Integer compared = DbUtils.compareVersion(maxVersion, maintain);
        if (compared != null && compared == 0) {
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public boolean checkFirstVersion(String id) {
        Maintain maintain = selectById(id);
        Maintain order_no = selectOne(new EntityWrapper<Maintain>().eq("table_name", maintain.getTableName()).orderBy("order_no"));
        if (maintain.getId().equalsIgnoreCase(order_no.getId())) {
            return true;
        }
        return false;
    }
 
    @Override
    public Result getUnSubmitData(TUser user, String tableName, Integer pageNo, String pageSize, String whereSegment) {
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().eq("table_name", tableName).isNull("flow_id").orderBy("order_no desc");
        Maintain maintain = selectOne(versionWrapper);
        if (maintain == null) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
        String maintainId = maintain.getId();
        String tempName = tableName + Constant.RECORD;
        Long aLong = maintainMapper.countUnSubmitData(tempName, whereSegment, DbUtils.quotedStr(maintainId));
        Page page = new Page(aLong);
        page.setPageNo(pageNo);
        if (!StringUtils.isEmpty(pageSize)) {
            page.setPageSize(Integer.valueOf(pageSize));
        }
        String tempFields = masterDataService.getTempFields(user, tableName);
        List<Map<String, Object>> maps = maintainMapper.selectUnSubmitData(tempName, tempFields, whereSegment, DbUtils.quotedStr(maintainId), page.getLimitSQL());
        JSONObject object = new JSONObject();
        object.fluentPut("records", maps);
        object.fluentPut("maintainId", maintainId);
        object.fluentPut("total", page.getRecordCount());
        object.fluentPut("passiveQueueSize", page.getPageSize());
        object.fluentPut("pages", page.getPageCount());
        object.fluentPut("current", page.getPageNo());
        return Result.success(object);
    }
 
    @Override
    public Maintain getNextMaintain(String tableName, String userId) {
        Maintain maintain = this.getMaxVersion(tableName);
        Maintain unFlowMaxVersion = this.getUnFlowMaxVersion(tableName);
        if (unFlowMaxVersion == null) {
            unFlowMaxVersion = new Maintain();
            if (maintain != null) {
                unFlowMaxVersion.setVersion(DbUtils.versionAddSub(maintain.getVersion()));
                int orderNo = maintain.getOrderNo();
                orderNo++;
                unFlowMaxVersion.setOrderNo(orderNo);
            } else {
                unFlowMaxVersion.setVersion(Constant.VERSION_Default);
                unFlowMaxVersion.setOrderNo(0);
            }
 
            unFlowMaxVersion.setId(DbUtils.getUUID());
            unFlowMaxVersion.setChargeId(userId);
            unFlowMaxVersion.setCreateTime(new Date());
            unFlowMaxVersion.setTableName(tableName);
        }
 
        Maintain nowMaintain = unFlowMaxVersion;
        if (DbUtils.compareVersion(maintain, unFlowMaxVersion) != 0) {
            nowMaintain = new Maintain();
            String maintanId = DbUtils.getUUID();
            nowMaintain.setId(maintanId);
            nowMaintain.setCreateTime(new Date());
            int orderNo = maintain.getOrderNo();
            orderNo++;
            nowMaintain.setVersion(DbUtils.versionAddSub(maintain.getVersion()));
            nowMaintain.setOrderNo(orderNo);
            nowMaintain.setTableName(tableName);
        }
 
        nowMaintain.setChargeId(userId);
        String maintainId = nowMaintain.getId();
        if(StringUtils.isEmpty(maintainId)) {
            nowMaintain.setId(DbUtils.getUUID());
        }
        return nowMaintain;
    }
 
    @Override
    public boolean checkdMaxVersion(String maintainId) {
        Maintain maintain = selectById(maintainId);
        Maintain maxVersion = getMaxVersion(maintain.getTableName());
        Integer compared = DbUtils.compareVersion(maxVersion, maintain);
        if (compared == 0) {
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public Maintain getMaxVersion(String tableName) {
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no desc");
 
        Maintain maxVersion = selectOne(versionWrapper);
        return maxVersion;
    }
 
    @Override
    public Maintain getUnFlowMaxVersion(String tableName) {
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().isNull("flow_id").eq("table_name", tableName).orderBy("order_no desc");
 
        Maintain maxVersion = selectOne(versionWrapper);
        return maxVersion;
    }
 
    @Override
    public Maintain getInvalidMaxVersion(String tableName) {
        Maintain result = null;
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no desc");
 
        List<Maintain> maintainList = selectList(versionWrapper);
 
        for (Maintain maintain : maintainList) {
            String flowId = maintain.getFlowId();
            if (StringUtils.isEmpty(flowId)) {
                result = maintain;
            } else {
                Flows flows = flowsService.selectById(flowId);
                if (flows == null) {
                    continue;
                }
                if (!flows.getStatus().equals(ActivitiStatus.open) && !flows.getStatus().equals(ActivitiStatus.close)) {
                    result = maintain;
                }
            }
 
        }
        return result;
    }
 
    @Override
    public Result getInvalidVerionData(TUser user, String tableName, String where, Integer pageNo, Integer pageSize) {
        Maintain invalidVersion = getInvalidMaxVersion(tableName);
        if (invalidVersion == null) {
            return Result.success(null);
        }
        String tableTempName = tableName + Constant.RECORD;
        String tempFields = masterDataService.getTempFields(user, tableName);
        Long count = maintainMapper.countInvalidVersionData(tableTempName, where, invalidVersion.getOrderNo());
        Page page = new Page(count);
        page.setPageNo(pageNo);
        if (pageSize != null) {
            page.setPageSize(pageSize);
        }
        List<Map<String, Object>> maps = maintainMapper.selectInvalidVersionData(tableTempName, tempFields, where, invalidVersion.getOrderNo(), page.getLimitSQL());
        JSONObject resultObj = new JSONObject();
        JSONObject object = new JSONObject();
        object.fluentPut("record", maps);
        object.fluentPut("total", page.getRecordCount());
        object.fluentPut("passiveQueueSize", page.getPageSize());
        object.fluentPut("pages", page.getPageCount());
        object.fluentPut("current", page.getPageNo());
        resultObj.fluentPut("grid", object);
        return Result.success(resultObj);
    }
 
    @Override
    public Maintain getNowVersion(String tableName) {
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().eq("table_name", tableName).isNotNull("flow_id").orderBy("order_no desc");
 
        List<Maintain> maintains = selectList(versionWrapper);
        for (Maintain maintain : maintains) {
            Flows flows = flowsService.selectById(maintain.getFlowId());
            if (flows == null){
                continue;
            }
            ActivitiStatus status = flows.getStatus();
            if (status.equals(ActivitiStatus.open)) {
                return maintain;
            }
        }
        return null;
        //return selectOne(new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("version desc"));
    }
 
    @Override
    public Maintain getRecentHistoryVersion(String tableName) {
        Maintain nowMaintain = null;
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().eq("table_name", tableName).isNotNull("flow_id").orderBy("order_no desc");
        List<Maintain> maintains = selectList(versionWrapper);
        for (Maintain maintain : maintains) {
            Flows flows = flowsService.selectById(maintain.getFlowId());
            if (flows == null){
                continue;
            }
            ActivitiStatus status = flows.getStatus();
            if (status.equals(ActivitiStatus.open) && nowMaintain == null) {
                nowMaintain = maintain;
                continue;
            }
            if (!status.equals(ActivitiStatus.open) && nowMaintain != null){
                nowMaintain = null;
            }
        }
        if (nowMaintain == null) {
            nowMaintain = selectOne(new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no desc"));
        }
        int orderNo = nowMaintain.getOrderNo();
        Maintain nowVersion = getNowVersion(tableName);
        if (nowVersion != null && nowVersion.getOrderNo() == orderNo) {
            orderNo--;
        }
        Maintain maintain = selectOne(new EntityWrapper<Maintain>().eq("table_name", nowMaintain.getTableName()).eq("order_no", orderNo));
        return maintain;
    }
 
    @Override
    public Maintain getMaintainFromVersion(String tableName, String verison) {
        Wrapper<Maintain> versionWrapper = new EntityWrapper<Maintain>().eq("version", verison).eq("table_name", tableName);
        Maintain maxVersion = selectOne(versionWrapper);
        return maxVersion;
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperator(String tableTempName, String maintaInId, String where) {
        return  maintainMapper.selectVersionOperator(tableTempName, where, maintaInId);
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperator(String tableTempName, String maintaInId) {
        return  maintainMapper.selectVersionOperator(tableTempName, Constant.WHERE_DEFAULT, maintaInId);
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperatorByType(String tableTempName, String maintaInId, Operate operate, String where) {
        return  maintainMapper.selectVersionOperatorByType(tableTempName, where, maintaInId, operate.name());
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperatorByType(String tableTempName, String maintaInId, Operate operate) {
        return  maintainMapper.selectVersionOperatorByType(tableTempName, Constant.WHERE_DEFAULT, maintaInId, operate.name());
    }
 
    @Override
    public long countVersionOperator(String tableTempName, String maintaInId, String whereSegment) {
        return maintainMapper.countVersionOperator(tableTempName, maintaInId, whereSegment);
    }
 
    @Override
    public long countVersionOperator(String tableTempName, String maintaInId) {
        return maintainMapper.countVersionOperator(tableTempName, maintaInId, Constant.WHERE_DEFAULT);
    }
 
 
    @Override
    public long countVersionOperatorByType(String tableTempName, String filterSegment, String maintaInId, Operate operate) {
        return maintainMapper.countVersionOperatorByType(tableTempName,filterSegment, maintaInId,operate.name());
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperatorByPage(String tableTempName, String maintaInId, String limit, String where) {
        return maintainMapper.selectVersionOperatorByPage(tableTempName, maintaInId, limit, where);
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperatorByPage(String tableTempName, String maintaInId, String limit) {
        return maintainMapper.selectVersionOperatorByPage(tableTempName, Constant.WHERE_DEFAULT, maintaInId, limit);
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperatorByTypeByPage(String tableTempName, String maintaInId, Operate operate, String limit) {
        return maintainMapper.selectVersionOperatorByTypeByPage(tableTempName, Constant.WHERE_DEFAULT, maintaInId, operate.name(), limit);
    }
 
    @Override
    public List<Map<String, Object>> selectVersionOperatorByTypeByPage(String tableTempName, String maintaInId, Operate operate, String limit, String where) {
        return maintainMapper.selectVersionOperatorByTypeByPage(tableTempName, where, maintaInId, operate.name(), limit);
    }
 
    @Override
    public List<String> getCompareVersionMaintains(Maintain unFlowMaxVersion, Maintain maintainFromVersion) {
        Integer compare = DbUtils.compareVersion(unFlowMaxVersion, maintainFromVersion);
        ArrayList<String> ids = new ArrayList<>();
        if (compare > 0) {
            getVerionMatintains(unFlowMaxVersion,maintainFromVersion, ids);
        }else if(compare == 0) {
            ids.add(unFlowMaxVersion.getId());
        }else {
            getVerionMatintains(maintainFromVersion,unFlowMaxVersion, ids);
        }
        return ids;
    }
 
    @Override
    public Result tempDataByVersionByFlow(TUser user, String flowId, String whereSegment, Integer pageNo, Integer pageSize) {
        Flows flows = flowsService.selectById(flowId);
        if (flows == null) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
        String businessId = flows.getBusinessId();
        Maintain maintain = selectById(businessId);
        if (maintain == null) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
        String fields  = masterDataService.getTempFields(user, maintain.getTableName());
        String tableTempName = maintain.getTableName() + Constant.RECORD;
        long total = maintainMapper.countTempDataByVersionByFlow(tableTempName, flowId, whereSegment);
 
        Page page = new Page(total);
        if (pageNo != null) {
            page.setPageNo(pageNo);
            if (pageSize != null) {
                page.setPageSize(pageSize);
            }
        } else {
            page.setPageNo(1);
        }
 
        List<Map<String, Object>> maps = maintainMapper.tempDataByVersionByFlow(tableTempName, flowId, fields, whereSegment, page.getLimitSQL());
 
        JSONObject object = new JSONObject();
 
        object.fluentPut("total", page.getRecordCount());
        object.fluentPut("passiveQueueSize", page.getPageSize());
        object.fluentPut("pages", page.getPageCount());
        object.fluentPut("current", page.getPageNo());
        object.fluentPut("record", maps);
 
        return Result.success(object);
    }
 
 
 
    private void getVerionMatintains(Maintain firstVersion, Maintain secondVersion, List<String> ids) {
        Integer fromNo = firstVersion.getOrderNo();
        Integer toNo = secondVersion.getOrderNo();
        for (int i = fromNo + 1; i <= toNo; i++) {
            Maintain maintain = selectOne(new EntityWrapper<Maintain>().eq("table_name", firstVersion.getTableName()).eq("order_no", i));
            ids.add(maintain.getId());
        }
    }
}