Files
Hushian/Hushian.Application/Services/ConversationService.cs
mmrbnjd 1924c88e7a ...
2025-07-07 22:04:07 +03:30

221 lines
10 KiB
C#

using AutoMapper;
using Common.Dtos.Conversation;
using Common.Enums;
using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models;
using Hushian.Domain.Entites;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hushian.Application.Services
{
public class ConversationService
{
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;
public ConversationService(
IGenericRepository<Conversation> conversationRepository
, IGenericRepository<ConversationResponse> conversationResponseRepository
, CompanyService companyService, UserService userService, GroupService groupService
, ExperService experService)
{
_ConversationRepository = conversationRepository;
_ConversationResponseRepository = conversationResponseRepository;
_companyService = companyService;
_userService = userService;
_groupService = groupService;
_experService = experService;
}
public async Task<ResponseBase<int>> NewConversation(ADD_ConversationDto dto, ConversationType type = ConversationType.UE)
{
var Response = new ResponseBase<int>();
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 } }
};
Response.Value = (await _ConversationRepository.ADD(conversation)).ID;
Response.Success = Response.Value > 0;
}
else Response.Errors.Add("شناسه گروه صحیح نمی باشد");
}
else Response.Errors.Add("شناسه کاربر یافت نشد");
}
else Response.Errors.Add("شناسه شرکت یافت نشد");
return Response;
}
public async Task<ResponseBase<int>> NewConversationResponse(ADD_ConversationResponseDto dto, int? ExperID)
{
var Response = new ResponseBase<int>();
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
};
Response.Value = (await _ConversationResponseRepository.ADD(response)).ID;
Response.Success = Response.Value > 0;
}
else Response.Errors.Add("گفتگویی یافت نشد");
}
return Response;
}
public async Task<List<Read_ConversationDto>> MyConversation(int UserID)
=> await _ConversationRepository.Get()
.Include(inc => inc.Group)
.Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper)
.Where(w => w.UserID == UserID)
.Select(s => new Read_ConversationDto()
{
ExperID = s.ConversationResponses.Last().ExperID,
ExperFullName = s.ConversationResponses.Last().Exper.FullName,
GroupID = s.GroupID,
GroupName = s.Group.Name,
LastText = s.ConversationResponses.Last().Text,
LastMsgdate = s.Cdatetime.GetDatePersian(),
LastMsgtime = s.Cdatetime.GetTime(),
LastMsgType = s.ConversationResponses.Last().Type,
NoReadCount = s.ConversationResponses.Count(c => !c.IsRead),
status = s.Status,
UserID = s.UserID,
UserFullName = s.User.FullName
}).ToListAsync();
public async Task<List<Read_ConversationDto>> MyConversationIsFinished(int ExperID)
=> 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 == ConversationStatus.Finished)
.Select(s => new Read_ConversationDto()
{
ExperID = s.ConversationResponses.Last().ExperID,
ExperFullName = s.ConversationResponses.Last().Exper.FullName,
GroupID = s.GroupID,
GroupName = s.Group.Name,
LastText = s.ConversationResponses.Last().Text,
LastMsgdate = s.Cdatetime.GetDatePersian(),
LastMsgtime = s.Cdatetime.GetTime(),
LastMsgType = s.ConversationResponses.Last().Type,
NoReadCount = s.ConversationResponses.Count(c => !c.IsRead),
status = s.Status,
UserID = s.UserID,
UserFullName = s.User.FullName
}).ToListAsync();
public async Task<List<Read_ConversationDto>> MyConversationIsInProgress(int ExperID)
=> 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 == ConversationStatus.InProgress)
.Select(s => new Read_ConversationDto()
{
ExperID = s.ConversationResponses.Last().ExperID,
ExperFullName = s.ConversationResponses.Last().Exper.FullName,
GroupID = s.GroupID,
GroupName = s.Group.Name,
LastText = s.ConversationResponses.Last().Text,
LastMsgdate = s.Cdatetime.GetDatePersian(),
LastMsgtime = s.Cdatetime.GetTime(),
LastMsgType = s.ConversationResponses.Last().Type,
NoReadCount = s.ConversationResponses.Count(c => !c.IsRead),
status = s.Status,
UserID = s.UserID,
UserFullName = s.User.FullName
}).ToListAsync();
public async Task<List<Read_ConversationDto>> ConversationAwaitingOurResponse(int CompanyID)
=> await _ConversationRepository.Get()
.Include(inc => inc.Group)
.Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper)
.Where(w => w.ConversationResponses.Any(a => !a.ExperID.HasValue) && w.CompanyID == CompanyID)
.Select(s => new Read_ConversationDto()
{
ExperID = s.ConversationResponses.Last().ExperID,
ExperFullName = s.ConversationResponses.Last().Exper.FullName,
GroupID = s.GroupID,
GroupName = s.Group.Name,
LastText = s.ConversationResponses.Last().Text,
LastMsgdate = s.Cdatetime.GetDatePersian(),
LastMsgtime = s.Cdatetime.GetTime(),
LastMsgType = s.ConversationResponses.Last().Type,
NoReadCount = s.ConversationResponses.Count(c => !c.IsRead),
status = s.Status,
UserID = s.UserID,
UserFullName = s.User.FullName
}).ToListAsync();
public async Task<bool> FinishConversation(int ConversationID)
{
var convModel = await _ConversationRepository.Get()
.Include(inc => inc.ConversationResponses)
.FirstOrDefaultAsync(w => w.ID == ConversationID);
if (convModel != null && convModel.Status != ConversationStatus.Finished)
{
convModel.Status = ConversationStatus.Finished;
return await _ConversationRepository.UPDATEBool(convModel);
}
return true;
}
}
}