public Map query(Map map, String stockKey, List fieldList, Map countFieldMap, Map sortMap, String collectionName) {
//默认查询
QueryUtil queryUtil = defautQuery(map, stockKey);
//需要查询的字段
queryUtil.setGroupField(fieldList);
//默认返回统计字段
queryUtil.setCountField(countFieldMap);
//返回统计字段
queryUtil.setReturnField(fieldList);
//排序字段
queryUtil.setSortField(sortMap);
//查询
List queryList = queryUtil.queryAssemble();
List
查询语句
import com.alibaba.fastjson.JSON;
import com.sckj.basemongodbtool.commonconstants.CommonConstants;
import lombok.Setter;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class QueryUtil {
private static final String GT = "$gt"; //大于
private static final String GTE = "$gte"; //大于等于
private static final String LT = "$lt"; //小于
private static final String LTE = "$lte"; //小于等于
private static final String NE = "$ne"; //不等于
private static final String MATCH = "$match"; //匹配关键字
private static final String GROUP = "$group"; //分组关键字
private static final String FACET = "$facet"; //部份关键字
private static final String COUNT = "$count"; //统计加关键字
private static final String SORT = "$sort"; //统计关键字
private String gtName = "gt";
private String gtValue = "gt";
private String gteName = "gte";
private String gteValue = "gte";
private String ltName = "lt";
private String ltValue = "lt";
private String lteName = "lte";
private String lteValue = "lt";
private String neName = "ne";
private String neValue = "lt";
@Setter
private Integer page = 1;//查询条数
@Setter
private Integer pageSize = 10;//跳过条数
@Setter
private Map> matchIn;//查询条件in {查询字段:[字段值]}
@Setter
private List> judgeNumber;//判断数字
@Setter
private List groupField;//分组字段
@Setter
private Map countField;//统计字段 key为查询字段,value 0 或1 0为字段本身 1为计数
@Setter
private List returnField;//返回字段
@Setter
private Map sortField;//排序字段
public LinkedHashMap mapIn(Map> matchIn) {
LinkedHashMap reMap = new LinkedHashMap<>();
matchIn.forEach((key, value) -> {
reMap.put(key, inFieldToMap(value));
});
return reMap;
}
public List queryAssemble() {
//查询条件
List queryList = new ArrayList<>();
LinkedHashMap queryMap = new LinkedHashMap<>();
LinkedHashMap matchMap = new LinkedHashMap<>();
if (matchIn.size() != 0) {
matchMap = mapIn(matchIn);
}
if (judgeNumber != null && !judgeNumber.equals("null") && judgeNumber.size() != 0) {
for (int i = 0; i < judgeNumber.size(); i++) {
matchMap.putAll(judgeNumber.get(i));
}
}
if (matchMap.size() != 0) {
//拼接
queryMap.put(MATCH, matchMap);
String match = JSON.toJSONString(queryMap);
System.out.println(match);
queryList.add(match);
}
//查询字段
LinkedHashMap groupMap = new LinkedHashMap<>();
LinkedHashMap groupMapAll = new LinkedHashMap<>();
//返回查询字段
LinkedHashMap reMap = returnFieldToMap(returnField);
//需要查询的字段
if (groupField.size() != 0) {
groupMap.put(CommonConstants.TABLE_ID, queryFieldToMap(groupField));
}
//需要统计的字段
if (countField.size() != 0) {
countField.forEach((key, Value) -> {
groupMap.put(key, countFieldToMap(key, Value));
reMap.put(key, 1);
});
}
groupMapAll.put(GROUP, groupMap);
String group = JSON.toJSONString(groupMapAll);
System.out.println(group);
queryList.add(group);
//返回统计字段
reMap.put(CommonConstants.TABLE_ID, 0);
HashMap
对时间数字的处理
import lombok.Setter;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Setter
@Component
public class NumberUtil {
String column = null;
String gtValue = null;
String gteValue = null;
String ltValue = null;
String lteValue = null;
String neValue = null;
private static final String GT = "$gt"; //大于
private static final String GTE = "$gte"; //大于等于
private static final String LT = "$lt"; //小于
private static final String LTE = "$lte"; //小于等于
private static final String NE = "$ne"; //不等于
public Map getNumber() {
HashMap reMap = new HashMap<>();
HashMap oMap = new HashMap<>();
if (column != null) {
if ( gtValue != null) {
oMap.put(GT, gtValue);
}
if ( gteValue != null) {
oMap.put(GTE, gteValue);
}
if ( ltValue != null) {
oMap.put(LT, ltValue);
}
if ( lteValue != null) {
oMap.put(LTE, lteValue);
}
if ( neValue != null) {
oMap.put(NE, neValue);
}
reMap.put(column, oMap);
}
return reMap;
}
}
查询条件组装
import dcocd.custom.slsyxt.Util.DateUtil;
import lombok.Data;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Data
public class SalesEntity {
public static final String STOCK = "stock";//门店
public static String PLATFORM = "platform";//站
public static String PARAGRAPH = "area";//区域
public static String WIRE = "wire";//线
public static String START_TIME = "startTime";//开始时间
public static String END_TIME = "endTime";//结束时间
public static String TYPE = "type";//类型
public static String OTHER = "other";//其他
public Integer page = 1;//查询条数
public Integer pageSize = 20;//跳过条数
public List stock = new ArrayList();
public List wire = new ArrayList();
public String startTime;
public String endTime;
public String type;
public Map> other;
public static SalesEntity getQuery(Map maps) {
SalesEntity salesEntity = new SalesEntity();
if (maps == null || maps.get("paraList") == null) {
return salesEntity;
}
//适配查询参数
HashMap map = new HashMap<>();
map.put("page", maps.get("page"));
map.put("pageSize", maps.get("pageSize"));
List paraList = (List) maps.get("paraList");
if (paraList != null) {
paraList.forEach((tmap) -> {
tmap.forEach((key, Value) -> {
map.put(tmap.get("column"), tmap.get("value"));
});
});
}
if (map == null || map.size() == 0) {
return salesEntity;
}
if (map.get(TYPE) != null) {
salesEntity.type = map.get(TYPE).toString();
}
if (map.get(OTHER) != null) {
salesEntity.other = (Map) map.get(OTHER);
}
if ((List) map.get(STOCK) != null) {
salesEntity.stock = (List) map.get(STOCK);//门店
}
if ((List) map.get(WIRE) != null) {
salesEntity.wire = (List) map.get(WIRE);//线
}
Object st = map.get(START_TIME);
if (st != null) {
salesEntity.startTime = DateUtil.localToUtc(String.valueOf(st));//开始时间
}
Object et = map.get(END_TIME);
if (et != null) {
salesEntity.endTime = DateUtil.localToUtc(String.valueOf(et));//结束时间
}
Object page = map.get("page");
Object pageSize = map.get("pageSize");
if (page != null && pageSize != null) {
salesEntity.page = Integer.parseInt(page.toString());
salesEntity.pageSize = Integer.parseInt(pageSize.toString());
}
return salesEntity;
}
}
public QueryUtil defautQuery(Map map, String name) {
QueryUtil queryUtil = new QueryUtil();
//获取查询条件
SalesEntity query = salesEntity.getQuery(map);
List stock = query.getStock();
List wire = query.getWire();
String startTime = query.getStartTime();
String endTime = query.getEndTime();
String type = query.getType();
Map> other = query.getOther();
//获取登录人
String deptCode = userUtil.getUserInfo().getDeptCode();
Map shops = getShops(deptCode);
List shopList = (List) shops.get("data");
//true
HashMap> match = new HashMap<>();
//判断
if (!StringUtils.isEmpty(stock) && stock.size() != 0 && !stock.equals("null")) {
match.put(name, stock);
} else if (!StringUtils.isEmpty(shopList) && shopList.size() != 0 && !shopList.equals("null")) {
match.put(name, shopList);
}
//判断
if (!StringUtils.isEmpty(wire) && wire.size() != 0 && !wire.equals("null")) {
match.put("wire", wire);
}
//判断
if (!StringUtils.isEmpty(type) && type.length() != 0 && !type.equals("null")) {
ArrayList strings = new ArrayList<>();
strings.add(type);
match.put("type", strings);
}
//判断其他
if (!StringUtils.isEmpty(other) && other.size() != 0 && !other.equals("null")) {
other.forEach((key, value) -> {
match.put("key", value);
});
}
queryUtil.setMatchIn(match);
//判断时间
NumberUtil numberUtil = new NumberUtil();
if (!StringUtils.isEmpty(startTime) && startTime != null && !startTime.equals("null")) {
numberUtil.setColumn("ctime");
numberUtil.setGteValue(startTime);
}
if (!StringUtils.isEmpty(endTime) && endTime != null && !endTime.equals("null")) {
numberUtil.setColumn("ctime");
numberUtil.setLteValue(endTime);
}
if (!StringUtils.isEmpty(numberUtil) && numberUtil != null && !numberUtil.equals("null")) {
Map number = numberUtil.getNumber();
if (number.size() != 0) {
ArrayList> list = new ArrayList<>();
list.add(number);
queryUtil.setJudgeNumber(list);
}
}
//分页
queryUtil.setPage(query.getPage());
queryUtil.setPageSize(query.getPageSize());
return queryUtil;
}
数字转换
public static BigDecimal getBigDecimal(Object value) {
BigDecimal ret = null;
if (value != null) {
if (value instanceof BigDecimal) {
ret = (BigDecimal) value;
} else if (value instanceof String) {
ret = new BigDecimal((String) value);
} else if (value instanceof BigInteger) {
ret = new BigDecimal((BigInteger) value);
} else if (value instanceof Number) {
ret = new BigDecimal(((Number) value).doubleValue());
} else {
throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
}
}
return ret;
}
//保留2位小数
public BigDecimal getBigDecimal(BigDecimal dou) {
BigDecimal bigDecimal = new BigDecimal(dou.toString());
BigDecimal moneys = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
return moneys;
}
语句查询aggregateByShell
public List> aggregateByShell(String key, List queryList) {
List bsonList = new ArrayList();
for(int r = 0; r < queryList.size(); ++r) {
BasicDBObject bson = BasicDBObject.parse((String)queryList.get(r));
bsonList.add(bson);
}
MongoCollection collection = ((MongoTemplate)MongoAop.threadLocal.get()).getCollection(key);
MongoCursor cursor = collection.aggregate(bsonList).iterator();
ArrayList result;
Document list;
for(result = new ArrayList(); cursor.hasNext(); result.add(list)) {
list = (Document)cursor.next();
if (list.get("_id") != null) {
list.put("_id", list.get("_id").toString());
}
}
return result;
}