using YieldChain.Models;
using YLErp.Configuration;
using YLErp.Helpers;
namespace YLErp.Modules.ApiModule
{
public static class StructureInfoApiHelper
{
static readonly HttpClientWrap _httpClientWrap;
static StructureInfoApiHelper()
{
_httpClientWrap = new HttpClientWrap(PS.Config.ErpElement.ExternalAPIForCustomCalc, PS.Config.ErpElement.ExternalAPIForCustomCalcTimeOut);
}
public static string PostJson(string apiPath, object postData)
{
if (_httpClientWrap.BaseAddress == null)
{
throw new ServiceException("ExternalAPI未配置");
}
try
{
return _httpClientWrap.PostJson(apiPath, postData, null);
}
catch (Exception ex)
{
LogFactory.GetLogger("ExternalAPI-Post").Error(apiPath, ex);
throw;
}
}
///
/// 自定义交易结构变更后通知外部系统
///
///
///
/// 1:新增
/// 2:修改
/// 3:删除
///
///
public static ApiResponseModel ChangeEvent(string structureType, List fields, int optType)
{
var postData = new Dictionary
{
["StructureType"] = structureType,
["Fields"] = fields.Select(O =>
{
var Type = EnumHelper.GetDescriptionByName(O.ColumnType);
Type = Type == "下拉列表" ? "文本" : Type;
return new
{
Name = O.ColumnName,
Type
};
})
};
switch (optType)
{
case 1:
postData["ChangeReason"] = "add";
break;
case 2:
postData["ChangeReason"] = "update";
break;
case 3:
postData["ChangeReason"] = "remove";
break;
default:
throw new Exception("未知的操作类型");
}
var res = PostJson("api/v1/structure/changeStructure", postData);
LogFactory.GetLogger().Info(res);
ApiResponseModel obj = null;
try
{
obj = JsonHelper.Deserialize(res);
}
catch { }
return obj ?? new ApiResponseModel();
}
}
}