AnnouncementService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Bowin.Common.Linq;
  2. using Bowin.Common.Linq.Entity;
  3. using YLShipBuildLandMap.Entity;
  4. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  5. using Microsoft.EntityFrameworkCore;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data.SqlTypes;
  9. using System.Linq;
  10. using System.Linq.Expressions;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Z.EntityFramework.Plus;
  14. namespace YLShipBuildLandMap.Services.SystemSetting
  15. {
  16. public class AnnouncementService : IAnnouncementService
  17. {
  18. private YLShipBuildLandMapContext DbContext { get; set; }
  19. private IUserRoleService userRoleService { get; set; }
  20. public AnnouncementService(YLShipBuildLandMapContext dbContext, IUserRoleService userRoleService)
  21. {
  22. DbContext = dbContext;
  23. this.userRoleService = userRoleService;
  24. }
  25. public IGridResultSet<SysAnnouncement> GetAnnouncementPageList(Guid userID, int? pageIndex = null, int? pageSize = null)
  26. {
  27. var nowDateTime = DateTime.Now;
  28. var sqlDateTimeMinValue = SqlDateTime.MinValue.Value;
  29. var sqlDateTimeMaxValue = SqlDateTime.MaxValue.Value;
  30. //var socAnnouncementType = AnnouncementDAL.AnnouncementTypeRepository.GetList();
  31. Expression<Func<SysAnnouncement, bool>> exp = (x => ((x.StartTime ?? sqlDateTimeMinValue) <= nowDateTime)
  32. && ((x.EndTime ?? sqlDateTimeMaxValue) >= nowDateTime));
  33. Expression<Func<SysAnnouncement, bool>> exp1 = exp.And(x => x.SysAnnouncementSysRole.Any(w => w.Role.SysUserSysRole.Any(u => u.UserId == userID)) || x.SysAnnouncementSysUser.Any(y => y.UserId == userID));
  34. Expression<Func<SysAnnouncement, bool>> exp2 = exp.And(x => x.SysAnnouncementSysRole.Count == 0 && x.SysAnnouncementSysUser.Count == 0);
  35. var sql = (from announce in DbContext.SysAnnouncement.Where(exp1)
  36. select announce)
  37. .Concat(
  38. from announce in DbContext.SysAnnouncement.Where(exp2)
  39. select announce
  40. );
  41. return sql
  42. .OrderBy(x => x.IsTop == null ? 2 : (x.IsTop.Value ? 0 : 1))
  43. .ThenByDescending(x => x.CreateTime)
  44. .ThenByDescending(x => x.StartTime).ToGridResultSet(pageIndex, pageSize);
  45. }
  46. private IQueryable<AnnouncementView> GetAnnouncementViewQueryable(Expression<Func<SysAnnouncement, bool>> exp)
  47. {
  48. var query = (from announce in DbContext.SysAnnouncement.Where(exp)
  49. from cu in DbContext.SysUser.Where(x => announce.CreateUserId == x.UserId).DefaultIfEmpty()
  50. from mu in DbContext.SysUser.Where(x => announce.ModifyUserId == x.UserId).DefaultIfEmpty()
  51. select new AnnouncementView
  52. {
  53. AnnouncementID = announce.AnnouncementId,
  54. Title = announce.Title,
  55. Content = announce.Content,
  56. StartTime = announce.StartTime,
  57. EndTime = announce.EndTime,
  58. RecordStatus = announce.RecordStatus,
  59. CreateUserID = announce.CreateUserId,
  60. CreateUserName = cu.Name,
  61. CreateTime = announce.CreateTime,
  62. ModifyUserID = announce.ModifyUserId,
  63. ModifyUserName = mu.Name,
  64. ModifyTime = announce.ModifyTime,
  65. IsTop = announce.IsTop,
  66. OrderByNum = 1,//announce.IsTop == null ? 2 : (announce.IsTop.Value ? 0 : 1),
  67. UserIds = announce.SysAnnouncementSysUser.Select(s => s.UserId),
  68. RoleIds = announce.SysAnnouncementSysRole.Select(s => s.RoleId)
  69. });
  70. return query;
  71. }
  72. public IGridResultSet<AnnouncementView> GetAnnouncementViewList(string title, DateTime? startDate, DateTime? endDate, int? pageIndex = null, int? pageSize = null)
  73. {
  74. Expression<Func<SysAnnouncement, bool>> exp = (x => x.RecordStatus > 0);
  75. if (!string.IsNullOrEmpty(title))
  76. {
  77. exp = exp.And(x => x.Title.Contains(title));
  78. }
  79. if (startDate.HasValue)
  80. {
  81. var startDatePart = startDate.Value.Date;
  82. exp = exp.And(x => x.EndTime >= startDatePart);
  83. }
  84. if (endDate.HasValue)
  85. {
  86. var endDatePart = endDate.Value.Date.AddDays(1);
  87. exp = exp.And(x => x.StartTime < endDatePart);
  88. }
  89. var query = GetAnnouncementViewQueryable(exp);
  90. return query.OrderBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime)
  91. .ToGridResultSet<AnnouncementView>(pageIndex, pageSize);
  92. }
  93. public IGridResultSet<AnnouncementView> GetAnnouncementNewList(string title, DateTime? nowDate, Guid? userId, DateTime? startDate, DateTime? endDate, int? pageIndex = null, int? pageSize = null)
  94. {
  95. Expression<Func<SysAnnouncement, bool>> exp = (x => x.RecordStatus > 0);
  96. var formatStartDate = DateTime.Parse(nowDate.Value.ToString("yyyy-MM-dd"));
  97. var formatEndDate = DateTime.Parse(nowDate.Value.ToString("yyyy-MM-dd")).AddDays(1);
  98. //筛选
  99. if (!string.IsNullOrEmpty(title))
  100. {
  101. exp = exp.And(x => x.Title.Contains(title));
  102. }
  103. if (nowDate.HasValue)
  104. {
  105. exp = exp.And(x => x.EndTime >= formatStartDate || !x.EndTime.HasValue);
  106. }
  107. if (nowDate.HasValue)
  108. {
  109. exp = exp.And(x => x.StartTime < formatEndDate || !x.StartTime.HasValue);
  110. }
  111. if (startDate.HasValue)
  112. {
  113. var startDatePart = startDate.Value.Date;
  114. exp = exp.And(x => x.StartTime >= startDatePart);
  115. }
  116. if (endDate.HasValue)
  117. {
  118. var endDatePart = endDate.Value.Date.AddDays(1);
  119. exp = exp.And(x => x.EndTime < endDatePart);
  120. }
  121. if (userId.HasValue)
  122. {
  123. //获取用户角色
  124. var roleIds = userRoleService.SelectUserRole(userId.Value).Result;
  125. exp = exp.And(x => x.SysAnnouncementSysUser.Any(s => s.UserId == userId)
  126. || x.SysAnnouncementSysRole.Any(s => roleIds.Contains(s.RoleId))
  127. || (x.SysAnnouncementSysRole.Count() == 0
  128. && x.SysAnnouncementSysUser.Count() == 0));
  129. }
  130. var query = GetAnnouncementViewQueryable(exp);
  131. var a = query.OrderBy(x => x.IsTop == true ? 0 : 1).ThenBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime)
  132. .ToGridResultSet<AnnouncementView>(pageIndex, pageSize);
  133. return query.OrderBy(x => x.IsTop == true ? 0 : 1).ThenBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime)
  134. .ToGridResultSet<AnnouncementView>(pageIndex, pageSize);
  135. }
  136. public AnnouncementView GetAnnouncementView(Guid announcementID)
  137. {
  138. return this.GetAnnouncementViewQueryable(x => x.AnnouncementId == announcementID).FirstOrDefault();
  139. }
  140. public IList<AnnouncementRoleView> GetAnnouncementRoleViewList(Guid announcementID)
  141. {
  142. var sql = (from announce in this.DbContext.SysAnnouncement
  143. from r in announce.SysAnnouncementSysRole
  144. where announce.AnnouncementId == announcementID
  145. select new AnnouncementRoleView
  146. {
  147. RoleID = r.Role.RoleId,
  148. RoleName = r.Role.RoleName,
  149. Description = r.Role.Description
  150. });
  151. return sql.OrderBy(x => x.RoleName).ToList();
  152. }
  153. public IList<AnnouncementUserView> GetAnnouncementUserViewList(Guid announcementID)
  154. {
  155. var sql = (from announce in this.DbContext.SysAnnouncement
  156. from u in announce.SysAnnouncementSysUser
  157. where announce.AnnouncementId == announcementID
  158. select new AnnouncementUserView
  159. {
  160. LoginID = u.User.LoginId,
  161. UserID = u.User.UserId,
  162. Name = u.User.Name
  163. });
  164. return sql.OrderBy(x => x.Name).ToList();
  165. }
  166. public async Task Save(AnnouncementView announce, List<AnnouncementRoleView> roleList, List<AnnouncementUserView> userList, Guid operatorID)
  167. {
  168. DateTime nowTime = DateTime.Now;
  169. SysAnnouncement announcement = new SysAnnouncement();
  170. if (announce.AnnouncementID.HasValue)
  171. {
  172. announcement = DbContext.SysAnnouncement
  173. .Include(x => x.SysAnnouncementSysRole)
  174. .Include(x => x.SysAnnouncementSysUser)
  175. .FirstOrDefault(x => x.AnnouncementId == announce.AnnouncementID);
  176. }
  177. else
  178. {
  179. announcement.AnnouncementId = Guid.NewGuid();
  180. announcement.RecordStatus = 1;
  181. announcement.CreateUserId = operatorID;
  182. announcement.CreateTime = nowTime;
  183. this.DbContext.Add(announcement);
  184. }
  185. announcement.Title = announce.Title;
  186. announcement.IsTop = announce.IsTop;
  187. announcement.StartTime = announce.StartTime;
  188. announcement.EndTime = announce.EndTime;
  189. announcement.Content = announce.Content;
  190. announcement.ModifyUserId = operatorID;
  191. announcement.ModifyTime = nowTime;
  192. announcement.SysAnnouncementSysRole = roleList.Select(x => new SysAnnouncementSysRole
  193. {
  194. AnnouncementId = announcement.AnnouncementId,
  195. RoleId = x.RoleID.Value
  196. }).ToList();
  197. announcement.SysAnnouncementSysUser = userList.Select(x => new SysAnnouncementSysUser
  198. {
  199. AnnouncementId = announcement.AnnouncementId,
  200. UserId = x.UserID.Value
  201. }).ToList();
  202. await this.DbContext.SaveChangesAsync();
  203. }
  204. public async Task Delete(List<Guid> announcementIDList)
  205. {
  206. await this.DbContext.SysAnnouncementSysUser.Where(x => announcementIDList.Contains(x.AnnouncementId)).DeleteAsync();
  207. await this.DbContext.SysAnnouncementSysRole.Where(x => announcementIDList.Contains(x.AnnouncementId)).DeleteAsync();
  208. await this.DbContext.SysAnnouncement.Where(x => announcementIDList.Contains(x.AnnouncementId)).DeleteAsync();
  209. }
  210. }
  211. }