using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Data.SqlTypes; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Z.EntityFramework.Plus; namespace YLShipBuildLandMap.Services.SystemSetting { public class AnnouncementService : IAnnouncementService { private YLShipBuildLandMapContext DbContext { get; set; } private IUserRoleService userRoleService { get; set; } public AnnouncementService(YLShipBuildLandMapContext dbContext, IUserRoleService userRoleService) { DbContext = dbContext; this.userRoleService = userRoleService; } public IGridResultSet GetAnnouncementPageList(Guid userID, int? pageIndex = null, int? pageSize = null) { var nowDateTime = DateTime.Now; var sqlDateTimeMinValue = SqlDateTime.MinValue.Value; var sqlDateTimeMaxValue = SqlDateTime.MaxValue.Value; //var socAnnouncementType = AnnouncementDAL.AnnouncementTypeRepository.GetList(); Expression> exp = (x => ((x.StartTime ?? sqlDateTimeMinValue) <= nowDateTime) && ((x.EndTime ?? sqlDateTimeMaxValue) >= nowDateTime)); Expression> exp1 = exp.And(x => x.SysAnnouncementSysRole.Any(w => w.Role.SysUserSysRole.Any(u => u.UserId == userID)) || x.SysAnnouncementSysUser.Any(y => y.UserId == userID)); Expression> exp2 = exp.And(x => x.SysAnnouncementSysRole.Count == 0 && x.SysAnnouncementSysUser.Count == 0); var sql = (from announce in DbContext.SysAnnouncement.Where(exp1) select announce) .Concat( from announce in DbContext.SysAnnouncement.Where(exp2) select announce ); return sql .OrderBy(x => x.IsTop == null ? 2 : (x.IsTop.Value ? 0 : 1)) .ThenByDescending(x => x.CreateTime) .ThenByDescending(x => x.StartTime).ToGridResultSet(pageIndex, pageSize); } private IQueryable GetAnnouncementViewQueryable(Expression> exp) { var query = (from announce in DbContext.SysAnnouncement.Where(exp) from cu in DbContext.SysUser.Where(x => announce.CreateUserId == x.UserId).DefaultIfEmpty() from mu in DbContext.SysUser.Where(x => announce.ModifyUserId == x.UserId).DefaultIfEmpty() select new AnnouncementView { AnnouncementID = announce.AnnouncementId, Title = announce.Title, Content = announce.Content, StartTime = announce.StartTime, EndTime = announce.EndTime, RecordStatus = announce.RecordStatus, CreateUserID = announce.CreateUserId, CreateUserName = cu.Name, CreateTime = announce.CreateTime, ModifyUserID = announce.ModifyUserId, ModifyUserName = mu.Name, ModifyTime = announce.ModifyTime, IsTop = announce.IsTop, OrderByNum = 1,//announce.IsTop == null ? 2 : (announce.IsTop.Value ? 0 : 1), UserIds = announce.SysAnnouncementSysUser.Select(s => s.UserId), RoleIds = announce.SysAnnouncementSysRole.Select(s => s.RoleId) }); return query; } public IGridResultSet GetAnnouncementViewList(string title, DateTime? startDate, DateTime? endDate, int? pageIndex = null, int? pageSize = null) { Expression> exp = (x => x.RecordStatus > 0); if (!string.IsNullOrEmpty(title)) { exp = exp.And(x => x.Title.Contains(title)); } if (startDate.HasValue) { var startDatePart = startDate.Value.Date; exp = exp.And(x => x.EndTime >= startDatePart); } if (endDate.HasValue) { var endDatePart = endDate.Value.Date.AddDays(1); exp = exp.And(x => x.StartTime < endDatePart); } var query = GetAnnouncementViewQueryable(exp); return query.OrderBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime) .ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetAnnouncementNewList(string title, DateTime? nowDate, Guid? userId, DateTime? startDate, DateTime? endDate, int? pageIndex = null, int? pageSize = null) { Expression> exp = (x => x.RecordStatus > 0); var formatStartDate = DateTime.Parse(nowDate.Value.ToString("yyyy-MM-dd")); var formatEndDate = DateTime.Parse(nowDate.Value.ToString("yyyy-MM-dd")).AddDays(1); //筛选 if (!string.IsNullOrEmpty(title)) { exp = exp.And(x => x.Title.Contains(title)); } if (nowDate.HasValue) { exp = exp.And(x => x.EndTime >= formatStartDate || !x.EndTime.HasValue); } if (nowDate.HasValue) { exp = exp.And(x => x.StartTime < formatEndDate || !x.StartTime.HasValue); } if (startDate.HasValue) { var startDatePart = startDate.Value.Date; exp = exp.And(x => x.StartTime >= startDatePart); } if (endDate.HasValue) { var endDatePart = endDate.Value.Date.AddDays(1); exp = exp.And(x => x.EndTime < endDatePart); } if (userId.HasValue) { //获取用户角色 var roleIds = userRoleService.SelectUserRole(userId.Value).Result; exp = exp.And(x => x.SysAnnouncementSysUser.Any(s => s.UserId == userId) || x.SysAnnouncementSysRole.Any(s => roleIds.Contains(s.RoleId)) || (x.SysAnnouncementSysRole.Count() == 0 && x.SysAnnouncementSysUser.Count() == 0)); } var query = GetAnnouncementViewQueryable(exp); var a = query.OrderBy(x => x.IsTop == true ? 0 : 1).ThenBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime) .ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.IsTop == true ? 0 : 1).ThenBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime) .ToGridResultSet(pageIndex, pageSize); } public AnnouncementView GetAnnouncementView(Guid announcementID) { return this.GetAnnouncementViewQueryable(x => x.AnnouncementId == announcementID).FirstOrDefault(); } public IList GetAnnouncementRoleViewList(Guid announcementID) { var sql = (from announce in this.DbContext.SysAnnouncement from r in announce.SysAnnouncementSysRole where announce.AnnouncementId == announcementID select new AnnouncementRoleView { RoleID = r.Role.RoleId, RoleName = r.Role.RoleName, Description = r.Role.Description }); return sql.OrderBy(x => x.RoleName).ToList(); } public IList GetAnnouncementUserViewList(Guid announcementID) { var sql = (from announce in this.DbContext.SysAnnouncement from u in announce.SysAnnouncementSysUser where announce.AnnouncementId == announcementID select new AnnouncementUserView { LoginID = u.User.LoginId, UserID = u.User.UserId, Name = u.User.Name }); return sql.OrderBy(x => x.Name).ToList(); } public async Task Save(AnnouncementView announce, List roleList, List userList, Guid operatorID) { DateTime nowTime = DateTime.Now; SysAnnouncement announcement = new SysAnnouncement(); if (announce.AnnouncementID.HasValue) { announcement = DbContext.SysAnnouncement .Include(x => x.SysAnnouncementSysRole) .Include(x => x.SysAnnouncementSysUser) .FirstOrDefault(x => x.AnnouncementId == announce.AnnouncementID); } else { announcement.AnnouncementId = Guid.NewGuid(); announcement.RecordStatus = 1; announcement.CreateUserId = operatorID; announcement.CreateTime = nowTime; this.DbContext.Add(announcement); } announcement.Title = announce.Title; announcement.IsTop = announce.IsTop; announcement.StartTime = announce.StartTime; announcement.EndTime = announce.EndTime; announcement.Content = announce.Content; announcement.ModifyUserId = operatorID; announcement.ModifyTime = nowTime; announcement.SysAnnouncementSysRole = roleList.Select(x => new SysAnnouncementSysRole { AnnouncementId = announcement.AnnouncementId, RoleId = x.RoleID.Value }).ToList(); announcement.SysAnnouncementSysUser = userList.Select(x => new SysAnnouncementSysUser { AnnouncementId = announcement.AnnouncementId, UserId = x.UserID.Value }).ToList(); await this.DbContext.SaveChangesAsync(); } public async Task Delete(List announcementIDList) { await this.DbContext.SysAnnouncementSysUser.Where(x => announcementIDList.Contains(x.AnnouncementId)).DeleteAsync(); await this.DbContext.SysAnnouncementSysRole.Where(x => announcementIDList.Contains(x.AnnouncementId)).DeleteAsync(); await this.DbContext.SysAnnouncement.Where(x => announcementIDList.Contains(x.AnnouncementId)).DeleteAsync(); } } }