/src/SSCMS.Core/Repositories/ContentRepository.StlCache.cs

https://github.com/siteserver/cms · C# · 411 lines · 346 code · 54 blank · 11 comment · 48 complexity · 95d57fa5cc6b084d2b887de78554c25f MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Dapper;
  5. using Datory;
  6. using SSCMS.Core.Utils;
  7. using SSCMS.Models;
  8. using SSCMS.Utils;
  9. namespace SSCMS.Core.Repositories
  10. {
  11. public partial class ContentRepository
  12. {
  13. public string GetStlWhereString(int siteId, string group, string groupNot, string tags, bool isImageExists, bool isImage, bool isVideoExists, bool isVideo, bool isFileExists, bool isFile, bool isTopExists, bool isTop, bool isRecommendExists, bool isRecommend, bool isHotExists, bool isHot, bool isColorExists, bool isColor, string where)
  14. {
  15. var whereBuilder = new StringBuilder();
  16. whereBuilder.Append($" AND SiteId = {siteId} ");
  17. if (isImageExists)
  18. {
  19. whereBuilder.Append(isImage
  20. ? $" AND {nameof(Content.ImageUrl)} <> '' "
  21. : $" AND {nameof(Content.ImageUrl)} = '' ");
  22. }
  23. if (isVideoExists)
  24. {
  25. whereBuilder.Append(isVideo
  26. ? $" AND {nameof(Content.VideoUrl)} <> '' "
  27. : $" AND {nameof(Content.VideoUrl)} = '' ");
  28. }
  29. if (isFileExists)
  30. {
  31. whereBuilder.Append(isFile
  32. ? $" AND {nameof(Content.FileUrl)} <> '' "
  33. : $" AND {nameof(Content.FileUrl)} = '' ");
  34. }
  35. if (isTopExists)
  36. {
  37. whereBuilder.Append($" AND {nameof(Content.Top)} = {StringUtils.ToLower(isTop.ToString())} ");
  38. }
  39. if (isRecommendExists)
  40. {
  41. whereBuilder.Append($" AND {nameof(Content.Recommend)} = {StringUtils.ToLower(isRecommend.ToString())} ");
  42. }
  43. if (isHotExists)
  44. {
  45. whereBuilder.Append($" AND {nameof(Content.Hot)} = {StringUtils.ToLower(isHot.ToString())} ");
  46. }
  47. if (isColorExists)
  48. {
  49. whereBuilder.Append($" AND {nameof(Content.Color)} = {StringUtils.ToLower(isColor.ToString())} ");
  50. }
  51. var databaseType = _settingsManager.Database.DatabaseType;
  52. if (!string.IsNullOrEmpty(group))
  53. {
  54. group = group.Trim().Trim(',');
  55. var groups = ListUtils.GetStringList(group);
  56. if (groups.Count > 0)
  57. {
  58. whereBuilder.Append(" AND (");
  59. foreach (var theGroup in groups)
  60. {
  61. var trimGroup = theGroup.Trim();
  62. whereBuilder.Append(
  63. $" ({nameof(Content.GroupNames)} = '{AttackUtils.FilterSql(trimGroup)}' OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), trimGroup + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup)}) OR ");
  64. }
  65. whereBuilder.Length -= 3;
  66. whereBuilder.Append(") ");
  67. }
  68. }
  69. if (!string.IsNullOrEmpty(groupNot))
  70. {
  71. groupNot = groupNot.Trim().Trim(',');
  72. var groupNots = ListUtils.GetStringList(groupNot);
  73. if (groupNots.Count > 0)
  74. {
  75. whereBuilder.Append(" AND (");
  76. foreach (var theGroupNot in groupNots)
  77. {
  78. var trimGroup = theGroupNot.Trim();
  79. //whereBuilder.Append(
  80. // $" ({ContentAttribute.GroupNameCollection} <> '{trimGroup}' AND CHARINDEX('{trimGroup},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{trimGroup},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{trimGroup}',{ContentAttribute.GroupNameCollection}) = 0) AND ");
  81. whereBuilder.Append(
  82. $" ({nameof(Content.GroupNames)} <> '{trimGroup}' AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), trimGroup + ",")} AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup + ",")} AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup)}) AND ");
  83. }
  84. whereBuilder.Length -= 4;
  85. whereBuilder.Append(") ");
  86. }
  87. }
  88. if (!string.IsNullOrEmpty(tags))
  89. {
  90. tags = tags.Trim().Trim(',');
  91. var tagNames = ListUtils.GetStringList(tags);
  92. if (tagNames.Count > 0)
  93. {
  94. whereBuilder.Append(" AND (");
  95. foreach (var tagName in tagNames)
  96. {
  97. whereBuilder.Append(
  98. $" ({nameof(Content.TagNames)} = '{AttackUtils.FilterSql(tagName)}' OR {SqlUtils.GetInStr(databaseType, nameof(Content.TagNames), tagName + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.TagNames), "," + tagName + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.TagNames), "," + tagName)}) OR ");
  99. }
  100. whereBuilder.Length -= 3;
  101. whereBuilder.Append(") ");
  102. }
  103. }
  104. if (!string.IsNullOrEmpty(where))
  105. {
  106. whereBuilder.Append($" AND ({where}) ");
  107. }
  108. return whereBuilder.ToString();
  109. }
  110. public string GetStlWhereStringBySearch(string group, string groupNot, bool isImageExists, bool isImage, bool isVideoExists, bool isVideo, bool isFileExists, bool isFile, bool isTopExists, bool isTop, bool isRecommendExists, bool isRecommend, bool isHotExists, bool isHot, bool isColorExists, bool isColor, string where)
  111. {
  112. var whereBuilder = new StringBuilder();
  113. if (isImageExists)
  114. {
  115. whereBuilder.Append(isImage
  116. ? $" AND {nameof(Content.ImageUrl)} <> '' "
  117. : $" AND {nameof(Content.ImageUrl)} = '' ");
  118. }
  119. if (isVideoExists)
  120. {
  121. whereBuilder.Append(isVideo
  122. ? $" AND {nameof(Content.VideoUrl)} <> '' "
  123. : $" AND {nameof(Content.VideoUrl)} = '' ");
  124. }
  125. if (isFileExists)
  126. {
  127. whereBuilder.Append(isFile
  128. ? $" AND {nameof(Content.FileUrl)} <> '' "
  129. : $" AND {nameof(Content.FileUrl)} = '' ");
  130. }
  131. if (isTopExists)
  132. {
  133. whereBuilder.Append($" AND {nameof(Content.Top)} = {StringUtils.ToLower(isTop.ToString())} ");
  134. }
  135. if (isRecommendExists)
  136. {
  137. whereBuilder.Append($" AND {nameof(Content.Recommend)} = {StringUtils.ToLower(isRecommend.ToString())} ");
  138. }
  139. if (isHotExists)
  140. {
  141. whereBuilder.Append($" AND {nameof(Content.Hot)} = {StringUtils.ToLower(isHot.ToString())} ");
  142. }
  143. if (isColorExists)
  144. {
  145. whereBuilder.Append($" AND {nameof(Content.Color)} = {StringUtils.ToLower(isColor.ToString())} ");
  146. }
  147. var databaseType = _settingsManager.Database.DatabaseType;
  148. if (!string.IsNullOrEmpty(group))
  149. {
  150. group = group.Trim().Trim(',');
  151. var groupArr = group.Split(',');
  152. if (groupArr != null && groupArr.Length > 0)
  153. {
  154. whereBuilder.Append(" AND (");
  155. foreach (var theGroup in groupArr)
  156. {
  157. var trimGroup = theGroup.Trim();
  158. whereBuilder.Append(
  159. $" ({nameof(Content.GroupNames)} = '{AttackUtils.FilterSql(trimGroup)}' OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), trimGroup + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup)}) OR ");
  160. }
  161. if (groupArr.Length > 0)
  162. {
  163. whereBuilder.Length -= 3;
  164. }
  165. whereBuilder.Append(") ");
  166. }
  167. }
  168. if (!string.IsNullOrEmpty(groupNot))
  169. {
  170. groupNot = groupNot.Trim().Trim(',');
  171. var groupNotArr = groupNot.Split(',');
  172. if (groupNotArr != null && groupNotArr.Length > 0)
  173. {
  174. whereBuilder.Append(" AND (");
  175. foreach (var theGroupNot in groupNotArr)
  176. {
  177. var trimGroup = theGroupNot.Trim();
  178. //whereBuilder.Append(
  179. // $" ({ContentAttribute.GroupNameCollection} <> '{trimGroup}' AND CHARINDEX('{trimGroup},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{trimGroup},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{trimGroup}',{ContentAttribute.GroupNameCollection}) = 0) AND ");
  180. whereBuilder.Append(
  181. $" ({nameof(Content.GroupNames)} <> '{trimGroup}' AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), trimGroup + ",")} AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup + ",")} AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup)}) AND ");
  182. }
  183. if (groupNotArr.Length > 0)
  184. {
  185. whereBuilder.Length -= 4;
  186. }
  187. whereBuilder.Append(") ");
  188. }
  189. }
  190. if (!string.IsNullOrEmpty(where))
  191. {
  192. whereBuilder.Append($" AND ({where}) ");
  193. }
  194. return whereBuilder.ToString();
  195. }
  196. public string GetStlWhereString(int siteId, string group, string groupNot, string tags, bool isTopExists, bool isTop, string where)
  197. {
  198. var whereStringBuilder = new StringBuilder();
  199. if (isTopExists)
  200. {
  201. whereStringBuilder.Append($" AND IsTop = '{isTop}' ");
  202. }
  203. var databaseType = Database.DatabaseType;
  204. if (!string.IsNullOrEmpty(group))
  205. {
  206. group = group.Trim().Trim(',');
  207. var groupArr = group.Split(',');
  208. if (groupArr.Length > 0)
  209. {
  210. whereStringBuilder.Append(" AND (");
  211. foreach (var theGroup in groupArr)
  212. {
  213. var trimGroup = theGroup.Trim();
  214. whereStringBuilder.Append(
  215. $" ({nameof(Content.GroupNames)} = '{AttackUtils.FilterSql(trimGroup)}' OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), trimGroup + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.GroupNames), "," + trimGroup)}) OR ");
  216. }
  217. if (groupArr.Length > 0)
  218. {
  219. whereStringBuilder.Length -= 3;
  220. }
  221. whereStringBuilder.Append(") ");
  222. }
  223. }
  224. if (!string.IsNullOrEmpty(groupNot))
  225. {
  226. groupNot = groupNot.Trim().Trim(',');
  227. var groupNotArr = groupNot.Split(',');
  228. if (groupNotArr.Length > 0)
  229. {
  230. whereStringBuilder.Append(" AND (");
  231. foreach (var theGroupNot in groupNotArr)
  232. {
  233. //whereStringBuilder.Append(
  234. // $" ({ContentAttribute.GroupNameCollection} <> '{theGroupNot.Trim()}' AND CHARINDEX('{theGroupNot.Trim()},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{theGroupNot.Trim()},',{ContentAttribute.GroupNameCollection}) = 0 AND CHARINDEX(',{theGroupNot.Trim()}',{ContentAttribute.GroupNameCollection}) = 0) AND ");
  235. whereStringBuilder.Append(
  236. $" ({nameof(Content.GroupNames)} <> '{theGroupNot.Trim()}' AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), theGroupNot.Trim() + ",")} AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), "," + theGroupNot.Trim() + ",")} AND {SqlUtils.GetNotInStr(databaseType, nameof(Content.GroupNames), "," + theGroupNot.Trim())}) AND ");
  237. }
  238. if (groupNotArr.Length > 0)
  239. {
  240. whereStringBuilder.Length -= 4;
  241. }
  242. whereStringBuilder.Append(") ");
  243. }
  244. }
  245. if (!string.IsNullOrEmpty(tags))
  246. {
  247. tags = tags.Trim().Trim(',');
  248. var tagNames = ListUtils.GetStringList(tags);
  249. if (tagNames.Count > 0)
  250. {
  251. whereStringBuilder.Append(" AND (");
  252. foreach (var tagName in tagNames)
  253. {
  254. whereStringBuilder.Append(
  255. $" ({nameof(Content.TagNames)} = '{AttackUtils.FilterSql(tagName)}' OR {SqlUtils.GetInStr(databaseType, nameof(Content.TagNames), tagName + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.TagNames), "," + tagName + ",")} OR {SqlUtils.GetInStr(databaseType, nameof(Content.TagNames), "," + tagName)}) OR ");
  256. }
  257. whereStringBuilder.Length -= 3;
  258. whereStringBuilder.Append(") ");
  259. }
  260. }
  261. if (!string.IsNullOrEmpty(where))
  262. {
  263. whereStringBuilder.Append($" AND ({where}) ");
  264. }
  265. return whereStringBuilder.ToString();
  266. }
  267. public async Task<int> GetContentIdAsync(string tableName, int channelId, int taxis, bool isNextContent)
  268. {
  269. var repository = GetRepository(tableName);
  270. var query = Q
  271. .Select(nameof(Content.Id))
  272. .Where(nameof(Content.ChannelId), channelId)
  273. .WhereTrue(nameof(Content.Checked))
  274. ;
  275. if (isNextContent)
  276. {
  277. query
  278. .Where(nameof(Content.Taxis), "<", taxis)
  279. .OrderByDesc(nameof(Content.Taxis));
  280. }
  281. else
  282. {
  283. query
  284. .Where(nameof(Content.Taxis), ">", taxis)
  285. .OrderBy(nameof(Content.Taxis));
  286. }
  287. return await repository.GetAsync<int>(query);
  288. }
  289. public int GetContentId(string tableName, int channelId, bool isCheckedOnly, string orderByString)
  290. {
  291. var contentId = 0;
  292. var whereString = $"WHERE {nameof(Content.ChannelId)} = {channelId}";
  293. if (isCheckedOnly)
  294. {
  295. whereString += $" AND {nameof(Content.Checked)} = {StringUtils.ToLower(true.ToString())}";
  296. }
  297. var sqlString = SqlUtils.ToTopSqlString(Database.DatabaseType, tableName, "Id", whereString, orderByString, 1);
  298. var repository = GetRepository(tableName);
  299. using (var connection = repository.Database.GetConnection())
  300. {
  301. using (var rdr = connection.ExecuteReader(sqlString))
  302. {
  303. if (rdr.Read())
  304. {
  305. if (!rdr.IsDBNull(0))
  306. {
  307. contentId = rdr.GetInt32(0);
  308. }
  309. }
  310. rdr.Close();
  311. }
  312. }
  313. return contentId;
  314. }
  315. public async Task<int> GetSequenceAsync(string tableName, int siteId, int channelId, int contentId)
  316. {
  317. var repository = GetRepository(tableName);
  318. var taxis = await repository.GetAsync<int>(GetQuery(siteId, channelId)
  319. .Select(nameof(Content.Taxis))
  320. .Where(nameof(Content.Id), contentId)
  321. );
  322. return await repository.CountAsync(GetQuery(siteId, channelId)
  323. .WhereTrue(nameof(Content.Checked))
  324. .Where(nameof(Content.Taxis), "<", taxis)
  325. ) + 1;
  326. }
  327. private List<ContentSummary> GetStlDataSourceByContentNumAndWhereString(string tableName, int totalNum, string whereString, string orderByString)
  328. {
  329. List<ContentSummary> dataset = null;
  330. if (!string.IsNullOrEmpty(tableName))
  331. {
  332. //var sqlSelect = _databaseRepository.GetSelectSqlString(tableName, totalNum, MinListColumns, whereString, orderByString);
  333. //dataset = _databaseApi.ExecuteDataset(WebConfigUtils.ConnectionString, sqlSelect);
  334. }
  335. return dataset;
  336. }
  337. private List<ContentSummary> GetStlDataSourceByStartNum(string tableName, int startNum, int totalNum, string whereString, string orderByString)
  338. {
  339. List<ContentSummary> dataset = null;
  340. if (!string.IsNullOrEmpty(tableName))
  341. {
  342. //var sqlSelect = _databaseRepository.GetSelectSqlString(tableName, startNum, totalNum, MinListColumns, whereString, orderByString);
  343. //var sqlSelect = _databaseRepository.GetPageSqlString(tableName, MinListColumns, whereString, orderByString, startNum - 1, totalNum);
  344. //dataset = _databaseApi.ExecuteDataset(WebConfigUtils.ConnectionString, sqlSelect);
  345. }
  346. return dataset;
  347. }
  348. public async Task<int> GetCountCheckedImageAsync(Site site, Channel channel)
  349. {
  350. var repository = GetRepository(site, channel);
  351. return await repository.CountAsync(GetQuery(site.Id, channel.Id)
  352. .WhereTrue(nameof(Content.Checked))
  353. .WhereNotNullOrEmpty(nameof(Content.ImageUrl))
  354. ) + 1;
  355. }
  356. }
  357. }