kimi
2020-03-19 807e2c7a2ca8283ba6d6f764c83320ad5e023349
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
package com.highdatas.mdm.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.SysField;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.entity.TableSchemaResult;
import com.highdatas.mdm.mapper.MaintainFieldMapper;
import com.highdatas.mdm.mapper.MasterModifiedMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.ActivitiStatus;
import com.highdatas.mdm.pojo.Segment;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kimi
 * @since 2020-03-11
 */
@Service
public class MasterModifiedServiceImpl implements IMasterModifiedService {
    @Autowired
    IMaintainService maintainService;
    @Autowired
    ISysFieldService fieldService;
    @Autowired
    MasterModifiedMapper masterModifiedMapper;
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    IMaintainFieldService maintainFieldService;
    @Autowired
    ITUserService userService;
    @Autowired
    MaintainFieldMapper maintainFieldMapper;
    @Autowired
    MasterDataService masterDataService;
 
    public static final String master_id = "master_id";
    public static final String pre_record = "pre_record";
    public static final String table_name = "table_name";
    public static final String master_modified = "master_modified";
 
    @Override
    public Map<String, String> getModifiedUserById(String maintainId, String masterId) {
        return null;
    }
 
    @Override
    public List<Map<String, String>> getModifiedUserByIds(String maintainId, List<String> masterId) {
        return null;
    }
 
    @Override
    public boolean updateModifiedUserById(String maintainId, String masterId, String userId, List<String> modifiedFieldList) {
        if (StringUtils.isEmpty(maintainId)) {
            return false;
        }
        try {
            Maintain nowMaintain = maintainService.selectById(maintainId);
            List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintainId);
            if (nowMaintain == null) {
                return false;
            }
            Map<String, String> masterModifiedMap = masterModifiedMapper.selectByTableMasterId(DbUtils.quotedStr(nowMaintain.getTableName()), DbUtils.quotedStr(masterId));
            if (masterModifiedMap == null) {
                masterModifiedMap = new HashMap<>();
                masterModifiedMap.put(table_name, nowMaintain.getTableName());
                masterModifiedMap.put(master_id, masterId);
            }
            String preRecordStr = masterModifiedMap.get(pre_record);
            JSONObject preRecordJSON = null;
            if (!StringUtils.isEmpty(preRecordStr)) {
                preRecordJSON = JSONObject.parseObject(preRecordStr);
            }else  {
                preRecordJSON = new JSONObject();
            }
            for (String field : modifiedFieldList) {
                preRecordJSON.fluentPut(field, userId);
            }
            masterModifiedMap.put(pre_record,  preRecordJSON.toJSONString());
            String id = masterModifiedMap.get(Constant.ID);
            if (StringUtils.isEmpty(id)) {
                masterModifiedMap.put(Constant.ID, DbUtils.getUUID());
                insert(masterModifiedMap);
            }else {
                updateById(masterModifiedMap);
            }
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
 
    private void insert(Map<String, String> masterModifiedMap) {
        String fields = masterModifiedMap.keySet().stream().collect(Collectors.joining(Constant.COMMA));
        String values = masterModifiedMap.values().stream().map(s -> DbUtils.quotedStr(s)).collect(Collectors.joining(Constant.COMMA));
        tableInfoMapper.insert(master_modified, fields, DbUtils.bracketStr(values));
    }
 
    private void updateById(Map<String, String> masterModifiedMap) {
        String id = masterModifiedMap.get(Constant.ID);
        Segment whereSegment = new Segment(Constant.ID, id);
        List<Segment> updateSgement = DbUtils.map2Segment(masterModifiedMap);
        String updateStr = updateSgement.stream().map(segment -> segment.toString()).collect(Collectors.joining(Constant.COMMA));
        tableInfoMapper.update(master_modified, updateStr, whereSegment.toString());
    }
 
    @Override
    @Async
    public void dealFlow(String maintainId, ActivitiStatus status) {
        Maintain maintain = maintainService.selectById(maintainId);
        String tableName = maintain.getTableName();
        String recordTableName = tableName + Constant.RECORD;
        List<Map<String,String>> modifiedList = masterModifiedMapper.selectByMaintainId(recordTableName, DbUtils.quotedStr(maintainId));
        switch (status) {
            case open:
                for (Map<String, String> masterModifiedMap : modifiedList) {
                    String preJson = masterModifiedMap.get(pre_record);
                    JSONObject jsonObject = JSONObject.parseObject(preJson);
                    if (jsonObject == null){
                        continue;
                    }
                    Set<String> keySet = jsonObject.keySet();
                    HashMap<String, String> userMap = new HashMap<>();
                    for (String field : keySet) {
                        String userId = jsonObject.getString(field);
                        if (userMap.containsKey(userId)) {
                            String preFieldStr = userMap.get(userId);
                            field = preFieldStr + Constant.SEMICOLON  + field;
                        }
                        userMap.put(userId, field);
                    }
                    List<TableSchemaResult> tableField = tableInfoMapper.getTableField(master_modified);
                    List<String> existsFieldList = tableField.stream().map(TableSchemaResult::getFieldName).collect(Collectors.toList());
                    Set<String> userIdList = userMap.keySet();
 
                    if (!existsFieldList.containsAll(userIdList)) {
                        //新增之前不存在的用户
                        List<String> unExistsUserList = userIdList.stream().filter(s -> !existsFieldList.contains(s)).collect(Collectors.toList());
                        for (String s : unExistsUserList) {
                            //新增字段
                            maintainFieldMapper.addCloumn(master_modified, s, Constant.Text);
                        }
                    }
                    masterModifiedMap.putAll(userMap);
                    updateById(masterModifiedMap);
                }
                break;
            case close:
                List<String> unUserIdList = new ArrayList<>();
                unUserIdList.add(Constant.ID);
                unUserIdList.add(master_id);
                unUserIdList.add(table_name);
                unUserIdList.add(pre_record);
                for (Map<String, String> masterModifiedMap : modifiedList) {
                    //todo   反更新 perjson  获取当前表所有可更改的用户 之后待修改 先从本地user表中拿
                    Set<String> keySet = masterModifiedMap.keySet();
                    List<String> userIdList = keySet.stream().filter(s -> !unUserIdList.contains(s)).collect(Collectors.toList());
                    JSONObject jsonObject = new JSONObject();
                    for (String s : userIdList) {
                        String fields = masterModifiedMap.get(s);
                        if (StringUtils.isEmpty(fields)) {
                            continue;
                        }
                        String[] split = fields.split(Constant.SEMICOLON);
                        for (String field : split) {
                            jsonObject.fluentPut(field, s);
                        }
                    }
                    masterModifiedMap.put(pre_record, jsonObject.toJSONString());
                    updateById(masterModifiedMap);
                }
                break;
        }
    }
 
    @Override
    public void dealAssemble(String maintainId, String userId, boolean audit) {
        List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintainId);
        String fieldStr = fieldByMaintain.stream().map(SysField::getField).collect(Collectors.joining(Constant.SEMICOLON));
        List<TableSchemaResult> tableField = tableInfoMapper.getTableField(master_modified);
        long count = tableField.stream().map(tableSchemaResult -> tableSchemaResult.getFieldName()).filter(s -> s.equalsIgnoreCase(userId)).count();
        if (count == 0) {
            maintainFieldMapper.addCloumn(master_modified, userId, Constant.Text);
        }
 
        Maintain maintain = maintainService.selectById(maintainId);
        String tableName = maintain.getTableName();
        String recordTableName = tableName + Constant.RECORD;
        if (!audit) {
            masterModifiedMapper.insertUserByTableNameUserId(DbUtils.quotedStr(maintainId), DbUtils.quotedStr(tableName), recordTableName, userId, DbUtils.quotedStr(fieldStr));
        }
       else {
            JSONObject jsonObject = new JSONObject();
            for (SysField field : fieldByMaintain) {
                String oneField = field.getField();
                jsonObject.fluentPut(oneField, userId);
            }
 
            masterModifiedMapper.insertJsonByTableNameUserId(DbUtils.quotedStr(maintainId), DbUtils.quotedStr(tableName), recordTableName, DbUtils.quotedStr(jsonObject.toJSONString()));
        }
    }
 
}