|
@@ -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; }
|
|
|
+ }
|
|
|
+}
|