| | |
| | | package com.highdatas.mdm.service.impl; |
| | | |
| | | import com.highdatas.mdm.entity.SysView; |
| | | import com.highdatas.mdm.mapper.SysViewMapper; |
| | | import com.highdatas.mdm.service.ISysViewService; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.mapper.Wrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.highdatas.mdm.entity.*; |
| | | import com.highdatas.mdm.mapper.SysViewMapper; |
| | | import com.highdatas.mdm.mapper.TableInfoMapper; |
| | | import com.highdatas.mdm.pojo.*; |
| | | import com.highdatas.mdm.pojo.kettle.UnBigDataDataSourceInfo; |
| | | import com.highdatas.mdm.service.*; |
| | | import com.highdatas.mdm.util.Constant; |
| | | import com.highdatas.mdm.util.ContentBuilder; |
| | | import com.highdatas.mdm.util.DbUtils; |
| | | import com.highdatas.mdm.util.ExcelUtil; |
| | | import com.highdatas.mdm.util.pool.MqEntity; |
| | | import com.highdatas.mdm.util.pool.MqMessage; |
| | | import lombok.SneakyThrows; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.text.MessageFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | */ |
| | | @Service |
| | | public class SysViewServiceImpl extends ServiceImpl<SysViewMapper, SysView> implements ISysViewService { |
| | | @Autowired |
| | | ISysViewJoinService joinService; |
| | | @Autowired |
| | | IMaintainService maintainService; |
| | | @Autowired |
| | | UnBigDataDataSourceInfo unBigDataDataSourceInfo; |
| | | @Autowired |
| | | TableInfoMapper tableInfoMapper; |
| | | @Autowired |
| | | SysViewMapper viewMapper; |
| | | @Autowired |
| | | MasterDataService masterDataService; |
| | | @Autowired |
| | | IMasterAuthorService masterAuthorService; |
| | | @Autowired |
| | | ISysMenuService menuService; |
| | | @Autowired |
| | | IMenuMappingService menuMappingService; |
| | | @Autowired |
| | | ISysFieldService fieldService; |
| | | @Autowired |
| | | DispenseService dispenseService; |
| | | |
| | | @Override |
| | | public Page getInitPageInfo(String viewId) { |
| | | Page page = new Page(0); |
| | | SysView sysView = selectById(viewId); |
| | | String viewTableName = sysView.getViewTableName(); |
| | | List<TableSchemaResult> tableField = tableInfoMapper.getTableField(viewTableName); |
| | | int totalLength = 0; |
| | | for (TableSchemaResult tableSchemaResult : tableField) { |
| | | int length = tableSchemaResult.getLength(); |
| | | totalLength += length; |
| | | } |
| | | int pageSize = Constant.MaxDispenseSize / totalLength; |
| | | page.setPageSize(pageSize); |
| | | Long viewCount = getViewCount(sysView); |
| | | if (viewCount == 0) { |
| | | return null; |
| | | } |
| | | int pages; |
| | | if (viewCount % pageSize == 0) { |
| | | pages = Long.valueOf(viewCount/pageSize).intValue(); |
| | | }else { |
| | | pages = (Long.valueOf(viewCount/pageSize).intValue()) + 1; |
| | | } |
| | | page.setPages(pages); |
| | | return page; |
| | | } |
| | | @Override |
| | | public boolean createView(String viewId) { |
| | | SysView sysView = selectById(viewId); |
| | | if (sysView == null) { |
| | | return false; |
| | | } |
| | | String userId = sysView.getUserId(); |
| | | TUser user = DbUtils.getUserById(userId); |
| | | if (user == null) { |
| | | return false; |
| | | } |
| | | |
| | | Maintain baseMaintain = getBaseMaintain(sysView); |
| | | if (baseMaintain == null) { |
| | | return false; |
| | | } |
| | | String baseTableName = baseMaintain.getTableName(); |
| | | sysView.setBaseTableName(baseTableName); |
| | | |
| | | List<String> totalFieldList = getTotalFieldList(sysView); |
| | | |
| | | // |
| | | String tableName = getViewTableName(sysView); |
| | | totalFieldList.add(Constant.ID); |
| | | boolean created = dropCreateViewTable(totalFieldList, tableName); |
| | | //TODO 主键 |
| | | if (!created) { |
| | | return false; |
| | | } |
| | | totalFieldList.remove(Constant.ID); |
| | | List<String> baseFieldList = getBaseFieldList(sysView); |
| | | //trans first |
| | | baseFieldList.add(Constant.ID); |
| | | String baseFields = baseFieldList.stream() |
| | | .map(s -> Constant.ID.equalsIgnoreCase(s) ? Constant.MYSQL_UUID : s) |
| | | .collect(Collectors.joining(Constant.COMMA)); |
| | | String fixBaseFields = baseFieldList.stream() |
| | | .map(s -> Constant.ID.equalsIgnoreCase(s) ? Constant.ID : changeFieldName(baseTableName, s)) |
| | | .collect(Collectors.joining(Constant.COMMA)); |
| | | |
| | | baseFieldList.remove(Constant.ID); |
| | | |
| | | String filter = masterAuthorService.getFilter(user, baseMaintain.getId()); |
| | | |
| | | String s = masterDataService.selectByVersionSql(null, baseTableName, baseFieldList, filter, baseMaintain.getVersion(), false); |
| | | |
| | | //base INSERT |
| | | tableInfoMapper.insertOneSelect(tableName, fixBaseFields, baseFields, s); |
| | | |
| | | // left insert |
| | | InsertJoin(sysView); |
| | | |
| | | // change val common |
| | | changeValCommon(sysView); |
| | | sysView.setStatus(ViewStatus.working).updateById(); |
| | | // |
| | | return true; |
| | | } |
| | | |
| | | private void InsertJoin(SysView sysView) { |
| | | String userId = sysView.getUserId(); |
| | | TUser user = DbUtils.getUserById(userId); |
| | | String baseTableName = sysView.getBaseTableName(); |
| | | String tableName = sysView.getViewTableName(); |
| | | ContentBuilder joinBuilder = new ContentBuilder(Constant.EMPTY); |
| | | ContentBuilder updateFieldBuilder = new ContentBuilder(Constant.COMMA); |
| | | List<SysViewJoin> viewJoinList = sysView.getViewJoinList(); |
| | | |
| | | for (SysViewJoin sysViewJoin : viewJoinList) { |
| | | sysViewJoin.setBaseField(changeFieldName(baseTableName, sysViewJoin.getBaseField())); |
| | | Maintain joinMaintain = getJoinMaintain(sysViewJoin); |
| | | String joinTableName = joinMaintain.getTableName(); |
| | | |
| | | sysViewJoin.setBaseTableName(sysView.getViewTableName()); |
| | | String joinStr = sysViewJoin.createJoinStr(); |
| | | if (StringUtils.isEmpty(joinStr)) { |
| | | continue; |
| | | } |
| | | joinBuilder.append(joinStr); |
| | | List<String> subFieldList = getSubFieldList(sysViewJoin, user); |
| | | for (String subOneField : subFieldList) { |
| | | String changeFieldName = changeFieldName(sysViewJoin.getJoinTableName(), subOneField); |
| | | String base = MessageFormat.format(Constant.Alias, sysView.getViewTableName(), changeFieldName); |
| | | String join = MessageFormat.format(Constant.Alias, joinTableName, subOneField); |
| | | Segment segment = new Segment(base, join); |
| | | updateFieldBuilder.append(segment.toRawString()); |
| | | } |
| | | } |
| | | // JOIN |
| | | String joinSql = joinBuilder.toString(); |
| | | if (StringUtils.isEmpty(joinSql)) { |
| | | return; |
| | | } |
| | | tableInfoMapper.updateJoin(tableName, joinSql, updateFieldBuilder.toString(), Constant.WHERE_DEFAULT); |
| | | } |
| | | |
| | | private void changeValCommon(SysView sysView) { |
| | | String mappingField = sysView.getMappingField(); |
| | | String tableName = sysView.getViewTableName(); |
| | | String mappingTable = sysView.getMappingTable(); |
| | | |
| | | List<String> split = DbUtils.split(mappingField); |
| | | for (String oneMapField : split) { |
| | | String base = MessageFormat.format(Constant.Alias, tableName, oneMapField); |
| | | String join = MessageFormat.format(Constant.Alias, mappingTable, Constant.Pre); |
| | | String updateJoin = MessageFormat.format(Constant.Alias, mappingTable, Constant.fix); |
| | | Segment joinSegment = new Segment(base, join); |
| | | Segment updateSegment = new Segment(base, updateJoin); |
| | | Segment filterSegment = new Segment(MessageFormat.format(Constant.Alias, mappingTable, Constant.Code), oneMapField); |
| | | String changeJoinStr = MessageFormat.format(Constant.InnerJoinTemplate, ViewJoinType.inner, mappingTable, joinSegment.toRawString()); |
| | | if (StringUtils.isEmpty(changeJoinStr)){ |
| | | return; |
| | | } |
| | | tableInfoMapper.updateJoin(tableName, changeJoinStr, updateSegment.toRawString(), filterSegment.toString()); |
| | | } |
| | | } |
| | | |
| | | private boolean dropCreateViewTable(List<String> totalFieldList, String tableName) { |
| | | boolean created = unBigDataDataSourceInfo.dropData(tableName); |
| | | if (!created) { |
| | | return false; |
| | | } |
| | | |
| | | created = unBigDataDataSourceInfo.createTable(tableName, totalFieldList); |
| | | if (!created) { |
| | | //TODO |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public String getViewTableName(SysView view) { |
| | | String viewTableName = view.getViewTableName(); |
| | | if (StringUtils.isEmpty(viewTableName)) { |
| | | Maintain baseMaintain = getBaseMaintain(view); |
| | | viewTableName = Constant.VIEW + baseMaintain.getTableName() + baseMaintain.getOrderNo() + DbUtils.getUUID(5); |
| | | view.setViewTableName(viewTableName).updateById(); |
| | | } |
| | | return viewTableName; |
| | | } |
| | | |
| | | @Override |
| | | public String getMappingTableName(SysView view) { |
| | | String id = view.getId(); |
| | | String top = id.substring(0, 5); |
| | | String mappingTableName = Constant.VIEW + top + "mapping" + DbUtils.getUUID(4); |
| | | view.setMappingTable(mappingTableName).updateById(); |
| | | return mappingTableName; |
| | | } |
| | | @Override |
| | | public List<String> getSubFieldList(SysViewJoin join, TUser user) { |
| | | String fieldsStr = join.getFields(); |
| | | List<String> baseFieldList = new ArrayList<>(); |
| | | if (Constant.All.equalsIgnoreCase(fieldsStr)) { |
| | | Maintain joinMaintain = getJoinMaintain(join); |
| | | List<SysField> fieldList = masterAuthorService.getField(user, joinMaintain.getId()); |
| | | baseFieldList = fieldList.stream().map(sysField -> sysField.getField()).collect(Collectors.toList()); |
| | | }else { |
| | | baseFieldList = DbUtils.split(fieldsStr); |
| | | } |
| | | |
| | | return baseFieldList; |
| | | } |
| | | |
| | | private List<String> getBaseFieldList(SysView sysView) { |
| | | String fieldsStr = sysView.getFields(); |
| | | List<String> baseFieldList = new ArrayList<>(); |
| | | if (Constant.All.equalsIgnoreCase(fieldsStr)) { |
| | | String userId = sysView.getUserId(); |
| | | TUser user = DbUtils.getUserById(userId); |
| | | Maintain baseMaintain = getBaseMaintain(sysView); |
| | | List<SysField> fieldList = masterAuthorService.getField(user, baseMaintain.getId()); |
| | | baseFieldList = fieldList.stream().map(sysField -> sysField.getField()).collect(Collectors.toList()); |
| | | }else { |
| | | baseFieldList = DbUtils.split(fieldsStr); |
| | | } |
| | | |
| | | return baseFieldList; |
| | | } |
| | | @Override |
| | | public List<ViewMapFieldItem> getMapField(SysView sysView) { |
| | | List<ViewMapFieldItem> result = new ArrayList<>(); |
| | | Maintain baseMaintain = getBaseMaintain(sysView); |
| | | SysMenu menuByTableName = menuMappingService.getMenuByTableName(baseMaintain.getTableName()); |
| | | if (menuByTableName == null) { |
| | | return null; |
| | | } |
| | | List<String> baseFieldList = getBaseFieldList(sysView); |
| | | |
| | | for (String field : baseFieldList) { |
| | | String changeFieldName = changeFieldName(baseMaintain.getTableName(), field); |
| | | SysField oneFieldByMaintainField = fieldService.getOneFieldByMaintain(baseMaintain.getId(), field); |
| | | if (oneFieldByMaintainField == null) { |
| | | continue; |
| | | } |
| | | ViewMapFieldItem viewMapFieldItem = new ViewMapFieldItem(); |
| | | viewMapFieldItem.setChangedField(changeFieldName); |
| | | viewMapFieldItem.setField(field); |
| | | viewMapFieldItem.setFieldName(oneFieldByMaintainField.getAlias()); |
| | | viewMapFieldItem.setMenuId(menuByTableName.getId()); |
| | | viewMapFieldItem.setMenuName(menuByTableName.getName()); |
| | | |
| | | result.add(viewMapFieldItem); |
| | | } |
| | | String userId = sysView.getUserId(); |
| | | TUser user = DbUtils.getUserById(userId); |
| | | List<SysViewJoin> joinList = getJoinList(sysView); |
| | | for (SysViewJoin sysViewJoin : joinList) { |
| | | Maintain joinMaintain = getJoinMaintain(sysViewJoin); |
| | | SysMenu subMenu = menuMappingService.getMenuByTableName(joinMaintain.getTableName()); |
| | | List<String> subFieldList = getSubFieldList(sysViewJoin, user); |
| | | for (String subField : subFieldList) { |
| | | String changeFieldName = changeFieldName(joinMaintain.getTableName(), subField); |
| | | SysField oneFieldByMaintainField = fieldService.getOneFieldByMaintain(joinMaintain.getId(), subField); |
| | | if (oneFieldByMaintainField == null) { |
| | | continue; |
| | | } |
| | | ViewMapFieldItem viewMapFieldItem = new ViewMapFieldItem(); |
| | | viewMapFieldItem.setChangedField(changeFieldName); |
| | | viewMapFieldItem.setField(subField); |
| | | viewMapFieldItem.setFieldName(oneFieldByMaintainField.getAlias()); |
| | | viewMapFieldItem.setMenuId(subMenu.getId()); |
| | | viewMapFieldItem.setMenuName(subMenu.getName()); |
| | | |
| | | result.add(viewMapFieldItem); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private List<SysViewJoin> getJoinList(SysView sysView) { |
| | | String id = sysView.getId(); |
| | | Wrapper<SysViewJoin> eq = new EntityWrapper<SysViewJoin>().eq(Constant.PARENT_ID, id); |
| | | List<SysViewJoin> joinList = joinService.selectList(eq); |
| | | return joinList; |
| | | } |
| | | |
| | | public List<String> getTotalFieldList(SysView sysView) { |
| | | String userId = sysView.getUserId(); |
| | | TUser user = DbUtils.getUserById(userId); |
| | | |
| | | List<String> totalFieldList = new ArrayList<>(); |
| | | Maintain baseMaintain = getBaseMaintain(sysView); |
| | | List<String> baseFieldList = getBaseFieldList(sysView); |
| | | totalFieldList.addAll(baseFieldList); |
| | | |
| | | totalFieldList = totalFieldList.stream().map(s -> changeFieldName(baseMaintain.getTableName(), s)).collect(Collectors.toList()); |
| | | List<SysViewJoin> sysViewJoins = joinService.selectList(new EntityWrapper<SysViewJoin>().eq(Constant.PARENT_ID, sysView.getId())); |
| | | for (SysViewJoin sysViewJoin : sysViewJoins) { |
| | | Maintain joinMaintain = getJoinMaintain(sysViewJoin); |
| | | List<String> subFieldList = getSubFieldList(sysViewJoin, user); |
| | | subFieldList = subFieldList.stream().map(s -> changeFieldName(joinMaintain.getTableName(), s)).collect(Collectors.toList()); |
| | | totalFieldList.addAll(subFieldList); |
| | | |
| | | sysViewJoin.setJoinTableName(joinMaintain.getTableName()); |
| | | sysViewJoin.setBaseTableName(baseMaintain.getTableName()); |
| | | } |
| | | sysView.setViewJoinList(sysViewJoins); |
| | | return totalFieldList; |
| | | } |
| | | |
| | | @Override |
| | | public Maintain getBaseMaintain(SysView sysView) { |
| | | |
| | | String baseMaintainStr = sysView.getBaseMaintain(); |
| | | if (StringUtils.isEmpty(baseMaintainStr)) { |
| | | return null; |
| | | } |
| | | Maintain baseMaintain; |
| | | if (sysView.getNeedUpdate()) { |
| | | baseMaintain = maintainService.getNowVersion(baseMaintainStr); |
| | | }else { |
| | | baseMaintain = maintainService.selectById(baseMaintainStr); |
| | | } |
| | | return baseMaintain; |
| | | } |
| | | |
| | | @Override |
| | | public Maintain getJoinMaintain(SysViewJoin viewJoin) { |
| | | |
| | | String baseMaintainStr = viewJoin.getJoinMaintain(); |
| | | if (StringUtils.isEmpty(baseMaintainStr)) { |
| | | return null; |
| | | } |
| | | Maintain baseMaintain; |
| | | if (viewJoin.getNeedUpdate()) { |
| | | baseMaintain = maintainService.getNowVersion(baseMaintainStr); |
| | | }else { |
| | | baseMaintain = maintainService.selectById(baseMaintainStr); |
| | | } |
| | | return baseMaintain; |
| | | } |
| | | |
| | | @Override |
| | | public String changeFieldName(String baseTableName, String field) { |
| | | return DbUtils.StrJoinLink(Constant.EMPTY_Str, baseTableName, Constant.UnderLine, field); |
| | | } |
| | | |
| | | @Override |
| | | public void dealFlow(String maintainId, ActivitiStatus status) { |
| | | if (!ActivitiStatus.open.equals(status)) { |
| | | return; |
| | | } |
| | | Maintain maintain = maintainService.selectById(maintainId); |
| | | String maintainTableName = maintain.getTableName(); |
| | | |
| | | Wrapper<SysView> eq = new EntityWrapper<SysView>() |
| | | .eq("need_update", true) |
| | | .eq("base_maintain", maintainTableName); |
| | | List<SysView> sysViews = selectList(eq); |
| | | for (SysView sysView : sysViews) { |
| | | ViewStatus viewStatus = sysView.getStatus(); |
| | | if (!ViewStatus.working.equals(viewStatus)) { |
| | | continue; |
| | | } |
| | | createView(sysView.getId()); |
| | | |
| | | Boolean subscribe = sysView.getSubscribe(); |
| | | if (!subscribe) { |
| | | continue; |
| | | } |
| | | MqEntity mqEntity = new MqEntity(); |
| | | mqEntity.setUserId(sysView.getUserId()); |
| | | mqEntity.setMsgTopicName(Constant.View); |
| | | mqEntity.setType(Constant.View); |
| | | mqEntity.setDataId(sysView.getId()); |
| | | mqEntity.setMsgTagName(sysView.getId()); |
| | | mqEntity.setMsgKey(DbUtils.getUUID(16)); |
| | | MqMessage mqMessage = new MqMessage(mqEntity); |
| | | dispenseService.pushActiveMq(mqMessage); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result deleteView(String id) throws Exception { |
| | | SysView sysView = selectById(id); |
| | | if (sysView == null) { |
| | | return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND); |
| | | } |
| | | joinService.delete(new EntityWrapper<SysViewJoin>().eq(Constant.PARENT_ID, id)); |
| | | String viewTableName = sysView.getViewTableName(); |
| | | if (!StringUtils.isEmpty(viewTableName)) { |
| | | unBigDataDataSourceInfo.dropData(viewTableName); |
| | | } |
| | | String mappingTable = sysView.getMappingTable(); |
| | | if (!StringUtils.isEmpty(mappingTable)) { |
| | | unBigDataDataSourceInfo.dropData(mappingTable); |
| | | } |
| | | sysView.deleteById(); |
| | | return Result.success(CodeMsg.DELETE_SUCCESS); |
| | | } |
| | | |
| | | @Override |
| | | public List<ViewDictItem> getViewFieldMap(String viewId) { |
| | | SysView sysView = selectById(viewId); |
| | | if (sysView == null) { |
| | | return null; |
| | | } |
| | | Maintain baseMaintain = getBaseMaintain(sysView); |
| | | if (baseMaintain == null) { |
| | | return null; |
| | | } |
| | | String menuId = sysView.getMenuId(); |
| | | SysMenu sysMenu = menuService.selectById(menuId); |
| | | if (sysMenu == null) { |
| | | return null; |
| | | } |
| | | List<String> baseFieldList = getBaseFieldList(sysView); |
| | | ArrayList<ViewDictItem> dictList = new ArrayList<>(); |
| | | |
| | | for (String field : baseFieldList) { |
| | | ViewDictItem viewDictItem = new ViewDictItem(); |
| | | viewDictItem.setMenuId(sysMenu.getId()); |
| | | viewDictItem.setRawField(field); |
| | | String changeFieldName = changeFieldName(baseMaintain.getTableName(), field); |
| | | viewDictItem.setViewField(changeFieldName); |
| | | dictList.add(viewDictItem); |
| | | } |
| | | |
| | | List<SysViewJoin> sysViewJoins = joinService.selectList(new EntityWrapper<SysViewJoin>().eq(Constant.PARENT_ID, sysView.getId())); |
| | | for (SysViewJoin sysViewJoin : sysViewJoins) { |
| | | String subMenuId = sysViewJoin.getMenuId(); |
| | | SysMenu subMenu = menuService.selectById(subMenuId); |
| | | if (subMenu == null) { |
| | | continue; |
| | | } |
| | | Maintain joinMaintain = getJoinMaintain(sysViewJoin); |
| | | if (joinMaintain == null) { |
| | | continue; |
| | | } |
| | | String fields = sysViewJoin.getFields(); |
| | | |
| | | List<String> subFieldList = DbUtils.split(fields); |
| | | for (String field : subFieldList) { |
| | | ViewDictItem viewDictItem = new ViewDictItem(); |
| | | viewDictItem.setMenuId(subMenu.getId()); |
| | | viewDictItem.setRawField(field); |
| | | String changeFieldName = changeFieldName(joinMaintain.getTableName(), field); |
| | | viewDictItem.setViewField(changeFieldName); |
| | | dictList.add(viewDictItem); |
| | | } |
| | | } |
| | | return dictList; |
| | | } |
| | | |
| | | @Override |
| | | public Result getMapData(SysView sysView, String changedField, Integer pageNo, int pageSize) { |
| | | String mappingTable = sysView.getMappingTable(); |
| | | if (StringUtils.isEmpty(mappingTable)) { |
| | | return null; |
| | | } |
| | | boolean exists = unBigDataDataSourceInfo.checkTableExists(mappingTable); |
| | | if (!exists) { |
| | | return null; |
| | | } |
| | | Segment segment = new Segment(Constant.Code, changedField); |
| | | |
| | | Long count = tableInfoMapper.getCount(mappingTable, segment.toString()); |
| | | if (count == 0) { |
| | | return null; |
| | | } |
| | | Page page = new Page(count); |
| | | page.setPageSize(pageSize); |
| | | page.setPageNo(pageNo); |
| | | ArrayList<String> fields = new ArrayList<>(); |
| | | fields.add(Constant.Code); |
| | | fields.add(Constant.Pre); |
| | | fields.add(Constant.fix); |
| | | String fieldStr = fields.stream().collect(Collectors.joining(Constant.COMMA)); |
| | | List<Map<String, Object>> maps = tableInfoMapper.selectByPage(mappingTable, fieldStr, segment.toString(), page.getLimitSQL()); |
| | | JSONObject object = new JSONObject(); |
| | | object.fluentPut("records", maps); |
| | | object.fluentPut("pages", page.getPageCount()); |
| | | object.fluentPut("current", pageNo); |
| | | object.fluentPut("total", count); |
| | | |
| | | return Result.success(object); |
| | | } |
| | | @Override |
| | | public long getViewCount(SysView sysView) { |
| | | String viewTableName = sysView.getViewTableName(); |
| | | if (StringUtils.isEmpty(viewTableName)) { |
| | | return 0; |
| | | } |
| | | boolean exists = unBigDataDataSourceInfo.checkTableExists(viewTableName); |
| | | if (!exists) { |
| | | return 0; |
| | | } |
| | | |
| | | Long count = tableInfoMapper.getCount(viewTableName, Constant.WHERE_DEFAULT); |
| | | if (count == null) { |
| | | return 0L; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | @Override |
| | | public Result getViewData(SysView sysView, Integer pageNo, int pageSize) { |
| | | String viewTableName = sysView.getViewTableName(); |
| | | long count = getViewCount(sysView); |
| | | if (count == 0) { |
| | | return null; |
| | | } |
| | | Page page = new Page(count); |
| | | page.setPageSize(pageSize); |
| | | page.setPageNo(pageNo); |
| | | List<TableSchemaResult> tableField = tableInfoMapper.getTableField(viewTableName); |
| | | String fields = tableField.stream().map(tableSchemaResult -> tableSchemaResult.getFieldName()).collect(Collectors.joining(Constant.COMMA)); |
| | | List<Map<String, Object>> maps = tableInfoMapper.selectByPage(viewTableName, fields, Constant.WHERE_DEFAULT, page.getLimitSQL()); |
| | | JSONObject object = new JSONObject(); |
| | | object.fluentPut("records", maps); |
| | | object.fluentPut("pages", page.getPageCount()); |
| | | object.fluentPut("current", pageNo); |
| | | object.fluentPut("total", count); |
| | | object.fluentPut("pageSize", page.getPageSize()); |
| | | |
| | | //fields |
| | | ArrayList<ViewField> sysFields = new ArrayList<>(); |
| | | Maintain baseMaintain = getBaseMaintain(sysView); |
| | | List<String> baseFieldList = getBaseFieldList(sysView); |
| | | |
| | | for (String field : baseFieldList) { |
| | | SysField oneFieldByMaintain = fieldService.getOneFieldByMaintain(baseMaintain.getId(), field); |
| | | if (oneFieldByMaintain == null) { |
| | | continue; |
| | | } |
| | | String changeFieldName = changeFieldName(baseMaintain.getTableName(), field); |
| | | ViewField viewField = new ViewField(); |
| | | viewField.setChangedField(changeFieldName); |
| | | viewField.setField(field); |
| | | viewField.setFieldName(oneFieldByMaintain.getAlias()); |
| | | viewField.setMenuId(sysView.getId()); |
| | | viewField.setMenuName(sysView.getName()); |
| | | sysFields.add(viewField); |
| | | } |
| | | String userId = sysView.getUserId(); |
| | | TUser user = DbUtils.getUserById(userId); |
| | | |
| | | List<SysViewJoin> joinList = joinService.selectList(new EntityWrapper<SysViewJoin>().eq(Constant.PARENT_ID, sysView.getId())); |
| | | for (SysViewJoin sysViewJoin : joinList) { |
| | | Maintain joinMaintain = getJoinMaintain(sysViewJoin); |
| | | List<String> subFieldList = getSubFieldList(sysViewJoin, user); |
| | | for (String subField : subFieldList) { |
| | | SysField oneFieldByMaintain = fieldService.getOneFieldByMaintain(joinMaintain.getId(), subField); |
| | | if (oneFieldByMaintain == null) { |
| | | continue; |
| | | } |
| | | String changeFieldName = changeFieldName(joinMaintain.getTableName(), subField); |
| | | ViewField viewField = new ViewField(); |
| | | viewField.setChangedField(changeFieldName); |
| | | viewField.setField(subField); |
| | | viewField.setFieldName(oneFieldByMaintain.getAlias()); |
| | | viewField.setMenuId(sysView.getId()); |
| | | viewField.setMenuName(sysView.getName()); |
| | | sysFields.add(viewField); |
| | | } |
| | | } |
| | | object.fluentPut("fields", sysFields); |
| | | |
| | | return Result.success(object); |
| | | } |
| | | |
| | | @SneakyThrows |
| | | @Override |
| | | public void downlodMap(SysView sysView, TUser user, HttpServletResponse response,String field, String menuId) { |
| | | Maintain maintain = getMaintainByMenu(sysView, menuId); |
| | | if (maintain == null) { |
| | | return; |
| | | } |
| | | ArrayList<String> fields = new ArrayList<>(); |
| | | fields.add(field); |
| | | fields.add(Constant.ID); |
| | | String filter = masterAuthorService.getFilter(user, maintain.getId()); |
| | | String sql = masterDataService.selectByVersionSql(user, maintain.getTableName(), fields, filter, maintain.getVersion(), false); |
| | | String tableName = MessageFormat.format(Constant.asTempSql, sql, Constant.H); |
| | | String mappingTable = sysView.getMappingTable(); |
| | | boolean exists = unBigDataDataSourceInfo.checkTableExists(mappingTable); |
| | | List<Map<String, Object>> maps; |
| | | if (exists) { |
| | | String changeFieldName = changeFieldName(maintain.getTableName(), field); |
| | | maps = viewMapper.selectMapVal(tableName, MessageFormat.format(Constant.FieldAsAlias, field, Constant.Pre), Constant.WHERE_DEFAULT, mappingTable, DbUtils.quotedStr(changeFieldName)); |
| | | |
| | | } else { |
| | | maps = tableInfoMapper.selectDistinct(tableName, MessageFormat.format(Constant.FieldAsAlias, field, Constant.Pre), Constant.WHERE_DEFAULT); |
| | | |
| | | } |
| | | |
| | | |
| | | //todo 大数据量优化 |
| | | fields.remove(Constant.ID); |
| | | ArrayList<SysField> sysFields = new ArrayList<>(); |
| | | sysFields.add(new SysField().setField(Constant.Pre).setAlias(Constant.PreValue)); |
| | | sysFields.add(new SysField().setField(Constant.fix).setAlias(Constant.fixValue)); |
| | | ExcelUtil.export(response, maps, sysFields); |
| | | } |
| | | |
| | | @Override |
| | | public Maintain getMaintainByMenu(SysView sysView, String menuId) { |
| | | Maintain maintain = null; |
| | | String baseMenu = sysView.getMenuId(); |
| | | if (baseMenu != null && baseMenu.equalsIgnoreCase(menuId)) { |
| | | maintain = getBaseMaintain(sysView); |
| | | }else { |
| | | List<SysViewJoin> joinList = joinService.selectList(new EntityWrapper<SysViewJoin>().eq(Constant.PARENT_ID, sysView.getId())); |
| | | |
| | | for (SysViewJoin sysViewJoin : joinList) { |
| | | String subMenuId = sysViewJoin.getMenuId(); |
| | | if (subMenuId != null && subMenuId.equalsIgnoreCase(menuId)) { |
| | | maintain = getJoinMaintain(sysViewJoin); |
| | | } |
| | | |
| | | } |
| | | } |
| | | return maintain; |
| | | } |
| | | |
| | | @Override |
| | | public boolean deleteMapField(SysView sysView, String changedField) { |
| | | try { |
| | | Segment segment = new Segment(Constant.Code, changedField); |
| | | String mappingTable = sysView.getMappingTable(); |
| | | String mappingfield = sysView.getMappingField(); |
| | | if (StringUtils.isEmpty(mappingTable) || StringUtils.isEmpty(mappingfield)) { |
| | | return true; |
| | | } |
| | | tableInfoMapper.delete(mappingTable, segment.toString()); |
| | | String mappingField = sysView.getMappingField(); |
| | | List<String> split = DbUtils.split(mappingField); |
| | | String collect = split.stream().filter(s -> !s.equalsIgnoreCase(changedField)).collect(Collectors.joining(Constant.SEMICOLON)); |
| | | boolean b = sysView.setMappingField(collect).updateById(); |
| | | return b; |
| | | } |
| | | catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | |
| | | } |
| | | } |