using System; using System.Collections.Generic; using System.Linq; using System.Text; using CZFW.Framework.Model.ViewModel; using CZFW.MDB.Util; using MongoDB.Bson; using MongoDB.Driver; using Newtonsoft.Json.Linq; namespace CZFW.MDB { public class PageRepository : IPage { ISchemaRepository _schemaRepository; IMongoRepository _repository; public PageRepository(ISchemaRepository schemaRepository, IMongoRepository repository) { _schemaRepository = schemaRepository; _repository = repository; } public BsonDocument Get(string pageKey, string dataKey, string type, string projection = null) { BsonDocument projectionDoc = _schemaRepository.GetHeader(pageKey, type, out string tableName); if (string.IsNullOrWhiteSpace(projection)) { projectionDoc = BsonDocument.Parse(projection); } _repository.SetCurrentTable(tableName); var data = _repository.Get(dataKey, projectionDoc.ToJson()); return data; } public BsonDocument GetByName(string pageName, string dataKey, string type, string projection = null) { BsonDocument projectionDoc; if (!string.IsNullOrWhiteSpace(projection)) { projectionDoc = BsonDocument.Parse(projection); } else { projectionDoc = _schemaRepository.GetHeaderByName(pageName, type, out string tableId); } _repository.SetCurrentTable(pageName); var data = _repository.Get(dataKey, projectionDoc.ToJson()); return data; } public long GetCount(string sid, string pageName, string query = "{}") { _repository.SetCurrentTable(pageName); var jobj = JObject.Parse(query); jobj.Add(Constants.SITE_NAME_FIELD, sid); query = jobj.ToString(); var count = _repository.GetCount(query); return count; } public IMongoCollection GetCurrentTable() { return _repository.CurrentTable; } public BsonArray GetList(string sid, string pageKey, string type, string query = "{}", string projection = null, int pageIndex = 1, int pageSize = 20, string sort = "{}", bool useCusor = false) { string tableName = string.Empty; if (string.IsNullOrWhiteSpace(projection)) projection = _schemaRepository.GetHeader(pageKey, type, out tableName).ToJson(); else tableName = _schemaRepository.GetNameById(pageKey); _repository.SetCurrentTable(tableName); var jobj = JObject.Parse(query); jobj.Add(Constants.SITE_NAME_FIELD, sid); query = jobj.ToString(); var data = _repository.GetList(query: query, projection: projection, pageIndex: pageIndex, pageSize: pageSize, sort: sort, useCusor: useCusor); return data; } public BsonArray GetListByName(string sid, string pageName, string type = null, string query = "{}", string projection = "{}", int pageIndex = 1, int pageSize = 20, string sort = "{}", bool useCusor = false) { _repository.SetCurrentTable(pageName); var jobj = JObject.Parse(query); jobj.Add(Constants.SITE_NAME_FIELD, sid); query = jobj.ToString(); var data = _repository.GetList(query: query, projection: projection, pageIndex: pageIndex, pageSize: pageSize, sort: sort, useCusor: useCusor); return data; } public BsonDocument GetOne(string sid, string pageKey, string query, string type, string projection = null, string sort = "{}") { string tableName = string.Empty; if (string.IsNullOrWhiteSpace(projection)) projection = _schemaRepository.GetHeader(pageKey, type, out tableName).ToJson(); else tableName = _schemaRepository.GetNameById(pageKey); _repository.SetCurrentTable(tableName); var jobj = JObject.Parse(query); jobj.Add(Constants.SITE_NAME_FIELD, sid); query = jobj.ToString(); var data = _repository.GetOne(query: query, projection: projection, sort: sort); return data; } public BsonDocument GetOneByName(string sid, string pageName, string query = "{}", string projection = "{}", string sort = "{}") { _repository.SetCurrentTable(pageName); query = QueryAppendSid(query, sid); var data = _repository.GetOne(query: query, projection: projection, sort: sort); return data; } public BsonArray GetRelated(string sid, string pageName, string excludeId = null, string query = "{}", string projection = "{}", int size = 4) { _repository.SetCurrentTable(pageName); size = size + 1; var paras = new List(); query = QueryAppendSid(query, sid); var queryDef = $"{{$match:{query}}}"; paras.Add(queryDef); var proj = JObject.Parse(projection); if (proj.HasValues) paras.Add($"{{$project:{projection}}}"); var sample = $"{{$sample:{{size:{size}}}}}"; paras.Add(sample); var res = _repository.Aggregate(paras.ToArray()); //排除本条记录 if (!string.IsNullOrWhiteSpace(excludeId)) { var existing = res.SingleOrDefault(x => x[Constants.THE_ID_FILED].AsObjectId == ObjectId.Parse(excludeId)); if (existing != null) res.Remove(existing); } if (res.Count > size - 1) res.RemoveAt(0); return res; } string QueryAppendSid(string query, string sid) { var jobj = JObject.Parse(query); if (jobj.ContainsKey(Constants.SITE_NAME_FIELD)) jobj[Constants.SITE_NAME_FIELD] = sid; else jobj.Add(Constants.SITE_NAME_FIELD, sid); query = jobj.ToString(); return query; } public ResultModel Inc(string pageName, string key, string field, int amount) { _repository.SetCurrentTable(pageName); var res = _repository.Increase(key, field, amount); return res; } } }