using iWareCommon.Utils;
using iWareModel.Entity.AGV;
using iWareModel.Entity.MES;
using iWareModel.Entity.WCS;
using iWareModel.Entity.WMS;
using Newtonsoft.Json;
using System;
using System.Dynamic;
using System.Net.Http;
using System.Windows.Forms;
namespace iWareCC.Common.Helper
{
///
/// 跟WMS交互的帮助类
///
public class WMSRequestHelper
{
///
/// WMS的主机host
///
private static string wmsApiHost = ConfigHelper.GetConfigString("WmsApiHost");
private static HttpClient _httpClient = null;
private static string base_Path = wmsApiHost;
///
/// 3巷道寻找空库位,用途:移库任务、入库任务
///
/// 货物高度
/// 要处理的托盘号
/// 标记,0:正常入库 1:移库
///
///
public static CriterionContainerOutput FindEmptyLocationForLane3(int high, string containerCode, int flag)
{
var flagName = flag == 1 ? "移库任务" : "入库任务";
var logTitle = $"[{flagName}],容器号{containerCode},高度{high}。";
var utl = string.Format(@"task/FindEmptyLocationForLane3?high={0}&containerCode={1}&flag={2}", high, containerCode, flag);
var result = new HTTPService(wmsApiHost).postContentForString(utl, "", "");
if (result == null)
{
throw new Exception(logTitle + "创建失败!");
}
Log4NetHelper.WriteErrorLog(LogType.CCWCFService, logTitle + "FindEmptyLocationForLane3 返回的值:" + result + ",参数:high:" + high
+ ",参数:containerCode:" + containerCode + ",参数:flag:" + flag + ",地址:" + wmsApiHost + "/task/FindEmptyLocationForLane3");
var wmsSysReturn = JsonConvert.DeserializeObject(result);
if (wmsSysReturn.success == false)
{
throw new Exception(logTitle + "请求WMS失败!" + wmsSysReturn.message);
}
if (wmsSysReturn.data == null)
{
return null;
}
var data = JsonConvert.DeserializeObject(wmsSysReturn.data.ToString());
return data;
}
///
/// Post请求
///
/// 返回参数的数据类型
/// 请求地址
/// 传入的数据
///
public static TResult Post(string url, object data)
{
try
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri(base_Path);
var jsonData = JsonConvert.SerializeObject(data);
HttpContent content = new StringContent(jsonData);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpResponseMessage res = _httpClient.PostAsync(url, content).Result;
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
string resMsgStr = res.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject(resMsgStr);
return result;
}
else
{
MessageBox.Show(res.StatusCode.ToString());
return default;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return default;
}
}
}
}