PageRepository.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CZFW.Framework.Model.ViewModel;
  6. using CZFW.MDB.Util;
  7. using MongoDB.Bson;
  8. using MongoDB.Driver;
  9. using Newtonsoft.Json.Linq;
  10. namespace CZFW.MDB
  11. {
  12. public class PageRepository : IPage
  13. {
  14. ISchemaRepository _schemaRepository;
  15. IMongoRepository _repository;
  16. public PageRepository(ISchemaRepository schemaRepository, IMongoRepository repository)
  17. {
  18. _schemaRepository = schemaRepository;
  19. _repository = repository;
  20. }
  21. public BsonDocument Get(string pageKey, string dataKey, string type, string projection = null)
  22. {
  23. BsonDocument projectionDoc = _schemaRepository.GetHeader(pageKey, type, out string tableName);
  24. if (string.IsNullOrWhiteSpace(projection))
  25. {
  26. projectionDoc = BsonDocument.Parse(projection);
  27. }
  28. _repository.SetCurrentTable(tableName);
  29. var data = _repository.Get(dataKey, projectionDoc.ToJson());
  30. return data;
  31. }
  32. public BsonDocument GetByName(string pageName, string dataKey, string type, string projection = null)
  33. {
  34. BsonDocument projectionDoc;
  35. if (!string.IsNullOrWhiteSpace(projection))
  36. {
  37. projectionDoc = BsonDocument.Parse(projection);
  38. }
  39. else
  40. {
  41. projectionDoc = _schemaRepository.GetHeaderByName(pageName, type, out string tableId);
  42. }
  43. _repository.SetCurrentTable(pageName);
  44. var data = _repository.Get(dataKey, projectionDoc.ToJson());
  45. return data;
  46. }
  47. public long GetCount(string sid, string pageName, string query = "{}")
  48. {
  49. _repository.SetCurrentTable(pageName);
  50. var jobj = JObject.Parse(query);
  51. jobj.Add(Constants.SITE_NAME_FIELD, sid);
  52. query = jobj.ToString();
  53. var count = _repository.GetCount(query);
  54. return count;
  55. }
  56. public IMongoCollection<BsonDocument> GetCurrentTable()
  57. {
  58. return _repository.CurrentTable;
  59. }
  60. 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)
  61. {
  62. string tableName = string.Empty;
  63. if (string.IsNullOrWhiteSpace(projection))
  64. projection = _schemaRepository.GetHeader(pageKey, type, out tableName).ToJson();
  65. else
  66. tableName = _schemaRepository.GetNameById(pageKey);
  67. _repository.SetCurrentTable(tableName);
  68. var jobj = JObject.Parse(query);
  69. jobj.Add(Constants.SITE_NAME_FIELD, sid);
  70. query = jobj.ToString();
  71. var data = _repository.GetList(query: query, projection: projection, pageIndex: pageIndex, pageSize: pageSize, sort: sort, useCusor: useCusor);
  72. return data;
  73. }
  74. 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)
  75. {
  76. _repository.SetCurrentTable(pageName);
  77. var jobj = JObject.Parse(query);
  78. jobj.Add(Constants.SITE_NAME_FIELD, sid);
  79. query = jobj.ToString();
  80. var data = _repository.GetList(query: query, projection: projection, pageIndex: pageIndex, pageSize: pageSize, sort: sort, useCusor: useCusor);
  81. return data;
  82. }
  83. public BsonDocument GetOne(string sid, string pageKey, string query, string type, string projection = null, string sort = "{}")
  84. {
  85. string tableName = string.Empty;
  86. if (string.IsNullOrWhiteSpace(projection))
  87. projection = _schemaRepository.GetHeader(pageKey, type, out tableName).ToJson();
  88. else
  89. tableName = _schemaRepository.GetNameById(pageKey);
  90. _repository.SetCurrentTable(tableName);
  91. var jobj = JObject.Parse(query);
  92. jobj.Add(Constants.SITE_NAME_FIELD, sid);
  93. query = jobj.ToString();
  94. var data = _repository.GetOne(query: query, projection: projection, sort: sort);
  95. return data;
  96. }
  97. public BsonDocument GetOneByName(string sid, string pageName, string query = "{}", string projection = "{}", string sort = "{}")
  98. {
  99. _repository.SetCurrentTable(pageName);
  100. query = QueryAppendSid(query, sid);
  101. var data = _repository.GetOne(query: query, projection: projection, sort: sort);
  102. return data;
  103. }
  104. public BsonArray GetRelated(string sid, string pageName, string excludeId = null, string query = "{}", string projection = "{}", int size = 4)
  105. {
  106. _repository.SetCurrentTable(pageName);
  107. size = size + 1;
  108. var paras = new List<string>();
  109. query = QueryAppendSid(query, sid);
  110. var queryDef = $"{{$match:{query}}}";
  111. paras.Add(queryDef);
  112. var proj = JObject.Parse(projection);
  113. if (proj.HasValues)
  114. paras.Add($"{{$project:{projection}}}");
  115. var sample = $"{{$sample:{{size:{size}}}}}";
  116. paras.Add(sample);
  117. var res = _repository.Aggregate(paras.ToArray());
  118. //排除本条记录
  119. if (!string.IsNullOrWhiteSpace(excludeId))
  120. {
  121. var existing = res.SingleOrDefault(x => x[Constants.THE_ID_FILED].AsObjectId == ObjectId.Parse(excludeId));
  122. if (existing != null)
  123. res.Remove(existing);
  124. }
  125. if (res.Count > size - 1)
  126. res.RemoveAt(0);
  127. return res;
  128. }
  129. string QueryAppendSid(string query, string sid)
  130. {
  131. var jobj = JObject.Parse(query);
  132. if (jobj.ContainsKey(Constants.SITE_NAME_FIELD))
  133. jobj[Constants.SITE_NAME_FIELD] = sid;
  134. else
  135. jobj.Add(Constants.SITE_NAME_FIELD, sid);
  136. query = jobj.ToString();
  137. return query;
  138. }
  139. public ResultModel Inc(string pageName, string key, string field, int amount) {
  140. _repository.SetCurrentTable(pageName);
  141. var res = _repository.Increase(key, field, amount);
  142. return res;
  143. }
  144. }
  145. }