using AutoMapper;
namespace Tiobon.Core.Model;
///
/// 服务层响应实体(泛型)
///
public class ServiceResult
{
///
/// 状态码
///
public int status { get; set; } = 200;
///
/// 操作是否成功
///
public bool success { get; set; } = false;
///
/// 返回信息
///
public string msg { get; set; } = "";
///
/// 开发者信息
///
public string msgDev { get; set; }
///
/// 返回数据集合
///
public T response { get; set; }
///
/// 返回成功
///
/// 消息
///
public static ServiceResult OprateSuccess(string msg)
{
return Message(true, msg, default);
}
///
/// 返回成功
///
/// 消息
/// 数据
///
public static ServiceResult OprateSuccess(string msg, T response)
{
return Message(true, msg, response);
}
///
/// 返回失败
///
/// 消息
///
public static ServiceResult OprateFailed(string msg)
{
return Message(false, msg, default);
}
///
/// 返回失败
///
/// 消息
/// 数据
///
public static ServiceResult OprateFailed(string msg, T response)
{
return Message(false, msg, response);
}
///
/// 返回消息
///
/// 失败/成功
/// 消息
/// 数据
///
public static ServiceResult Message(bool success, string msg, T response)
{
return new ServiceResult() { msg = msg, response = response, success = success };
}
}
///
/// 服务层响应实体
///
public class ServiceResult
{
///
/// 状态码
///
public int status { get; set; } = 200;
///
/// 操作是否成功
///
public bool success { get; set; } = false;
///
/// 返回信息
///
public string msg { get; set; } = "";
///
/// 返回数据集合
///
public object response { get; set; }
///
/// 操作成功
///
///
///
public static ServiceResult OprateSuccess(string msg = "操作成功")
{
return new ServiceResult
{
msg = msg,
success = true,
response = null
};
}
///
/// 操作失败
///
///
///
///
public static ServiceResult OprateFailed(string msg = "操作失败", int status = 500)
{
return new ServiceResult
{
msg = msg,
status = status,
response = null
};
}
}
///
/// 服务层分页响应实体(泛型)
///
public class ServicePageResult
{
///
/// 状态码
///
public int Status { get; set; } = 200;
///
/// 操作是否成功
///
public bool Success { get; set; } = false;
///
/// 返回信息
///
public string Message { get; set; } = null;
///
/// 当前页标
///
public int Page { get; set; } = 1;
///
/// 总页数
///
public int PageCount => (int)Math.Ceiling((decimal)TotalCount / PageSize);
///
/// 数据总数
///
public int TotalCount { get; set; } = 0;
///
/// 每页大小
///
public int PageSize { set; get; } = 20;
///
/// 返回数据
///
public List Data { get; set; }
public ServicePageResult() { }
public ServicePageResult(int page, int totalCount, int pageSize, List data)
{
this.Page = page;
this.TotalCount = totalCount;
PageSize = pageSize;
this.Data = data;
}
public PageModel ConvertTo()
{
return new PageModel(Page, TotalCount, PageSize, default);
}
public PageModel ConvertTo(IMapper mapper)
{
var model = ConvertTo();
if (Data != null)
{
model.data = mapper.Map>(Data);
}
return model;
}
public PageModel ConvertTo(IMapper mapper, Action options)
{
var model = ConvertTo();
if (Data != null)
{
model.data = mapper.Map>(Data, options);
}
return model;
}
}