Browse Source

修复onlyCount返回总数量bug

fengyanjie 3 years ago
parent
commit
981966c481

+ 206 - 202
dmp-rwd/src/main/java/com/persagy/dmp/rwd/service/RelationInstanceService.java

@@ -27,208 +27,212 @@ import java.util.stream.Collectors;
 @Service
 public class RelationInstanceService extends BaseService {
 
-	@Autowired
-	private ObjectInstanceQueryService objectInstanceQueryService;
-
-	@Autowired
-	private CriteriaUtils criteriaUtils;
-
-	private List<BooleanExpression> parse(ObjectNode criteria) {
-		// TODO
-		return null;
-	}
-
-	public PagedResponse<RelationInstanceModel> query(JacksonCriteria criteria) {
-		PagedResponse<RelationInstanceModel> result = new PagedResponse<>();
-		super.checkParam(result, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
-		if (!result.success()) {
-			return result;
-		}
-		criteria.add(RwdConstants.OBJECT_GROUPCODE, DmpParameterStorage.getGroupCode());
-		criteria.add(RwdConstants.OBJECT_PROJECTID, DmpParameterStorage.getProjectId());
-		PagedResponse<RelationInstance> resp = criteriaUtils.query(QRelationInstance.relationInstance, this::parse, criteria);
-		result.setCount(resp.getCount());
-		List<RelationInstance> dataList = resp.getData();
-		if (dataList != null && dataList.size() > 0) {
-			List<RelationInstanceModel> collect = dataList.stream().map(entity -> {
-				RelationInstanceModel model = entity.toModel();
-				return model;
-			}).collect(Collectors.toList());
-			result.setData(collect);
-		}
-		return result;
-	}
-
-	@Transactional
-	public MapResponse create(List<RelationInstanceModel> paramList) {
-		MapResponse response = new MapResponse();
-		super.checkParam(response, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
-		if (!response.success()) {
-			return response;
-		}
-		// TODO
-		if (paramList == null || paramList.size() == 0) {
-			return response;
-		}
-
-		String projectId = DmpParameterStorage.getProjectId();
-		String groupCode = DmpParameterStorage.getGroupCode();
-		if (projectId == null) {
-			response.setFail("[projectId] is required");
-			return response;
-		}
-
-		List<RelationInstance> list = new ArrayList<>(paramList.size());
-		for (RelationInstanceModel param : paramList) {
-			RelationInstance entity = RelationInstance.fromModel(param);
-			entity.setGroupCode(DmpParameterStorage.getGroupCode());
-			entity.setProjectId(projectId);
-			if (entity.getId() == null) {
-				entity.setId(UUID.randomUUID().toString().replace("-", ""));
-			}
-			if (param.getGraphCode() == null && param.getGraphId() == null) {
-				response.setFail("[graphId | graphCode] is required");
-				return response;
-			}
-
-			QGraphInstance qt = QGraphInstance.graphInstance;
-			// 优先级: graphId > graphCode
-			if (param.getGraphId() != null) {
-				GraphInstance gi = graphInstanceRepository.getOne(entity.getGraphId());
-				entity.setGraphCode(gi.getGraphCode());
-			} else if (param.getGraphCode() != null) {
-				Sort orders = Sort.by(new Sort.Order(Sort.Direction.ASC, "id"));
-				Iterable<GraphInstance> all = graphInstanceRepository.findAll(qt.groupCode.eq(groupCode).and(qt.projectId.eq(projectId).and(qt.graphCode.eq(entity.getGraphCode()))), orders);
-				GraphInstance next = all.iterator().next();
-				entity.setGraphId(next.getId());
-			}
-			entity.setStatus(1);
-			if (entity.getCreateApp() == null) {
-				entity.setCreateApp(DmpParameterStorage.getAppId());
-			}
-			// 判断是否已存在
-			QRelationInstance rt = QRelationInstance.relationInstance;
-			BooleanExpression exp = rt.groupCode.eq(entity.getGroupCode())
-					.and(rt.projectId.eq(entity.getProjectId()))
-					.and(rt.graphId.eq(entity.getGraphId()))
-					.and(rt.relCode.eq(entity.getRelCode()))
-					.and(rt.objFrom.eq(entity.getObjFrom()))
-					.and(rt.objTo.eq(entity.getObjTo()));
-			if (entity.getRelValue() != null) {
-				exp = exp.and(rt.relValue.eq(entity.getRelValue()));
-			} else {
-				exp = exp.and(rt.relValue.isNull());
-			}
-
-			long count = relationInstanceRepository.count(exp);
-			if (count == 0) {
-				// 数据不存在, 新增, 已存在时忽略
-				list.add(entity);
-			}
-		}
-		if (list.size() > 0) {
-			relationInstanceRepository.saveAll(list);
-			String now = DateUtils.format(new Date());
-			for (RelationInstance relation : list) {
-				DmpMessage message = new DmpMessage(RwdConstants.MESSAGE_INSTANCE_RELATION_CREATE, null);
-				message.setGroupCode(DmpParameterStorage.getGroupCode());
-				message.setProjectId(DmpParameterStorage.getProjectId());
-				message.setAppId(DmpParameterStorage.getAppId());
-				message.setUserId(DmpParameterStorage.getUserId());
-				message.setSendTime(now);
-				message.add("graphId", relation.getGraphId());
-				message.add("graphCode", relation.getGraphCode());
-				message.add("relCode", relation.getRelCode());
-				message.add("relValue", relation.getRelValue());
-				message.add("objFrom", relation.getObjFrom());
-				message.add("objTo", relation.getObjTo());
-				response.add(message);
-			}
-		}
-		return response;
-	}
-
-	@Transactional
-	public MapResponse delete(JacksonCriteria criteria) {
-		MapResponse response = new MapResponse();
-		super.checkParam(response, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
-		if (!response.success()) {
-			return response;
-		}
-		criteria.add(RwdConstants.OBJECT_GROUPCODE, DmpParameterStorage.getGroupCode());
-		criteria.add(RwdConstants.OBJECT_PROJECTID, DmpParameterStorage.getProjectId());
-		criteria.setWithoutCount(true);
-		QRelationInstance qt = QRelationInstance.relationInstance;
-		PagedResponse<RelationInstance> resp = criteriaUtils.query(qt, this::parse, criteria);
-		List<RelationInstance> data = resp.getData();
-		if (data != null && data.size() > 0) {
-			int cnt = 0;
-			for (RelationInstance datum : data) {
-				long execute = jpaQueryFactory.delete(qt).where(qt.groupCode.eq(DmpParameterStorage.getGroupCode()), qt.projectId.eq(DmpParameterStorage.getProjectId()), qt.id.eq(datum.getId())).execute();
-				cnt += execute;
-			}
-			response.add("deleteCount", cnt);
-
-			String now = DateUtils.format(new Date());
-			for (RelationInstance relation : data) {
-				DmpMessage message = new DmpMessage(RwdConstants.MESSAGE_INSTANCE_RELATION_DELETE, null);
-				message.setGroupCode(DmpParameterStorage.getGroupCode());
-				message.setProjectId(DmpParameterStorage.getProjectId());
-				message.setAppId(DmpParameterStorage.getAppId());
-				message.setUserId(DmpParameterStorage.getUserId());
-				message.setSendTime(now);
-				message.add("graphId", relation.getGraphId());
-				message.add("graphCode", relation.getGraphCode());
-				message.add("relCode", relation.getRelCode());
-				message.add("objFrom", relation.getObjFrom());
-				message.add("objTo", relation.getObjTo());
-				response.add(message);
-			}
-		} else {
-			response.add("deleteCount", 0);
-		}
-		return response;
-	}
-
-	public PagedResponse<ObjectNode> queryObject(JacksonCriteria criteria) {
-		PagedResponse response = new PagedResponse();
-		super.checkParam(response, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
-		if (!response.success()) {
-			return response;
-		}
-		PagedResponse<RelationInstanceModel> queryResp = query(criteria);
-		response.setCount(queryResp.getCount());
-		List<RelationInstanceModel> data = queryResp.getData();
-		if (data == null || data.size() == 0) {
-			return response;
-		}
-
-		List<String> objectIds = null;
-		if (criteria.getCriteria().has(RwdConstants.RELATION_OBJ_FROM)) {
-			objectIds = data.stream().map(RelationInstanceModel::getObjTo).collect(Collectors.toList());
-		} else {
-			objectIds = data.stream().map(RelationInstanceModel::getObjFrom).collect(Collectors.toList());
-		}
-		if (objectIds == null || objectIds.size() == 0) {
-			return response;
-		}
-
-		JacksonCriteria objectCriteria = JacksonCriteria.newInstance();
-		JsonNode objectNode = criteria.getCriteria().get("objectCriteria");
-		if (objectNode != null && objectNode.isObject()) {
-			objectCriteria.getCriteria().setAll((ObjectNode) objectNode);
-		}
-		objectCriteria.add("id", objectIds);
-
-		Set<String> withColumns = criteria.getWithColumns();
-		if (withColumns != null && withColumns.size() > 0) {
-			objectCriteria.setWithColumns(withColumns);
-		}
-		PagedResponse<ObjectNode> query = objectInstanceQueryService.query(objectCriteria);
-		response.setData(query.getData());
-		response.setCount(query.getCount());
-		return response;
-	}
+    @Autowired
+    private ObjectInstanceQueryService objectInstanceQueryService;
+
+    @Autowired
+    private CriteriaUtils criteriaUtils;
+
+    private List<BooleanExpression> parse(ObjectNode criteria) {
+        // TODO
+        return null;
+    }
+
+    public PagedResponse<RelationInstanceModel> query(JacksonCriteria criteria) {
+        PagedResponse<RelationInstanceModel> result = new PagedResponse<>();
+        super.checkParam(result, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
+        if (!result.success()) {
+            return result;
+        }
+        criteria.add(RwdConstants.OBJECT_GROUPCODE, DmpParameterStorage.getGroupCode());
+        criteria.add(RwdConstants.OBJECT_PROJECTID, DmpParameterStorage.getProjectId());
+        PagedResponse<RelationInstance> resp = criteriaUtils.query(QRelationInstance.relationInstance, this::parse, criteria);
+        result.setCount(resp.getCount());
+        List<RelationInstance> dataList = resp.getData();
+        if (dataList != null && dataList.size() > 0) {
+            List<RelationInstanceModel> collect = dataList.stream().map(entity -> {
+                RelationInstanceModel model = entity.toModel();
+                return model;
+            }).collect(Collectors.toList());
+            result.setData(collect);
+        }
+        return result;
+    }
+
+    @Transactional
+    public MapResponse create(List<RelationInstanceModel> paramList) {
+        MapResponse response = new MapResponse();
+        super.checkParam(response, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
+        if (!response.success()) {
+            return response;
+        }
+        // TODO
+        if (paramList == null || paramList.size() == 0) {
+            return response;
+        }
+
+        String projectId = DmpParameterStorage.getProjectId();
+        String groupCode = DmpParameterStorage.getGroupCode();
+        if (projectId == null) {
+            response.setFail("[projectId] is required");
+            return response;
+        }
+
+        List<RelationInstance> list = new ArrayList<>(paramList.size());
+        for (RelationInstanceModel param : paramList) {
+            RelationInstance entity = RelationInstance.fromModel(param);
+            entity.setGroupCode(DmpParameterStorage.getGroupCode());
+            entity.setProjectId(projectId);
+            if (entity.getId() == null) {
+                entity.setId(UUID.randomUUID().toString().replace("-", ""));
+            }
+            if (param.getGraphCode() == null && param.getGraphId() == null) {
+                response.setFail("[graphId | graphCode] is required");
+                return response;
+            }
+
+            QGraphInstance qt = QGraphInstance.graphInstance;
+            // 优先级: graphId > graphCode
+            if (param.getGraphId() != null) {
+                GraphInstance gi = graphInstanceRepository.getOne(entity.getGraphId());
+                entity.setGraphCode(gi.getGraphCode());
+            } else if (param.getGraphCode() != null) {
+                Sort orders = Sort.by(new Sort.Order(Sort.Direction.ASC, "id"));
+                Iterable<GraphInstance> all = graphInstanceRepository.findAll(qt.groupCode.eq(groupCode).and(qt.projectId.eq(projectId).and(qt.graphCode.eq(entity.getGraphCode()))), orders);
+                GraphInstance next = all.iterator().next();
+                entity.setGraphId(next.getId());
+            }
+            entity.setStatus(1);
+            if (entity.getCreateApp() == null) {
+                entity.setCreateApp(DmpParameterStorage.getAppId());
+            }
+            // 判断是否已存在
+            QRelationInstance rt = QRelationInstance.relationInstance;
+            BooleanExpression exp = rt.groupCode.eq(entity.getGroupCode())
+                    .and(rt.projectId.eq(entity.getProjectId()))
+                    .and(rt.graphId.eq(entity.getGraphId()))
+                    .and(rt.relCode.eq(entity.getRelCode()))
+                    .and(rt.objFrom.eq(entity.getObjFrom()))
+                    .and(rt.objTo.eq(entity.getObjTo()));
+            if (entity.getRelValue() != null) {
+                exp = exp.and(rt.relValue.eq(entity.getRelValue()));
+            } else {
+                exp = exp.and(rt.relValue.isNull());
+            }
+
+            long count = relationInstanceRepository.count(exp);
+            if (count == 0) {
+                // 数据不存在, 新增, 已存在时忽略
+                list.add(entity);
+            }
+        }
+        if (list.size() > 0) {
+            relationInstanceRepository.saveAll(list);
+            String now = DateUtils.format(new Date());
+            for (RelationInstance relation : list) {
+                DmpMessage message = new DmpMessage(RwdConstants.MESSAGE_INSTANCE_RELATION_CREATE, null);
+                message.setGroupCode(DmpParameterStorage.getGroupCode());
+                message.setProjectId(DmpParameterStorage.getProjectId());
+                message.setAppId(DmpParameterStorage.getAppId());
+                message.setUserId(DmpParameterStorage.getUserId());
+                message.setSendTime(now);
+                message.add("graphId", relation.getGraphId());
+                message.add("graphCode", relation.getGraphCode());
+                message.add("relCode", relation.getRelCode());
+                message.add("relValue", relation.getRelValue());
+                message.add("objFrom", relation.getObjFrom());
+                message.add("objTo", relation.getObjTo());
+                response.add(message);
+            }
+        }
+        return response;
+    }
+
+    @Transactional
+    public MapResponse delete(JacksonCriteria criteria) {
+        MapResponse response = new MapResponse();
+        super.checkParam(response, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
+        if (!response.success()) {
+            return response;
+        }
+        criteria.add(RwdConstants.OBJECT_GROUPCODE, DmpParameterStorage.getGroupCode());
+        criteria.add(RwdConstants.OBJECT_PROJECTID, DmpParameterStorage.getProjectId());
+        criteria.setWithoutCount(true);
+        QRelationInstance qt = QRelationInstance.relationInstance;
+        PagedResponse<RelationInstance> resp = criteriaUtils.query(qt, this::parse, criteria);
+        List<RelationInstance> data = resp.getData();
+        if (data != null && data.size() > 0) {
+            int cnt = 0;
+            for (RelationInstance datum : data) {
+                long execute = jpaQueryFactory.delete(qt).where(qt.groupCode.eq(DmpParameterStorage.getGroupCode()), qt.projectId.eq(DmpParameterStorage.getProjectId()), qt.id.eq(datum.getId())).execute();
+                cnt += execute;
+            }
+            response.add("deleteCount", cnt);
+
+            String now = DateUtils.format(new Date());
+            for (RelationInstance relation : data) {
+                DmpMessage message = new DmpMessage(RwdConstants.MESSAGE_INSTANCE_RELATION_DELETE, null);
+                message.setGroupCode(DmpParameterStorage.getGroupCode());
+                message.setProjectId(DmpParameterStorage.getProjectId());
+                message.setAppId(DmpParameterStorage.getAppId());
+                message.setUserId(DmpParameterStorage.getUserId());
+                message.setSendTime(now);
+                message.add("graphId", relation.getGraphId());
+                message.add("graphCode", relation.getGraphCode());
+                message.add("relCode", relation.getRelCode());
+                message.add("objFrom", relation.getObjFrom());
+                message.add("objTo", relation.getObjTo());
+                response.add(message);
+            }
+        } else {
+            response.add("deleteCount", 0);
+        }
+        return response;
+    }
+
+    public PagedResponse<ObjectNode> queryObject(JacksonCriteria criteria) {
+        PagedResponse response = new PagedResponse();
+        response.setCount(0L);
+        super.checkParam(response, RwdConstants.QUERY_GROUPCODE, RwdConstants.QUERY_PROJECTID);
+        if (!response.success()) {
+            return response;
+        }
+
+        JacksonCriteria objectCriteria = JacksonCriteria.newInstance();
+        JsonNode objectNode = criteria.getCriteria().get("objectCriteria");
+        if (objectNode != null && objectNode.isObject()) {
+            objectCriteria.getCriteria().setAll((ObjectNode) objectNode);
+        }
+
+        PagedResponse<RelationInstanceModel> queryResp = query(criteria);
+        List<RelationInstanceModel> data = queryResp.getData();
+        if (data == null || data.size() == 0) {
+            if (objectNode == null || !objectNode.isObject()) {
+                response.setCount(queryResp.getCount());
+                return response;
+            }
+        }
+
+        List<String> objectIds = null;
+        if (criteria.getCriteria().has(RwdConstants.RELATION_OBJ_FROM)) {
+            objectIds = data.stream().map(RelationInstanceModel::getObjTo).collect(Collectors.toList());
+        } else {
+            objectIds = data.stream().map(RelationInstanceModel::getObjFrom).collect(Collectors.toList());
+        }
+        if (objectIds == null || objectIds.size() == 0) {
+            return response;
+        }
+        objectCriteria.add("id", objectIds);
+
+        Set<String> withColumns = criteria.getWithColumns();
+        if (withColumns != null && withColumns.size() > 0) {
+            objectCriteria.setWithColumns(withColumns);
+        }
+        PagedResponse<ObjectNode> query = objectInstanceQueryService.query(objectCriteria);
+        response.setData(query.getData());
+        response.setCount(query.getCount());
+        return response;
+    }
 
 }