379 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
using LMS.Common.Extensions;
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 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 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 IDName
/// <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,
Remark = prompt.Remark,
Description = prompt.Description
};
promptNames.Add(promptName);
}
return APIResponseModel<List<PromptNameDto>>.CreateSuccessResponseModel(promptNames);
}
catch (Exception e)
{
return APIResponseModel<List<PromptNameDto>>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
}
}
#endregion
}