123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- /* ==============================================================================
- * 功能描述:数据库对应的实体类
- * 创 建 者:Garrett
- * 创建日期:2017/10/12 9:49:22
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Xml.Serialization;
- using Newtonsoft.Json.Linq;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Serializer;
- using SAGA.DotNetUtils.WPF;
- using SAGA.MBI.Common;
- using SAGA.MBI.FileStream;
- using SAGA.MBI.RequestData;
- using SAGA.MBI.Tools;
- namespace SAGA.MBI.Model
- {
- [Serializable]
- public class MFileBase : BaseViewModelStub
- {
- public MFileBase()
- {
- PropertyChanged += MFileBase_PropertyChanged;
- State = false;
- }
- #region CloudProperty
- private string m_Id;
- public string Id
- {
- get { return m_Id; }
- set
- {
- m_Id = value;
- NotifyPropertyChanged("Id");
- }
- }
- private string m_Name;
- public string Name
- {
- get { return m_Name; }
- set
- {
- m_Name = value;
- NotifyPropertyChanged("Name");
- }
- }
- private string m_LocalId;
- public string LocalId
- {
- get { return m_LocalId; }
- set
- {
- m_LocalId = value;
- NotifyPropertyChanged("LocalId");
- State = true;
- }
- }
- private string m_LocalName;
- public string LocalName
- {
- get { return m_LocalName; }
- set
- {
- m_LocalName = value;
- NotifyPropertyChanged("LocalName");
- }
- }
- #endregion Model
- #region AttatchProperty
- private string _BeCopyPath;
- /// <summary>
- /// 准备copy revit文件的名称
- /// </summary>
- public string BeCopyPath
- {
- set
- {
- _BeCopyPath = value;
- NotifyPropertyChanged(nameof(BeCopyPath));
- }
- get { return _BeCopyPath; }
- }
- private bool _state;
- /// <summary>
- ///
- /// </summary>
- public bool State
- {
- set
- {
- _state = value;
- NotifyPropertyChanged("State");
- }
- get { return _state; }
- }
- /// <summary>
- /// 模型文件存放的文件夹
- /// </summary>
- public string LocalPath => GetLocalPath();
- private string _RelativePath;
- /// <summary>
- /// 相对路径
- /// </summary>
- public string RelativePath
- {
- set
- {
- _RelativePath = value;
- }
- get { return _RelativePath; }
- }
- private string _PathDisplay;
- /// <summary>
- /// 显示的revit文件的名称
- /// </summary>
- public string PathDisplay
- {
- set
- {
- _PathDisplay = value;
- NotifyPropertyChanged("PathDisplay");
- }
- get { return _PathDisplay; }
- }
- #endregion
- #region UserControl
- [NonSerialized]
- [XmlIgnore]
- private UserControl m_UserControlUp;
- [XmlIgnore]
- public UserControl UserControlUp
- {
- get { return m_UserControlUp; }
- set { m_UserControlUp = value; }
- }
- [NonSerialized]
- [XmlIgnore]
- public UserControl m_UserControlDown;
- [XmlIgnore]
- public UserControl UserControlDown
- {
- get { return m_UserControlDown; }
- set { m_UserControlDown = value; }
- }
- #endregion
- #region Method
- private void MFileBase_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- string name = e.PropertyName;
- //忽略通知的属性列表
- List<string> list = new List<string>();
- list.Add(nameof(State));
- list.Add(nameof(TvItemName));
- list.Add("IsMissFile");
- list.Add(nameof(PathDisplay));
- list.Add("TipLabelText");
- list.Add("FloorLock");
- if (list.Contains(name)) return;
- this.State = true;
- }
- private string m_TvItemName;
- public string TvItemName
- {
- get
- {
- string str = null;
- if (this is MFloor floor)
- {
- str = floor.ToStringCheckLock;
- }
- else
- {
- str = ToString();
- }
- return str;
- }
- set
- {
- m_TvItemName = value;
- NotifyPropertyChanged("TvItemName");
- }
- }
- public override string ToString()
- {
- var instanceInfo = this;
- string result = instanceInfo.LocalName;
- if (result.IsNotNullEmpty())
- return result;
- result = instanceInfo.Name;
- if (result.IsNotNullEmpty())
- return result;
- return "未知";
- }
- protected object m_MFileBaseBak;
- /// <summary>
- /// 类备份,用于取消更改
- /// </summary>
- /// <returns></returns>
- public virtual void Clone()
- {
- //FileOperateBase.Serialize(MBIConst.MBITempSettingPath, "CloneFile.xml", this);
- //Thread.Sleep(100);
- //m_MFileBaseBak = FileOperateBase.Deserialize<MFileBase>(MBIConst.MBITempSettingPath, "CloneFile.xml");
- //BinaryFormatter bf = new BinaryFormatter();
- //MemoryStream ms = new MemoryStream();
- //bf.Serialize(ms, this); //复制到流中
- //ms.Position = 0;
- //m_MFileBaseBak = (bf.Deserialize(ms));
- m_MFileBaseBak = SerializerByXml.Clone<MFileBase>(this);
- }
- /// <summary>
- /// 放弃修改
- /// </summary>
- /// <param name="bakBase"></param>
- private void ReleaseClone()
- {
- if (m_MFileBaseBak == null) return;
- var properties = m_MFileBaseBak.GetType().GetProperties();
- List<string> ignoreList = new List<string>();
- ignoreList.Add(nameof(UserControlUp));
- ignoreList.Add(nameof(UserControlDown));
- foreach (PropertyInfo info in properties)
- {
- var descriptionAttribute = info.GetCustomAttribute(typeof(XmlIgnoreAttribute));
- if (descriptionAttribute != null) continue;
- if (ignoreList.Contains(info.Name)) continue;
- if (info.GetSetMethod() == null) continue;
- info.SetValue(this, info.GetValue(m_MFileBaseBak));
- }
- this.State = false;
- }
- /// <summary>
- /// 文件路径改变了
- /// </summary>
- /// <returns></returns>
- public bool IsPathChanged()
- {
- if (!State || m_MFileBaseBak == null) return false;
- var bakFileBase = (MFileBase)m_MFileBaseBak;
- return bakFileBase.BeCopyPath.IsNotNullEmpty();
- }
- /// <summary>
- /// 属性改变了
- /// </summary>
- /// <returns></returns>
- public bool IsPropertyChanged()
- {
- bool result = false;
- if (!State || m_MFileBaseBak == null) return false;
- var properties = m_MFileBaseBak.GetType().GetProperties();
- foreach (PropertyInfo info in properties)
- {
- if (info.Name == nameof(PathDisplay) || info.Name == nameof(State)) continue;
- var bakValue = info.GetValue(m_MFileBaseBak);
- var value = info.GetValue(this);
- if (bakValue == null && value == null) continue;
- if (bakValue != null && value != null)
- {
- if (!bakValue.ToString().Equals(value.ToString()))
- {
- result = true;
- break;
- }
- }
- else
- {
- result = true;
- break;
- }
- }
- return result;
- }
- /// <summary>
- /// 获取更新的字符串
- /// </summary>
- /// <returns></returns>
- public string GetUpdateDTEJson(string datatime)
- {
- JObject idJObject = new JObject();
- idJObject.Add("id", this.Id);
- idJObject.Add("infos", this.GetJsonProperty());
- JArray idJArray = new JArray(idJObject);
- JObject updateJObject = new JObject();
- updateJObject.Add("criterias", idJArray);
- return updateJObject.ToString();
- }
- #endregion
- #region Virtual Method
- public virtual string GetLocalPath()
- {
- return Path.Combine(MBIControl.ProjectBasePath, RelativePath);
- }
- public virtual bool Save()
- {
- try
- {
- //if (IsPropertyChanged())
- {
- CommonConvert.UpdateInfosSigle(Id, GetJsonProperty());
- }
- BeCopyPath = "";
- State = false;
- this.Clone();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- return false;
- }
- return true;
- }
- public virtual bool Cancel()
- {
- try
- {
- ReleaseClone();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- return false;
- }
- return true;
- }
- /// <summary>
- /// 从云平台读取数据,对本地数据赋值
- /// </summary>
- /// <param name="json"></param>
- public virtual void SetJsonProperty(string json)
- {
- this.ReflectSetPropertyValue(json, (values, key) =>
- {
- JObject jObject = JObject.Parse(values.ToString());
- return jObject.GetValueEx(key);
- });
- }
- /// <summary>
- /// 将本地数据整理是json格式,待上传
- /// </summary>
- /// <returns></returns>
- public virtual JObject GetJsonProperty()
- {
- JObject jObject = new JObject();
- foreach (PropertyInfo info in this.GetType().GetProperties())
- {
- var attributes = info.GetCustomAttributes(typeof(DescriptionAttribute));
- var att = attributes.FirstOrDefault();
- if (att is DescriptionAttribute descriptionAttribute)
- {
- var description = descriptionAttribute.Description;
- if (description.IsNotNullEmpty())
- {
- object value = info.GetValue(this);
- if (info.PropertyType == typeof(int))
- value = ((int)value).ToString();
- if (value != null && (string)value != "")
- {
- JArray jArray = CommonTool.GetPropertyJArray((string)value);
- jObject.Add(description, jArray);
- }
- }
- }
- }
- return jObject;
- }
- #endregion
- }
- }
|