using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using vCloud.GeneratePackages.Entitys; using vCloud.GeneratePackages.Utilities; using Vinno.IUS.Product.Notes.Data; namespace Vinno.IUS.Product.Notes.Service { class DataAccessor { private static DataAccessor _instance; private readonly object _dbLock = new object(); private string _notesPath; private string _testNotesPath; private IList _notes = new List(); private IList _testNotes = new List(); private IList _accounts = new List(); private string _accountPath; /// /// Notes /// public IList Notes => _notes.ToList(); /// /// Test Notes /// public IList TestNotes => _testNotes.ToList(); /// /// Acccounts /// public IList Accounts => _accounts.ToList(); public static DataAccessor Instance => _instance ?? (_instance = new DataAccessor()); /// /// Initialize db /// public void Initialize() { var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } const string dbName = "Notes.data"; _notesPath = Path.Combine(folder, dbName); var dbExist = File.Exists(_notesPath); const string accountName = "Accounts.data"; _accountPath = Path.Combine(folder, accountName); const string testNotesFileName = "TestNotes.data"; _testNotesPath = Path.Combine(folder, testNotesFileName); if (!dbExist) { var account = new Account() { Name = "Loki", Password = "vinno@123" }; CreateOrUpdateAccount(account); var warr = new Account() { Name = "Warr", Password = "vinno@123" }; CreateOrUpdateAccount(warr); var yoyi = new Account() { Name = "Yori", Password = "vinno@123" }; CreateOrUpdateAccount(yoyi); var jimmy = new Account() { Name = "Jimmy", Password = "vinno@123" }; CreateOrUpdateAccount(jimmy); var melon = new Account() { Name = "Melon", Password = "vinno@123" }; CreateOrUpdateAccount(melon); var july = new Account() { Name = "July", Password = "vinno@123" }; CreateOrUpdateAccount(july); var hayley = new Account() { Name = "Hayley", Password = "vinno@123" }; CreateOrUpdateAccount(hayley); var updateNote = new Note(); updateNote.Version = "1.7.4.16355"; updateNote.InternalRecord = "1. 教师端直播改成混流模式2.将官网PC端下载链接从live引擎包改成完整包"; updateNote.Publisher = new AccountInfo() { Id = account.Id, Name = account.Name }; var item = new UpdatePlatformNote() { Platform = GeneratePackagePlatform.PC }; item.LanguageNotes.Add(new LanguageItem() { Language = "Chinese", Content = "这是PC中文注释" }); item.LanguageNotes.Add(new LanguageItem() { Language = "English", Content = "This is PC English" }); item.LanguageNotes.Add(new LanguageItem() { Language = "Portuguese", Content = "这是 PC Portuguese" }); item.LanguageNotes.Add(new LanguageItem() { Language = "Romanian", Content = "这是 PC Romanian" }); updateNote.Notes.Add(item); item = new UpdatePlatformNote() { Platform = GeneratePackagePlatform.Android }; item.LanguageNotes.Add(new LanguageItem() { Language = "Chinese", Content = "这是Android中文注释" }); item.LanguageNotes.Add(new LanguageItem() { Language = "English", Content = "This is Android English" }); item.LanguageNotes.Add(new LanguageItem() { Language = "Portuguese", Content = "这是 Android Portuguese" }); item.LanguageNotes.Add(new LanguageItem() { Language = "Romanian", Content = "这是 Android Romanian" }); updateNote.Notes.Add(item); item = new UpdatePlatformNote() { Platform = GeneratePackagePlatform.Mac }; item.LanguageNotes.Add(new LanguageItem() { Language = "Chinese", Content = "这是Mac中文注释" }); item.LanguageNotes.Add(new LanguageItem() { Language = "English", Content = "This is Mac English" }); item.LanguageNotes.Add(new LanguageItem() { Language = "Portuguese", Content = "这是 Mac Portuguese" }); item.LanguageNotes.Add(new LanguageItem() { Language = "Romanian", Content = "这是 Mac Romanian" }); updateNote.Notes.Add(item); CreateNote(updateNote); } else { var text = File.ReadAllText(_notesPath); _notes = JsonConvert.DeserializeObject>(text); if(File.Exists(_testNotesPath)) { var textTestNotes = File.ReadAllText(_testNotesPath); _testNotes = JsonConvert.DeserializeObject>(textTestNotes); } text = File.ReadAllText(_accountPath); _accounts = JsonConvert.DeserializeObject>(text); } } /// /// get note with specified version /// /// /// internal Note GetNote(string version, ProductType productType) { lock (_dbLock) { var result = _notes.FirstOrDefault(m => m.Version == version && m.ProductType == productType); if (result == null) { return _testNotes.FirstOrDefault(m => m.Version == version && m.ProductType == productType); } else { return result; } } } /// /// get note with specified id /// /// /// internal Note GetNoteWithId(string id) { lock (_dbLock) { return _notes.FirstOrDefault(m => m.Id == id); } } /// /// get note with specified id /// /// /// internal Note GetTestNoteWithId(string id) { lock (_dbLock) { return _testNotes.FirstOrDefault(m => m.Id == id); } } /// /// Delete note with specified id /// /// internal void DeleteNote(string id) { lock (_dbLock) { var note = _notes.FirstOrDefault(m => m.Id == id); if (note != null) { _notes.Remove(note); SaveNotes(); } } } /// /// Delete test note with specified id /// /// internal void DeleteTestNote(string id) { lock (_dbLock) { var note = _testNotes.FirstOrDefault(m => m.Id == id); if (note != null) { _testNotes.Remove(note); SaveTestNotes(); } } } /// /// Create or update account /// /// public void CreateOrUpdateAccount(Account model) { lock (_dbLock) { var now = DateTime.Now; var existingItem = _accounts.FirstOrDefault(m => m.Id == model.Id); if (existingItem == null) { model.CreateTime = now; _accounts.Add(model); } else { var index = _accounts.IndexOf(existingItem); _accounts.RemoveAt(index); _accounts.Insert(index, model); } model.UpdateTime = now; SaveAccounts(); } } private void SaveAccounts() { var content = JsonConvert.SerializeObject(_accounts); File.WriteAllText(_accountPath, content); } /// /// Create or update a Update Note /// /// public bool CreateNote(Note model) { lock (_dbLock) { var now = DateTime.Now; var existingItem = _notes.FirstOrDefault(m => m.Version == model.Version && m.ProductType == model.ProductType); if (existingItem == null) { model.CreateTime = now; _notes.Add(model); } else { return false; } model.UpdateTime = now; SaveNotes(); return true; } } /// /// Create a test Note /// /// public bool CreateTestNote(Note model) { lock (_dbLock) { var now = DateTime.Now; var existingItem = _testNotes.FirstOrDefault(m => m.Version == model.Version && m.ProductType == model.ProductType); if (existingItem == null) { model.CreateTime = now; _testNotes.Add(model); } else { return false; } model.UpdateTime = now; SaveTestNotes(); return true; } } /// /// Create or update a Update Note /// /// public void UpdateNote(Note model) { lock (_dbLock) { var now = DateTime.Now; var existingItem = _notes.FirstOrDefault(m => m.Id == model.Id); if (existingItem == null) { return; } else { model.UpdateTime = now; var index = _notes.IndexOf(existingItem); _notes.RemoveAt(index); _notes.Insert(index, model); } SaveNotes(); } } private void SaveNotes() { var content = JsonConvert.SerializeObject(_notes); File.WriteAllText(_notesPath, content); } private void SaveTestNotes() { var content = JsonConvert.SerializeObject(_testNotes); File.WriteAllText(_testNotesPath, content); } } }