- Added PersonName property to AnnouncementDto to display person's full name - Added lookup logic to fetch person names from Persons table based on phone numbers - Added InsertTime shadow property retrieval from EF Core to display creation date - Updated model snapshot with new shadow properties (InsertTime, IsRemoved, RemoveTime, UpdateTime) for Chat entity
124 lines
4.0 KiB
C#
124 lines
4.0 KiB
C#
using Complex.Application;
|
|
using Complex.Common.Dto;
|
|
using Complex.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Complex.Application.Services.Announcement
|
|
{
|
|
public interface IAnnouncementService
|
|
{
|
|
ResultDto<List<AnnouncementDto>> GetAnnouncements(Guid complexId, int page = 1, int pageSize = 20);
|
|
ResultDto<long> SendAnnouncement(Guid complexId, string body, byte? chatTypeId, string userPhone);
|
|
}
|
|
|
|
public class AnnouncementService : IAnnouncementService
|
|
{
|
|
private readonly IComplexDBContext _complexDBContext;
|
|
|
|
public AnnouncementService(IComplexDBContext complexDBContext)
|
|
{
|
|
_complexDBContext = complexDBContext;
|
|
}
|
|
|
|
public ResultDto<List<AnnouncementDto>> GetAnnouncements(Guid complexId, int page = 1, int pageSize = 20)
|
|
{
|
|
var query = _complexDBContext.Chats
|
|
.Where(c => c.ComplexId == complexId)
|
|
.OrderByDescending(c => c.Id);
|
|
|
|
var totalCount = query.Count();
|
|
|
|
var chats = query
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToList();
|
|
|
|
// Get unique phone numbers to look up person names
|
|
var phoneNumbers = chats
|
|
.Select(c => c.PersonMobileNumber)
|
|
.Where(p => p != null)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
var personNames = _complexDBContext.Persons
|
|
.Where(p => phoneNumbers.Contains(p.Id))
|
|
.ToDictionary(p => p.Id, p => $"{p.FirstName} {p.LastName}".Trim());
|
|
|
|
var result = chats.Select(c =>
|
|
{
|
|
personNames.TryGetValue(c.PersonMobileNumber, out var personName);
|
|
|
|
// Read InsertTime from EF Core shadow property
|
|
var insertTimeEntry = _complexDBContext.Entry(c).Property("InsertTime");
|
|
var insertTime = insertTimeEntry?.CurrentValue as DateTime?;
|
|
|
|
return new AnnouncementDto
|
|
{
|
|
Id = c.Id,
|
|
ComplexId = c.ComplexId,
|
|
PersonMobileNumber = c.PersonMobileNumber,
|
|
PersonName = personName,
|
|
Body = c.Body,
|
|
ChatTypeId = c.ChatTypeId,
|
|
InsertTime = insertTime.HasValue
|
|
? insertTime.Value.ToString("yyyy/MM/dd HH:mm")
|
|
: null
|
|
};
|
|
}).ToList();
|
|
|
|
return new ResultDto<List<AnnouncementDto>>
|
|
{
|
|
Data = result,
|
|
IsSuccess = true,
|
|
RowCount = totalCount
|
|
};
|
|
}
|
|
|
|
public ResultDto<long> SendAnnouncement(Guid complexId, string body, byte? chatTypeId, string userPhone)
|
|
{
|
|
var chat = new Chat
|
|
{
|
|
ComplexId = complexId,
|
|
Body = body,
|
|
ChatTypeId = chatTypeId ?? 0,
|
|
PersonMobileNumber = userPhone
|
|
};
|
|
|
|
_complexDBContext.Chats.Add(chat);
|
|
_complexDBContext.SaveChanges();
|
|
|
|
return new ResultDto<long>
|
|
{
|
|
Data = chat.Id,
|
|
IsSuccess = true,
|
|
Message = "ثبت با موفقیت انجام شد"
|
|
};
|
|
}
|
|
}
|
|
|
|
public class AnnouncementDto
|
|
{
|
|
public long Id { get; set; }
|
|
public Guid ComplexId { get; set; }
|
|
public string? PersonMobileNumber { get; set; }
|
|
public string? PersonName { get; set; }
|
|
public string? Body { get; set; }
|
|
public byte? ChatTypeId { get; set; }
|
|
public string? InsertTime { get; set; }
|
|
}
|
|
|
|
public class AnnouncementListRequest
|
|
{
|
|
public Guid ComplexId { get; set; }
|
|
public int Page { get; set; } = 1;
|
|
public int PageSize { get; set; } = 20;
|
|
}
|
|
|
|
public class SendAnnouncementRequest
|
|
{
|
|
public Guid ComplexId { get; set; }
|
|
public string Body { get; set; }
|
|
public byte? ChatTypeId { get; set; }
|
|
}
|
|
}
|