using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Yi.Framework.AiHub.Application.Contracts.Dtos.Channel; using Yi.Framework.AiHub.Application.Contracts.IServices; using Yi.Framework.AiHub.Domain.Entities.Model; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.AiHub.Application.Services; /// /// 渠道商管理服务实现 /// [Authorize] public class ChannelService : ApplicationService, IChannelService { private readonly ISqlSugarRepository _appRepository; private readonly ISqlSugarRepository _modelRepository; public ChannelService( ISqlSugarRepository appRepository, ISqlSugarRepository modelRepository) { _appRepository = appRepository; _modelRepository = modelRepository; } #region AI应用管理 /// /// 获取AI应用列表 /// [HttpGet("channel/app")] public async Task> GetAppListAsync(AiAppGetListInput input) { RefAsync total = 0; var entities = await _appRepository._DbQueryable .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), x => x.Name.Contains(input.SearchKey)) .OrderByDescending(x => x.OrderNum) .OrderByDescending(x => x.CreationTime) .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); var output = entities.Adapt>(); return new PagedResultDto(total, output); } /// /// 根据ID获取AI应用 /// [HttpGet("channel/app/{id}")] public async Task GetAppByIdAsync([FromRoute]Guid id) { var entity = await _appRepository.GetByIdAsync(id); return entity.Adapt(); } /// /// 创建AI应用 /// public async Task CreateAppAsync(AiAppCreateInput input) { var entity = new AiAppAggregateRoot { Name = input.Name, Endpoint = input.Endpoint, ExtraUrl = input.ExtraUrl, ApiKey = input.ApiKey, OrderNum = input.OrderNum }; await _appRepository.InsertAsync(entity); return entity.Adapt(); } /// /// 更新AI应用 /// public async Task UpdateAppAsync(AiAppUpdateInput input) { var entity = await _appRepository.GetByIdAsync(input.Id); entity.Name = input.Name; entity.Endpoint = input.Endpoint; entity.ExtraUrl = input.ExtraUrl; entity.ApiKey = input.ApiKey; entity.OrderNum = input.OrderNum; await _appRepository.UpdateAsync(entity); return entity.Adapt(); } /// /// 删除AI应用 /// [HttpDelete("channel/app/{id}")] public async Task DeleteAppAsync([FromRoute]Guid id) { // 检查是否有关联的模型 var hasModels = await _modelRepository._DbQueryable .Where(x => x.AiAppId == id && !x.IsDeleted) .AnyAsync(); if (hasModels) { throw new Volo.Abp.UserFriendlyException("该应用下存在模型,无法删除"); } await _appRepository.DeleteAsync(id); } #endregion #region AI模型管理 /// /// 获取AI模型列表 /// [HttpGet("channel/model")] public async Task> GetModelListAsync(AiModelGetListInput input) { RefAsync total = 0; var query = _modelRepository._DbQueryable .Where(x => !x.IsDeleted) .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), x => x.Name.Contains(input.SearchKey) || x.ModelId.Contains(input.SearchKey)) .WhereIF(input.AiAppId.HasValue, x => x.AiAppId == input.AiAppId.Value) .WhereIF(input.IsPremiumOnly == true, x => x.IsPremium); var entities = await query .OrderBy(x => x.OrderNum) .OrderByDescending(x => x.Id) .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); var output = entities.Adapt>(); return new PagedResultDto(total, output); } /// /// 根据ID获取AI模型 /// [HttpGet("channel/model/{id}")] public async Task GetModelByIdAsync([FromRoute]Guid id) { var entity = await _modelRepository.GetByIdAsync(id); return entity.Adapt(); } /// /// 创建AI模型 /// public async Task CreateModelAsync(AiModelCreateInput input) { // 验证应用是否存在 var appExists = await _appRepository._DbQueryable .Where(x => x.Id == input.AiAppId) .AnyAsync(); if (!appExists) { throw new Volo.Abp.UserFriendlyException("指定的AI应用不存在"); } var entity = new AiModelEntity { HandlerName = input.HandlerName, ModelId = input.ModelId, Name = input.Name, Description = input.Description, OrderNum = input.OrderNum, AiAppId = input.AiAppId, ExtraInfo = input.ExtraInfo, ModelType = input.ModelType, ModelApiType = input.ModelApiType, Multiplier = input.Multiplier, MultiplierShow = input.MultiplierShow, ProviderName = input.ProviderName, IconUrl = input.IconUrl, IsPremium = input.IsPremium, IsDeleted = false }; await _modelRepository.InsertAsync(entity); return entity.Adapt(); } /// /// 更新AI模型 /// public async Task UpdateModelAsync(AiModelUpdateInput input) { var entity = await _modelRepository.GetByIdAsync(input.Id); // 验证应用是否存在 if (entity.AiAppId != input.AiAppId) { var appExists = await _appRepository._DbQueryable .Where(x => x.Id == input.AiAppId) .AnyAsync(); if (!appExists) { throw new Volo.Abp.UserFriendlyException("指定的AI应用不存在"); } } entity.HandlerName = input.HandlerName; entity.ModelId = input.ModelId; entity.Name = input.Name; entity.Description = input.Description; entity.OrderNum = input.OrderNum; entity.AiAppId = input.AiAppId; entity.ExtraInfo = input.ExtraInfo; entity.ModelType = input.ModelType; entity.ModelApiType = input.ModelApiType; entity.Multiplier = input.Multiplier; entity.MultiplierShow = input.MultiplierShow; entity.ProviderName = input.ProviderName; entity.IconUrl = input.IconUrl; entity.IsPremium = input.IsPremium; await _modelRepository.UpdateAsync(entity); return entity.Adapt(); } /// /// 删除AI模型(软删除) /// [HttpDelete("channel/model/{id}")] public async Task DeleteModelAsync(Guid id) { await _modelRepository.DeleteByIdAsync(id); } #endregion }