功能描述:实现通过会员ID、线路ID 、IP 、指纹设备、IP地址信息 、时间 、是否扣除物流币、访问来源、扣除说明、地址 中的任意组合查询出符合条件的结果。如下所示:
1、搜索条件展示:
2、搜索结果展示:
后台查询方法:
关键点:
1、通过 List<FilterDefinition<PageColllection>> filters = new List<FilterDefinition<PageColllection>>() 实现动态拼接查询条件
2、通过 filterBuilder.Regex(p => p.ip, new BsonRegularExpression($"/^{ip}/") 实现模糊查询
3、查询条件组合
if (filters.Count > 0) { filter = filterBuilder.And(filters); //查询条件 }
public (bool flag, long recordNum, List<PageColllection> colllections) GetPageColllectionData(string fromStr,string toStr, string cust_id,string PXID,string fingerOrImei,string ip,string beginTime,string endTime,int state,string remark,string Location, string tag, int pageIndex,int pageSize) { //返回值 List<PageColllection> colllections = new List<PageColllection>();//集合 long recordNum = 0;//记录总数 //过滤条件 var filterBuilder = Builders<PageColllection>.Filter; var filter = FilterDefinition<PageColllection>.Empty; var builder_sort = Builders<PageColllection>.Sort.Descending(p => p.unixTime);//排序 //动态查询条件拼接 List<FilterDefinition<PageColllection>> filters = new List<FilterDefinition<PageColllection>>(); //出发地 if (!string.IsNullOrEmpty(fromStr)) { string[] fromStrs = fromStr.Split('-'); if (fromStrs.Length == 3)//数据格式:河南省-郑州市-市辖区 { filters.Add(filterBuilder.Eq(p => p.FromPro, fromStrs[0]) & filterBuilder.Eq(p => p.FromCity, fromStrs[1]) & filterBuilder.Eq(p => p.FromArea, fromStrs[2])); } else if (fromStrs.Length==2)//数据格式:河南省-郑州市 { filters.Add(filterBuilder.Eq(p => p.FromPro, fromStrs[0]) & filterBuilder.Eq(p => p.FromCity, fromStrs[1])); } else //处理特殊地区:港 澳 台 数据格式:香港 { filters.Add(filterBuilder.Eq(p => p.FromPro, fromStrs[0])); } } //到达地 if (!string.IsNullOrEmpty(toStr)) { string[] toStrs = toStr.Split('-'); if (toStrs.Length == 3)//数据格式:河南省-郑州市-市辖区 { filters.Add(filterBuilder.Eq(p => p.ToPro, toStrs[0]) & filterBuilder.Eq(p => p.ToCity, toStrs[1]) & filterBuilder.Eq(p => p.ToArea, toStrs[2])); } else if (toStrs.Length==2)//数据格式:河南省-郑州市 { filters.Add(filterBuilder.Eq(p => p.ToPro, toStrs[0]) & filterBuilder.Eq(p => p.ToCity, toStrs[1])); } else //处理特殊地区:港 澳 台 数据格式:香港 { filters.Add(filterBuilder.Eq(p => p.ToArea, toStrs[0])); } } //会员ID int cust_idNum = 0; if (int.TryParse(cust_id,out cust_idNum)) { filters.Add(filterBuilder.Eq(p => p.Cust_id, cust_idNum)); } //线路ID int PXIDNum = 0; if (int.TryParse(PXID, out PXIDNum)) { filters.Add(filterBuilder.Eq(p => p.PXID, PXIDNum)); } //指纹ID if (!string.IsNullOrEmpty(fingerOrImei)) { filters.Add(filterBuilder.Eq(p => p.fingerOrImei, fingerOrImei)); } //模糊匹配IP查询 if (!string.IsNullOrEmpty(ip)&&ip.Split(".").Length<4) { filters.Add(filterBuilder.Regex(p => p.ip, new BsonRegularExpression($"/^{ip}/"))); } //非模糊匹配IP查询 if (!string.IsNullOrEmpty(ip) && ip.Split(".").Length == 4) { filters.Add(filterBuilder.Eq(p => p.ip, ip)); } //是否扣除 if (state != -1) { filters.Add(filterBuilder.Eq(p => p.state, state)); } //开始时间 DateTime bgTime; if (!string.IsNullOrEmpty(beginTime) & DateTime.TryParse(beginTime, out bgTime)) { long startTimeStamp = TimeHelper.ConvertDateTimeToInt(bgTime);//开始时间时间戳 filters.Add(filterBuilder.Gte(p => p.unixTime, startTimeStamp)); } //结束时间 DateTime edTime; if (!string.IsNullOrEmpty(endTime) & DateTime.TryParse(endTime, out edTime)) { long endTimeStamp = TimeHelper.ConvertDateTimeToInt(edTime);//结束时间时间戳 filters.Add(filterBuilder.Lte(p => p.unixTime, endTimeStamp)); } //扣除说明模糊查询 if (!string.IsNullOrEmpty(remark)) { filters.Add(filterBuilder.Regex(p => p.remarks, new BsonRegularExpression($"/^{remark}/"))); } //纯真反解析地区模糊查询 if (!string.IsNullOrEmpty(Location)) { filters.Add(filterBuilder.Regex(p => p.RegionalLocation, new BsonRegularExpression($"/^{Location}/"))); } //访问站点 if (!string.IsNullOrEmpty(tag)) { filters.Add(filterBuilder.Eq(p => p.Tag, tag)); } if (filters.Count > 0) { filter = filterBuilder.And(filters); //查询条件 } colllections = _mongodbPageView.GetCollection<PageColllection>("PageColllection").Find(filter).Sort(builder_sort).Skip((pageIndex-1)*pageSize).Limit(pageSize).ToList(); if (colllections.Count<=0) { return (false, recordNum, colllections); } recordNum = _mongodbPageView.GetCollection<PageColllection>("PageColllection").Find(filter).CountDocuments(); return (true, recordNum, colllections); }