|
- using DTO;
- using HKCardOUT.Helper;
- using HKCardOUT.Logic.Model;
- using HKCardOUT.Logic.Service;
- using HKLib.Dto;
- using HKLog;
- using Logic.RemoteModel;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Markup;
- using UHFHelper;
- using XExten.Advance.HttpFramework.MultiCommon;
- using XExten.Advance.HttpFramework.MultiFactory;
- using XExten.Advance.LinqFramework;
- using XExten.Advance.StaticFramework;
- using BPA.Helper;
-
- namespace HKCardOUT.Logic
- {
- public class RemoteService
- {
- private static readonly object locker = new();
- /// <summary>
- /// 获取所有卡状态和卡号
- /// </summary>
- /// <param name="input"></param>
- public static int GetCardStuatas()
- {
- try
- {
- var res = APIHelper.GetInstance.HttpGetRequest(ApiRoute.CheckCardStatus);
- //var res = IHttpMultiClient.HttpMulti.AddNode(t =>
- //{
- // t.NodePath = ApiRoute.CheckCardStatus;
- // t.ReqType = MultiType.GET;
- //}).Build().RunStringFirst();
- if (string.IsNullOrEmpty(res)) return 0;
- var data = res.ToModel<JObject>()["data"]["data"].ToJson().ToModel<List<CardInfo>>();
- var entity = data.Select(t => new CardStutas
- {
- CardNo = t.CardNum,
- Stutas = t.State
- }).ToList();
- HKCore.AddCard(entity);
- return data != null ? data.ToString().AsInt() : 0;
- }
- catch (Exception ex)
- {
- HKLogImport.WriteError(ex);
- return 0;
- }
-
- }
- /// <summary>
- /// 同步时间段
- /// </summary>
- public static void SyncTime()
- {
- var data = IHttpMultiClient.HttpMulti.AddNode(t =>
- {
- t.NodePath = ApiRoute.GetTime;
- t.ReqType = MultiType.GET;
- }).Build().RunStringFirst();
-
- var dtoTime = data.ToModel<JObject>()["data"]["data"].ToJson().ToModel<TimeSpilteDto>();
-
- HKLogImport.WriteInfo($"获取接口数据:{data}");
-
- var time = GetNetWorkTime();
- var tims = $"{time.Year}-{time.Month}-{time.Day}";
- DataBus.Times = new TimeSpilte();
- DataBus.Times.AMStartTime = DateTime.Parse($"{tims} {dtoTime.AMStartTime}");
- DataBus.Times.AMEndTime = DateTime.Parse($"{tims} {dtoTime.AMEndTime}");
- DataBus.Times.PMStartTime = DateTime.Parse($"{tims} {dtoTime.PMStartTime}");
- DataBus.Times.PMEndTime = DateTime.Parse($"{tims} {dtoTime.PMEndTime}");
- DataBus.Times.ATStartTime = DateTime.Parse($"{tims} {dtoTime.ATStartTime}");
- DataBus.Times.ATEndTime = DateTime.Parse($"{tims} {dtoTime.ATEndTime}");
-
- HKLogImport.WriteInfo($"同步时间:早上用餐时间段{DataBus.Times.AMStartTime},{DataBus.Times.AMEndTime};中午用餐时间段{DataBus.Times.PMStartTime},{DataBus.Times.PMEndTime};晚上用餐时间段{DataBus.Times.ATStartTime},{DataBus.Times.ATEndTime}");
-
- }
-
- /// <summary>
- /// 同步消费记录
- /// </summary>
- /// <param name="CardNo"></param>
- /// <param name="Money"></param>
- public static bool SyncSaleLog(string CardNo, string StallId)
- {
- try
- {
- var GateId = DataBus.StoreInfo.Devices.FirstOrDefault(t => t.Address.AsInt() == StallId.AsInt())?.GateId;
- var data = IHttpMultiClient.HttpMulti.AddNode(t =>
- {
- t.ReqType = MultiType.POST;
- t.JsonParam = (new { GateId = GateId, Type = -1, CardNum = CardNo }).ToJson();
- t.NodePath = ApiRoute.SyncSaleLog;
- }).Build().RunStringFirst();
- HKLog.HKLogImport.WriteInfo($"同步消费时候调用:{data}");
- return bool.Parse(data.ToModel<JObject>()["data"].ToString());
- }
- catch (Exception ex)
- {
- HKLog.HKLogImport.WriteError(ex);
- return false;
- }
-
- }
- /// <summary>
- /// 批量同步同步消费记录
- /// </summary>
- /// <returns></returns>
- public static List<FailDto> SyncSaleLogList(List<SaleDto> dto)
- {
- try
- {
- lock (locker)
- {
- var Ids = dto.Select(t => t.Id).ToList();
- var check = DbContext.Context.Queryable<SaleLog>().Where(t => Ids.Contains(t.Id)).Any(t => t.IsSync);
- if (check) return null;
- //var cc = APIHelper.GetInstance.PostRequest(ApiRoute.SyncSaleLogList, dto.ToJson());
- var data = IHttpMultiClient.HttpMulti.AddHeader(t =>
- {
- t.HeaderKey = "TenantId";
- t.HeaderValue = DataBus.TenantId;
- }).AddNode(t =>
- {
- t.ReqType = MultiType.POST;
- t.JsonParam = dto.ToJson();
- t.NodePath = ApiRoute.SyncSaleLogList;
- }).Build().RunStringFirst();
-
- HKLog.HKLogImport.WriteInfo($"推送数据:{dto.ToJson()}");
- List<FailDto> result = data.ToModel<JObject>()["data"].ToString().ToModel<JObject>()["failList"].ToJson().ToModel<List<FailDto>>();
- HKLog.HKLogImport.WriteInfo($"{result.Count}批量同步同步消费记录调用:{data}");
- return result;
- }
- }
- catch (Exception ex)
- {
- HKLog.HKLogImport.WriteError(ex);
- return null;
- }
-
-
- }
- /// <summary>
- /// 获取店铺信息
- /// </summary>
- public static void PullShopInfo()
- {
- if (DataBus.NetWordState)
- {
- /* var data = IHttpMultiClient.HttpMulti.AddNode(t =>
- {
- t.NodePath = ApiRoute.PullShopInfo;
- }).Build().RunStringFirst()*/
- var data = APIHelper.GetInstance.HttpGetRequest(ApiRoute.PullShopInfo)
- .ToModel<JObject>()["data"].ToJson().ToModel<StoreInfoResponse>();
- DataBus.StoreInfo = data;
- var storeinfo = SyncStatic.CreateFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StoreInfo.json"));
- SyncStatic.WriteFile(Encoding.UTF8.GetBytes(data.ToJson()), storeinfo);
- }
- else
- {
- DataBus.StoreInfo = SyncStatic.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StoreInfo.json")).ToModel<StoreInfoResponse>();
- }
- }
- /// <summary>
- /// 获取菜单
- /// </summary>
- public static void GetFoodMenus()
- {
-
- if (DataBus.NetWordState)
- {
- //var data = IHttpMultiClient.HttpMulti.AddNode(t =>
- //{
- // t.CacheNode = false;
- // t.NodePath = ApiRoute.FoodCtrl;
- //}).Build().RunStringFirst();
-
- var data = APIHelper.GetInstance.HttpGetRequest(ApiRoute.FoodCtrl);
-
- DataBus.Menu = data.ToModel<JObject>()["data"].ToJson().ToModel<FoodDto>();
- }
- }
- public static void GetRangeCtrl()
- {
-
- if (DataBus.NetWordState)
- {
- /*var data = IHttpMultiClient.HttpMulti.AddNode(t =>
- {
- t.NodePath = ApiRoute.RangCtrl;
- }).Build().RunStringFirst();*/
- var data = APIHelper.GetInstance.HttpGetRequest(ApiRoute.RangCtrl);
- DataBus.RangeCtrl = data.ToModel<JObject>()["data"].ToJson().ToModel<List<GateSetUp>>();
-
- }
- }
-
- /// <summary>
- /// 获取网络服务器时间
- /// </summary>
- public static DateTime GetNetWorkTime()
- {
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.baidu.com");
- request.Method = "HEAD";
- request.AllowAutoRedirect = false;
- HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
- string cc = reponse.GetResponseHeader("date");
- reponse.Close();
- DateTime time;
- bool s = GMTStrParse(cc, out time);
- return time.AddHours(8); //GMT要加8个小时才是北京时间
- }
- public static bool GMTStrParse(string gmtStr, out DateTime gmtTime) //抓取的date是GMT格式的字符串,这里转成datetime
- {
- CultureInfo enUS = new CultureInfo("en-US");
- bool s = DateTime.TryParseExact(gmtStr, "r", enUS, DateTimeStyles.None, out gmtTime);
- return s;
- }
- }
- }
|