DicomUploadContext.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using SQLite;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Vinno.FIS.Sonopost.Features.Dicom
  6. {
  7. public class DicomUploadContextOperator
  8. {
  9. public static DicomUploadContextOperator Instance = new DicomUploadContextOperator();
  10. private readonly object _locker = new object();
  11. private DicomUploadContextOperator()
  12. {
  13. }
  14. public DicomUploadContext GetCacheByScanId(string scanDataId)
  15. {
  16. lock (_locker)
  17. {
  18. return DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.ScanId == scanDataId);
  19. }
  20. }
  21. public DicomUploadContext GetCacheById(string id)
  22. {
  23. lock (_locker)
  24. {
  25. return DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.Id == id);
  26. }
  27. }
  28. public List<DicomUploadContext> GetAll()
  29. {
  30. List<DicomUploadContext> list;
  31. lock (_locker)
  32. {
  33. list = DicomCacheDB.Instance.GetAll<DicomUploadContext>().OrderBy(c => c.CreateTime).ToList();
  34. }
  35. list.ForEach(x => x.CreateTime = x.CreateTime.ToLocalTime());
  36. return list;
  37. }
  38. public void Create(DicomUploadContext cache)
  39. {
  40. lock (_locker)
  41. {
  42. DicomCacheDB.Instance.Create(cache);
  43. }
  44. }
  45. public void UpdateVidPath(string id, string path)
  46. {
  47. lock (_locker)
  48. {
  49. var existingItem = DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.Id == id);
  50. if (existingItem != null)
  51. {
  52. var tmpPath = existingItem.VidPath;
  53. existingItem.VidPath = path;
  54. var ret = DicomCacheDB.Instance.Update(existingItem);
  55. if (ret == false)
  56. {
  57. existingItem.VidPath = tmpPath;
  58. }
  59. }
  60. }
  61. }
  62. public void UpdateVidPathByScanId(string scanId, string path)
  63. {
  64. lock (_locker)
  65. {
  66. var existingItem = DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.ScanId == scanId);
  67. if (existingItem != null)
  68. {
  69. var tmpPath = existingItem.VidPath;
  70. existingItem.VidPath = path;
  71. var ret = DicomCacheDB.Instance.Update(existingItem);
  72. if (ret == false)
  73. {
  74. existingItem.VidPath = tmpPath;
  75. }
  76. }
  77. }
  78. }
  79. public void UpdateScanId(string id, string scanId)
  80. {
  81. lock (_locker)
  82. {
  83. var existingItem = DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.Id == id);
  84. if (existingItem != null)
  85. {
  86. var tmp = existingItem.ScanId;
  87. existingItem.ScanId = scanId;
  88. var ret = DicomCacheDB.Instance.Update(existingItem);
  89. if (ret == false)
  90. {
  91. existingItem.ScanId = tmp;
  92. }
  93. }
  94. }
  95. }
  96. public void UpdateStatus(string id, DicomUploadStatus status)
  97. {
  98. lock (_locker)
  99. {
  100. var existingItem = DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.Id == id);
  101. if (existingItem != null)
  102. {
  103. var tmp = existingItem.Status;
  104. existingItem.Status = status;
  105. var ret = DicomCacheDB.Instance.Update(existingItem);
  106. if (ret == false)
  107. {
  108. existingItem.Status = tmp;
  109. }
  110. }
  111. }
  112. }
  113. public void UpdateStatusByScanId(string scanId, DicomUploadStatus status)
  114. {
  115. lock (_locker)
  116. {
  117. var existingItem = DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.ScanId == scanId);
  118. if (existingItem != null)
  119. {
  120. var tmp = existingItem.Status;
  121. existingItem.Status = status;
  122. var ret = DicomCacheDB.Instance.Update(existingItem);
  123. if (ret == false)
  124. {
  125. existingItem.Status = tmp;
  126. }
  127. }
  128. }
  129. }
  130. internal void UpdateRetryCount(DicomUploadContext item, int count)
  131. {
  132. lock (_locker)
  133. {
  134. var existingItem = DicomCacheDB.Instance.Get<DicomUploadContext>(c => c.Id == item.Id);
  135. if (existingItem != null)
  136. {
  137. existingItem.Count = count;
  138. DicomCacheDB.Instance.Update(existingItem);
  139. }
  140. }
  141. }
  142. public bool DeleteById(string id)
  143. {
  144. lock (_locker)
  145. {
  146. var ret = DicomCacheDB.Instance.Delete<DicomUploadContext>(c => c.Id == id);
  147. return ret;
  148. }
  149. }
  150. public bool DeleteByScanId(string scanId)
  151. {
  152. lock (_locker)
  153. {
  154. var ret = DicomCacheDB.Instance.Delete<DicomUploadContext>(c => c.ScanId == scanId);
  155. return ret;
  156. }
  157. }
  158. }
  159. public enum DicomUploadStatus
  160. {
  161. Create,
  162. ConvertFail,
  163. CreateScanDataFail,
  164. UploadFail,
  165. UploadFailBecauseExamIsFinished,
  166. Unknown,
  167. Waiting,
  168. Uploading,
  169. Success
  170. }
  171. public class DicomUploadContext
  172. {
  173. [PrimaryKey]
  174. public string Id { get; set; }
  175. public string DicomPath { get; set; }
  176. public string PatientId { get; set; }
  177. public string PatientName { get; set; }
  178. public DateTime CreateTime { get; set; }
  179. public string VidPath { get; set; }
  180. public string ScanId { get; set; }
  181. public DicomUploadStatus Status { get; set; }
  182. [Ignore]
  183. public int Count { get; set; }
  184. public DicomUploadContext()
  185. {
  186. }
  187. public DicomUploadContext(string id, string path, string patientId, string patientName)
  188. {
  189. Id = id;
  190. DicomPath = path;
  191. PatientId = patientId;
  192. PatientName = patientName;
  193. CreateTime = DateTime.UtcNow;
  194. Status = DicomUploadStatus.Create;
  195. Count = 0;
  196. }
  197. }
  198. }