NewManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using CP.Cache;
  9. using CP.Dapper;
  10. using CP.Model;
  11. using CP.Model.ZiXun;
  12. using CP.Model.ZiXun.Entity;
  13. using CP.Model.ZiXun.Query;
  14. using Dapper;
  15. namespace CP.Business.ZiXun
  16. {
  17. public class NewManager : DbHelper
  18. {
  19. /// 缓存
  20. /// </summary>
  21. static WMCache cache = WMCache.GetCacheService();
  22. /// <summary>
  23. /// 获取新闻,资讯数据
  24. /// </summary>
  25. /// <param name="query"></param>
  26. /// <returns></returns>
  27. public List<newManagerModels> GetNewManagerList(NewManagerTopQuery query)
  28. {
  29. var list = new List<newManagerModels>();
  30. query.NewsTypeList.ForEach(p =>
  31. {
  32. string key = "NewManagerList" + (int)p;
  33. var data = cache.GetObject<List<newManagerModels>>(key);
  34. if (data == null)
  35. {
  36. var listAll = cache.GetObject<List<newManagerModels>>(CacheKeys.NewManagerListList);
  37. if (listAll == null)
  38. {
  39. listAll = GetAll(query.TopNumber);
  40. cache.AddObject(CacheKeys.NewManagerListList, listAll, (int)CacheTime.NewManager);
  41. }
  42. data = listAll.Where(q => q.menuId == (int)p).ToList().OrderByDescending(q => q.createTime).ToList();
  43. cache.AddObject(key, data, (int)CacheTime.NewManager);
  44. }
  45. list = list.Concat(data).ToList();
  46. });
  47. return list;
  48. }
  49. private List<newManagerModels> GetAll(int topNumber)
  50. {
  51. var sql = string.Empty;
  52. DynamicParameters Parameters = new DynamicParameters();
  53. FieldInfo[] enumFields = typeof(NewManagerTypeEnum).GetFields(); //获取字段信息对象集合
  54. var typeList = new List<int>();
  55. //遍历集合
  56. foreach (FieldInfo field in enumFields)
  57. {
  58. if (!field.IsSpecialName)
  59. {
  60. typeList.Add(Convert.ToInt32(field.GetRawConstantValue()));
  61. }
  62. }
  63. typeList.ForEach(p =>
  64. {
  65. sql += $"SELECT * FROM (SELECT TOP {topNumber} * FROM newManagerModels WHERE menuId = @menuId{typeList.IndexOf(p)} ORDER BY createTime DESC ) tem ";
  66. Parameters.Add("menuId" + typeList.IndexOf(p), p);
  67. if (typeList.IndexOf(p) != typeList.Count - 1)
  68. sql += " UNION ";
  69. });
  70. return db.FindList<newManagerModels>(sql, Parameters).ToList();
  71. }
  72. /// <summary>
  73. /// 得到公告
  74. /// </summary>
  75. /// <returns></returns>
  76. public NewList GetNewList()
  77. {
  78. string key = CacheKeys.NewList;
  79. var data = cache.GetObject<NewList>(key);
  80. if (data == null || data.ID <= 0)
  81. {
  82. string sql = $"" +
  83. $"SELECT TOP 1 [ID],[title],[description],[menuId],[createTime],[createUserName] FROM [dbo].[newManagerModels] " +
  84. $"WHERE menuId = @menuId AND isDelete = 0 ORDER BY createTime DESC ";
  85. var list = db.FindList<NewList>(sql, new { menuId = 108 }).ToList();
  86. data = list.Count > 0 ? list[0] : new NewList();
  87. cache.AddObject(key, data, (int)CacheTime.NewManager);
  88. }
  89. return data;
  90. }
  91. }
  92. }