142 lines
4.5 KiB
C#
142 lines
4.5 KiB
C#
using DRS9.Dashboard.Application.DTOs;
|
||
using DRS9.Dashboard.Infrastructure.Data;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using AppEntity = DRS9.Dashboard.Domain.Entities.Application;
|
||
|
||
namespace DRS9.Dashboard.Application.Services;
|
||
|
||
public class ApplicationService
|
||
{
|
||
private readonly DashboardDbContext _context;
|
||
|
||
public ApplicationService(DashboardDbContext context)
|
||
{
|
||
_context = context;
|
||
}
|
||
|
||
public async Task<ApplicationListResponse> GetAllAsync()
|
||
{
|
||
var applications = await _context.Applications
|
||
.OrderBy(a => a.Priority)
|
||
.ThenBy(a => a.Id)
|
||
.ToListAsync();
|
||
|
||
return new ApplicationListResponse
|
||
{
|
||
Success = true,
|
||
Data = applications.Select(a => MapToDto(a)).ToList(),
|
||
Total = applications.Count
|
||
};
|
||
}
|
||
|
||
public async Task<ApplicationResponse?> GetByIdAsync(int id)
|
||
{
|
||
var application = await _context.Applications.FindAsync(id);
|
||
if (application == null)
|
||
{
|
||
return new ApplicationResponse { Success = false, Message = "应用不存在" };
|
||
}
|
||
|
||
return new ApplicationResponse
|
||
{
|
||
Success = true,
|
||
Data = MapToDto(application)
|
||
};
|
||
}
|
||
|
||
public async Task<ApplicationResponse> CreateAsync(ApplicationCreateRequest request)
|
||
{
|
||
// 验证:Dashboard 类型必须有 ContentUrl
|
||
if (request.Type == "Dashboard" && string.IsNullOrWhiteSpace(request.ContentUrl))
|
||
{
|
||
return new ApplicationResponse
|
||
{
|
||
Success = false,
|
||
Message = "Dashboard 类型应用必须提供 URL"
|
||
};
|
||
}
|
||
|
||
var application = new AppEntity
|
||
{
|
||
Name = request.Name,
|
||
Type = request.Type,
|
||
Description = request.Description,
|
||
ContentUrl = request.ContentUrl ?? "", // 轮播类型可以为空
|
||
ThumbnailUrl = request.ThumbnailUrl,
|
||
Priority = request.Priority,
|
||
IsEnabled = true
|
||
};
|
||
|
||
_context.Applications.Add(application);
|
||
await _context.SaveChangesAsync();
|
||
|
||
return new ApplicationResponse
|
||
{
|
||
Success = true,
|
||
Data = MapToDto(application),
|
||
Message = "创建成功"
|
||
};
|
||
}
|
||
|
||
public async Task<ApplicationResponse?> UpdateAsync(int id, ApplicationUpdateRequest request)
|
||
{
|
||
var application = await _context.Applications.FindAsync(id);
|
||
if (application == null)
|
||
{
|
||
return new ApplicationResponse { Success = false, Message = "应用不存在" };
|
||
}
|
||
|
||
if (request.Name != null) application.Name = request.Name;
|
||
if (request.Type != null) application.Type = request.Type;
|
||
if (request.Description != null) application.Description = request.Description;
|
||
if (request.ContentUrl != null) application.ContentUrl = request.ContentUrl;
|
||
if (request.ThumbnailUrl != null) application.ThumbnailUrl = request.ThumbnailUrl;
|
||
if (request.Priority.HasValue) application.Priority = request.Priority.Value;
|
||
if (request.IsEnabled.HasValue) application.IsEnabled = request.IsEnabled.Value;
|
||
application.UpdatedAt = DateTime.UtcNow;
|
||
|
||
await _context.SaveChangesAsync();
|
||
|
||
return new ApplicationResponse
|
||
{
|
||
Success = true,
|
||
Data = MapToDto(application),
|
||
Message = "更新成功"
|
||
};
|
||
}
|
||
|
||
public async Task<bool> DeleteAsync(int id)
|
||
{
|
||
var application = await _context.Applications.FindAsync(id);
|
||
if (application == null) return false;
|
||
|
||
// 删除相关的设备分配
|
||
var assignments = await _context.DeviceAssignments
|
||
.Where(da => da.ApplicationId == id)
|
||
.ToListAsync();
|
||
_context.DeviceAssignments.RemoveRange(assignments);
|
||
|
||
_context.Applications.Remove(application);
|
||
await _context.SaveChangesAsync();
|
||
|
||
return true;
|
||
}
|
||
|
||
private static ApplicationDto MapToDto(AppEntity application)
|
||
{
|
||
return new ApplicationDto
|
||
{
|
||
Id = application.Id,
|
||
Name = application.Name,
|
||
Type = application.Type,
|
||
Description = application.Description,
|
||
ContentUrl = application.ContentUrl,
|
||
ThumbnailUrl = application.ThumbnailUrl,
|
||
Priority = application.Priority,
|
||
IsEnabled = application.IsEnabled,
|
||
CreatedAt = application.CreatedAt,
|
||
UpdatedAt = application.UpdatedAt
|
||
};
|
||
}
|
||
}
|