diff --git a/src/main/java/cn/cloudowr/dict/Controller.java b/src/main/java/cn/cloudowr/dict/Controller.java index c4c1837..609c9a4 100644 --- a/src/main/java/cn/cloudowr/dict/Controller.java +++ b/src/main/java/cn/cloudowr/dict/Controller.java @@ -4,11 +4,10 @@ import cn.cloudowr.sdk.FastJSONUtil; import cn.cloudowr.sdk.jfinal.IdInterceptor; import cn.cloudowr.sdk.jfinal.RestfulStyle; import com.jfinal.aop.Before; +import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.UpdateOptions; -import com.mongodb.client.model.Updates; +import com.mongodb.client.model.*; import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; import org.bson.Document; @@ -18,6 +17,7 @@ import org.bson.types.ObjectId; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; /** * Created by lyf66 on 2017/2/16. @@ -31,22 +31,96 @@ public class Controller extends cn.cloudowr.sdk.jfinal.Controller implements Res String json = getPara("json"); Document document = null; if (json != null) { - document = Document.parse(json); + try { + document = Document.parse(json); + } catch (Exception e) { + renderError(400, "invalid json"); + return; + } + } + String projStr = getPara("proj"); + Document projDoc = null; + if (projStr != null) { + try { + projDoc = Document.parse(projStr); + } catch (Exception e) { + renderError(400, "invalid proj json"); + return; + } + } + String sortStr = getPara("sort"); + Document sortDoc = null; + if (sortStr != null) { + try { + sortDoc = Document.parse(sortStr); + } catch (Exception e) { + renderError(400, "invalid sort json"); + return; + } } List filter = new ArrayList<>(); if (document != null) { document.forEach((key, value) -> { - filter.add(Filters.eq(key, value)); + if ("$ex".equals(value)) { + filter.add(Filters.exists(key)); + } else if ("$nex" .equals(value)) { + filter.add(Filters.exists(key, false)); + } + else if (value != null && value.toString().startsWith("$re")) { + String s = value.toString(); + filter.add(Filters.regex(key, Pattern.compile(s.substring(3, s.length())))); + } else { + filter.add(Filters.eq(key, value)); + } }); } - List result = new ArrayList<>(); + List documents = new ArrayList<>(); + MongoCollection collection = getCollection(); + long total; + FindIterable findIterable; if (filter.size() != 0) { - getCollection().find(Filters.and(filter)).skip(pageSize * (pageNum - 1)).limit(pageSize).into(result); + total = collection.count(Filters.and(filter)); + findIterable = collection.find(Filters.and(filter)); } else { - getCollection().find().skip(pageSize * (pageNum - 1)).limit(pageSize).into(result); + total = collection.count(); + findIterable = collection.find(); } + int mod = (int) (total % pageSize); + long totalPage = mod == 0 ? total / pageSize : total / pageSize + 1; + findIterable.skip(pageSize * (pageNum - 1)).limit(pageSize); + if (projDoc != null) { + List projs = new ArrayList<>(); + projDoc.forEach((key, val) -> { + if ("id".equals(key)) { + projs.add(Projections.excludeId()); + } else if (1 == (int) val) { + projs.add(Projections.include(key)); + } else if (0 == (int) val) { + projs.add(Projections.exclude(key)); + } + }); + findIterable.projection(Projections.fields(projs)); + } + if (sortDoc != null) { + List sorts = new ArrayList<>(); + sortDoc.forEach((key, val) -> { + if ("id".equals(key)) key = "_id"; + if ("asc".equals(val)) { + sorts.add(Sorts.ascending(key)); + } else if ("desc".equals(val)) { + sorts.add(Sorts.descending(key)); + } + }); + findIterable.sort(Sorts.orderBy(sorts)); + } + + findIterable.into(documents); + Document result = new Document(); + result.put("documents", documents); + result.put("total", total); + result.put("totalPage", totalPage); renderJson(result); } diff --git a/src/main/webapp/doc/api.txt b/src/main/webapp/doc/api.txt index 4e986b9..4a736e2 100644 --- a/src/main/webapp/doc/api.txt +++ b/src/main/webapp/doc/api.txt @@ -3,6 +3,11 @@ index ( pageNum optional default 1, json optional ) { + special json: { + key: $ex //exists + key: $nex //not exists + key: $revalue //模糊查询,例:$re^te te开头的value + } return dict list json } show (id required) {