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.

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