DataAccessor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using vCloud.GeneratePackages.Entitys;
  8. using vCloud.GeneratePackages.Utilities;
  9. using Vinno.IUS.Product.Notes.Data;
  10. namespace Vinno.IUS.Product.Notes.Service
  11. {
  12. class DataAccessor
  13. {
  14. private static DataAccessor _instance;
  15. private readonly object _dbLock = new object();
  16. private string _notesPath;
  17. private string _testNotesPath;
  18. private IList<Note> _notes = new List<Note>();
  19. private IList<Note> _testNotes = new List<Note>();
  20. private IList<Account> _accounts = new List<Account>();
  21. private string _accountPath;
  22. /// <summary>
  23. /// Notes
  24. /// </summary>
  25. public IList<Note> Notes => _notes.ToList();
  26. /// <summary>
  27. /// Test Notes
  28. /// </summary>
  29. public IList<Note> TestNotes => _testNotes.ToList();
  30. /// <summary>
  31. /// Acccounts
  32. /// </summary>
  33. public IList<Account> Accounts => _accounts.ToList();
  34. public static DataAccessor Instance => _instance ?? (_instance = new DataAccessor());
  35. /// <summary>
  36. /// Initialize db
  37. /// </summary>
  38. public void Initialize()
  39. {
  40. var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db");
  41. if (!Directory.Exists(folder))
  42. {
  43. Directory.CreateDirectory(folder);
  44. }
  45. const string dbName = "Notes.data";
  46. _notesPath = Path.Combine(folder, dbName);
  47. var dbExist = File.Exists(_notesPath);
  48. const string accountName = "Accounts.data";
  49. _accountPath = Path.Combine(folder, accountName);
  50. const string testNotesFileName = "TestNotes.data";
  51. _testNotesPath = Path.Combine(folder, testNotesFileName);
  52. if (!dbExist)
  53. {
  54. var account = new Account() { Name = "Loki", Password = "vinno@123" };
  55. CreateOrUpdateAccount(account);
  56. var warr = new Account() { Name = "Warr", Password = "vinno@123" };
  57. CreateOrUpdateAccount(warr);
  58. var yoyi = new Account() { Name = "Yori", Password = "vinno@123" };
  59. CreateOrUpdateAccount(yoyi);
  60. var jimmy = new Account() { Name = "Jimmy", Password = "vinno@123" };
  61. CreateOrUpdateAccount(jimmy);
  62. var melon = new Account() { Name = "Melon", Password = "vinno@123" };
  63. CreateOrUpdateAccount(melon);
  64. var july = new Account() { Name = "July", Password = "vinno@123" };
  65. CreateOrUpdateAccount(july);
  66. var hayley = new Account() { Name = "Hayley", Password = "vinno@123" };
  67. CreateOrUpdateAccount(hayley);
  68. var updateNote = new Note();
  69. updateNote.Version = "1.7.4.16355";
  70. updateNote.InternalRecord = "1. 教师端直播改成混流模式2.将官网PC端下载链接从live引擎包改成完整包";
  71. updateNote.Publisher = new AccountInfo() { Id = account.Id, Name = account.Name };
  72. var item = new UpdatePlatformNote() { Platform = GeneratePackagePlatform.PC };
  73. item.LanguageNotes.Add(new LanguageItem() { Language = "Chinese", Content = "这是PC中文注释" });
  74. item.LanguageNotes.Add(new LanguageItem() { Language = "English", Content = "This is PC English" });
  75. item.LanguageNotes.Add(new LanguageItem() { Language = "Portuguese", Content = "这是 PC Portuguese" });
  76. item.LanguageNotes.Add(new LanguageItem() { Language = "Romanian", Content = "这是 PC Romanian" });
  77. updateNote.Notes.Add(item);
  78. item = new UpdatePlatformNote() { Platform = GeneratePackagePlatform.Android };
  79. item.LanguageNotes.Add(new LanguageItem() { Language = "Chinese", Content = "这是Android中文注释" });
  80. item.LanguageNotes.Add(new LanguageItem() { Language = "English", Content = "This is Android English" });
  81. item.LanguageNotes.Add(new LanguageItem() { Language = "Portuguese", Content = "这是 Android Portuguese" });
  82. item.LanguageNotes.Add(new LanguageItem() { Language = "Romanian", Content = "这是 Android Romanian" });
  83. updateNote.Notes.Add(item);
  84. item = new UpdatePlatformNote() { Platform = GeneratePackagePlatform.Mac };
  85. item.LanguageNotes.Add(new LanguageItem() { Language = "Chinese", Content = "这是Mac中文注释" });
  86. item.LanguageNotes.Add(new LanguageItem() { Language = "English", Content = "This is Mac English" });
  87. item.LanguageNotes.Add(new LanguageItem() { Language = "Portuguese", Content = "这是 Mac Portuguese" });
  88. item.LanguageNotes.Add(new LanguageItem() { Language = "Romanian", Content = "这是 Mac Romanian" });
  89. updateNote.Notes.Add(item);
  90. CreateNote(updateNote);
  91. }
  92. else
  93. {
  94. var text = File.ReadAllText(_notesPath);
  95. _notes = JsonConvert.DeserializeObject<IList<Note>>(text);
  96. if(File.Exists(_testNotesPath))
  97. {
  98. var textTestNotes = File.ReadAllText(_testNotesPath);
  99. _testNotes = JsonConvert.DeserializeObject<IList<Note>>(textTestNotes);
  100. }
  101. text = File.ReadAllText(_accountPath);
  102. _accounts = JsonConvert.DeserializeObject<IList<Account>>(text);
  103. }
  104. }
  105. /// <summary>
  106. /// get note with specified version
  107. /// </summary>
  108. /// <param name="version"></param>
  109. /// <returns></returns>
  110. internal Note GetNote(string version, ProductType productType)
  111. {
  112. lock (_dbLock)
  113. {
  114. var result = _notes.FirstOrDefault(m => m.Version == version && m.ProductType == productType);
  115. if (result == null)
  116. {
  117. return _testNotes.FirstOrDefault(m => m.Version == version && m.ProductType == productType);
  118. }
  119. else {
  120. return result;
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// get note with specified id
  126. /// </summary>
  127. /// <param name="id"></param>
  128. /// <returns></returns>
  129. internal Note GetNoteWithId(string id)
  130. {
  131. lock (_dbLock)
  132. {
  133. return _notes.FirstOrDefault(m => m.Id == id);
  134. }
  135. }
  136. /// <summary>
  137. /// get note with specified id
  138. /// </summary>
  139. /// <param name="id"></param>
  140. /// <returns></returns>
  141. internal Note GetTestNoteWithId(string id)
  142. {
  143. lock (_dbLock)
  144. {
  145. return _testNotes.FirstOrDefault(m => m.Id == id);
  146. }
  147. }
  148. /// <summary>
  149. /// Delete note with specified id
  150. /// </summary>
  151. /// <param name="id"></param>
  152. internal void DeleteNote(string id)
  153. {
  154. lock (_dbLock)
  155. {
  156. var note = _notes.FirstOrDefault(m => m.Id == id);
  157. if (note != null)
  158. {
  159. _notes.Remove(note);
  160. SaveNotes();
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// Delete test note with specified id
  166. /// </summary>
  167. /// <param name="id"></param>
  168. internal void DeleteTestNote(string id)
  169. {
  170. lock (_dbLock)
  171. {
  172. var note = _testNotes.FirstOrDefault(m => m.Id == id);
  173. if (note != null)
  174. {
  175. _testNotes.Remove(note);
  176. SaveTestNotes();
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Create or update account
  182. /// </summary>
  183. /// <param name="model"></param>
  184. public void CreateOrUpdateAccount(Account model)
  185. {
  186. lock (_dbLock)
  187. {
  188. var now = DateTime.Now;
  189. var existingItem = _accounts.FirstOrDefault(m => m.Id == model.Id);
  190. if (existingItem == null)
  191. {
  192. model.CreateTime = now;
  193. _accounts.Add(model);
  194. }
  195. else
  196. {
  197. var index = _accounts.IndexOf(existingItem);
  198. _accounts.RemoveAt(index);
  199. _accounts.Insert(index, model);
  200. }
  201. model.UpdateTime = now;
  202. SaveAccounts();
  203. }
  204. }
  205. private void SaveAccounts()
  206. {
  207. var content = JsonConvert.SerializeObject(_accounts);
  208. File.WriteAllText(_accountPath, content);
  209. }
  210. /// <summary>
  211. /// Create or update a Update Note
  212. /// </summary>
  213. /// <param name="model"></param>
  214. public bool CreateNote(Note model)
  215. {
  216. lock (_dbLock)
  217. {
  218. var now = DateTime.Now;
  219. var existingItem = _notes.FirstOrDefault(m => m.Version == model.Version && m.ProductType == model.ProductType);
  220. if (existingItem == null)
  221. {
  222. model.CreateTime = now;
  223. _notes.Add(model);
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. model.UpdateTime = now;
  230. SaveNotes();
  231. return true;
  232. }
  233. }
  234. /// <summary>
  235. /// Create a test Note
  236. /// </summary>
  237. /// <param name="model"></param>
  238. public bool CreateTestNote(Note model)
  239. {
  240. lock (_dbLock)
  241. {
  242. var now = DateTime.Now;
  243. var existingItem = _testNotes.FirstOrDefault(m => m.Version == model.Version && m.ProductType == model.ProductType);
  244. if (existingItem == null)
  245. {
  246. model.CreateTime = now;
  247. _testNotes.Add(model);
  248. }
  249. else
  250. {
  251. return false;
  252. }
  253. model.UpdateTime = now;
  254. SaveTestNotes();
  255. return true;
  256. }
  257. }
  258. /// <summary>
  259. /// Create or update a Update Note
  260. /// </summary>
  261. /// <param name="model"></param>
  262. public void UpdateNote(Note model)
  263. {
  264. lock (_dbLock)
  265. {
  266. var now = DateTime.Now;
  267. var existingItem = _notes.FirstOrDefault(m => m.Id == model.Id);
  268. if (existingItem == null)
  269. {
  270. return;
  271. }
  272. else
  273. {
  274. model.UpdateTime = now;
  275. var index = _notes.IndexOf(existingItem);
  276. _notes.RemoveAt(index);
  277. _notes.Insert(index, model);
  278. }
  279. SaveNotes();
  280. }
  281. }
  282. private void SaveNotes()
  283. {
  284. var content = JsonConvert.SerializeObject(_notes);
  285. File.WriteAllText(_notesPath, content);
  286. }
  287. private void SaveTestNotes()
  288. {
  289. var content = JsonConvert.SerializeObject(_testNotes);
  290. File.WriteAllText(_testNotesPath, content);
  291. }
  292. }
  293. }