using System;
using System.Collections.Generic;
namespace Haoliang.Models.Common
{
///
/// 非泛型API响应基类
///
public class ApiResponse
{
public bool Success { get; set; }
public object? Data { get; set; }
public string? Message { get; set; }
public int ErrorCode { get; set; }
public DateTime Timestamp { get; set; }
public string? RequestId { get; set; }
public Dictionary? Meta { get; set; }
///
/// 创建成功响应
///
public static ApiResponse Ok(object? data = null, string? message = null)
{
return new ApiResponse
{
Success = true,
Data = data,
Message = message ?? "Success",
ErrorCode = 200,
Timestamp = DateTime.Now
};
}
///
/// 创建错误响应
///
public static ApiResponse Error(string? message, int errorCode = 500)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Error",
ErrorCode = errorCode,
Timestamp = DateTime.Now
};
}
///
/// 创建错误响应(带错误详情列表)
///
public static ApiResponse Error(string? message, IEnumerable? errors, int errorCode = 500)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Error",
ErrorCode = errorCode,
Data = errors?.ToList(),
Timestamp = DateTime.Now
};
}
///
/// 创建未找到响应
///
public static ApiResponse NotFound(string? message = null)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Not Found",
ErrorCode = 404,
Timestamp = DateTime.Now
};
}
///
/// 创建内部服务器错误响应
///
public static ApiResponse InternalServerError(string? message = null)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Internal Server Error",
ErrorCode = 500,
Timestamp = DateTime.Now
};
}
}
///
/// 泛型API响应类
///
public class ApiResponse : ApiResponse
{
public new T? Data { get; set; }
///
/// 创建成功响应
///
public static ApiResponse Ok(T? data = default, string? message = null)
{
return new ApiResponse
{
Success = true,
Data = data,
Message = message ?? "Success",
ErrorCode = 200,
Timestamp = DateTime.Now
};
}
///
/// 创建错误响应
///
public static ApiResponse ErrorResult(string? message, int errorCode = 500)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Error",
ErrorCode = errorCode,
Timestamp = DateTime.Now
};
}
///
/// 创建未找到响应
///
public static ApiResponse NotFoundResult(string? message = null)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Not Found",
ErrorCode = 404,
Timestamp = DateTime.Now
};
}
///
/// 创建内部服务器错误响应
///
public static ApiResponse InternalServerErrorResult(string? message = null)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Internal Server Error",
ErrorCode = 500,
Timestamp = DateTime.Now
};
}
///
/// 创建.bad request响应
///
public static ApiResponse BadRequestResult(string? message = null)
{
return new ApiResponse
{
Success = false,
Message = message ?? "Bad Request",
ErrorCode = 400,
Timestamp = DateTime.Now
};
}
}
///
/// 分页响应类
///
public class PaginatedResponse
{
public List Items { get; set; } = new List();
public int TotalCount { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalPages { get; set; }
public bool HasPreviousPage { get; set; }
public bool HasNextPage { get; set; }
public ApiResponse Response { get; set; } = new ApiResponse();
}
///
/// 自定义验证结果(避免与System.ComponentModel.DataAnnotations.ValidationResult冲突)
///
public class ModelValidationResult
{
public bool IsValid { get; set; }
public List Errors { get; set; } = new List();
}
///
/// 验证错误
///
public class ValidationError
{
public string? Field { get; set; }
public string? Message { get; set; }
public string? Code { get; set; }
}
///
/// 下拉选项
///
public class SelectOption
{
public string? Value { get; set; }
public string? Label { get; set; }
public bool Disabled { get; set; }
public string? Group { get; set; }
}
///
/// 树节点
///
public class TreeNode
{
public string? Id { get; set; }
public string? Text { get; set; }
public string? Type { get; set; }
public List Children { get; set; } = new List();
public Dictionary? Data { get; set; }
public bool Expanded { get; set; }
public bool Selected { get; set; }
public bool HasChildren { get; set; }
}
///
/// 图表数据
///
public class ChartData
{
public string? Type { get; set; }
public string? Title { get; set; }
public List Labels { get; set; } = new List();
public List Datasets { get; set; } = new List();
public Dictionary? Options { get; set; }
}
///
/// 数据集
///
public class Dataset
{
public string? Label { get; set; }
public List Data { get; set; } = new List();
public string? BackgroundColor { get; set; }
public string? BorderColor { get; set; }
public decimal BorderWidth { get; set; }
public string? BorderDash { get; set; }
public bool Fill { get; set; }
public string? Tension { get; set; }
}
///
/// 仪表盘组件
///
public class DashboardWidget
{
public string? Id { get; set; }
public string? Title { get; set; }
public string? Type { get; set; }
public string? Size { get; set; }
public int Row { get; set; }
public int Col { get; set; }
public Dictionary? Config { get; set; }
public bool Refreshable { get; set; }
public int RefreshInterval { get; set; }
public bool Visible { get; set; }
}
///
/// 过滤条件
///
public class FilterCondition
{
public string? Field { get; set; }
public string? Operator { get; set; }
public object? Value { get; set; }
public string? Logic { get; set; }
}
}