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.Character;
|
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 com.highdatas.mdm.util.NoticeClient;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.transaction.annotation.Transactional;
|
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
|
@Slf4j
|
@RequestMapping("/master")
|
public class MasterDataController {
|
@Autowired
|
MasterDataService masterDataService;
|
|
@Autowired
|
IMaintainService maintainService;
|
@Autowired
|
IMaintainDetailService maintainDetailService;
|
@Autowired
|
IMaintainFieldService maintainFieldService;
|
@Autowired
|
IMenuMappingService menuMappingService;
|
|
@Autowired
|
TableInfoMapper tableInfoMapper;
|
|
@Autowired
|
IMasterModifiedService masterModifiedService;
|
@Autowired
|
IMasterAuthorService masterAuthorService;
|
@Autowired
|
ISysMenuService menuService;
|
@Autowired
|
NoticeClient noticeClient;
|
|
/**
|
*
|
* @description: 上传后,关联版本信息
|
* @param tableName 表名
|
* @return 是否关联成功
|
*
|
*/
|
@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;
|
}else {
|
uploadType = SysAssembleUpdateType.valueOf(uploadTypeStr);
|
}
|
|
TUser user = DbUtils.getUser(request);
|
//上传数据后的操作,关联版本
|
Maintain maintain = masterDataService.uploadedData(tableName, uploadType, user.getUserId());
|
// 处理关联人
|
//masterModifiedService.dealAssemble(maintain.getId(), user.getUserId(), true);
|
//添加 std_id 索引
|
String recordTableName = tableName + Constant.RECORD;
|
masterDataService.createIdx(recordTableName);
|
|
JSONObject object = new JSONObject();
|
object.fluentPut("maintainId",maintain.getId());
|
object.fluentPut("version",maintain.getVersion());
|
return Result.success(object);
|
}
|
|
/**
|
*
|
* @description: 通过表名和id获取具体信息
|
* @param tableName 表名
|
* @param id 数据id
|
* @return 具体数据
|
*
|
*/
|
@RequestMapping(value = "{tableName}/get/{id}", method = RequestMethod.GET)
|
public Result get(@PathVariable String tableName, @PathVariable String id) {
|
return masterDataService.selectById(tableName,id);
|
}
|
/**
|
*
|
* @description: 通过表名分页获取数据
|
* @param tableName 表名
|
* @param pageNo 页数
|
* @return 具体数据
|
*
|
*/
|
@RequestMapping(value = "{tableName}/all/{pageNo}", method = RequestMethod.GET)
|
public Result getByPage(@PathVariable String tableName, @PathVariable Integer pageNo, HttpServletRequest request) throws UnsupportedEncodingException {
|
String uuid = DbUtils.getUUID();
|
Date start = new Date();
|
Character character = DbUtils.getCharacter(request);
|
Date end = new Date();
|
log.info(MessageFormat.format("master tag:{0} MASTER-- form userID selct user:{1} ms",uuid, (end.getTime() - start.getTime())));
|
|
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;
|
Maintain maxVersion = maintainService.getMaxVersion(tableName);
|
if (maxVersion == null) {
|
return Result.success(null);
|
}
|
version = String.valueOf(maxVersion.getVersion());
|
}
|
Maintain maintainFromVersion = maintainService.getMaintainFromVersion(tableName, version);
|
if (maintainFromVersion == null) {
|
return null;
|
}
|
//ADD Filter
|
start = new Date();
|
log.info(MessageFormat.format("master tag:{0} MASTER-- select maintain:{1} ms",uuid, (start.getTime() - end.getTime())));
|
|
String filter = masterAuthorService.getFilter(character, maintainFromVersion.getId(), uuid);
|
if (!StringUtils.isEmpty(filter)) {
|
whereSegment = DbUtils.StrJoin(whereSegment, Constant.AND, filter);
|
}
|
end = new Date();
|
log.info(MessageFormat.format("master tag:{0} MASTER-- select filter:{1} ms",uuid, (end.getTime() - start.getTime())));
|
|
if (StringUtils.isEmpty(fields)) {
|
if (StringUtils.isEmpty(pageSizeStr)) {
|
return masterDataService.selectListByPageByVersion(character, tableName, whereSegment,pageNo,version, findMax,uuid);
|
}
|
return masterDataService.selectListByPageByVersion(character, tableName, null, whereSegment,pageNo, Integer.valueOf(pageSizeStr),version,findMax, uuid);
|
}else {
|
String[] split = fields.split(Constant.COMMA_TRIM);
|
List<String> fieldList = Arrays.stream(split).collect(Collectors.toList());
|
if (StringUtils.isEmpty(pageSizeStr)) {
|
return masterDataService.selectListByPageByVersion(character, tableName, fieldList, whereSegment,pageNo, null,version, findMax, uuid);
|
}
|
return masterDataService.selectListByPageByVersion(character, tableName, fieldList, whereSegment,pageNo, Integer.valueOf(pageSizeStr), findMax, uuid);
|
|
}
|
}
|
/**
|
*
|
* @description: 通过表名获取数据
|
* @param tableName 表名
|
* @return 具体数据
|
*
|
*/
|
@RequestMapping(value = "{tableName}/all", method = RequestMethod.GET)
|
public Result<MenuMapping> getAll(@PathVariable String tableName,HttpServletRequest request) {
|
TUser user = DbUtils.getUser(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(user, tableName,Constant.WHERE_DEFAULT, version);
|
}
|
return masterDataService.selectList(user, tableName,whereSegment, version);
|
}
|
|
/**
|
*
|
* @description: 通过表名和操作类型更新待修改数据
|
* @param maintainId 未生效的版本id
|
* @param operateStr 操作类型
|
* @return 具体数据
|
*
|
*/
|
@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 = DbUtils.getUser(request);
|
|
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;
|
}
|
|
/**
|
*
|
* @description: 修改一条数据, 供在线编辑使用
|
* @param tableName 表名
|
* @param operateStr 操作类型
|
* @return 具体数据
|
*
|
*/
|
@RequestMapping(value = "{tableName}/{operateStr}", method = RequestMethod.POST)
|
@Transactional(rollbackFor = {RuntimeException.class, Error.class})
|
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 = DbUtils.getUser(request);
|
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()));
|
String menuId = menuMapping.getMenuId();
|
SysMenu menu = menuService.selectById(menuId);
|
if (menu != null) {
|
LinkedHashSet<String> parentIdSet = new LinkedHashSet<>();
|
parentIdSet.add(menuId);
|
LinkedHashSet<String> byParentId = menuService.getByParentId(parentIdSet);
|
if (byParentId == null) {
|
noticeClient.EditMasterData(menu, null, user.getUserId(), operate);
|
} else {
|
if (!byParentId.isEmpty()) {
|
List<SysMenu> sysMenus = menuService.selectBatchIds(byParentId);
|
SysMenu parentMenu = sysMenus.get(0);
|
noticeClient.EditMasterData(menu, parentMenu, user.getUserId(),operate);
|
}
|
|
}
|
}
|
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;
|
|
}
|
|
/**
|
*
|
* @description: 直接更新一条数据
|
* @param tableName 表名
|
* @param id 数据id
|
* @return 具体数据
|
*
|
*/
|
@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);
|
}
|
|
}
|
/**
|
*
|
* @description: 直接删除一条数据
|
* @param tableName 表名
|
* @param id 数据id
|
* @return 删除结果
|
*
|
*/
|
@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
|
public Result deleteById(@PathVariable String tableName, @PathVariable String id) {
|
return masterDataService.deleteById(tableName,id);
|
}
|
|
/**
|
*
|
* @description: 通过筛选条件删除数据
|
* @param tableName 表名
|
* @return 具体数据
|
*
|
*/
|
@RequestMapping(value = "delete", method = RequestMethod.GET)
|
public Result delete(@PathVariable String tableName, HttpServletRequest request) {
|
//whereSegment 筛选条件
|
String whereSegment = request.getParameter(Constant.WHERE_SEGMENT);
|
if (StringUtils.isEmpty(whereSegment)) {
|
return masterDataService.delete(tableName);
|
} else {
|
return masterDataService.delete(tableName, whereSegment);
|
}
|
}
|
/**
|
*
|
* @description: 通过字段版本获取某个字段的所有的数据
|
* @param maintainFieldId 字段版本
|
* @param menuId 主题id
|
* @param field 字段
|
* @return 具体数据
|
*
|
*/
|
@RequestMapping(value = "/getValByMaintainFieldId/{maintainFieldId}", method = RequestMethod.GET)
|
public Result getValByMaintainFieldId(@PathVariable String maintainFieldId,@RequestParam String menuId, @RequestParam String field, HttpServletRequest request) {
|
String tableName = menuMappingService.getTableNameByMenu(menuId);
|
if (StringUtils.isEmpty(tableName)) {
|
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
|
}
|
if (maintainFieldId.equalsIgnoreCase(Constant.All)) {
|
Set<String> fieldValList = masterDataService.getFieldValByTable(tableName, field);
|
return Result.success(fieldValList);
|
}
|
|
Set<String> fieldValByMaintainField = masterDataService.getFieldValByMaintainField(maintainFieldId, field, tableName);
|
|
return Result.success(fieldValByMaintainField);
|
}
|
|
|
}
|