using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; namespace Mogafa.App.AppDatas { public delegate void AppDataChangedHandler(string code, long newValue, long oldValue); public class UserAppData { public event AppDataChangedHandler AppDataChanged; [JsonProperty("userId")] public string UserId { get; set; } [JsonProperty("appDatas")] public List AppDatas { get; set; } [JsonProperty("records")] public List Records { get; set; } public UserAppData() { AppDatas = new List(); Records = new List(); } public long GetAppData(string code) { var appData = AppDatas.FirstOrDefault(ad => ad.Code == code); if(appData == null) { return 0L; } return appData.Value; } public void Set(string code, long value, string source, string sourceId) { var appData = AppDatas.FirstOrDefault(a => a.Code == code); if (appData == null) { appData = new AppData { Code = code, Value = 0L }; AppDatas.Add(appData); } var oldValue = appData.Value; if(Records == null) { Records = new List(); } Records.Add(new UserAppDataRecord { Code = code, Source = source, SourceId = sourceId, Time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000, UserId = UserId, Value = value, ValueOfBefore = oldValue }); appData.Value = value; AppDataChanged?.Invoke(code, appData.Value, oldValue); return; } } }