完善提示词预设的接口,完善转发接口
This commit is contained in:
parent
26d190afa1
commit
3b0c316928
@ -6,6 +6,7 @@ public class AllOptions
|
||||
{
|
||||
{ "all", [] },
|
||||
{ "tts", ["EdgeTTsRoles"] },
|
||||
{ "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]}
|
||||
{ "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]},
|
||||
{ "trial" , ["LaiToolTrialDays"] }
|
||||
};
|
||||
}
|
||||
|
||||
@ -2,14 +2,6 @@
|
||||
{
|
||||
public class PromptEnum
|
||||
{
|
||||
public enum PromptType
|
||||
{
|
||||
/// <summary>
|
||||
/// 开头提示词
|
||||
/// </summary>
|
||||
StartPrompt = 0,
|
||||
}
|
||||
|
||||
public class PromptStatus
|
||||
{
|
||||
/// <summary>
|
||||
@ -21,6 +13,15 @@
|
||||
///提示词状态:禁用
|
||||
/// </summary>
|
||||
public const string Disable = "disable";
|
||||
|
||||
/// <summary>
|
||||
/// 所有有效的提示词类型状态
|
||||
/// </summary>
|
||||
public static readonly HashSet<string> ValidStatuses =
|
||||
[
|
||||
Enable,
|
||||
Disable
|
||||
];
|
||||
}
|
||||
|
||||
public class PromptTypeStatus
|
||||
@ -38,11 +39,11 @@
|
||||
/// <summary>
|
||||
/// 所有有效的提示词类型状态
|
||||
/// </summary>
|
||||
public static readonly HashSet<string> ValidStatuses = new HashSet<string>
|
||||
{
|
||||
public static readonly HashSet<string> ValidStatuses =
|
||||
[
|
||||
Enable,
|
||||
Disable
|
||||
};
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -20,6 +20,10 @@ namespace LMS.DAO
|
||||
|
||||
public DbSet<PermissionType> PermissionType { get; set; }
|
||||
|
||||
public DbSet<PromptType> PromptType { get; set; }
|
||||
|
||||
public DbSet<Prompt> Prompt { get; set; }
|
||||
|
||||
public DbSet<Machine> Machine { get; set; }
|
||||
|
||||
public DbSet<RefreshTokens> RefreshTokens { get; set; }
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using LMS.Repository.Models.DB;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using OneOf.Types;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -24,6 +25,24 @@ namespace LMS.DAO.UserDAO
|
||||
}
|
||||
return await _userManager.FindByIdAsync(userId.ToString()) != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断传入的数据是不是有管理员权限或者是超级管理员权限
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public async Task<bool> CheckUserIsAdminOrSuperAdmin(long? userId)
|
||||
{
|
||||
if (userId == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
|
||||
|
||||
bool isAdminOrSuperAdmin = await _userManager.IsInRoleAsync(user, "Admin") || await _userManager.IsInRoleAsync(user, "Super Admin");
|
||||
return isAdminOrSuperAdmin;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using LMS.Common.Enum;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
@ -18,44 +19,47 @@ public class Options
|
||||
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
||||
|
||||
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
||||
[NotMapped]
|
||||
public object? ValueObject
|
||||
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
||||
public T? GetValueObject<T>()
|
||||
{
|
||||
get
|
||||
if (string.IsNullOrEmpty(Value))
|
||||
{
|
||||
if (string.IsNullOrEmpty(Value))
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
|
||||
if (Type == OptionTypeEnum.JSON)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Value ?? "{}");
|
||||
}
|
||||
|
||||
if (Type == OptionTypeEnum.Number)
|
||||
{
|
||||
return Convert.ToDouble(Value);
|
||||
}
|
||||
|
||||
return Value;
|
||||
return default;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
Value = string.Empty;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Type == OptionTypeEnum.JSON)
|
||||
if (Type == OptionTypeEnum.JSON)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(Value ?? "{}");
|
||||
}
|
||||
|
||||
if (Type == OptionTypeEnum.Number)
|
||||
{
|
||||
if (double.TryParse(Value, out double result))
|
||||
{
|
||||
Value = JsonConvert.SerializeObject(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
Value = value.ToString();
|
||||
return (T)Convert.ChangeType(result, typeof(T));
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
return (T)Convert.ChangeType(Value, typeof(T));
|
||||
}
|
||||
|
||||
// 写一个方法,设置Value的值
|
||||
public void SetValueObject<T>(T value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
Value = string.Empty;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Type == OptionTypeEnum.JSON)
|
||||
{
|
||||
Value = JsonConvert.SerializeObject(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
Value = value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
namespace LMS.Repository.Models.DB
|
||||
namespace LMS.Repository.DB
|
||||
{
|
||||
public class Prompt
|
||||
{
|
||||
@ -45,7 +45,7 @@
|
||||
/// <summary>
|
||||
/// 创建者
|
||||
/// </summary>
|
||||
public string CreateUserId { get; set; }
|
||||
public long CreateUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
@ -55,7 +55,7 @@
|
||||
/// <summary>
|
||||
/// 更新者
|
||||
/// </summary>
|
||||
public string UpdateUserId { get; set; }
|
||||
public long UpdateUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
|
||||
@ -1,35 +1,12 @@
|
||||
namespace LMS.Repository.Models.DB
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
|
||||
namespace LMS.Repository.Models.DB
|
||||
{
|
||||
/// <summary>
|
||||
/// 提示词类型的库
|
||||
/// </summary>
|
||||
public class PromptType
|
||||
public class PromptType : PromptTypeBasic
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型编码
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型描述
|
||||
/// </summary>
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型状态
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否删除
|
||||
/// </summary>
|
||||
@ -38,7 +15,7 @@
|
||||
/// <summary>
|
||||
/// 创建用户ID
|
||||
/// </summary>
|
||||
public string CreateUserId { get; set; }
|
||||
public long CreateUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
@ -48,14 +25,11 @@
|
||||
/// <summary>
|
||||
/// 更新者ID
|
||||
/// </summary>
|
||||
public string UpdateUserId { get; set; }
|
||||
public long UpdateUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using LMS.Tools.Extensions;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace LMS.Repository.Models.DB
|
||||
{
|
||||
|
||||
15
LMS.Repository/DTO/PromptDto/PromptDto.cs
Normal file
15
LMS.Repository/DTO/PromptDto/PromptDto.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using LMS.Repository.DB;
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
using LMS.Repository.DTO.UserDto;
|
||||
|
||||
namespace LMS.Repository.DTO.PromptDto;
|
||||
|
||||
public class PromptDto : Prompt
|
||||
{
|
||||
|
||||
public UserBaseDto CreatedUser { get; set; }
|
||||
|
||||
public UserBaseDto UpdatedUser { get; set; }
|
||||
|
||||
public PromptTypeBasic? PromptType { get; set; }
|
||||
}
|
||||
10
LMS.Repository/DTO/PromptDto/PromptNameDto.cs
Normal file
10
LMS.Repository/DTO/PromptDto/PromptNameDto.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace LMS.Repository.DTO.PromptDto;
|
||||
|
||||
public class PromptNameDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string PromptTypeId { get; set; }
|
||||
}
|
||||
8
LMS.Repository/DTO/PromptTypeDto/PrompTypeNameModel.cs
Normal file
8
LMS.Repository/DTO/PromptTypeDto/PrompTypeNameModel.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace LMS.Repository.DTO.PromptTypeDto;
|
||||
|
||||
public class PrompTypeNameModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Id { get; set; }
|
||||
}
|
||||
29
LMS.Repository/DTO/PromptTypeDto/PromptTypeBasic.cs
Normal file
29
LMS.Repository/DTO/PromptTypeDto/PromptTypeBasic.cs
Normal file
@ -0,0 +1,29 @@
|
||||
namespace LMS.Repository.DTO.PromptTypeDto;
|
||||
|
||||
public class PromptTypeBasic
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型编码
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型描述
|
||||
/// </summary>
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型状态
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
}
|
||||
12
LMS.Repository/DTO/PromptTypeDto/PromptTypeDto.cs
Normal file
12
LMS.Repository/DTO/PromptTypeDto/PromptTypeDto.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using LMS.Repository.DTO.UserDto;
|
||||
using LMS.Repository.Models.DB;
|
||||
|
||||
namespace LMS.Repository.DTO.PromptTypeDto
|
||||
{
|
||||
public class PromptTypeDto : PromptType
|
||||
{
|
||||
public UserBaseDto? UpdatedUser { get; set; }
|
||||
|
||||
public UserBaseDto? CreatedUser { get; set; }
|
||||
}
|
||||
}
|
||||
48
LMS.Repository/Forward/ForwardModel.cs
Normal file
48
LMS.Repository/Forward/ForwardModel.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LMS.Repository.Forward;
|
||||
|
||||
public class ForwardModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 提示词类型ID
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string PromptTypeId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词ID
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string PromptId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// GPT 请求网址
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string GptUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 调用的模型
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Model { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 机器码
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string MachineId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// API Key
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string ApiKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 处理之前的文案,基础文案
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Word { get; set; } = string.Empty;
|
||||
}
|
||||
@ -6,10 +6,6 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Prompt\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="8.0.8" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
|
||||
45
LMS.Repository/PromptModel/ModifyPromptModel.cs
Normal file
45
LMS.Repository/PromptModel/ModifyPromptModel.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LMS.Repository.PromptModel;
|
||||
|
||||
public class ModifyPromptModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string PromptTypeId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词预设字符串
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string PromptString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本
|
||||
/// </summary>
|
||||
public int Version { get; set; }
|
||||
|
||||
[Required]
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
}
|
||||
29
LMS.Repository/PromptModel/ModifyPromptTypeModal.cs
Normal file
29
LMS.Repository/PromptModel/ModifyPromptTypeModal.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LMS.Repository.PromptModel;
|
||||
|
||||
public class ModifyPromptTypeModal
|
||||
{
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示词类型编码
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? Remark { get; set; } = string.Empty;
|
||||
}
|
||||
@ -1,10 +1,13 @@
|
||||
using AutoMapper;
|
||||
using LMS.Repository.DB;
|
||||
using LMS.Repository.DTO;
|
||||
using LMS.Repository.DTO.PromptDto;
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
using LMS.Repository.DTO.UserDto;
|
||||
using LMS.Repository.Models.DB;
|
||||
using LMS.Repository.Models.Machine;
|
||||
using LMS.Repository.Models.Promission;
|
||||
using LMS.Repository.PromptModel;
|
||||
using LMS.Repository.RequestModel.Permission;
|
||||
using static LMS.Repository.DTO.MachineResponse.MachineDto;
|
||||
|
||||
@ -30,6 +33,14 @@ namespace Lai_server.Configuration
|
||||
|
||||
CreateMap<Options, OptionsDto>();
|
||||
|
||||
CreateMap<PromptType, PromptTypeDto>();
|
||||
|
||||
CreateMap<PromptType, PromptTypeBasic>();
|
||||
|
||||
CreateMap<Prompt, PromptDto>();
|
||||
|
||||
CreateMap<ModifyPromptModel, Prompt>();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,6 +102,7 @@ public class DatabaseConfiguration(IServiceProvider serviceProvider) : IHostedSe
|
||||
new Options { Key = "LaitoolUpdateContent", Value = string.Empty, Type = OptionTypeEnum.String },
|
||||
new Options { Key = "LaitoolNotice", Value = string.Empty, Type = OptionTypeEnum.String },
|
||||
new Options { Key = "LaitoolVersion", Value = string.Empty, Type = OptionTypeEnum.String },
|
||||
new Options { Key = "LaiToolTrialDays", Value = "2" , Type = OptionTypeEnum.Number}
|
||||
];
|
||||
|
||||
// 遍历所有的配置项,如果没有则添加
|
||||
|
||||
@ -5,6 +5,7 @@ using LMS.DAO.UserDAO;
|
||||
using LMS.service.Configuration.InitConfiguration;
|
||||
using LMS.service.Service;
|
||||
using LMS.service.Service.PermissionService;
|
||||
using LMS.service.Service.PromptService;
|
||||
using LMS.service.Service.RoleService;
|
||||
using LMS.service.Service.UserService;
|
||||
|
||||
@ -27,6 +28,10 @@ namespace Lai_server.Configuration
|
||||
services.AddScoped<RoleService>();
|
||||
services.AddScoped<UserService>();
|
||||
services.AddScoped<OptionsService>();
|
||||
services.AddScoped<PromptTypeService>();
|
||||
services.AddScoped<PromptService>();
|
||||
services.AddScoped<ForwardWordService>();
|
||||
|
||||
|
||||
|
||||
// 注入 DAO
|
||||
|
||||
73
LMS.service/Controllers/ForwardController.cs
Normal file
73
LMS.service/Controllers/ForwardController.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using LMS.DAO;
|
||||
using LMS.Repository.Forward;
|
||||
using LMS.service.Service;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
|
||||
namespace LMS.service.Controllers;
|
||||
|
||||
[Route("lms/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class ForwardController(ForwardWordService forwardWordService) : ControllerBase
|
||||
{
|
||||
private readonly ForwardWordService _forwardWordService = forwardWordService;
|
||||
|
||||
/// <summary>
|
||||
/// 转发OpenAi格式的请求
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<APIResponseModel<object>>> ForwardWord([FromBody] ForwardModel request)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
return await _forwardWordService.ForwardWord(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流式转发
|
||||
/// </summary>
|
||||
/// <param name="req"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> ForwardWordStream([FromBody] ForwardModel req)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpContext.Response.ContentType = "text/event-stream";
|
||||
HttpContext.Response.Headers.Add("Cache-Control", "no-cache");
|
||||
HttpContext.Response.Headers.Add("Connection", "keep-alive");
|
||||
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new LowercaseContractResolver(),
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
|
||||
await foreach (var s in _forwardWordService.ForwardWordStream(req))
|
||||
{
|
||||
// 确保遵循SSE的消息格式
|
||||
//var data = "data: " + JsonConvert.SerializeObject(s, settings) + "\n\n";
|
||||
await Response.WriteAsync(s);
|
||||
await Response.Body.FlushAsync(); // 确保即时发送
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
return new EmptyResult();
|
||||
}
|
||||
public class LowercaseContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected override string ResolvePropertyName(string propertyName)
|
||||
{
|
||||
return propertyName.ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
216
LMS.service/Controllers/PromptController.cs
Normal file
216
LMS.service/Controllers/PromptController.cs
Normal file
@ -0,0 +1,216 @@
|
||||
using LMS.DAO;
|
||||
using LMS.Repository.DTO;
|
||||
using LMS.Repository.DTO.PromptDto;
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
using LMS.Repository.PromptModel;
|
||||
using LMS.service.Service.PromptService;
|
||||
using LMS.Tools.Extensions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using static LMS.Common.Enums.PromptEnum;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
|
||||
namespace LMS.service.Controllers
|
||||
{
|
||||
[Route("lms/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class PromptController : ControllerBase
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly PromptTypeService _promptTypeService;
|
||||
private readonly PromptService _promptService;
|
||||
|
||||
|
||||
public PromptController(ApplicationDbContext context, PromptTypeService promptTypeService, PromptService promptService)
|
||||
{
|
||||
_context = context;
|
||||
_promptTypeService = promptTypeService;
|
||||
_promptService = promptService;
|
||||
}
|
||||
|
||||
#region 提示词类型相关
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前的所有的提示词类型(分页)
|
||||
/// </summary>
|
||||
/// <param name="pageSize"> 每页的数量 </param>
|
||||
/// <param name="current"> 当前页 </param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<CollectionResponse<PromptTypeDto>>>> QueryPromptypeCollection([Required] int page, [Required] int pageSize, string? code, string? name, string? status, string? remark)
|
||||
{
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptTypeService.QueryPromptypeCollection(page, pageSize, code, name, status, remark, reuqertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的提示词类型的选项,ID和Name
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<APIResponseModel<List<PrompTypeNameModel>>>> GetPromptTypeOptions()
|
||||
{
|
||||
return await _promptTypeService.GetPromptTypeOptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取详细的提示词类型信息
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<PromptTypeDto>>> GetPromptTypeInfo(string id)
|
||||
{
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptTypeService.GetPromptTypeInfo(id, reuqertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改提示词数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="modal"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> ModityPromptType(string id, [FromBody] ModifyPromptTypeModal modal)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptTypeService.ModityPromptType(id, modal, reuqertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加提示词类型数据
|
||||
/// </summary>
|
||||
/// <param name="modal"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> AddPromptType([FromBody] ModifyPromptTypeModal modal)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptTypeService.AddPromptType(modal, reuqertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除提示词类型数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{id}/{deletePrompt}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> DeletePromptType(string id, bool deletePrompt)
|
||||
{
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptTypeService.DeletePromptType(id, reuqertUserId, deletePrompt);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region 提示词相关
|
||||
|
||||
/// <summary>
|
||||
/// 获取提示词数据的集合
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="promptTypeId"></param>
|
||||
/// <param name="status"></param>
|
||||
/// <param name="remark"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<CollectionResponse<PromptDto>>>> QueryPromptStringCollection([Required] int page, [Required] int pageSize, string? name, string? promptTypeId, string? status, string? remark)
|
||||
{
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptService.QueryPromptStringCollection(page, pageSize, name, promptTypeId, status, remark, reuqertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定提示词的详细信息
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<PromptDto>>> GetPromptInfo(string id)
|
||||
{
|
||||
long reuqertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptService.GetPromptInfo(id, reuqertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改提示词预设数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> ModifyPrompt(string id, [FromBody] ModifyPromptModel model)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long requertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptService.ModifyPrompt(id, model, requertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加提示词数据
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> AddPrompt([FromBody] ModifyPromptModel model)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long requertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptService.AddPrompt(model, requertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除提示词数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> DeletePrompt(string id)
|
||||
{
|
||||
long requertUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _promptService.DeletePrompt(id, requertUserId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的或者是指定的提示词预设数据 ID、Name
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{option}")]
|
||||
public async Task<ActionResult<APIResponseModel<List<PromptNameDto>>>> GetPromptOptions(string option)
|
||||
{
|
||||
return await _promptService.GetPromptOptions(option);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="Betalgo.Ranul.OpenAI" Version="8.9.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
||||
|
||||
147
LMS.service/Service/ForwardWordService.cs
Normal file
147
LMS.service/Service/ForwardWordService.cs
Normal file
@ -0,0 +1,147 @@
|
||||
using LMS.DAO;
|
||||
using LMS.Repository.DB;
|
||||
using LMS.Repository.Forward;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using LMS.Repository.Model;
|
||||
using LMS.Tools;
|
||||
using Betalgo.Ranul.OpenAI.Managers;
|
||||
using Betalgo.Ranul.OpenAI;
|
||||
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
|
||||
|
||||
namespace LMS.service.Service;
|
||||
|
||||
public class ForwardWordService(ApplicationDbContext context)
|
||||
{
|
||||
private readonly ApplicationDbContext _context = context;
|
||||
/// <summary>
|
||||
/// 转发OpenAi格式的请求 非流
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<object>>> ForwardWord(ForwardModel request)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 要校验机器码,但是目前不需要
|
||||
|
||||
if (request.Word == null || request.Word == "")
|
||||
{
|
||||
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
|
||||
// 获取提示词预设
|
||||
Prompt? prompt = await _context.Prompt.FirstOrDefaultAsync(x => x.PromptTypeId == request.PromptTypeId && x.Id == request.PromptId);
|
||||
if (prompt == null)
|
||||
{
|
||||
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.FindPromptStringFail);
|
||||
}
|
||||
|
||||
// 开始拼接请求体
|
||||
using HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.ApiKey);
|
||||
string json = JsonConvert.SerializeObject(new
|
||||
{
|
||||
model = request.Model,
|
||||
temperature = 0.3,
|
||||
messages = new List<OpenAI.RequestMessage>
|
||||
{
|
||||
new OpenAI.RequestMessage
|
||||
{
|
||||
role = "system",
|
||||
content = prompt.PromptString
|
||||
|
||||
},
|
||||
new OpenAI.RequestMessage
|
||||
{
|
||||
role = "user",
|
||||
content = request.Word
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 判断请求的url是不是满足条件
|
||||
if (string.IsNullOrEmpty(request.GptUrl))
|
||||
{
|
||||
throw new Exception("请求的url为空");
|
||||
}
|
||||
if (!request.GptUrl.StartsWith("https://ark.cn-beijing.volces.com") && !request.GptUrl.StartsWith("https://api.moonshot.cn") && !request.GptUrl.StartsWith("https://laitool.net") && !request.GptUrl.StartsWith("https://api.laitool.cc") && !request.GptUrl.StartsWith("https://laitool.cc"))
|
||||
{
|
||||
throw new Exception("请求的url不合法");
|
||||
}
|
||||
client.Timeout = Timeout.InfiniteTimeSpan;
|
||||
var response = await client.PostAsync(request.GptUrl, new StringContent(json, Encoding.UTF8, "application/json"));
|
||||
|
||||
// 判断返回的状态码
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
// 读取响应体
|
||||
string responseContent = await response.Content.ReadAsStringAsync();
|
||||
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.ForwardWordFail, responseContent, "请求失败");
|
||||
}
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
// 序列化一下
|
||||
return APIResponseModel<object>.CreateSuccessResponseModel(content);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<string> ForwardWordStream(ForwardModel request)
|
||||
{
|
||||
|
||||
// 要校验机器码,但是目前不需要
|
||||
|
||||
if (request.Word == null || request.Word == "")
|
||||
{
|
||||
throw new Exception("参数错误");
|
||||
}
|
||||
|
||||
// 获取提示词预设
|
||||
Prompt? prompt = await _context.Prompt.FirstOrDefaultAsync(x => x.PromptTypeId == request.PromptTypeId && x.Id == request.PromptId);
|
||||
if (prompt == null)
|
||||
{
|
||||
throw new Exception(ResponseCode.FindPromptStringFail.GetResult());
|
||||
}
|
||||
var openAiService = new OpenAIService(new OpenAIOptions()
|
||||
{
|
||||
ApiKey = request.ApiKey,
|
||||
BaseDomain = request.GptUrl,
|
||||
});
|
||||
var completionResult = openAiService.ChatCompletion.CreateCompletionAsStream(new ChatCompletionCreateRequest
|
||||
{
|
||||
Messages = new List<ChatMessage>
|
||||
{
|
||||
ChatMessage.FromSystem(prompt.PromptString),
|
||||
ChatMessage.FromUser(request.Word)
|
||||
},
|
||||
Model = request.Model,
|
||||
Stream = true
|
||||
});
|
||||
|
||||
await foreach (var completion in completionResult)
|
||||
{
|
||||
if (completion.Successful)
|
||||
{ // 这边只返回数据,不返回全部的数据结构了
|
||||
yield return completion.Choices.First().Message.Content ?? "";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (completion.Error == null)
|
||||
{
|
||||
throw new Exception("Unknown Error");
|
||||
}
|
||||
|
||||
throw new Exception($"{completion.Error.Code}: {completion.Error.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -109,7 +109,7 @@ namespace LMS.service.Service
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
// 判断OwnerUserId是不是还能添加
|
||||
User? ownerUser = await _userManager.FindByIdAsync(request.UserId.ToString());
|
||||
User? ownerUser = await _userManager.FindByIdAsync((request.UserId ?? reqId).ToString());
|
||||
if (ownerUser == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindUserByIdFail);
|
||||
@ -142,7 +142,7 @@ namespace LMS.service.Service
|
||||
|
||||
if (request.UseStatus == MachineUseStatus.Trial)
|
||||
{
|
||||
var checkRes = await CanAddPermanentMachine(ownerUser, (long)request.UserId, null, false);
|
||||
var checkRes = await CanAddPermanentMachine(ownerUser, (long)(request.UserId ?? reqId), null, false);
|
||||
if (checkRes.Code != 1)
|
||||
{
|
||||
return checkRes;
|
||||
@ -160,11 +160,19 @@ namespace LMS.service.Service
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.InvalidOptions, "到期时间不能小于当前时间");
|
||||
}
|
||||
Console.WriteLine(BeijingTimeExtension.GetBeijingTime().ToString());
|
||||
int s = (request.DeactivationTime - BeijingTimeExtension.GetBeijingTime()).Value.Days;
|
||||
// 判断当前时间和现在的时间差大于三天,报错
|
||||
if ((request.DeactivationTime - BeijingTimeExtension.GetBeijingTime()).Value.Days >= 3)
|
||||
|
||||
int s = ((request.DeactivationTime ?? BeijingTimeExtension.GetBeijingTime().AddDays(1)) - BeijingTimeExtension.GetBeijingTime()).Days;
|
||||
|
||||
var LaiToolTrialDays = await _context.Options.FirstOrDefaultAsync(x => x.Key == "LaiToolTrialDays");
|
||||
if (LaiToolTrialDays == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.InvalidOptions, "到期时间不能超过三天");
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, "系统错误,未设置最大试用天数");
|
||||
}
|
||||
double maxTrialDays = LaiToolTrialDays.GetValueObject<double>();
|
||||
// 判断当前时间和现在的时间差大于三天,报错
|
||||
if (s >= maxTrialDays)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.InvalidOptions, $"到期时间不能超过 {maxTrialDays}天");
|
||||
}
|
||||
// 先修改用户的免费更换次数
|
||||
ownerUser.FreeCount -= 1;
|
||||
@ -172,7 +180,7 @@ namespace LMS.service.Service
|
||||
}
|
||||
else
|
||||
{
|
||||
var checkRes = await CanAddPermanentMachine(ownerUser, (long)request.UserId, null, true);
|
||||
var checkRes = await CanAddPermanentMachine(ownerUser, (long)(request.UserId ?? reqId), null, true);
|
||||
if (checkRes.Code != 1)
|
||||
{
|
||||
return checkRes;
|
||||
@ -283,12 +291,6 @@ namespace LMS.service.Service
|
||||
using var transaction = _context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
// 修改
|
||||
// 判断当前用户是否有修改和管理机器码的权限
|
||||
//if (!await _context.Permission.AnyAsync(x => x.UserId == reqId && (x.PermissionCode == SubPermissionType.ManageMachine || x.PermissionCode == SubPermissionType.ModifyMachine)))
|
||||
//{
|
||||
// return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
//}
|
||||
// 判断传入的userId是否存在
|
||||
User? user = await _userManager.FindByIdAsync(reqId.ToString());
|
||||
if (user == null)
|
||||
@ -398,7 +400,10 @@ namespace LMS.service.Service
|
||||
machine.DeactivationTime = null;
|
||||
}
|
||||
machine.UseStatus = request.UseStatus;
|
||||
machine.MachineId = request.MachineId;
|
||||
if (isAdminOrSuperAdmin)
|
||||
{
|
||||
machine.MachineId = request.MachineId;
|
||||
}
|
||||
machine.Status = request.Status;
|
||||
machine.Remark = request.Remark;
|
||||
_context.Machine.Update(machine);
|
||||
|
||||
377
LMS.service/Service/PromptService/PromptService.cs
Normal file
377
LMS.service/Service/PromptService/PromptService.cs
Normal file
@ -0,0 +1,377 @@
|
||||
using AutoMapper;
|
||||
using LMS.DAO;
|
||||
using LMS.DAO.UserDAO;
|
||||
using LMS.Repository.DB;
|
||||
using LMS.Repository.DTO;
|
||||
using LMS.Repository.DTO.PromptDto;
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
using LMS.Repository.DTO.UserDto;
|
||||
using LMS.Repository.Models.DB;
|
||||
using LMS.Repository.PromptModel;
|
||||
using LMS.Tools.Extensions;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using static LMS.Common.Enums.PromptEnum;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
|
||||
namespace LMS.service.Service.PromptService;
|
||||
|
||||
public class PromptService(UserBasicDao userBasicDao, ApplicationDbContext context, IMapper mapper, UserManager<User> userManager)
|
||||
{
|
||||
private readonly UserBasicDao _userBasicDao = userBasicDao;
|
||||
private readonly ApplicationDbContext _context = context;
|
||||
private readonly IMapper _mapper = mapper;
|
||||
private readonly UserManager<User> _userManager = userManager;
|
||||
|
||||
#region 查询数据集合
|
||||
|
||||
/// <summary>
|
||||
/// 获取提示词数据的集合
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="promptTypeId"></param>
|
||||
/// <param name="status"></param>
|
||||
/// <param name="remark"></param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<CollectionResponse<PromptDto>>>> QueryPromptStringCollection(int page, int pageSize, string? name, string? promptTypeId, string? status, string? remark, long requertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminAndSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(requertUserId);
|
||||
if (!isAdminAndSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<PromptDto>>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
IQueryable<Prompt>? query = _context.Prompt;
|
||||
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
query = query.Where(x => x.Name.Contains(name));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(promptTypeId))
|
||||
{
|
||||
query = query.Where(x => x.PromptTypeId == promptTypeId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(status))
|
||||
{
|
||||
if (!PromptStatus.ValidStatuses.Contains(status))
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<PromptDto>>.CreateErrorResponseModel(ResponseCode.ParameterError, "Status is invalid");
|
||||
}
|
||||
query = query.Where(x => x.Status == status);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(remark))
|
||||
{
|
||||
query = query.Where(x => x.Remark.Contains(remark));
|
||||
}
|
||||
|
||||
// 开始返回数据
|
||||
// 通过ID降序
|
||||
query = query.OrderByDescending(x => x.CreateTime);
|
||||
|
||||
// 查询总数
|
||||
int total = await query.CountAsync();
|
||||
|
||||
// 分页
|
||||
query = query.Skip((page - 1) * pageSize).Take(pageSize);
|
||||
|
||||
|
||||
List<Prompt>? prompts = await query.ToListAsync();
|
||||
|
||||
List<PromptDto> promptDtos = [];
|
||||
|
||||
for (int i = 0; prompts != null && i < prompts.Count; i++)
|
||||
{
|
||||
Prompt prompt = prompts[i];
|
||||
User? createdUser = await _userManager.FindByIdAsync(prompt.CreateUserId.ToString());
|
||||
User? updatedUser = await _userManager.FindByIdAsync(prompt.UpdateUserId.ToString());
|
||||
PromptDto promptDto = _mapper.Map<Prompt, PromptDto>(prompt);
|
||||
promptDto.CreatedUser = _mapper.Map<UserBaseDto>(createdUser);
|
||||
promptDto.UpdatedUser = _mapper.Map<UserBaseDto>(updatedUser);
|
||||
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == prompt.PromptTypeId);
|
||||
promptDto.PromptType = _mapper.Map<PromptTypeBasic>(promptType);
|
||||
promptDtos.Add(promptDto);
|
||||
}
|
||||
return APIResponseModel<CollectionResponse<PromptDto>>.CreateSuccessResponseModel(new CollectionResponse<PromptDto>
|
||||
{
|
||||
Total = total,
|
||||
Collection = promptDtos,
|
||||
Current = page
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<PromptDto>>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取指定提示词的详细信息
|
||||
/// <summary>
|
||||
/// 获取指定提示词的详细信息
|
||||
/// </summary>
|
||||
/// <param name="id">提示词数据的ID</param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<PromptDto>>> GetPromptInfo(string id, long reuqertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminAndSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(reuqertUserId);
|
||||
if (!isAdminAndSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<PromptDto>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
Prompt? prompt = await _context.Prompt.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (prompt == null)
|
||||
{
|
||||
return APIResponseModel<PromptDto>.CreateErrorResponseModel(ResponseCode.FindPromptStringFail);
|
||||
}
|
||||
PromptDto promptDto = _mapper.Map<PromptDto>(prompt);
|
||||
User? createdUser = await _userManager.FindByIdAsync(prompt.CreateUserId.ToString());
|
||||
User? updatedUser = await _userManager.FindByIdAsync(prompt.UpdateUserId.ToString());
|
||||
promptDto.CreatedUser = _mapper.Map<UserBaseDto>(createdUser);
|
||||
promptDto.UpdatedUser = _mapper.Map<UserBaseDto>(updatedUser);
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == prompt.PromptTypeId);
|
||||
promptDto.PromptType = _mapper.Map<PromptTypeBasic>(promptType);
|
||||
return APIResponseModel<PromptDto>.CreateSuccessResponseModel(promptDto);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<PromptDto>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 修改提示词预设数据
|
||||
/// <summary>
|
||||
/// 修改提示词预设数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="requertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> ModifyPrompt(string id, ModifyPromptModel model, long requertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminAndSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(requertUserId);
|
||||
if (!isAdminAndSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
|
||||
Prompt? prompt = await _context.Prompt.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (prompt == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindPromptStringFail);
|
||||
}
|
||||
|
||||
if (!PromptStatus.ValidStatuses.Contains(model.Status))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "Status is invalid");
|
||||
}
|
||||
|
||||
// 判断名字是不是存在
|
||||
if (await _context.Prompt.AnyAsync(x => x.Name == model.Name && x.Id != id))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "名字已存在,新增失败");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(model.PromptTypeId))
|
||||
{
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == model.PromptTypeId);
|
||||
if (promptType == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "指定的提示词ID不存在,请检查");
|
||||
}
|
||||
prompt.PromptTypeCode = promptType.Code;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(model.PromptString))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "提示词预设不能为空,请检查");
|
||||
}
|
||||
|
||||
prompt.Name = model.Name;
|
||||
prompt.PromptTypeId = model.PromptTypeId;
|
||||
prompt.PromptString = model.PromptString;
|
||||
prompt.Description = model.Description;
|
||||
prompt.Remark = model.Remark;
|
||||
prompt.Status = model.Status;
|
||||
prompt.Version = model.Version;
|
||||
prompt.UpdateTime = BeijingTimeExtension.GetBeijingTime();
|
||||
prompt.UpdateUserId = requertUserId;
|
||||
_context.Update(prompt);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("修改成功");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 添加提示词数据
|
||||
|
||||
/// <summary>
|
||||
/// 添加提示词数据
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="requertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> AddPrompt(ModifyPromptModel model, long requertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Prompt prompt = _mapper.Map<Prompt>(model);
|
||||
bool isAdminAndSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(requertUserId);
|
||||
if (!isAdminAndSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
|
||||
// 判断是不是有相同的名字的数存在
|
||||
if (await _context.Prompt.AnyAsync(x => x.Name == model.Name))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.PromptStringExist);
|
||||
}
|
||||
// 判断提示词类型是不是存在
|
||||
if (string.IsNullOrWhiteSpace(model.PromptTypeId))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "提示词类型不能为空,请检查");
|
||||
}
|
||||
else
|
||||
{
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == model.PromptTypeId);
|
||||
if (promptType == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "指定的提示词类型ID不存在,请检查");
|
||||
}
|
||||
prompt.PromptTypeCode = promptType.Code;
|
||||
}
|
||||
|
||||
// 判断状态
|
||||
if (!PromptStatus.ValidStatuses.Contains(model.Status))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "Status is invalid");
|
||||
}
|
||||
prompt.Id = Guid.NewGuid().ToString();
|
||||
prompt.CreateTime = BeijingTimeExtension.GetBeijingTime();
|
||||
prompt.UpdateTime = BeijingTimeExtension.GetBeijingTime();
|
||||
prompt.CreateUserId = requertUserId;
|
||||
prompt.UpdateUserId = requertUserId;
|
||||
prompt.Version = 1;
|
||||
await _context.Prompt.AddAsync(prompt);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel(prompt.Id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 删除提示词数据
|
||||
|
||||
/// <summary>
|
||||
/// 删除提示词数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="requertUserId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> DeletePrompt(string id, long requertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminAndSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(requertUserId);
|
||||
if (!isAdminAndSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
Prompt? prompt = await _context.Prompt.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (prompt == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindPromptStringFail);
|
||||
}
|
||||
|
||||
_context.Prompt.Remove(prompt);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("删除成功");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取指定的或者是所有的提示词预设数据 ID、Name
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定的或者是所有的提示词预设数据 ID、Name
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<List<PromptNameDto>>>> GetPromptOptions(string option)
|
||||
{
|
||||
try
|
||||
{
|
||||
IQueryable<Prompt> query = _context.Prompt;
|
||||
List<Prompt> promptList = [];
|
||||
if (option.Equals("all", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
// 全部
|
||||
promptList = await query.ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == option);
|
||||
if (promptType == null)
|
||||
{
|
||||
return APIResponseModel<List<PromptNameDto>>.CreateErrorResponseModel(ResponseCode.ParameterError, "指定的提示词类型不存在,请检查");
|
||||
}
|
||||
query = query.Where(x => x.PromptTypeId == option);
|
||||
promptList = await query.ToListAsync();
|
||||
}
|
||||
List<PromptNameDto> promptNames = [];
|
||||
for (int i = 0; promptList != null && i < promptList.Count; i++)
|
||||
{
|
||||
Prompt prompt = promptList[i];
|
||||
PromptNameDto promptName = new()
|
||||
{
|
||||
Id = prompt.Id,
|
||||
Name = prompt.Name,
|
||||
PromptTypeId = prompt.PromptTypeId
|
||||
};
|
||||
promptNames.Add(promptName);
|
||||
}
|
||||
return APIResponseModel<List<PromptNameDto>>.CreateSuccessResponseModel(promptNames);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<List<PromptNameDto>>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
358
LMS.service/Service/PromptService/PromptTypeService.cs
Normal file
358
LMS.service/Service/PromptService/PromptTypeService.cs
Normal file
@ -0,0 +1,358 @@
|
||||
using AutoMapper;
|
||||
using LMS.DAO;
|
||||
using LMS.DAO.UserDAO;
|
||||
using LMS.Repository.DTO;
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
using LMS.Repository.DTO.UserDto;
|
||||
using LMS.Repository.Models.DB;
|
||||
using LMS.Repository.PromptModel;
|
||||
using LMS.Tools.Extensions;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using static LMS.Common.Enums.PromptEnum;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
|
||||
namespace LMS.service.Service.PromptService;
|
||||
|
||||
public class PromptTypeService
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly UserBasicDao _userBaseDao;
|
||||
private readonly UserManager<User> _userManager;
|
||||
public PromptTypeService(ApplicationDbContext context, IMapper mapper, UserBasicDao userBaseDao, UserManager<User> userManager)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_userBaseDao = userBaseDao;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
#region 获取当前的所有的提示词类型数据(分页)
|
||||
/// <summary>
|
||||
/// 获取当前的所有的提示词类型数据(分页)
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="status"></param>
|
||||
/// <param name="remark"></param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<CollectionResponse<PromptTypeDto>>>> QueryPromptypeCollection(int page, int pageSize, string? code, string? name, string? status, string? remark, long reuqertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBaseDao.CheckUserIsAdminOrSuperAdmin(reuqertUserId);
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<PromptTypeDto>>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
|
||||
// 开始查询
|
||||
IQueryable<PromptType>? query = _context.PromptType;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(code))
|
||||
{
|
||||
query = query.Where(x => x.Code.Contains(code));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
query = query.Where(x => x.Name.Contains(name));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(status))
|
||||
{
|
||||
if (!PromptTypeStatus.ValidStatuses.Contains(status))
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<PromptTypeDto>>.CreateErrorResponseModel(ResponseCode.ParameterError, "Status is invalid");
|
||||
}
|
||||
query = query.Where(x => x.Status == status);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(remark))
|
||||
{
|
||||
query = query.Where(x => !string.IsNullOrWhiteSpace(x.Remark) && x.Remark.Contains(remark));
|
||||
}
|
||||
|
||||
// 通过ID降序
|
||||
query = query.OrderByDescending(x => x.CreateTime);
|
||||
|
||||
// 查询总数
|
||||
int total = await query.CountAsync();
|
||||
|
||||
// 分页
|
||||
query = query.Skip((page - 1) * pageSize).Take(pageSize);
|
||||
|
||||
List<PromptType>? prompttypes = await query.ToListAsync();
|
||||
|
||||
List<PromptTypeDto> promptTypeDtos = [];
|
||||
|
||||
for (int i = 0; prompttypes != null && i < prompttypes.Count; i++)
|
||||
{
|
||||
PromptType prompttype = prompttypes[i];
|
||||
User? createdUser = await _userManager.FindByIdAsync(prompttype.CreateUserId.ToString());
|
||||
User? updatedUser = await _userManager.FindByIdAsync(prompttype.UpdateUserId.ToString());
|
||||
PromptTypeDto promptTypeDto = _mapper.Map<PromptType, PromptTypeDto>(prompttype);
|
||||
promptTypeDto.CreatedUser = _mapper.Map<UserBaseDto>(createdUser);
|
||||
promptTypeDto.UpdatedUser = _mapper.Map<UserBaseDto>(updatedUser);
|
||||
promptTypeDtos.Add(promptTypeDto);
|
||||
}
|
||||
|
||||
return APIResponseModel<CollectionResponse<PromptTypeDto>>.CreateSuccessResponseModel(new CollectionResponse<PromptTypeDto>
|
||||
{
|
||||
Total = total,
|
||||
Collection = promptTypeDtos,
|
||||
Current = page,
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<PromptTypeDto>>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 通过ID获取详细的提示词类型信息
|
||||
/// <summary>
|
||||
/// 通过ID获取详细的提示词类型信息
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<PromptTypeDto>>> GetPromptTypeInfo(string id, long reuqertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBaseDao.CheckUserIsAdminOrSuperAdmin(reuqertUserId);
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<PromptTypeDto>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (promptType == null)
|
||||
{
|
||||
return APIResponseModel<PromptTypeDto>.CreateErrorResponseModel(ResponseCode.FindPromptTypeFail);
|
||||
}
|
||||
PromptTypeDto promptTypeDto = _mapper.Map<PromptTypeDto>(promptType);
|
||||
User? createdUser = await _userManager.FindByIdAsync(promptType.CreateUserId.ToString());
|
||||
User? updatedUser = await _userManager.FindByIdAsync(promptType.UpdateUserId.ToString());
|
||||
promptTypeDto.CreatedUser = _mapper.Map<UserBaseDto>(createdUser);
|
||||
promptTypeDto.UpdatedUser = _mapper.Map<UserBaseDto>(updatedUser);
|
||||
return APIResponseModel<PromptTypeDto>.CreateSuccessResponseModel(promptTypeDto);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<PromptTypeDto>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 修改提示词类型数据
|
||||
/// <summary>
|
||||
/// 修改提示词类型数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="modal"></param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> ModityPromptType(string id, ModifyPromptTypeModal modal, long reuqertUserId)
|
||||
{
|
||||
using var transaction = _context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBaseDao.CheckUserIsAdminOrSuperAdmin(reuqertUserId);
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (promptType == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindPromptTypeFail);
|
||||
}
|
||||
|
||||
if (!PromptTypeStatus.ValidStatuses.Contains(modal.Status))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "Status is invalid");
|
||||
}
|
||||
|
||||
// 判断name是不是存在相同的
|
||||
if (await _context.PromptType.AnyAsync(x => x.Name == modal.Name && x.Id != id))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "名称已存在");
|
||||
}
|
||||
|
||||
if (await _context.PromptType.AnyAsync(x => x.Code == modal.Code && x.Id != id))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "Code已存在");
|
||||
}
|
||||
|
||||
promptType.Code = modal.Code;
|
||||
promptType.Name = modal.Name;
|
||||
promptType.Status = modal.Status;
|
||||
promptType.Remark = modal.Remark;
|
||||
promptType.UpdateTime = BeijingTimeExtension.GetBeijingTime();
|
||||
promptType.UpdateUserId = reuqertUserId;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("修改提示词类型数据成功");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 添加提示词类型数据
|
||||
/// <summary>
|
||||
/// 添加提示词类型数据
|
||||
/// </summary>
|
||||
/// <param name="modal"></param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> AddPromptType(ModifyPromptTypeModal modal, long reuqertUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBaseDao.CheckUserIsAdminOrSuperAdmin(reuqertUserId);
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
|
||||
if (!PromptTypeStatus.ValidStatuses.Contains(modal.Status))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "Status is invalid");
|
||||
}
|
||||
|
||||
// 判断name是不是存在相同的
|
||||
if (await _context.PromptType.AnyAsync(x => x.Name == modal.Name))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "名称已存在");
|
||||
}
|
||||
|
||||
if (await _context.PromptType.AnyAsync(x => x.Code == modal.Code))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "Code已存在");
|
||||
}
|
||||
|
||||
// 开始添加
|
||||
// 开始添加数据
|
||||
PromptType promptType = new()
|
||||
{
|
||||
Id = System.Guid.NewGuid().ToString(),
|
||||
Code = modal.Code,
|
||||
Name = modal.Name,
|
||||
CreateTime = System.DateTime.Now,
|
||||
UpdateTime = System.DateTime.Now,
|
||||
CreateUserId = reuqertUserId,
|
||||
UpdateUserId = reuqertUserId,
|
||||
Remark = modal.Remark,
|
||||
Status = modal.Status,
|
||||
IsDeleted = false
|
||||
};
|
||||
await _context.PromptType.AddAsync(promptType);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("添加提示词类型数据成功");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 删除提示词类型数据
|
||||
/// <summary>
|
||||
/// 删除提示词类型数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="reuqertUserId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> DeletePromptType(string id, long reuqertUserId, bool deletePrompt)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBaseDao.CheckUserIsAdminOrSuperAdmin(reuqertUserId);
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
|
||||
PromptType? promptType = await _context.PromptType.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (promptType == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindPromptTypeFail);
|
||||
}
|
||||
|
||||
// 判断是不是删除所有的提示词类型相关的提示词数据
|
||||
bool hasPrompt = await _context.Prompt.AnyAsync(x => x.PromptTypeId == promptType.Id);
|
||||
if (hasPrompt && !deletePrompt)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.PromptStringExist, "当前删除的提示词类型数据有关联的提示词数据");
|
||||
}
|
||||
|
||||
if (hasPrompt && deletePrompt)
|
||||
{
|
||||
// 直接删除全部数据
|
||||
_context.Prompt.RemoveRange(_context.Prompt.Where(x => x.PromptTypeId == promptType.Id));
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
_context.PromptType.Remove(promptType);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("删除提示词类型数据成功以及所有的关联提示词数据成功");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取所有的提示词类型的选项,ID和Name
|
||||
/// <summary>
|
||||
/// 获取所有的提示词类型的选项,ID和Name
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ActionResult<APIResponseModel<List<PrompTypeNameModel>>>> GetPromptTypeOptions()
|
||||
{
|
||||
try
|
||||
{
|
||||
List<PromptType> promptTypes = await _context.PromptType.ToListAsync();
|
||||
List<PrompTypeNameModel> promptNameModels = [];
|
||||
foreach (var item in promptTypes)
|
||||
{
|
||||
promptNameModels.Add(new PrompTypeNameModel
|
||||
{
|
||||
Id = item.Id,
|
||||
Name = item.Name
|
||||
});
|
||||
};
|
||||
return APIResponseModel<List<PrompTypeNameModel>>.CreateSuccessResponseModel(promptNameModels);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return APIResponseModel<List<PrompTypeNameModel>>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user