Browse Source

数据推送记录

jeremy 8 months ago
parent
commit
f2bcf6a70e

+ 1 - 1
src/VitalMixtureService/DBService/Service/FacturyPostHistoryDBService.cs

@@ -54,7 +54,7 @@ namespace VitalService.Service
                     var update = Builders<FacturyPostHistoryEntity>.Update
                         .Set(f => f.Status, request.Status)
                         .Set(f => f.Result, request.Result)
-                        .Set(f => f.PostTime, DateTime.UtcNow)
+                        .Set(f => f.PostTime, request.PostTime)
                         .Set(f => f.UpdateTime, DateTime.UtcNow);
                     return await _facturyPostHistoryDBRepository.UpdateOneAsync(filter, update) > 0;
                 }

+ 99 - 0
src/VitalMixtureService/Factury/FacturyManage.cs

@@ -0,0 +1,99 @@
+using Newtonsoft.Json;
+using WingInterfaceLibrary.DTO.Vital;
+using WingServerCommon.Log;
+using WingServerCommon.Utilities.Executors;
+
+namespace VitalService.Factury
+{
+    public class FacturyManage
+    {
+        private BaseApiHelper _baseApiHelper;
+        private Action<string, bool, DateTime, string> OnUpdateHistoryStatus;
+        private Func<List<FacturyPostHistoryDTO>> OnGetNonPostHistory;
+        private readonly SequenceExecutor<FacturyPostHistoryDTO> _sequenceExecutor = new SequenceExecutor<FacturyPostHistoryDTO>("FacturySequenceExecutor");
+
+        public FacturyManage(Action<string, bool, DateTime, string> funUpdateHistoryStatus
+            , Func<List<FacturyPostHistoryDTO>> funGetNonPostHistory)
+        {
+            _baseApiHelper = new BaseApiHelper(60);
+            OnUpdateHistoryStatus = funUpdateHistoryStatus;
+            OnGetNonPostHistory = funGetNonPostHistory;
+            SendNonPostHistory();
+        }
+
+        public void Add(FacturyPostHistoryDTO history)
+        {
+            _sequenceExecutor.Add(DoPost, history);
+        }
+
+        private void SendNonPostHistory()
+        {
+            var nonPostRecords = OnGetNonPostHistory?.Invoke();
+            Logger.WriteLineInfo($"VitalMixtureService FacturyManage SendNonPostHistory nonPostRecords totalCount:{nonPostRecords?.Count ?? 0}");
+            if (nonPostRecords?.Any() ?? false)
+            {
+                foreach (var record in nonPostRecords)
+                {
+                    Add(record);
+                }
+            }
+        }
+
+        private bool DoPost(FacturyPostHistoryDTO history)
+        {
+            var idCardNo = history.IDCardNo;
+            var postTime = DateTime.UtcNow;
+            try
+            {
+                Logger.WriteLineInfo($"VitalMixtureService FacturyManage DoPost start, idCardNo:{idCardNo}");
+                var res = GetResult(history.FacturyUrl, history.Content, history.Headers, idCardNo).Result;
+
+                var success = res.Item1;
+                var rpcRes = res.Item2;
+                if (success)
+                {
+                    Logger.WriteLineInfo($"VitalMixtureService FacturyManage DoPost success, idCardNo:{idCardNo}, res:{rpcRes}");
+                }
+                else
+                {
+                    Logger.WriteLineError($"VitalMixtureService FacturyManage DoPost failed, idCardNo:{idCardNo}, res:{rpcRes}");
+                }
+                OnUpdateHistoryStatus?.Invoke(history.Code, success, postTime, rpcRes);
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineInfo($"VitalMixtureService FacturyManage DoPost error, ex:{ex}");
+            }
+            finally
+            {
+                Logger.WriteLineInfo($"VitalMixtureService FacturyManage DoPost finally, idCardNo:{idCardNo}");
+            }
+            return true;
+        }
+
+        private async Task<Tuple<bool, string>> GetResult(string url, string request, Dictionary<string, string> headers, string idCardNo)
+        {
+            try
+            {
+                var res = await _baseApiHelper.GetResult(url, request, headers);
+                if (!string.IsNullOrWhiteSpace(res))
+                {
+                    var facturyRes = JsonConvert.DeserializeObject<FacturyResult>(res);
+                    return new Tuple<bool, string>(facturyRes.status == "0", res);
+                }
+                return new Tuple<bool, string>(false, res);
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineWarn($"VitalMixtureService FacturyManage error, idCardNo:{idCardNo}, ex:{ex}");
+                return new Tuple<bool, string>(false, ex.ToString());
+            }
+        }
+
+    }
+
+    public class FacturyResult
+    {
+        public string status { get; set; }
+    }
+}

+ 1 - 1
src/VitalMixtureService/Factury/JingQiApiHelper.cs

@@ -23,7 +23,7 @@ namespace VitalService.Factury
             }
         }
 
-        public JingQiApiHelper(string serverUrl, double seconds = 30) : base(seconds)
+        public JingQiApiHelper(string serverUrl, double seconds = 60) : base(seconds)
         {
             _serverUrl = serverUrl?.Trim('/') ?? string.Empty;
             Logger.WriteLineInfo($"VitalMixtureService JingQiApiHelper init, serverUrl:{_serverUrl}, seconds:{seconds}");

+ 7 - 5
src/VitalMixtureService/FrontService/Service/BaseService.cs

@@ -79,6 +79,7 @@ namespace VitalService.Service
         private PigeonUpload _pigeonUpload;
         private List<string> _appsettingCrowdLabels = new List<string>();
         private JingQiApiHelper _jingQiApiHelper;
+        private FacturyManage _facturyManage;
         /// <summary>
         /// Init service
         /// </summary>
@@ -136,11 +137,6 @@ namespace VitalService.Service
             AnalyzeStrategy.StorageHandler = UploadData;
             StartApi();
             LoadResourceInfos();
-            var jingQiServerUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Vital", "JingQiServerUrl").Value;
-            if (!string.IsNullOrWhiteSpace(jingQiServerUrl))
-            {
-                _jingQiApiHelper = new JingQiApiHelper(jingQiServerUrl);
-            }
         }
 
         /// <summary>
@@ -155,6 +151,12 @@ namespace VitalService.Service
                     StartCheckInitOrganizationAsync().Wait();
                     _pigeonUpload = new PigeonUpload(_cloudServerUrl, _syncToken);
                 }
+                var jingQiServerUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Vital", "JingQiServerUrl").Value;
+                if (!string.IsNullOrWhiteSpace(jingQiServerUrl))
+                {
+                    _jingQiApiHelper = new JingQiApiHelper(jingQiServerUrl);
+                }
+                _facturyManage = new FacturyManage(OnUpdateHistoryStatus, OnGetNonPostHistory);
             }
             catch (Exception ex)
             {

+ 41 - 1
src/VitalMixtureService/FrontService/Service/FacturyPostHistoryService.cs

@@ -4,6 +4,7 @@ using WingInterfaceLibrary.Request.Vital;
 using WingInterfaceLibrary.Request.DBVitalRequest;
 using WingInterfaceLibrary.DTO.Vital;
 using WingInterfaceLibrary.Request;
+using WingServerCommon.Log;
 
 namespace VitalService.Service
 {
@@ -94,7 +95,7 @@ namespace VitalService.Service
             });
             if (history != null)
             {
-                await _vitalFacturyPostHistoryDBService.CreateFacturyPostHistoryDBAsync(new CreateFacturyPostHistoryDBRequest
+                var code = await _vitalFacturyPostHistoryDBService.CreateFacturyPostHistoryDBAsync(new CreateFacturyPostHistoryDBRequest
                 {
                     FacturyUniqueCode = history.FacturyUniqueCode,
                     ApiType = history.ApiType,
@@ -107,9 +108,48 @@ namespace VitalService.Service
                     Content = history.Content,
                     Result = history.Result,
                 });
+                var dto = await _vitalFacturyPostHistoryDBService.GetFacturyPostHistoryDBAsync(new GetFacturyPostHistoryDBRequest { Code = code });
+                _facturyManage.Add(dto);
                 return true;
             }
             return false;
         }
+
+        private void OnUpdateHistoryStatus(string code, bool success, DateTime postTime, string rpcResult)
+        {
+            try
+            {
+                var dbRequest = new UpdateFacturyPostHistoryDBRequest
+                {
+                    Code = code,
+                    PostTime = postTime,
+                    Result = rpcResult,
+                    Status = success ? 2 : 3,
+                };
+                var res = _vitalFacturyPostHistoryDBService.UpdateFacturyPostHistoryDBAsync(dbRequest).Result;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineWarn($"FacturyPostHistoryService OnUpdateHistoryStatus error, ex:{ex}");
+            }
+        }
+
+        private List<FacturyPostHistoryDTO> OnGetNonPostHistory()
+        {
+            var nonPostHistory = new List<FacturyPostHistoryDTO>();
+            try
+            {
+                nonPostHistory = _vitalFacturyPostHistoryDBService.GetNonPostHistoryDBAsync(new GetNonPostHistoryDBRequest
+                {
+
+                }).Result;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineWarn($"FacturyPostHistoryService OnGetNonPostHistory error, ex:{ex}");
+            }
+            return nonPostHistory;
+        }
+
     }
 }