BOB/Model/Result.cs
2025-12-04 10:51:21 +08:00

181 lines
4.8 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public class Result
{
public Result()
{
}
private int _code;
private readonly string _msg;
/// <summary>
/// 结果是否成功
/// </summary>
public bool IsSuccess => _code == 0;
/// <summary>
/// 结果代码
/// </summary>
public int Code { set => _code = value; get => _code; }
/// <summary>
/// 结果消息
/// </summary>
public string Msg => _msg;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="code"></param>
/// <param name="msg"></param>
/// <param name="exception"></param>
protected Result(int code, string msg, Exception? exception = null)
{
if (exception != null)
{
var listMoreMsg = new List<string>();
if (exception is ResultException resultException)
{
listMoreMsg.AddRange(resultException.MessageList);
}
//else if (exception is DeviceControlException deviceControlException)
//{
// listMoreMsg.Add(deviceControlException.ErrorInfo);
//}
else
{
listMoreMsg.Add(exception.Message);
}
var strMoreMsg = string.Join("、", listMoreMsg.Where(it => !string.IsNullOrWhiteSpace(it)));
if (!string.IsNullOrWhiteSpace(strMoreMsg))
{
msg += $"{strMoreMsg}";
}
}
_code = code;
_msg = msg;
}
/// <summary>
/// 返回成功结果
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static Result Success()
{
return new Result(0, "");
}
/// <summary>
/// 返回错误结果
/// </summary>
/// <param name="msg"></param>
/// <param name="exception"></param>
/// <returns></returns>
public static Result Error(string msg, Exception? exception = null)
{
return new Result(-1, msg, exception);
}
}
public class Result<T> : Result
{
public Result()
{
}
private T? _data;
/// <summary>
/// 结果数据
/// </summary>
public T? Data { set => _data = value; get => _data; }
/// <summary>
/// 构造方法
/// </summary>
/// <param name="code"></param>
/// <param name="msg"></param>
/// <param name="data"></param>
/// <param name="exception"></param>
private Result(int code, string msg, T? data, Exception? exception = null) : base(code, msg, exception)
{
_data = data;
}
/// <summary>
/// 返回成功数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static Result<T> Success(T data)
{
return new Result<T>(0, "", data);
}
/// <summary>
/// 返回失败信息
/// </summary>
/// <param name="msg"></param>
/// <param name="exception"></param>
/// <returns></returns>
public new static Result<T> Error(string msg, Exception? exception = null)
{
return new Result<T>(-1, msg, default, exception);
}
/// <summary>
/// 返回失败信息和数据
/// </summary>
/// <param name="msg"></param>
/// <param name="data"></param>
/// <param name="exception"></param>
/// <returns></returns>
public static Result<T> Error(string msg, T data, Exception? exception = null)
{
return new Result<T>(-1, msg, data, exception);
}
}
public class ResultException : Exception
{
private List<string>? _messageList;
public List<string> MessageList
{
get
{
List<string> messages = new();
if (_messageList != null)
{
messages.AddRange(_messageList.Where(it => !string.IsNullOrWhiteSpace(it)));
}
return messages;
}
}
public override string Message => string.Join(", ", MessageList);
public ResultException() : base(null)
{
}
public void AddAdditionMessage(string message)
{
_messageList ??= new();
_messageList.Add(message);
}
}
}