kimi42345
2020-03-22 d0451fdd55195901e65e5c4b3b64028a86f9e669
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
package com.highdatas.mdm.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Operate;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.pojo.SysAssembleUpdateType;
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.pentaho.reporting.libraries.formula.function.text.StringCountFunction;
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.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author kimi
 * @description
 * @date 2019-12-17 12:02
 */
 
@RestController
@RequestMapping("/master")
public class MasterDataController {
    @Autowired
    MasterDataService masterDataService;
 
    @Autowired
    IMaintainService maintainService;
    @Autowired
    IMaintainDetailService maintainDetailService;
 
    @Autowired
    IMenuMappingService menuMappingService;
 
    @Autowired
    TableInfoMapper tableInfoMapper;
 
    @Autowired
    IMasterModifiedService masterModifiedService;
 
    @RequestMapping(value = "{tableName}/uploaded",  method = RequestMethod.GET)
    public Result get(@PathVariable String tableName, HttpServletRequest request) {
        String uploadTypeStr = request.getParameter("uploadType");
        SysAssembleUpdateType uploadType;
        if (StringUtils.isEmpty(uploadTypeStr)) {
            uploadType = SysAssembleUpdateType.Increment;
        }
        uploadType = SysAssembleUpdateType.valueOf(uploadTypeStr);
        TUser user = (TUser) request.getSession().getAttribute(Constant.USER);
        Maintain maintain = masterDataService.uploadedData(tableName, uploadType, user.getUserId());
        // 处理关联人
        masterModifiedService.dealAssemble(maintain.getId(), user.getUserId(), true);
        JSONObject object = new JSONObject();
        object.fluentPut("maintainId",maintain.getId());
        object.fluentPut("version",maintain.getVersion());
        return Result.success(object);
    }
 
 
    @RequestMapping(value = "{tableName}/get/{id}",  method = RequestMethod.GET)
    public Result get(@PathVariable String tableName, @PathVariable String id) {
        return masterDataService.selectById(tableName,id);
    }
 
    @RequestMapping(value = "{tableName}/all/{pageNo}",  method = RequestMethod.GET)
    public Result getByPage(@PathVariable String tableName, @PathVariable Integer pageNo, HttpServletRequest request) throws UnsupportedEncodingException {
        String pageSizeStr = request.getParameter("pageSize");
        String whereSegment = request.getParameter(Constant.WHERE_SEGMENT);
        String fields = request.getParameter(Constant.FIELDS);
        if (StringUtils.isEmpty(whereSegment)) {
            whereSegment = Constant.WHERE_DEFAULT;
        }else {
            whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
        }
        String version = request.getParameter(Constant.VERSION);
        String tableByName = tableInfoMapper.selectTableByName(tableName);
        if (StringUtils.isEmpty(tableByName)) {
            return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
        }
 
        boolean findMax = false;
        if (StringUtils.isEmpty(version)) {
            Maintain nowVersion = maintainService.getNowVersion(tableName);
            if(nowVersion == null) {
                return Result.error(new CodeMsg(100001,"无当前版本"));
            }
            version = String.valueOf(nowVersion.getVersion());
        }else if(version.equalsIgnoreCase(Constant.MAX)){
            findMax = true;
            version = String.valueOf(maintainService.getMaxVersion(tableName).getVersion());
        }
 
        if (StringUtils.isEmpty(fields)) {
            if (StringUtils.isEmpty(pageSizeStr)) {
                return masterDataService.selectListByPageByVersion(tableName, whereSegment,pageNo,version, findMax);
            }
            return masterDataService.selectListByPageByVersion(tableName, null, whereSegment,pageNo, Integer.valueOf(pageSizeStr),version,findMax);
        }else {
            String[] split = fields.split(Constant.COMMA_TRIM);
            List<String> fieldList = Arrays.stream(split).collect(Collectors.toList());
            if (StringUtils.isEmpty(pageSizeStr)) {
                return masterDataService.selectListByPageByVersion(tableName, fieldList, whereSegment,pageNo, null,version, findMax);
            }
            return masterDataService.selectListByPageByVersion(tableName, fieldList, whereSegment,pageNo, Integer.valueOf(pageSizeStr), findMax);
 
        }
    }
    @RequestMapping(value = "{tableName}/all",  method = RequestMethod.GET)
    public Result<MenuMapping> getAll(@PathVariable String tableName,HttpServletRequest request) {
        String whereSegment = request.getParameter(Constant.WHERE_SEGMENT);
        String version = request.getParameter(Constant.VERSION);
        if (StringUtils.isEmpty(version)) {
            version = String.valueOf(maintainService.getNowVersion(tableName).getVersion());
        }
        if (StringUtils.isEmpty(whereSegment)) {
            return masterDataService.selectList(tableName);
        }
        return masterDataService.selectList(tableName,whereSegment);
    }
 
    @RequestMapping(value = "{maintainId}/modify/{operateStr}", method = RequestMethod.POST)
    public Result modify(@RequestParam String datas, @PathVariable String maintainId, @PathVariable String operateStr,  @RequestParam String modify, HttpServletRequest request) throws UnsupportedEncodingException {
        Result result = null;
        Operate operate = Operate.parse(operateStr);
        Maintain maintain = maintainService.selectById(maintainId);
        List<String> modifyFields = null;
        if (!StringUtils.isEmpty(modify)) {
            String[] split = modify.split(Constant.SEMICOLON);
            modifyFields = Arrays.stream(split).collect(Collectors.toList());
        }
        TUser user = (TUser) request.getSession().getAttribute("user");
 
        switch (operate) {
            case delete:
                //datas is id;
                Integer deleteCount = tableInfoMapper.deleteRecordByMaintainId(maintain.getTableName() + Constant.RECORD, DbUtils.quotedStr(datas), DbUtils.quotedStr(maintainId));
                result = Result.success(deleteCount);
                break;
            case update:
                JSONObject jsonObject = JSONObject.parseObject(datas);
                String id = (String) jsonObject.get("id");
 
                masterModifiedService.updateModifiedUserById(maintainId, id, user.getUserId(), modifyFields);
                jsonObject.remove(Constant.ID);
                datas = jsonObject.toJSONString();
                String updateSegment = masterDataService.getUpdateSegment(maintain.getTableName(), maintain.getTableName() + Constant.RECORD, datas);
 
                Integer updateCount = tableInfoMapper.updateRecordByMaintainId(maintain.getTableName() + Constant.RECORD, updateSegment, DbUtils.quotedStr(maintainId));
                result = Result.success(updateCount);
                break;
            case create:
                JSONObject createObject = JSONObject.parseObject(datas);
                String recordId = DbUtils.getUUID();
 
                masterModifiedService.updateModifiedUserById(maintainId, recordId, user.getUserId(), modifyFields);
 
                createObject.fluentPut(Constant.ID, recordId);
                createObject.fluentPut(Constant.STD_ID, DbUtils.getUUID());
                datas = createObject.toJSONString();
 
                MaintainDetail maintainDetail = new MaintainDetail();
                maintainDetail.setParentId(maintainId);
                maintainDetail.setId(DbUtils.getUUID());
                maintainDetail.setPreMergeId(recordId);
                maintainDetail.setCreateTime(new Date());
                maintainDetail.setOperate(Operate.create);
 
                result = masterDataService.insert(maintain.getTableName() + Constant.RECORD, datas);
                if (result.getSuccess()) {
                    maintainDetail.insert();
                }
                break;
                default:
                    result = Result.error(CodeMsg.OPERATR_ERROR);
                    break;
        }
 
        return result;
    }
 
 
    @RequestMapping(value = "{tableName}/{operateStr}", method = RequestMethod.POST)
    public Result insert(@PathVariable String tableName,@PathVariable String operateStr, @RequestParam String datas, @RequestParam String modify, HttpServletRequest request) {
        String fields = request.getParameter("fields");
        Operate operate = Operate.parse(operateStr);
        TUser user = (TUser) request.getSession().getAttribute("user");
        String userId = user.getUserId();
        Maintain maintain = maintainService.getMaxVersion(tableName);
        Maintain nowMaintain = maintainService.getNextMaintain(tableName, userId);
 
        List<String> modifyFields = null;
        if (!StringUtils.isEmpty(modify)) {
            String[] split = modify.split(Constant.SEMICOLON);
            modifyFields = Arrays.stream(split).collect(Collectors.toList());
        }
 
        Result result;
        if (operate.equals(Operate.delete)) {
            String nowMaintainId = nowMaintain.getId();
            String tempId = tableInfoMapper.getTempIdByStdId(tableName + Constant.RECORD,DbUtils.quotedStr(datas), DbUtils.quotedStr(nowMaintainId));
            if (!StringUtils.isEmpty(tempId)){
                maintainDetailService.delete(new EntityWrapper<MaintainDetail>().eq("parent_id", maintain.getId()).eq("pre_merge_id",tempId));
                result = masterDataService.deleteById(tableName + Constant.RECORD, tempId);
                return result;
            }else {
                result = masterDataService.selectById(tableName, datas);
                Object data = result.getData();
                datas = JSON.toJSONString(data);
                JSONObject object = JSON.parseObject(datas);
                Object grid = object.get("grid");
                datas = JSON.toJSONString(grid);
            }
        } else if (operate.equals(Operate.update)) {
            JSONObject jsonObject = JSONObject.parseObject(datas);
            Object id = jsonObject.get("id");
 
            String nowMaintainId = nowMaintain.getId();
            String tempId = tableInfoMapper.getTempIdByStdId(tableName + Constant.RECORD,DbUtils.quotedStr(id.toString()), DbUtils.quotedStr(nowMaintainId));
            if (StringUtils.isEmpty(tempId)) {
                result = masterDataService.selectById(tableName, id.toString());
            } else {
                masterModifiedService.updateModifiedUserById(nowMaintain.getId(), id.toString(), user.getUserId(), modifyFields);
                jsonObject.put(Constant.STD_ID, id);
                jsonObject.put(Constant.ID, tempId);
                return masterDataService.updateById(tableName + Constant.RECORD, JSON.toJSONString(jsonObject), tempId,false);
            }
        }
 
 
        //merge 2 temp
        String tableTempName = tableName + Constant.RECORD;
 
        JSONObject object = JSON.parseObject(datas);
        Object dataId = object.get(Constant.ID);
        if (dataId == null) {
            dataId = DbUtils.getUUID();
        }
        masterModifiedService.updateModifiedUserById(nowMaintain.getId(), dataId.toString(), user.getUserId(), modifyFields);
        object.put(Constant.STD_ID, dataId);
        object.put(Constant.ID, DbUtils.getUUID());
        datas = JSON.toJSONString(object);
        if (StringUtils.isEmpty(fields)) {
            result = masterDataService.insert(tableTempName, datas);
        } else {
            result = masterDataService.insert(tableTempName, fields, datas);
        }
 
        List<String> ids = (List<String>) result.getData();
 
        nowMaintain.insertOrUpdate();
        for (String id : ids) {
            MaintainDetail maintainDetail = new MaintainDetail();
            maintainDetail.setId(DbUtils.getUUID());
            maintainDetail.setParentId(nowMaintain.getId());
            maintainDetail.setPreMergeId(id);
            maintainDetail.setOperate(operate);
            maintainDetail.setCreateTime(new Date());
            maintainDetail.insert();
        }
 
        result.setData(nowMaintain.getId());
 
        Maintain firstVersionMaintain = maintainService.getMaintainFromVersion(tableName, Constant.VERSION_Default);
 
        MenuMapping menuMapping = menuMappingService.selectOne(new EntityWrapper<MenuMapping>().eq("table_name", firstVersionMaintain.getTableName()));
 
        SysOperateLog operateLog = new SysOperateLog();
        operateLog.setChargeId(user.getUserId()).setId(DbUtils.getUUID())
                .setMaintainId(nowMaintain.getId())
                .setDesp(MessageFormat.format(operate.getLogMessage(), ids.size()))
                .setOperate("更新主题")
                .setCreateTime(new Date())
                .setMenumappingId(menuMapping.getId());
        operateLog.insert();
 
        return result;
 
    }
    @RequestMapping(value = "update/{id}", method = RequestMethod.POST)
    public Result updateById(@PathVariable String tableName,@RequestParam String datas, @PathVariable String id, HttpServletRequest request)  {
        String totalStr = request.getParameter(Constant.TOTAL);
        boolean total = false;
        if (!StringUtils.isEmpty(totalStr)) {
            total = true;
        }
        return masterDataService.updateById(tableName, datas, id, total);
    }
 
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result update(@PathVariable String tableName, @RequestParam String datas, HttpServletRequest request) {
        String whereSegment = request.getParameter(Constant.WHERE_SEGMENT);
        String totalStr = request.getParameter(Constant.TOTAL);
        boolean total = false;
        if (!StringUtils.isEmpty(totalStr)) {
            total = true;
        }
        if (StringUtils.isEmpty(whereSegment)) {
            return masterDataService.update(tableName, datas,total);
        }  else {
            return masterDataService.update(tableName, datas, whereSegment, total);
        }
 
    }
 
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public Result deleteById(@PathVariable String tableName, @PathVariable String id) {
       return masterDataService.deleteById(tableName,id);
    }
 
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public Result delete(@PathVariable String tableName, HttpServletRequest request) {
        String whereSegment = request.getParameter(Constant.WHERE_SEGMENT);
        if (StringUtils.isEmpty(whereSegment)) {
            return masterDataService.delete(tableName);
        } else {
            return masterDataService.delete(tableName, whereSegment);
        }
    }
}