DbViewerModel.cs 3.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
{
    public class DbViewerModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath;

        public List<DbProfilePack> ProfilePacks { get; } = new List<DbProfilePack>();
22 23 24
        /// <summary>
        /// 数据准备好了
        /// </summary>
25
        public bool IsPackReady { get; private set; }
26

27 28
        public DateTime PackBeginDate { get; private set; }
        public DateTime PackEndDate { get; private set; }
29 30 31 32

        /// <summary>
        /// ProfilePacks.Count()
        /// </summary>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        public int PackCnt { get; private set; }


        /// <summary>
        /// 只统计运行中的数据
        /// </summary>
        public bool IsJustRunning { get; set; } = false;
        


        public double Step { get; set; } = 1;

        public DbHelper mDbHelper;
        public DbViewerModel()
        {
        }
        public void Init()
        {
            if (!Load())
                Save();
            mDbHelper = new DbHelper();
            mDbHelper.Init(DbDirPath, null);
        }
        public bool GetProfilePacks(List<Db_Profile> profiles)
        {

            ProfilePacks.Clear();
            foreach (var db_profile in profiles)
            {
                var profilePack = mDbHelper.GetProfilePack(db_profile);
                profilePack.Init(IsJustRunning);
                if (profilePack.ScanDatas.Count > 0) 
                {
                    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<DbViewerModel, DbViewerModelJsonDb>().ReverseMap();
        }));
        public static bool Save(DbViewerModel src, string filePath)
        {
            var p = Mapper.Map<DbViewerModelJsonDb>(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<DbViewerModelJsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常,没有json 解码失败
            }
            return false;
        }
        public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath;

        public double Step = 1;

        public double YRangePercent = 2;

        public bool IsJustRunning = false;
    }
}