using AutoMapper; using FLY.Thick.FilmCasting.Server.Model; using FLY.Thick.FilmCasting.UI.DbViewer.Db; using Misc; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; namespace FLY.Thick.FilmCasting.UI.DbViewer.Core { //整个DbViewer软件的运行核心 public class DbViewerModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public List ProfilePacks { get; } = new List(); /// /// 数据准备好了 /// public bool IsPackReady { get; private set; } public DateTime PackBeginDate { get; private set; } public DateTime PackEndDate { get; private set; } /// /// ProfilePacks.Count() /// public int PackCnt { get; private set; } /// /// 数据库根目录路径 /// public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath; /// /// 直方图统计步距 1um /// public int Step { get; set; } = 1; /// /// 扫描图显示的范围比例 3倍 /// public double YRangePercent { get; set; } = 3; /// /// 混合数 /// public int Mix { get; set; } = 4; public DbHelper mDbHelper; public DbViewerModel() { } public void Init() { if (!Load()) Save(); mDbHelper = new DbHelper(); mDbHelper.Init(DbDirPath, null); } public bool GetProfilePacks(List profiles) { ProfilePacks.Clear(); foreach (var db_profile in profiles) { var profilePack = mDbHelper.GetProfilePack(db_profile); profilePack.Mix = Mix; profilePack.YRangePercent = YRangePercent; profilePack.Step = Step; if (profilePack.Init()) { ProfilePacks.Add(profilePack); } } if (ProfilePacks.Count() > 0) { IsPackReady = true; PackCnt = ProfilePacks.Count(); PackBeginDate = ProfilePacks.First().Profile.StartTime; PackEndDate = ProfilePacks.Last().Profile.EndTime; return true; } else { IsPackReady = false; return false; } } private string filePath = "param.json"; bool Load() { return DbViewerModelJsonDb.Load(this, filePath); } public bool Save() { return DbViewerModelJsonDb.Save(this, filePath); } } public class DbViewerModelJsonDb { static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c => { c.CreateMap().ReverseMap(); })); public static bool Save(DbViewerModel src, string filePath) { var p = Mapper.Map(src); try { File.WriteAllText(filePath, JsonConvert.SerializeObject(p, Formatting.Indented)); } catch { //异常,没有json 编码失败 return false; } return true; } public static bool Load(DbViewerModel src, string filePath) { try { if (File.Exists(filePath)) { string json = File.ReadAllText(filePath); var p = JsonConvert.DeserializeObject(json); Mapper.Map(p, src); return true; } } catch { //异常,没有json 解码失败 } return false; } /// /// 数据库根目录路径 /// public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath; /// /// 直方图统计步距 1um /// public int Step = 1; /// /// 扫描图显示的范围比例 3倍 /// public double YRangePercent = 3; /// /// 混合数 /// public int Mix { get; set; } = 4; } }