using Newtonsoft.Json;
namespace IRaCIS.Core.Infrastructure.Extention
{
///
/// 响应数据输出 泛型
///
public class ResponseOutput : IResponseOutput
{
///
/// 是否成功标记
///
public bool IsSuccess { get; private set; }
public ApiResponseCodeEnum Code { get; set; } = ApiResponseCodeEnum.OK;
///
/// 消息
///
public string ErrorMessage { get; private set; }
///
/// 数据 兼顾以前 Json序列化的时候返回属性名为“Result”
///
[JsonProperty("Result")]
public T Data { get; private set; }
public object OtherData { get; set; }
///
/// 成功
///
/// 数据
/// 消息
//public ResponseOutput Ok(T data, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
//{
// IsSuccess = true;
// Code = code;
// Data = data;
// ErrorMessage = msg;
// return this;
//}
public ResponseOutput Ok(T data, object otherData, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
{
IsSuccess = true;
Code = code;
Data = data;
OtherData=otherData;
ErrorMessage = msg;
return this;
}
///
/// 失败
///
/// 提示消息
/// 数据
///
public ResponseOutput NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
{
IsSuccess = false;
Code = code;
ErrorMessage = msg;
Data = data;
return this;
}
}
public class ResponseOutput : IResponseOutput
{
[JsonProperty("Result")]
public T Data { get; private set; }
public T2 OtherInfo { get; private set; }
public bool IsSuccess { get; private set; }
public ApiResponseCodeEnum Code { get; set; }
public string ErrorMessage { get; private set; }
public ResponseOutput Ok(T data, T2 otherInfo, string msg = "")
{
IsSuccess = true;
Data = data;
OtherInfo = otherInfo;
ErrorMessage = msg;
return this;
}
}
///
/// 响应数据静态输出 为了代码简洁 不用每处都New
///
public static class ResponseOutput
{
public static IResponseOutput Ok(T data, T2 otherInfo, string msg = "")
{
return new ResponseOutput().Ok(data, otherInfo);
}
///
/// 成功 -----适合查询
///
/// 数据
/// 消息
///
//public static IResponseOutput Ok(T data = default, string msg = "")
//{
// return new ResponseOutput().Ok(data, msg);
//}
public static IResponseOutput Ok(T data = default, object otherData = default, string msg = "")
{
return new ResponseOutput().Ok(data, otherData, msg);
}
///
/// 成功
///
///
public static IResponseOutput Ok()
{
return Ok();
}
///
/// 失败
///
/// 消息
/// 数据
///
public static IResponseOutput NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
{
return new ResponseOutput().NotOk(msg, data, code);
}
//public static IResponseOutput NotOk( T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
//{
// return new ResponseOutput().NotOk("", data, code);
//}
///
/// 失败
///
/// 消息
///
public static IResponseOutput NotOk(string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
{
return new ResponseOutput().NotOk(msg,code:code);
}
public static IResponseOutput DBNotExistIfNUll(object businessObject)
{
return new ResponseOutput().NotOk("The business object{businessObject.GetType().Name} does not exist in the database, or was deleted by someone else, or an incorrect parameter query caused");
}
///
/// 根据布尔值返回结果 --适合删除
///
///
///
public static IResponseOutput Result(bool success)
{
return success ? Ok() : NotOk("Expect a change, but the database data has not changed");
}
///
/// 根据布尔值返回结果 --适合添加和更新一起
///
///
///
public static IResponseOutput Result(bool success, T data = default)
{
return success ? Ok(data) : NotOk("Saved failed",data);
}
/////
///// 根据布尔值返回结果
/////
/////
/////
//public static IResponseOutput Result(bool success)
//{
// return success ? Ok() : NotOk();
//}
}
}