using JSMachine.WMS.Infrastructure.Helper;
using JSMachine.WMS.RPC.PaperMESServerRPC.Bo;
using Newtonsoft.Json;
using RestSharp;
namespace JSMachine.WMS.RPC.Common.Base
{
///
/// RPC 通用 HTTP 服务基类,封装 JSON 请求、响应反序列化和错误日志记录。
///
/// 远程接口使用的业务数据类型。
public class BaseService where T : class, new()
{
///
/// 添加
///
///
///
///
///
public async Task AddAsync(T t, string url, Method method)
{
string resStr = await HttpRequestHelper.RequestByJson(url, method, JsonConvert.SerializeObject(t));
if (string.IsNullOrEmpty(resStr))
return false;
try
{
ApiResultBo apiResultDto = JsonConvert.DeserializeObject>(resStr);
return apiResultDto.Data;
}
catch (Exception ex)
{
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
return false;
}
}
///
/// 修改
///
///
///
///
///
public async Task EditAsync(T t, string url, Method method)
{
string resStr = await HttpRequestHelper.RequestByJson(url, method, JsonConvert.SerializeObject(t));
if (string.IsNullOrEmpty(resStr))
return false;
try
{
ApiResultBo apiResultDto = JsonConvert.DeserializeObject>(resStr);
return apiResultDto.Data;
}
catch (Exception ex)
{
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
return false;
}
}
///
/// 删除单个
///
///
///
///
///
public async Task DelteOneAsync(Guid id,string url, Method method)
{
string resStr = await HttpRequestHelper.RequestByDic(url, method, new Dictionary { { "id", id } });
if (string.IsNullOrEmpty(resStr))
return false;
try
{
ApiResultBo apiResultDto = JsonConvert.DeserializeObject>(resStr);
return apiResultDto.Data;
}
catch (Exception)
{
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
return false;
}
}
///
/// 删除多个
///
///
///
///
///
public async Task DelteManyAsync(List ids, string url, Method method)
{
string resStr = await HttpRequestHelper.RequestByJson(url, method, JsonConvert.SerializeObject(ids));
if (string.IsNullOrEmpty(resStr))
return false;
try
{
ApiResultBo apiResultDto = JsonConvert.DeserializeObject>(resStr);
return apiResultDto.Data;
}
catch (Exception)
{
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
return false;
}
}
///
/// 获取所有
///
///
///
public async Task> GetAllAsync(string url)
{
string resStr = await HttpRequestHelper.RequestByDic(url, Method.GET);
if (string.IsNullOrEmpty(resStr))
return null;
try
{
ApiResultBo> apiResultDto = JsonConvert.DeserializeObject>>(resStr);
return apiResultDto.Data;
}
catch (Exception)
{
LogHelper.Error($"HTTP请求 {url} 返回结果错误,原始返回信息 {resStr}");
return null;
}
}
}
}