赞
踩
- package com.bx.utils;
-
- import com.google.common.collect.Lists;
- import com.java.framework.model.WebViewModel;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.domain.Sort;
- import org.springframework.data.mongodb.core.query.Criteria;
- import org.springframework.data.mongodb.core.query.Query;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import java.util.regex.Pattern;
-
- import static org.springframework.data.mongodb.core.query.Criteria.where;
-
- /**
- * @author: buwt
- * @date: 2023/02/01
- * @time: 12:45
- */
- public class MongodbCriteriaUtils {
-
- public static void setQuery(Query query, WebViewModel model) {
- WebViewModel.Relation relation = model.getRelationsCondition();
- if (relation == null) {
- List<WebViewModel.Condition> conditions = model.getCondition();
- if (conditions == null || conditions.isEmpty()) {
- return;
- }
- relation = new WebViewModel.Relation();
- relation.setRelationType("AND");
- relation.setConditions(conditions);
- }
- List<Criteria> criteriaList = getCriteria(relation);
- if (criteriaList.isEmpty()) {
- return;
- }
- query.addCriteria(operator(relation.getRelationType(), criteriaList));
- }
-
- public static Pageable pageable(WebViewModel viewModel) {
- WebViewModel.Page page = viewModel.getPage();
- int currPage = page.getCurrPage();
- int pageSize = page.getPageSize();
- if (pageSize < 1) {
- pageSize = 10;
- }
- List<WebViewModel.Order> orders = viewModel.getOrder();
- if (orders != null && orders.size() > 0) {
- List<Sort.Order> orderList = Lists.newArrayList();
- for (WebViewModel.Order order : orders) {
- try {
- if (StringUtils.isBlank(order.getColName())) {
- continue;
- }
- Sort.Order ord;
- if (StringUtils.isBlank(order.getOrderType())) {
- ord = new Sort.Order(Sort.Direction.ASC, order.getColName());
- } else {
- ord = new Sort.Order(Sort.Direction.fromString(order.getOrderType()), order.getColName());
- }
- orderList.add(ord);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (!orders.isEmpty()) {
- Sort sort = new Sort(orderList);
- return new PageRequest(Math.max(currPage - 1, 0), pageSize, sort);
- }
- }
- return new PageRequest(Math.max(currPage - 1, 0), pageSize);
- }
-
-
- private static List<Criteria> getCriteria(WebViewModel.Relation relation) {
- List<Criteria> criteriaList = new ArrayList<>();
- List<WebViewModel.Condition> conditions = relation.getConditions();
- List<WebViewModel.Relation> relations = relation.getRelations();
- if (isNotEmpty(conditions)) {
- for (WebViewModel.Condition condition : conditions) {
- if (StringUtils.isBlank(condition.getColName())) {
- continue;
- }
- Criteria criteria = getCriteria(condition.getColName(), condition.getRuleType(), condition.getValue());
- if (criteria == null) {
- continue;
- }
- criteriaList.add(criteria);
- }
- }
- if (isNotEmpty(relations)) {
- for (WebViewModel.Relation relation1 : relations) {
- List<Criteria> critters = getCriteria(relation1);
- if (isNotEmpty(critters)) {
- Criteria criteria = operator(relation1.getRelationType(), critters);
- criteriaList.add(criteria);
- }
- }
- }
- return criteriaList;
- }
-
- private static boolean isEmpty(Collection<?> collection) {
- return collection == null || collection.isEmpty();
- }
-
- private static boolean isNotEmpty(Collection<?> collection) {
- return !isEmpty(collection);
- }
-
- private static Criteria getCriteria(String colName, String ruleType, Object value) {
- if (StringUtils.isBlank(ruleType)) {
- ruleType = "EQ";
- }
- switch (ruleType.toUpperCase()) {
- case "NE":
- return where(colName).ne(value);
- case "LIKE":
- return where(colName).regex(Pattern.compile("^.*" + value + ".*$"));
- case "IN": {
- if (value == null || StringUtils.isBlank(value.toString())) {
- return null;
- }
- ArrayList<String> arrayList = Lists.newArrayList(value.toString().split(","));
- return where(colName).in(arrayList);
- }
- case "NIN": {
- if (value == null || StringUtils.isBlank(value.toString())) {
- return null;
- }
- ArrayList<String> arrayList = Lists.newArrayList(value.toString().split(","));
- return where(colName).nin(arrayList);
- }
- case "GE":
- return where(colName).gte(value);
- case "LE":
- return where(colName).lte(value);
- case "GT":
- return where(colName).gt(value);
- case "LT":
- return where(colName).lt(value);
- case "EQ":
- default:
- return where(colName).is(value);
- }
- }
-
- private static Criteria operator(String ruleType, List<Criteria> criteriaList) {
- Criteria criteria = new Criteria();
- switch (ruleType.toUpperCase()) {
- case "OR":
- criteria.orOperator(criteriaList.toArray(new Criteria[0]));
- break;
- case "AND":
- default:
- criteria.andOperator(criteriaList.toArray(new Criteria[0]));
- break;
- }
- return criteria;
- }
- }
-

- package com.java.framework.model;
-
- import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
-
- import java.io.Serializable;
- import java.io.StringWriter;
- import java.util.ArrayList;
- import java.util.List;
-
- @JsonIgnoreProperties(ignoreUnknown = true)
- @ApiModel(value = "查询数据请求对象", description = "前端的数据查询接口请求数据封装到这个对象")
- public class WebViewModel implements Serializable {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- // 查询条件
- private List<Condition> condition;
-
- //关系条件
- private Relation relationsCondition;
-
- //组
- // private List<Map<String,Object>> group;
-
- // 排序
- private List<Order> order;
-
- // 分页
- private Page page;
-
- public List<Condition> getCondition() {
- return condition;
- }
-
- public void setCondition(List<Condition> condition) {
- this.condition = condition;
- }
-
- public List<Order> getOrder() {
- return order;
- }
-
- public void setOrder(List<Order> order) {
- this.order = order;
- }
-
- public Page getPage() {
- return page;
- }
-
- public void setPage(Page page) {
- this.page = page;
- }
-
- public Relation getRelationsCondition() {
- return relationsCondition;
- }
-
- public void setRelationsCondition(Relation relationsCondition) {
- this.relationsCondition = relationsCondition;
- }
-
- // 条件
- // @ApiModel(value = "查询条件", description = "查询条件组装对象")
- public static class Condition implements Serializable{
- @ApiModelProperty(value = "属性名(property)")
- private String colName;
- @ApiModelProperty(value = "匹配规则", allowableValues = "EQ, NE, LIKE, GT, LT, GE, LE, IN, NIN")
- private String ruleType;
- @ApiModelProperty(value = "属性值")
- private Object value;
- public Condition() {
- // TODO Auto-generated constructor stub
- }
- public String getColName() {
- return colName;
- }
- public void setColName(String colName) {
- this.colName = colName;
- }
- public String getRuleType() {
- return ruleType;
- }
- public void setRuleType(String ruleType) {
- this.ruleType = ruleType;
- }
- public Object getValue() {
- return value;
- }
- public void setValue(Object value) {
- this.value = value;
- }
- @Override
- public String toString() {
- return "Condition {colName=" + colName + ", ruleType=" + ruleType + ", value=" + value + "}";
- }
- }
-
- public static class Relation implements Serializable {
- @ApiModelProperty(value = "条件关系符(默认AND)", allowableValues = "AND,OR")
- private String relationType = "AND";
- private List<Condition> conditions = new ArrayList<>();
- private List<Relation> relations = new ArrayList<>();
- public Relation(){}
-
- public String getRelationType() {
- return relationType;
- }
-
- public void setRelationType(String relationType) {
- this.relationType = relationType;
- }
-
- public List<Condition> getConditions() {
- return conditions;
- }
-
- public void setConditions(List<Condition> conditions) {
- this.conditions = conditions;
- }
-
- public void addCondition(Condition condition) {
- this.conditions.add(condition);
- }
-
- public void removeCondition(Condition condition) {
- this.conditions.remove(condition);
- }
-
- public List<Relation> getRelations() {
- return relations;
- }
-
- public void setRelations(List<Relation> relations) {
- this.relations = relations;
- }
-
- @Override
- public String toString() {
- return "Relation{" +
- "relationType='" + relationType + '\'' +
- ", conditions=" + conditions +
- ", relations=" + relations +
- '}';
- }
- }
-
- // @ApiModel(value = "排序对象", description = "排序方式组装对象")
- public static class Order implements Serializable {
- @ApiModelProperty(value = "属性名(property)")
- private String colName;
- @ApiModelProperty(value = "排序类型", allowableValues = "asc,desc")
- private String orderType;
- public String getColName() {
- return colName;
- }
- public void setColName(String colName) {
- this.colName = colName;
- }
- public String getOrderType() {
- return orderType;
- }
- public void setOrderType(String orderType) {
- this.orderType = orderType;
- }
- @Override
- public String toString() {
- return "Order [colName=" + colName + ", orderType=" + orderType + "]";
- }
- }
-
- @ApiModel(value = "分页对象", description = "分页数据组装对象")
- public static class Page implements Serializable {
- @ApiModelProperty(value = "当前页码", dataType = "int", required = true)
- private int currPage;
- @ApiModelProperty(value = "每页条数", dataType = "int", required = true)
- private int pageSize;
-
- public int getCurrPage() {
- return currPage;
- }
-
- public void setCurrPage(int currPage) {
- this.currPage = currPage;
- }
-
- public int getPageSize() {
- return pageSize;
- }
-
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
-
- @Override
- public String toString() {
- return "Page [currPage=" + currPage + ", pageSize=" + pageSize + "]";
- }
- }
- }

- @PostMapping("taskDoListTwo")
- @ApiOperation("我的已办")
- public DataModel<ProcessHandleHistory> taskDoListTwo(@RequestBody WebViewModel model) throws Exception {
- Pageable pageable = MongodbCriteriaUtils.pageable(model);
- Query query = new Query();
- MongodbCriteriaUtils.setQuery(query, model);
- List<ProcessHandleHistory> handleHistories =
- mongoTemplate.find(query.with(pageable), ProcessHandleHistory.class, "process_handle_history");
- long count = mongoTemplate.count(query, ProcessHandleHistory.class, "process_handle_history");
- return SupplierHelper.create(DataModel.Builder<ProcessHandleHistory>::new)
- .setDatas(handleHistories)
- .setTotal(count)
- .setPageSize(pageable.getPageSize())
- .setCurrPage(pageable.getPageNumber()+1)
- .setTotalPages((int) (count / pageable.getPageSize()))
- .build();
- }

- {
- "page": {
- "currPage": 0,
- "pageSize": 10
- },
- "condition": [
- {
- "colName": "owner",
- "ruleType": "EQ",
- "value": "5d5a3cebcfb7e91344537723"
- },
- {
- "colName": "commentType",
- "ruleType": "NE",
- "value": "发起申请"
- },
- {
- "colName": "applyUser.bussTitle",
- "ruleType": "LIKE",
- "value": "测试"
- }
- ],
- "order": [
- {
- "colName": "startTime",
- "orderType": "desc"
- }
- ]
- }

- {
- "flag": true,
- "shortMessage": null,
- "message": null,
- "condition": null,
- "datas": [
- {
- "applyUser": {
- "applyUser": "吴婷婷",
- "bussTitle": "测试27",
- "bussId": "60adece23bc08a6180fd966c"
- },
- "owner": "5d5a3cebcfb7e91344537723"
- },
- {
- "applyUser": {
- "applyUser": "administ",
- "bussTitle": "新流程测试",
- "bussId": "5efef4fbe42b5a09cd7f3205"
- }
- "owner": "5d5a3cebcfb7e91344537723"
- }
- ],
- "data": null,
- "page": {
- "currPage": 1,
- "pageSize": 10,
- "total": 53,
- "totalPages": 5
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。