2025-08-18 00:28:32 +03:30
|
|
|
using AutoMapper;
|
|
|
|
using Common.Dtos;
|
2025-08-18 17:30:02 +03:30
|
|
|
using Hushian.Application.Contracts;
|
2025-08-18 00:28:32 +03:30
|
|
|
using Hushian.Application.Contracts.Persistence;
|
|
|
|
using Hushian.Application.Models;
|
|
|
|
using Hushian.Domain.Entites;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Hushian.Application.Services
|
|
|
|
{
|
2025-08-18 17:30:02 +03:30
|
|
|
public class AIService
|
|
|
|
{
|
|
|
|
private readonly IGenericRepository<AIA> _aiaRepository;
|
|
|
|
private readonly IGenericRepository<Prompt> _promprtRepository;
|
|
|
|
private readonly Iaiass _aiass;
|
2025-08-18 00:28:32 +03:30
|
|
|
|
|
|
|
|
2025-08-18 17:30:02 +03:30
|
|
|
public AIService(IGenericRepository<AIA> aiaRepository, Iaiass aiass, IGenericRepository<Prompt> promprtRepository)
|
|
|
|
{
|
|
|
|
_aiaRepository = aiaRepository;
|
|
|
|
_aiass = aiass;
|
|
|
|
_promprtRepository = promprtRepository;
|
|
|
|
}
|
2025-08-18 00:28:32 +03:30
|
|
|
|
2025-08-18 17:30:02 +03:30
|
|
|
public async Task<ResponseBase<aiResponseDto>> NewRequest
|
|
|
|
(aiNewResponseDto dto)
|
|
|
|
{
|
|
|
|
ResponseBase<aiResponseDto> response = new();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string responeai = "";
|
|
|
|
bool sucessresponseai = false;
|
|
|
|
|
|
|
|
var prompts = await _promprtRepository.Get()
|
|
|
|
.Where(w => w.CompanyID == dto.companyId).Select(s => s.Test).ToArrayAsync();
|
|
|
|
var requsetai= await _aiass.SendQuestion(new aiRequestModel(dto.requestText,prompts));
|
|
|
|
if(requsetai.Success)
|
|
|
|
{
|
|
|
|
sucessresponseai = true;
|
|
|
|
responeai = requsetai.Value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response.Errors = requsetai.Errors;
|
|
|
|
}
|
2025-08-18 00:28:32 +03:30
|
|
|
var entity = new AIA
|
2025-08-18 17:30:02 +03:30
|
|
|
{
|
|
|
|
CompanyID = dto.companyId,
|
|
|
|
aiKeyUser = dto.aiKeyUser,
|
|
|
|
Request = dto.requestText,
|
|
|
|
Response = responeai,
|
|
|
|
Cdatetime = DateTime.Now
|
|
|
|
};
|
|
|
|
var added = await _aiaRepository.ADD(entity);
|
2025-08-18 00:28:32 +03:30
|
|
|
|
2025-08-18 17:30:02 +03:30
|
|
|
if (sucessresponseai)
|
|
|
|
response.Value = new() { dateTime = added.Cdatetime, responseText = added.Response, requestText = added.Request };
|
|
|
|
response.Success = sucessresponseai;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
response.Errors.Add("خطا در ذخیره سازی");
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
2025-08-18 00:28:32 +03:30
|
|
|
|
2025-08-18 17:30:02 +03:30
|
|
|
public async Task<List<aiResponseDto>> GetCurrent(int companyId, string aiKeyUser)
|
|
|
|
{
|
|
|
|
return await _aiaRepository
|
|
|
|
.Get()
|
|
|
|
.Where(w => w.CompanyID == companyId && w.aiKeyUser == aiKeyUser && w.Cdatetime.AddHours(3) >= DateTime.Now)
|
|
|
|
.OrderBy(o => o.ID)
|
|
|
|
.Select(s => new aiResponseDto() { responseText = s.Response, requestText = s.Request, dateTime = s.Cdatetime })
|
|
|
|
.ToListAsync();
|
|
|
|
}
|
2025-08-18 00:28:32 +03:30
|
|
|
|
2025-08-18 17:30:02 +03:30
|
|
|
//public async Task<bool> DeleteEntry(int id, int companyId)
|
|
|
|
//{
|
|
|
|
// var entity = await _aiaRepository.Get().FirstOrDefaultAsync(f => f.ID == id && f.CompanyID == companyId);
|
|
|
|
// if (entity == null) return false;
|
|
|
|
// return await _aiaRepository.DELETE(entity);
|
|
|
|
//}
|
|
|
|
}
|
2025-08-18 00:28:32 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|