...
This commit is contained in:
246
Hushian.Application/Services/ChatService.cs
Normal file
246
Hushian.Application/Services/ChatService.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
using Common.Dtos.Conversation;
|
||||
using Common.Enums;
|
||||
using Hushian.Application.Contracts.Persistence;
|
||||
using Hushian.Application.Models;
|
||||
using Hushian.Domain.Entites;
|
||||
using Hushian.WebApi;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hushian.Application.Services
|
||||
{
|
||||
public class ChatService
|
||||
{
|
||||
private readonly IGenericRepository<Conversation> _ConversationRepository;
|
||||
private readonly IGenericRepository<ConversationResponse> _ConversationResponseRepository;
|
||||
private readonly CompanyService _companyService;
|
||||
private readonly UserService _userService;
|
||||
private readonly GroupService _groupService;
|
||||
private readonly ExperService _experService;
|
||||
private readonly IHubContext<ChatNotificationHub> _hubContext;
|
||||
public async Task<List<ChatItemDto>> GeChatsByExperID(int ExperID, ConversationStatus status)
|
||||
=> await _ConversationRepository.Get()
|
||||
.Include(inc => inc.Group)
|
||||
.Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper)
|
||||
.Where(w => w.ConversationResponses.Any(a => a.ExperID == ExperID) && w.Status == status)
|
||||
.Select(s => new ChatItemDto()
|
||||
{
|
||||
ID = s.ID,
|
||||
ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last().ExperID,
|
||||
ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName,
|
||||
GroupID = s.GroupID,
|
||||
GroupName = s.Group.Name,
|
||||
LastText = s.ConversationResponses.OrderBy(o => o.ID).Last().Text,
|
||||
LastMsgdate = s.Cdatetime.GetDatePersian(),
|
||||
LastMsgtime = s.Cdatetime.GetTime(),
|
||||
LastMsgType = s.ConversationResponses.OrderBy(o => o.ID).Last().Type,
|
||||
status = s.Status,
|
||||
UserID = s.UserID,
|
||||
UserFullName = string.IsNullOrEmpty(s.User.FullName) ? s.User.Mobile : s.User.FullName,
|
||||
Responses = s.ConversationResponses.Select(ss=>new ChatItemResponseDto()
|
||||
{
|
||||
ChatItemID = ss.ConversationID,
|
||||
ExperID = ss.ExperID,
|
||||
ExperName = ss.Exper.FullName,
|
||||
FileContent = ss.FileContent,
|
||||
FileName = ss.FileName,
|
||||
FileType = ss.FileType,
|
||||
ID = ss.ID,
|
||||
IsRead = ss.IsRead,
|
||||
text = ss.Text,
|
||||
Type = ss.Type
|
||||
})
|
||||
|
||||
}).ToListAsync();
|
||||
public async Task<List<ChatItemDto>> GetChatsByCompanyID(int CompanyID, ConversationStatus status)
|
||||
=> await _ConversationRepository.Get()
|
||||
.Include(inc => inc.Group)
|
||||
.Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper)
|
||||
.Where(w => w.CompanyID == CompanyID && w.Status == status)
|
||||
.Select(s => new ChatItemDto()
|
||||
{
|
||||
ID = s.ID,
|
||||
ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last().ExperID,
|
||||
ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName,
|
||||
GroupID = s.GroupID,
|
||||
GroupName = s.Group.Name,
|
||||
LastText = s.ConversationResponses.OrderBy(o => o.ID).Last().Text,
|
||||
LastMsgdate = s.Cdatetime.GetDatePersian(),
|
||||
LastMsgtime = s.Cdatetime.GetTime(),
|
||||
LastMsgType = s.ConversationResponses.OrderBy(o => o.ID).Last().Type,
|
||||
status = s.Status,
|
||||
UserID = s.UserID,
|
||||
UserFullName = string.IsNullOrEmpty(s.User.FullName) ? s.User.Mobile : s.User.FullName,
|
||||
Responses = s.ConversationResponses.Select(ss => new ChatItemResponseDto()
|
||||
{
|
||||
ChatItemID = ss.ConversationID,
|
||||
ExperID = ss.ExperID,
|
||||
ExperName = ss.Exper.FullName,
|
||||
FileContent = ss.FileContent,
|
||||
FileName = ss.FileName,
|
||||
FileType = ss.FileType,
|
||||
ID = ss.ID,
|
||||
IsRead = ss.IsRead,
|
||||
text = ss.Text,
|
||||
Type = ss.Type
|
||||
})
|
||||
|
||||
}).ToListAsync();
|
||||
public async Task<List<ChatItemDto>> ChatsAwaitingOurResponse(int CompanyID)
|
||||
=> await _ConversationRepository.Get()
|
||||
.Include(inc => inc.Group)
|
||||
.Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper)
|
||||
.Where(w => w.Status == ConversationStatus.Recorded && w.CompanyID == CompanyID)
|
||||
.Select(s => new ChatItemDto()
|
||||
{
|
||||
ID = s.ID,
|
||||
ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last().ExperID,
|
||||
ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName,
|
||||
GroupID = s.GroupID,
|
||||
GroupName = s.Group.Name,
|
||||
LastText = s.ConversationResponses.OrderBy(o => o.ID).Last().Text,
|
||||
LastMsgdate = s.Cdatetime.GetDatePersian(),
|
||||
LastMsgtime = s.Cdatetime.GetTime(),
|
||||
LastMsgType = s.ConversationResponses.OrderBy(o => o.ID).Last().Type,
|
||||
status = s.Status,
|
||||
UserID = s.UserID,
|
||||
UserFullName = string.IsNullOrEmpty(s.User.FullName) ? s.User.Mobile : s.User.FullName,
|
||||
Responses = s.ConversationResponses.Select(ss => new ChatItemResponseDto()
|
||||
{
|
||||
ChatItemID = ss.ConversationID,
|
||||
ExperID = ss.ExperID,
|
||||
ExperName = ss.Exper.FullName,
|
||||
FileContent = ss.FileContent,
|
||||
FileName = ss.FileName,
|
||||
FileType = ss.FileType,
|
||||
ID = ss.ID,
|
||||
IsRead = ss.IsRead,
|
||||
text = ss.Text,
|
||||
Type = ss.Type
|
||||
})
|
||||
|
||||
}).ToListAsync();
|
||||
//------------------------
|
||||
public async Task<ResponseBase<ChatItemDto>> NewChat(ADD_ConversationDto dto, ConversationType type = ConversationType.UE)
|
||||
{
|
||||
var Response = new ResponseBase<ChatItemDto>();
|
||||
if (await _companyService.AnyCompany(dto.CompanyID))
|
||||
{
|
||||
if (await _userService.AnyUser(dto.UserID))
|
||||
{
|
||||
if (type == ConversationType.Bot)
|
||||
{
|
||||
if (!await _companyService.AllowBot(dto.CompanyID))
|
||||
{
|
||||
Response.Errors.Add("دستیار گفتگو هوشمند برای این شرکت در دسترس نمی باشد");
|
||||
return Response;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dto.GroupID.HasValue || (dto.GroupID.HasValue
|
||||
&& await _groupService.AnyGroup(dto.GroupID.Value)
|
||||
&& await _groupService.CHeckGroupMemberCompany(dto.GroupID.Value, dto.CompanyID)))
|
||||
{
|
||||
Conversation conversation = new Conversation()
|
||||
{
|
||||
CompanyID = dto.CompanyID,
|
||||
UserID = dto.UserID,
|
||||
GroupID = dto.GroupID,
|
||||
ConversationResponses = new List<ConversationResponse>() { new() { Text = dto.Question, Type = type } }
|
||||
};
|
||||
var mi = await _ConversationRepository.ADD(conversation);
|
||||
Response.Value = new ChatItemDto()
|
||||
{
|
||||
ID = mi.ID,
|
||||
ExperID = mi.ConversationResponses.OrderBy(o => o.ID).Last().ExperID,
|
||||
ExperFullName = /*mi.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName*/ "",
|
||||
GroupID = mi.GroupID,
|
||||
GroupName = /*mi.Group.Name*/ "",
|
||||
LastText = mi.ConversationResponses.OrderBy(o => o.ID).Last().Text,
|
||||
LastMsgdate = mi.Cdatetime.GetDatePersian(),
|
||||
LastMsgtime = mi.Cdatetime.GetTime(),
|
||||
LastMsgType = mi.ConversationResponses.OrderBy(o => o.ID).Last().Type,
|
||||
status = mi.Status,
|
||||
UserID = mi.UserID,
|
||||
UserFullName = /*string.IsNullOrEmpty(mi.User.FullName) ? mi.User.Mobile : mi.User.FullName*/ ""
|
||||
|
||||
};
|
||||
Response.Success = mi.ID > 0;
|
||||
}
|
||||
else Response.Errors.Add("شناسه گروه صحیح نمی باشد");
|
||||
|
||||
|
||||
|
||||
}
|
||||
else Response.Errors.Add("شناسه کاربر یافت نشد");
|
||||
|
||||
}
|
||||
else Response.Errors.Add("شناسه شرکت یافت نشد");
|
||||
|
||||
|
||||
return Response;
|
||||
}
|
||||
public async Task<ResponseBase<ChatItemResponseDto>> ADDChatResponse(ADD_ConversationResponseDto dto, int? ExperID)
|
||||
{
|
||||
var Response = new ResponseBase<ChatItemResponseDto>();
|
||||
if (dto.Type == ConversationType.EU && !ExperID.HasValue)
|
||||
{
|
||||
Response.Errors.Add("کارشناس گفتگو را مشخص گنید");
|
||||
}
|
||||
else
|
||||
{
|
||||
var convModel = await _ConversationRepository.Get()
|
||||
.Include(inc => inc.ConversationResponses)
|
||||
.FirstOrDefaultAsync(w => w.ID == dto.ConversationID);
|
||||
|
||||
if (convModel != null)
|
||||
{
|
||||
if (ExperID.HasValue && !await _experService.CheckExperInCompany(convModel.CompanyID, ExperID.Value))
|
||||
{
|
||||
Response.Errors.Add("کارشناس گفتگو صحیح نمی باشد");
|
||||
return Response;
|
||||
}
|
||||
ConversationResponse response = new ConversationResponse()
|
||||
{
|
||||
ConversationID = convModel.ID,
|
||||
Text = dto.Text,
|
||||
Type = dto.Type,
|
||||
ExperID = ExperID,
|
||||
FileContent = dto.FileContent,
|
||||
FileType = dto.FileType,
|
||||
FileName = dto.FileName
|
||||
};
|
||||
var statuschangedb = await _ConversationResponseRepository.ADD(response);
|
||||
Response.Value = new ChatItemResponseDto()
|
||||
{
|
||||
ChatItemID = statuschangedb.ConversationID,
|
||||
ExperID = statuschangedb.ExperID,
|
||||
ExperName = /*statuschangedb.Exper.FullName*/ "",
|
||||
FileContent = statuschangedb.FileContent,
|
||||
FileName = statuschangedb.FileName,
|
||||
FileType = statuschangedb.FileType,
|
||||
ID = statuschangedb.ID,
|
||||
IsRead = statuschangedb.IsRead,
|
||||
text = statuschangedb.Text,
|
||||
Type = statuschangedb.Type
|
||||
};
|
||||
Response.Success = statuschangedb.ID > 0;
|
||||
|
||||
if (convModel.Status == ConversationStatus.Recorded && Response.Success)
|
||||
{
|
||||
convModel.Status = ConversationStatus.InProgress;
|
||||
await _ConversationRepository.UPDATE(convModel);
|
||||
}
|
||||
}
|
||||
else Response.Errors.Add("گفتگویی یافت نشد");
|
||||
}
|
||||
|
||||
return Response;
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,6 +11,7 @@ namespace Hushian.Application.Services
|
||||
{
|
||||
public class ExperService
|
||||
{
|
||||
private readonly IGenericRepository<Company> _CompanyRepository;
|
||||
private readonly IGenericRepository<Exper> _ExperRepository;
|
||||
private readonly VerificationService _VerificationService;
|
||||
private readonly IMapper _mapper;
|
||||
@@ -62,6 +63,30 @@ namespace Hushian.Application.Services
|
||||
}
|
||||
public async Task<Read_ExperDto?> GetInfoExper(int ExperID)
|
||||
=> _mapper.Map<Read_ExperDto>(await _ExperRepository.Get().FirstOrDefaultAsync(w => w.ID == ExperID));
|
||||
public async Task<CurrentUserInfo?> GetCurrentUserInfo(int CompanyID, int? ExperID)
|
||||
{
|
||||
if (ExperID.HasValue)
|
||||
{
|
||||
return await _ExperRepository.Get().Where(w => w.ID == ExperID).Select(s => new CurrentUserInfo
|
||||
{
|
||||
CompanyID = CompanyID,
|
||||
ExperID = ExperID,
|
||||
Username = s.UserName,
|
||||
Role = "Exper"
|
||||
}).FirstOrDefaultAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
return await _CompanyRepository.Get().Where(w => w.ID == CompanyID).Select(s => new CurrentUserInfo
|
||||
{
|
||||
CompanyID = CompanyID,
|
||||
ExperID = null,
|
||||
Username = s.Mobile,
|
||||
Role = "Company"
|
||||
}).FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Read_ExperDto>> GetExpersInCompany(int companyID)
|
||||
=> _mapper.Map<List<Read_ExperDto>>(await _ExperRepository.Get().Where(w => w.CompanyID == companyID).ToListAsync());
|
||||
public async Task<Read_ExperDto?> GetExpersInGroup(int GroupID)
|
||||
@@ -72,15 +97,15 @@ namespace Hushian.Application.Services
|
||||
exper.FullName = model.FullName;
|
||||
return await _ExperRepository.UPDATEBool(exper);
|
||||
}
|
||||
public async Task<bool> ChangeAvailableExper(int ExperID,int CompanyID,bool Available)
|
||||
public async Task<bool> ChangeAvailableExper(int ExperID, int CompanyID, bool Available)
|
||||
{
|
||||
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(w => w.ID == ExperID && w.CompanyID==CompanyID);
|
||||
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(w => w.ID == ExperID && w.CompanyID == CompanyID);
|
||||
if (exper == null) return false;
|
||||
exper.Available = Available;
|
||||
return await _ExperRepository.UPDATEBool(exper);
|
||||
}
|
||||
public async Task<bool> CheckExperInCompany(int CompanyID, int ExperID)
|
||||
=>await _ExperRepository.Get().AnyAsync(w => w.ID == ExperID && w.CompanyID==CompanyID);
|
||||
=> await _ExperRepository.Get().AnyAsync(w => w.ID == ExperID && w.CompanyID == CompanyID);
|
||||
public async Task<bool> DeleteExper(int ExperID, int CompanyID)
|
||||
{
|
||||
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(w => w.ID == ExperID && w.CompanyID == CompanyID);
|
||||
@@ -151,7 +176,7 @@ namespace Hushian.Application.Services
|
||||
}
|
||||
public async Task<bool> AvailableExperInCompany(int ExperID)
|
||||
{
|
||||
return await _ExperRepository.Get().AnyAsync(w => w.ID == ExperID && w.Available);
|
||||
return await _ExperRepository.Get().AnyAsync(w => w.ID == ExperID && w.Available);
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user