You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.8 KiB

3 weeks ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using NFine.Code;
  9. using NFine.Repository;
  10. using System.Data.Common;
  11. using NFine.Domain._03_Entity.SRM;
  12. using ICS.Application.Entity;
  13. using Newtonsoft.Json;
  14. using System.Configuration;
  15. using System.Data.SqlClient;
  16. using ICS.Data;
  17. using System.Net;
  18. using System.IO;
  19. using Newtonsoft.Json.Linq;
  20. using NFine.Domain._03_Entity.WMS;
  21. namespace NFine.Application.WMS
  22. {
  23. public class ICSAPIErrorHandleApp : RepositoryFactory<ICSVendor>
  24. {
  25. public string WMSInterfaceReCall(string IDList, string WorkPoint)
  26. {
  27. string msg = "";
  28. try
  29. {
  30. string LogIDList = "";
  31. foreach (string ID in IDList.TrimEnd(',').Split(','))
  32. {
  33. LogIDList += "'" + ID + "',";
  34. }
  35. string sql = @"select ID,Url,JosnStr from ICSAPIErrorLog
  36. where ID in ({0}) AND Status='0' AND WorkPoint='{1}'";
  37. sql = string.Format(sql, LogIDList.TrimEnd(','), WorkPoint);
  38. DataTable Sapdt = SqlHelper.GetDataTableBySql(sql);
  39. foreach (DataRow dr in Sapdt.Rows)
  40. {
  41. string APIURL = dr["Url"].ToString();
  42. string result = HttpPost(APIURL, dr["JosnStr"].ToString());
  43. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  44. string MessAge = Obj["Message"].ToString();
  45. string Success = Obj["Success"].ToString();
  46. if (Success.ToUpper() == "FALSE")
  47. {
  48. sql = @"update ICSAPIErrorLog set ErrorMsg='{0}'
  49. where ID='{1}' and WorkPoint='{2}'";
  50. sql = string.Format(sql, MessAge, dr["ID"].ToString(), WorkPoint);
  51. SqlHelper.CmdExecuteNonQueryLi(sql);
  52. throw new Exception(MessAge);
  53. }
  54. else
  55. {
  56. sql = @"update ICSAPIErrorLog set Status='1'
  57. where ID='{0}' and WorkPoint='{1}'";
  58. sql = string.Format(sql, dr["ID"].ToString(), WorkPoint);
  59. SqlHelper.CmdExecuteNonQueryLi(sql);
  60. }
  61. }
  62. return msg;
  63. }
  64. catch (Exception ex)
  65. {
  66. msg = ex.Message;
  67. return msg;
  68. }
  69. }
  70. public static string HttpPost(string url, string body)
  71. {
  72. try
  73. {
  74. Encoding encoding = Encoding.UTF8;
  75. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  76. request.Method = "POST";
  77. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  78. request.ContentType = "application/json; charset=utf-8";
  79. byte[] buffer = encoding.GetBytes(body);
  80. request.ContentLength = buffer.Length;
  81. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  82. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  83. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  84. {
  85. return reader.ReadToEnd();
  86. }
  87. }
  88. catch (WebException ex)
  89. {
  90. throw new Exception(ex.Message);
  91. }
  92. }
  93. public class WMSResult
  94. {
  95. public bool Success { get; set; }
  96. public string Message { get; set; }
  97. }
  98. }
  99. }