123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- 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<SysAnnouncement> 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<Func<SysAnnouncement, bool>> exp = (x => ((x.StartTime ?? sqlDateTimeMinValue) <= nowDateTime)
- && ((x.EndTime ?? sqlDateTimeMaxValue) >= nowDateTime));
- 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));
- Expression<Func<SysAnnouncement, bool>> 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<AnnouncementView> GetAnnouncementViewQueryable(Expression<Func<SysAnnouncement, bool>> 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<AnnouncementView> GetAnnouncementViewList(string title, DateTime? startDate, DateTime? endDate, int? pageIndex = null, int? pageSize = null)
- {
- Expression<Func<SysAnnouncement, bool>> 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<AnnouncementView>(pageIndex, pageSize);
- }
- public IGridResultSet<AnnouncementView> GetAnnouncementNewList(string title, DateTime? nowDate, Guid? userId, DateTime? startDate, DateTime? endDate, int? pageIndex = null, int? pageSize = null)
- {
- Expression<Func<SysAnnouncement, bool>> 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<AnnouncementView>(pageIndex, pageSize);
- return query.OrderBy(x => x.IsTop == true ? 0 : 1).ThenBy(x => x.OrderByNum).ThenByDescending(x => x.CreateTime).ThenByDescending(x => x.StartTime)
- .ToGridResultSet<AnnouncementView>(pageIndex, pageSize);
- }
- public AnnouncementView GetAnnouncementView(Guid announcementID)
- {
- return this.GetAnnouncementViewQueryable(x => x.AnnouncementId == announcementID).FirstOrDefault();
- }
- public IList<AnnouncementRoleView> 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<AnnouncementUserView> 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<AnnouncementRoleView> roleList, List<AnnouncementUserView> 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<Guid> 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();
- }
- }
- }
|