Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

212 рядки
7.7 KiB

  1. using DTO;
  2. using HKCardOUT.Helper;
  3. using HKCardOUT.Logic.Model;
  4. using HKCardOUT.Logic.Service;
  5. using HKLib.Dto;
  6. using HKLog;
  7. using Logic.RemoteModel;
  8. using Newtonsoft.Json.Linq;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Markup;
  18. using UHFHelper;
  19. using XExten.Advance.HttpFramework.MultiCommon;
  20. using XExten.Advance.HttpFramework.MultiFactory;
  21. using XExten.Advance.LinqFramework;
  22. using XExten.Advance.StaticFramework;
  23. namespace HKCardOUT.Logic
  24. {
  25. public class RemoteService
  26. {
  27. private static readonly object locker = new();
  28. /// <summary>
  29. /// 获取所有卡状态和卡号
  30. /// </summary>
  31. /// <param name="input"></param>
  32. public static int GetCardStuatas()
  33. {
  34. var res = IHttpMultiClient.HttpMulti.AddNode(t =>
  35. {
  36. t.NodePath = ApiRoute.CheckCardStatus;
  37. t.ReqType = MultiType.GET;
  38. }).Build().RunStringFirst();
  39. var data = res.ToModel<JObject>()["data"]["data"].ToJson().ToModel<List<CardInfo>>();
  40. var entity = data.Select(t => new CardStutas
  41. {
  42. CardNo = t.CardNum,
  43. Stutas = t.State
  44. }).ToList();
  45. HKCore.AddCard(entity);
  46. return data != null ? data.ToString().AsInt() : 0;
  47. }
  48. /// <summary>
  49. /// 同步时间段
  50. /// </summary>
  51. public static void SyncTime()
  52. {
  53. var data = IHttpMultiClient.HttpMulti.AddNode(t =>
  54. {
  55. t.NodePath = ApiRoute.GetTime;
  56. t.ReqType = MultiType.GET;
  57. }).Build().RunStringFirst();
  58. var dtoTime = data.ToModel<JObject>()["data"]["data"].ToJson().ToModel<TimeSpilteDto>();
  59. var time = GetNetWorkTime();
  60. var tims = $"{time.Year}-{time.Month}-{time.Day}";
  61. DataBus.Times = new TimeSpilte();
  62. DataBus.Times.AMStartTime = DateTime.Parse($"{tims} {dtoTime.AMStartTime}");
  63. DataBus.Times.AMEndTime = DateTime.Parse($"{tims} {dtoTime.AMEndTime}");
  64. DataBus.Times.PMStartTime = DateTime.Parse($"{tims} {dtoTime.PMStartTime}");
  65. DataBus.Times.PMEndTime = DateTime.Parse($"{tims} {dtoTime.PMEndTime}");
  66. DataBus.Times.ATStartTime = DateTime.Parse($"{tims} {dtoTime.ATStartTime}");
  67. DataBus.Times.ATEndTime = DateTime.Parse($"{tims} {dtoTime.ATEndTime}");
  68. HKLogImport.WriteInfo("同步时间:" + DataBus.Times);
  69. }
  70. /// <summary>
  71. /// 同步消费记录
  72. /// </summary>
  73. /// <param name="CardNo"></param>
  74. /// <param name="Money"></param>
  75. public static bool SyncSaleLog(string CardNo, string StallId)
  76. {
  77. try
  78. {
  79. var GateId = DataBus.StoreInfo.Devices.FirstOrDefault(t => t.Address.AsInt() == StallId.AsInt())?.GateId;
  80. var data = IHttpMultiClient.HttpMulti.AddNode(t =>
  81. {
  82. t.ReqType = MultiType.POST;
  83. t.JsonParam = (new { GateId = GateId, Type = -1, CardNum = CardNo }).ToJson();
  84. t.NodePath = ApiRoute.SyncSaleLog;
  85. }).Build().RunStringFirst();
  86. HKLog.HKLogImport.WriteInfo($"同步消费时候调用:{data}");
  87. return bool.Parse(data.ToModel<JObject>()["data"].ToString());
  88. }
  89. catch (Exception ex)
  90. {
  91. HKLog.HKLogImport.WriteError(ex);
  92. return false;
  93. }
  94. }
  95. /// <summary>
  96. /// 批量同步同步消费记录
  97. /// </summary>
  98. /// <returns></returns>
  99. public static List<FailDto> SyncSaleLogList(List<SaleDto> dto)
  100. {
  101. try
  102. {
  103. lock (locker)
  104. {
  105. var Ids = dto.Select(t => t.Id).ToList();
  106. var check = DbContext.Context.Queryable<SaleLog>().Where(t => Ids.Contains(t.Id)).Any(t => t.IsSync);
  107. if (check) return null;
  108. var data = IHttpMultiClient.HttpMulti.AddHeader(t =>
  109. {
  110. t.HeaderKey = "TenantId";
  111. t.HeaderValue = DataBus.TenantId;
  112. }).AddNode(t =>
  113. {
  114. t.ReqType = MultiType.POST;
  115. t.JsonParam = dto.ToJson();
  116. t.NodePath = ApiRoute.SyncSaleLogList;
  117. }).Build().RunStringFirst();
  118. HKLog.HKLogImport.WriteInfo($"批量同步同步消费记录调用:{data}");
  119. return data.ToModel<JObject>()["data"].ToString().ToModel<JObject>()["failList"].ToJson().ToModel<List<FailDto>>();
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. HKLog.HKLogImport.WriteError(ex);
  125. return null;
  126. }
  127. }
  128. /// <summary>
  129. /// 获取店铺信息
  130. /// </summary>
  131. public static void PullShopInfo()
  132. {
  133. if (DataBus.NetWordState)
  134. {
  135. var data = IHttpMultiClient.HttpMulti.AddNode(t =>
  136. {
  137. t.NodePath = ApiRoute.PullShopInfo;
  138. }).Build().RunStringFirst()
  139. .ToModel<JObject>()["data"].ToJson().ToModel<StoreInfoResponse>();
  140. DataBus.StoreInfo = data;
  141. var storeinfo = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StoreInfo.json"));
  142. SyncStatic.WriteFile(Encoding.UTF8.GetBytes(data.ToJson()), storeinfo);
  143. }
  144. else
  145. {
  146. DataBus.StoreInfo = SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StoreInfo.json")).ToModel<StoreInfoResponse>();
  147. }
  148. }
  149. /// <summary>
  150. /// 获取菜单
  151. /// </summary>
  152. public static void GetFoodMenus()
  153. {
  154. if (DataBus.NetWordState)
  155. {
  156. var data = IHttpMultiClient.HttpMulti.AddNode(t =>
  157. {
  158. t.NodePath = ApiRoute.FoodCtrl;
  159. }).Build().RunStringFirst();
  160. DataBus.Menu = data.ToModel<JObject>()["data"].ToJson().ToModel<FoodDto>();
  161. }
  162. }
  163. public static void GetRangeCtrl()
  164. {
  165. if (DataBus.NetWordState)
  166. {
  167. var data = IHttpMultiClient.HttpMulti.AddNode(t =>
  168. {
  169. t.NodePath = ApiRoute.RangCtrl;
  170. }).Build().RunStringFirst();
  171. DataBus.RangeCtrl = data.ToModel<JObject>()["data"].ToJson().ToModel<List<GateSetUp>>();
  172. }
  173. }
  174. /// <summary>
  175. /// 获取网络服务器时间
  176. /// </summary>
  177. public static DateTime GetNetWorkTime()
  178. {
  179. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.baidu.com");
  180. request.Method = "HEAD";
  181. request.AllowAutoRedirect = false;
  182. HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
  183. string cc = reponse.GetResponseHeader("date");
  184. reponse.Close();
  185. DateTime time;
  186. bool s = GMTStrParse(cc, out time);
  187. return time.AddHours(8); //GMT要加8个小时才是北京时间
  188. }
  189. public static bool GMTStrParse(string gmtStr, out DateTime gmtTime) //抓取的date是GMT格式的字符串,这里转成datetime
  190. {
  191. CultureInfo enUS = new CultureInfo("en-US");
  192. bool s = DateTime.TryParseExact(gmtStr, "r", enUS, DateTimeStyles.None, out gmtTime);
  193. return s;
  194. }
  195. }
  196. }