| | |
| | | import com.highdatas.mdm.pojo.*; |
| | | import com.highdatas.mdm.service.*; |
| | | import com.highdatas.mdm.util.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.ibatis.session.SqlSessionFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | */ |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class MasterDataServiceImpl implements MasterDataService { |
| | | @Autowired |
| | | IMenuMappingService menuMappingService; |
| | |
| | | SqlSessionFactory sqlSessionFactory; |
| | | @Resource |
| | | protected DataSource dataSource; |
| | | @Autowired |
| | | AntianaphylaxisClient antianaphylaxisClient; |
| | | |
| | | |
| | | |
| | | @Override |
| | | public Result selectById(String tableName, String id) { |
| | |
| | | |
| | | String fields = getFields(character,tableName, maintain); |
| | | String Hfields = getFields(character, tableName, Constant.H, maintain); |
| | | String tempFields = getTempFields(character, tableName, maintain); |
| | | String tempFields = getTempFields(character, tableName, Constant.MT, maintain); |
| | | String A1fields = getFields(character, tableName, Constant.A1, maintain); |
| | | String A2fields = getFields(character, tableName, Constant.A2, maintain); |
| | | String tempHFields = getTempFields(character, tableName, Constant.H, maintain); |
| | |
| | | |
| | | String fields = getFields(character, tableName, maintain); |
| | | String Hfields = getFields(character, tableName, Constant.H, maintain); |
| | | String tempFields = getTempFields(character, tableName, maintain); |
| | | String tempFields = getTempFields(character, tableName, Constant.MT, maintain); |
| | | String A1fields = getFields(character, tableName, Constant.A1, maintain); |
| | | String A2fields = getFields(character, tableName, Constant.A2, maintain); |
| | | String tempHFields = getTempFields(character, tableName, Constant.H, maintain); |
| | |
| | | public String getFields(Character character, String tableName, Maintain maintain) { |
| | | |
| | | return getFields(character, tableName, null, maintain); |
| | | } |
| | | @Override |
| | | public String getFields(Character character, String tableName, Maintain maintain, String uuid) { |
| | | |
| | | return getFields(character, tableName, null, maintain, uuid); |
| | | } |
| | | |
| | | private String getCaseWhenFields(String tableName) { |
| | |
| | | } |
| | | return contentBuilder.toString(); |
| | | } |
| | | |
| | | @Override |
| | | public String getFields(Character character, String tableName, String alias, Maintain maintain) { |
| | | return getFields(character, tableName, alias, maintain, null); |
| | | } |
| | | @Override |
| | | public String getFields(Character character, String tableName, String alias, Maintain maintain, String uuid) { |
| | | List<SysField> fields; |
| | | if (character == null) { |
| | | fields = fieldService.getFieldByMaintain(maintain.getId()); |
| | | }else { |
| | | fields = masterAuthorService.getField(character, maintain.getId()); |
| | | fields = masterAuthorService.getField(character, maintain.getId(), uuid); |
| | | } |
| | | ContentBuilder contentBuilder = new ContentBuilder(Constant.COMMA); |
| | | if (fields == null) { |
| | |
| | | if (!fields.contains(Constant.ID)) { |
| | | fields.add(new SysField().setField(Constant.ID)); |
| | | } |
| | | List<String> joinFieldList = getJoinFieldList(); |
| | | |
| | | for (SysField field : fields) { |
| | | String fieldName = field.getField(); |
| | |
| | | if (StringUtils.isEmpty(alias)) { |
| | | contentBuilder.append(MessageFormat.format("{0} as id", Constant.STD_ID)); |
| | | } else { |
| | | contentBuilder.append(MessageFormat.format("{0}.{1} as id", alias, Constant.STD_ID)); |
| | | if (joinFieldList.contains(fieldName)) { |
| | | contentBuilder.append(MessageFormat.format("{0}.{1} as id", alias, Constant.STD_ID)); |
| | | } else { |
| | | contentBuilder.append(MessageFormat.format("{0} as id", Constant.STD_ID)); |
| | | } |
| | | |
| | | } |
| | | continue; |
| | | } |
| | | if (StringUtils.isEmpty(alias)) { |
| | | contentBuilder.append(fieldName); |
| | | } else { |
| | | contentBuilder.append(MessageFormat.format(Constant.Alias,alias,fieldName)); |
| | | if (joinFieldList.contains(fieldName)) { |
| | | contentBuilder.append(MessageFormat.format(Constant.Alias,alias,fieldName)); |
| | | } else { |
| | | contentBuilder.append(fieldName); |
| | | } |
| | | } |
| | | } |
| | | return contentBuilder.toString(); |
| | | } |
| | | |
| | | private List<String> getJoinFieldList() { |
| | | List<TableSchemaResult> fieldList = new ArrayList<>(); |
| | | List<TableSchemaResult> maintainList = mapper.getTableField("maintain"); |
| | | if (maintainList != null) { |
| | | fieldList.addAll(maintainList); |
| | | } |
| | | List<TableSchemaResult> flows = mapper.getTableField("flows"); |
| | | if (flows != null) { |
| | | fieldList.addAll(flows); |
| | | } |
| | | List<TableSchemaResult> maintain_detail = mapper.getTableField("maintain_detail"); |
| | | if (maintain_detail != null) { |
| | | fieldList.addAll(maintain_detail); |
| | | } |
| | | return fieldList.stream().map(tableSchemaResult -> tableSchemaResult.getFieldName()).collect(Collectors.toList()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName, String whereSegment, Integer pageNo, String version, boolean findMax) { |
| | | return selectListByPageByVersion(character, tableName, null,whereSegment, pageNo, null,version, findMax); |
| | | } |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName, String whereSegment, Integer pageNo, String version, boolean findMax, String uuid) { |
| | | return selectListByPageByVersion(character, tableName, null,whereSegment, pageNo, null,version, findMax, uuid); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName, List<String> fieldList, String whereSegment, Integer pageNo, Integer pageSize, String version, boolean findMax) { |
| | | public Long getCountByVersion(Character character, String tableName, String whereSegment, String version, boolean findMax) { |
| | | Maintain maintainFromVersion = maintainService.getMaintainFromVersion(tableName, version); |
| | | Maintain nowVersionMaintain = maintainService.getNowVersion(tableName); |
| | | boolean isMax = maintainService.checkdMaxVersion(maintainFromVersion.getId()); |
| | |
| | | |
| | | Long count; |
| | | |
| | | if (isNow && !findMax){ |
| | | if (isNow && !findMax){ |
| | | count = getCount(tableName, whereSegment); |
| | | } |
| | | else if (isMax && findMax) { |
| | | if (isFirst && nowVersionMaintain == null) { |
| | | nowVersionOrderNo = nowVersionOrderNo - 1; |
| | | } |
| | | |
| | | count = getCountByMaxVersion(character, maintainFromVersion.getId(), tableName, tableTempName, fromVersionOrderNo, nowVersionOrderNo, whereSegment); |
| | | }else { |
| | | count = getCountByVersion(character, maintainFromVersion.getId(), tableName, tableTempName, nowVersionOrderNo, fromVersionOrderNo, whereSegment); |
| | | } |
| | | return count; |
| | | } |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName, List<String> fieldList, String whereSegment, Integer pageNo, Integer pageSize, String version, boolean findMax) { |
| | | return selectListByPageByVersion(character, tableName, fieldList, whereSegment, pageNo, pageSize, version, findMax, null); |
| | | } |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName, List<String> fieldList, String whereSegment, Integer pageNo, Integer pageSize, String version, boolean findMax, String uuid) { |
| | | return selectListByPageByVersion(character, tableName, fieldList, whereSegment, pageNo, pageSize, version, findMax, uuid, null); |
| | | } |
| | | |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName, List<String> fieldList, String whereSegment, Integer pageNo, Integer pageSize, String version, boolean findMax, String uuid, Page page) { |
| | | String fieldStr = ""; |
| | | if (fieldList == null || fieldList.isEmpty()) { |
| | | fieldStr = ""; |
| | | } else { |
| | | fieldList.stream().collect(Collectors.joining(Constant.UnderLine)); |
| | | } |
| | | String key; |
| | | if (page == null) { |
| | | key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(), tableName, whereSegment, String.valueOf(pageNo), String.valueOf(pageSize), version, String.valueOf(findMax)); |
| | | |
| | | } else { |
| | | key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(), tableName, whereSegment, String.valueOf(pageNo), String.valueOf(pageSize), version, String.valueOf(findMax), page.getLimitSQL()); |
| | | } |
| | | |
| | | if (!findMax) { |
| | | Object redisValObj = redisClient.getRedisValObj(key); |
| | | if (redisValObj != null) { |
| | | JSONObject object = (JSONObject) redisValObj; |
| | | Result result = JSONObject.parseObject(object.toJSONString(), Result.class); |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | Maintain maintainFromVersion = maintainService.getMaintainFromVersion(tableName, version); |
| | | Maintain nowVersionMaintain = maintainService.getNowVersion(tableName); |
| | | boolean isMax = maintainService.checkdMaxVersion(maintainFromVersion.getId()); |
| | | boolean isNow = maintainService.checkNowVersion(maintainFromVersion.getId()); |
| | | boolean isFirst = maintainService.checkFirstVersion(maintainFromVersion.getId()); |
| | | |
| | | Integer fromVersionOrderNo = maintainFromVersion.getOrderNo(); |
| | | Integer nowVersionOrderNo = 0; |
| | | if (nowVersionMaintain != null) { |
| | | nowVersionOrderNo = nowVersionMaintain.getOrderNo(); |
| | | } |
| | | |
| | | String tableTempName = tableName + Constant.RECORD; |
| | | |
| | | Long count; |
| | | Date startDate = new Date(); |
| | | if (isNow && !findMax){ |
| | | count = getCount(tableName, whereSegment); |
| | | } |
| | | else if (isMax && findMax) { |
| | |
| | | }else { |
| | | count = getCountByVersion(character, maintainFromVersion.getId(), tableName, tableTempName, nowVersionOrderNo, fromVersionOrderNo, whereSegment); |
| | | } |
| | | if (count == 0) { |
| | | Date endDate = new Date(); |
| | | log.info(MessageFormat.format("master tag:{0} MASTER-- select count:{1} ms",uuid, (endDate.getTime() - startDate.getTime()))); |
| | | |
| | | if (page != null && count != null) { |
| | | page.setRecordCount(count.intValue()); |
| | | } |
| | | if (count == null || count == 0) { |
| | | return Result.success(null); |
| | | } |
| | | //mapper.selectByPageByVersion(tableName, tableTempName, fieldList, whereSegment,); |
| | | Page page = new Page(count); |
| | | if (pageSize != null && pageSize.compareTo(1) != -1) { |
| | | page.setPageSize(pageSize); |
| | | } |
| | | List<Map<String, Object>> result = null; |
| | | if (page.getPageCount() < pageNo) { |
| | | return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND); |
| | | |
| | | if (page == null) { |
| | | page = new Page(count); |
| | | if (pageSize != null && pageSize.compareTo(1) != -1) { |
| | | page.setPageSize(pageSize); |
| | | } |
| | | |
| | | if (page.getPageCount() < pageNo) { |
| | | return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND); |
| | | } |
| | | page.setPageNo(pageNo); |
| | | } |
| | | page.setPageNo(pageNo); |
| | | |
| | | String limitSQL = page.getLimitSQL(); |
| | | String fields; |
| | |
| | | String A2fields; |
| | | |
| | | String tempHfields; |
| | | |
| | | if (fieldList == null || fieldList.size() == 0) { |
| | | fields = getFields(character, tableName, maintainFromVersion); |
| | | Hfields = getFields(character, tableName,Constant.H, maintainFromVersion); |
| | | A1fields = getFields(character, tableName,Constant.A1, maintainFromVersion); |
| | | A2fields = getFields(character, tableName,Constant.A2, maintainFromVersion); |
| | | tempFields = getTempFields(character, tableName, maintainFromVersion); |
| | | tempHfields = getTempFields(character, tableName, Constant.H, maintainFromVersion); |
| | | fields = getFields(character, tableName, maintainFromVersion, uuid); |
| | | List<String> fieldStrList = DbUtils.split(fields, Constant.COMMA); |
| | | |
| | | Hfields = fieldStrList.stream().map(s -> MessageFormat.format(Constant.Alias,Constant.H, s)).collect(Collectors.joining(Constant.COMMA)); |
| | | A1fields = fieldStrList.stream().map(s -> MessageFormat.format(Constant.Alias,Constant.A1, s)).collect(Collectors.joining(Constant.COMMA)); |
| | | A2fields = fieldStrList.stream().map(s -> MessageFormat.format(Constant.Alias,Constant.A2, s)).collect(Collectors.joining(Constant.COMMA)); |
| | | tempFields = fieldStrList.stream() |
| | | .map(s -> |
| | | s.equalsIgnoreCase(Constant.ID) ? MessageFormat.format("{0} as id",Constant.STD_ID): s) |
| | | .map(s -> MessageFormat.format(Constant.Alias,Constant.MT, s)) |
| | | .collect(Collectors.joining(Constant.COMMA)); |
| | | |
| | | tempHfields = fieldStrList.stream() |
| | | .map(s -> |
| | | s.equalsIgnoreCase(Constant.ID) ? MessageFormat.format("{0} as id",Constant.STD_ID): s) |
| | | .map(s -> MessageFormat.format(Constant.Alias,Constant.H,s)) |
| | | .collect(Collectors.joining(Constant.COMMA)); |
| | | |
| | | } else { |
| | | if (!fieldList.contains(Constant.ID)) { |
| | | fieldList.add(Constant.ID); |
| | | } |
| | | fields = fieldList.stream().collect(Collectors.joining(Constant.COMMA)); |
| | | Hfields = fieldList.stream().map(s -> MessageFormat.format(Constant.Alias,Constant.H, s)).collect(Collectors.joining(Constant.COMMA)); |
| | | A1fields = fieldList.stream().map(s -> MessageFormat.format(Constant.Alias,Constant.A1, s)).collect(Collectors.joining(Constant.COMMA)); |
| | | A2fields = fieldList.stream().map(s -> MessageFormat.format(Constant.Alias,Constant.A2, s)).collect(Collectors.joining(Constant.COMMA)); |
| | | tempFields = fieldList.stream() |
| | | .map(s -> |
| | | s.equalsIgnoreCase(Constant.ID) ? MessageFormat.format("{} as id",Constant.STD_ID): s) |
| | | s.equalsIgnoreCase(Constant.ID) ? MessageFormat.format("{0} as id",Constant.STD_ID): s) |
| | | .map(s -> MessageFormat.format(Constant.Alias,Constant.MT, s)) |
| | | .collect(Collectors.joining(Constant.COMMA)); |
| | | |
| | | tempHfields = fieldList.stream() |
| | | .map(s -> |
| | | s.equalsIgnoreCase(Constant.ID) ? MessageFormat.format("{} as id",Constant.STD_ID): s) |
| | | s.equalsIgnoreCase(Constant.ID) ? MessageFormat.format("{0} as id",Constant.STD_ID): s) |
| | | .map(s -> MessageFormat.format(Constant.Alias,Constant.H,s)) |
| | | .collect(Collectors.joining(Constant.COMMA)); |
| | | |
| | | } |
| | | startDate = new Date(); |
| | | log.info(MessageFormat.format("master tag:{0} MASTER-- select field :{1} ms",uuid, (startDate.getTime() - endDate.getTime()))); |
| | | |
| | | |
| | | if (isNow && !findMax){ |
| | | result = mapper.selectByPage(tableName, fields, whereSegment, page.getLimitSQL()); |
| | | } |
| | |
| | | }else { |
| | | result = mapper.selectByPageByVersion(tableName,tableTempName,fields,tempFields, Hfields, A1fields, A2fields, tempHfields, nowVersionOrderNo, fromVersionOrderNo, DbUtils.quotedStr(maintainFromVersion.getId()), whereSegment, limitSQL); |
| | | } |
| | | endDate = new Date(); |
| | | log.info(MessageFormat.format("master tag:{0} MASTER-- select dataMap :{1} ms",uuid, (endDate.getTime() - startDate.getTime()))); |
| | | |
| | | EntityWrapper<SysField> sysFieldEntityWrapper = new EntityWrapper<>(); |
| | | sysFieldEntityWrapper.eq("table_name", tableName); |
| | | List<SysField> sysFields = fieldService.getFieldByMaintain(maintainFromVersion.getId()); |
| | | List<SysField> sysFields = masterAuthorService.getField(character, maintainFromVersion.getId()); |
| | | if (isMax) { |
| | | sysFields.add(new SysField().setAlias("数据状态").setField("operate").setVisible(true).setFormat("operate_")); |
| | | sysFields.add(new SysField().setAlias("审批状态").setField("status").setVisible(true).setFormat("status_")); |
| | |
| | | |
| | | JSONObject object = new JSONObject(); |
| | | |
| | | Map<String, AntianaphylaxisResult> helpfulField = antianaphylaxisClient.getHelpfulField(fields, tableName); |
| | | antianaphylaxisClient.fixMasterData(result,helpfulField); |
| | | startDate = new Date(); |
| | | log.info(MessageFormat.format("master tag:{0} MASTER-- antianaphy dataMap :{1} ms",uuid, (startDate.getTime() - endDate.getTime()))); |
| | | |
| | | object.fluentPut("total", page.getRecordCount()); |
| | | object.fluentPut("passiveQueueSize", page.getPageSize()); |
| | | object.fluentPut("size", page.getPageSize()); |
| | | object.fluentPut("pages", page.getPageCount()); |
| | | object.fluentPut("current", page.getPageNo()); |
| | | object.fluentPut("record", result); |
| | |
| | | resultobject.fluentPut("grid",object); |
| | | resultobject.fluentPut("fields",sysFields); |
| | | resultobject.fluentPut("maintainId", maintainFromVersion.getId()); |
| | | |
| | | |
| | | redisClient.putRedisValObj(key, Result.success(resultobject)); |
| | | |
| | | return Result.success(resultobject); |
| | | } |
| | | |
| | |
| | | JSONObject object = new JSONObject(); |
| | | |
| | | object.fluentPut("total", page.getRecordCount()); |
| | | object.fluentPut("passiveQueueSize", page.getPageSize()); |
| | | object.fluentPut("size", page.getPageSize()); |
| | | object.fluentPut("pages", page.getPageCount()); |
| | | object.fluentPut("current", page.getPageNo()); |
| | | object.fluentPut("record", result); |
| | |
| | | return Result.success(resultobject); |
| | | } |
| | | |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName,List<String> fieldList, String whereSegment, Integer pageNo, Integer pageSize, boolean findMax, String uuid) { |
| | | return selectListByPageByVersion(character, tableName, fieldList, whereSegment,pageNo,pageSize,String.valueOf(maintainService.getNowVersion(tableName).getVersion()), findMax, uuid); |
| | | } |
| | | @Override |
| | | public Result selectListByPageByVersion(Character character, String tableName,List<String> fieldList, String whereSegment, Integer pageNo, Integer pageSize, boolean findMax) { |
| | | return selectListByPageByVersion(character, tableName, fieldList, whereSegment,pageNo,pageSize,String.valueOf(maintainService.getNowVersion(tableName).getVersion()), findMax); |
| | |
| | | |
| | | public Result insert(String tableName, String json) { |
| | | try { |
| | | List<SysField> fieldByTable = fieldService.getFieldByTable(tableName); |
| | | |
| | | List<String> fieldNameList = fieldByTable.stream().map(sysField -> sysField.getField()).collect(Collectors.toList()); |
| | | String fields = fieldByTable.stream().map(sysField -> sysField.getField()).collect(Collectors.joining(Constant.COMMA)); |
| | | |
| | | JSONObject dataObject = JSON.parseObject(json); |
| | | Set<String> keySet = dataObject.keySet(); |
| | | List<String> fieldNameList = new ArrayList<>(keySet); |
| | | // List<SysField> fieldByTable = fieldService.getFieldByTable(tableName); |
| | | // List<String> fieldNameList = fieldByTable.stream().map(sysField -> sysField.getField()).collect(Collectors.toList()); |
| | | String fields = keySet.stream().collect(Collectors.joining(Constant.COMMA)); |
| | | ContentBuilder builder = new ContentBuilder(Constant.COMMA); |
| | | Object parse = JSON.parse(json); |
| | | List<String> ids = new ArrayList<>(); |
| | |
| | | public Set<String> getFieldValByTable(String tableName, String field) { |
| | | //查缓存 |
| | | String key = DbUtils.getFieldRedisKey(tableName,field); |
| | | String redisVal = redisClient.getRedisVal(key); |
| | | if (!StringUtils.isEmpty(redisVal)) { |
| | | List<String> valList = DbUtils.split(redisVal); |
| | | if (valList != null) { |
| | | return new HashSet<String>(valList); |
| | | List<Object> redisList = redisClient.getRedisList(key); |
| | | if (redisList != null) { |
| | | HashSet<String> strings = new HashSet<>(); |
| | | for (Object o : redisList) { |
| | | strings.add(o.toString()); |
| | | } |
| | | return strings; |
| | | } |
| | | // 读数据库 |
| | | String tempTableName = tableName + Constant.RECORD; |
| | |
| | | .map(one -> one.get(field)) |
| | | .filter(o -> o != null) |
| | | .map(o -> o.toString()).collect(Collectors.toSet()); |
| | | redisClient.putRedisList(key, new ArrayList(result)); |
| | | return result; |
| | | } |
| | | |
| | |
| | | //查缓存 |
| | | MaintainField maintainField = maintainFieldService.selectById(maintainFieldId); |
| | | |
| | | String key = DbUtils.getFieldRedisKey(tableName, field); |
| | | String redisVal = redisClient.getRedisVal(key); |
| | | if (!StringUtils.isEmpty(redisVal)) { |
| | | List<String> valList = DbUtils.split(redisVal); |
| | | if (valList != null) { |
| | | HashSet<String> strings = new HashSet<>(valList); |
| | | return strings; |
| | | } |
| | | } |
| | | |
| | | // 读数据库 |
| | | List<Maintain> list = maintainFieldService.getMaintainByMaintainField(maintainFieldId, tableName); |
| | | Maintain max = list.stream().max(new Comparator<Maintain>() { |
| | |
| | | .filter(o -> o != null) |
| | | .map(o -> o.toString()).collect(Collectors.toSet()); |
| | | String val = result.stream().collect(Collectors.joining(Constant.SEMICOLON)); |
| | | redisClient.putRedisVal(key, val); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public void createIdx(String recordTableName) { |
| | | String idxName = MessageFormat.format(Constant.IDX_TEMPLATE, recordTableName, Constant.STD_ID); |
| | | |
| | | List<Map<String, Object>> maps = mapper.selectIdxByTableName(DbUtils.quotedStr(recordTableName)); |
| | | long existsIdx = maps.stream().filter(stringObjectMap -> stringObjectMap.get("index_name") != null) |
| | | .map(stringObjectMap -> stringObjectMap.get("index_name").toString()) |
| | | .filter(s -> idxName.equalsIgnoreCase(s)).count(); |
| | | if (existsIdx == 0) { |
| | | mapper.createIdx(recordTableName, idxName, Constant.STD_ID); |
| | | } |
| | | } |
| | | } |