MFileBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* ==============================================================================
  2. * 功能描述:数据库对应的实体类
  3. * 创 建 者:Garrett
  4. * 创建日期:2017/10/12 9:49:22
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using System.Threading;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Xml.Serialization;
  17. using Newtonsoft.Json.Linq;
  18. using SAGA.DotNetUtils;
  19. using SAGA.DotNetUtils.Extend;
  20. using SAGA.DotNetUtils.Serializer;
  21. using SAGA.DotNetUtils.WPF;
  22. using SAGA.MBI.Common;
  23. using SAGA.MBI.FileStream;
  24. using SAGA.MBI.RequestData;
  25. using SAGA.MBI.Tools;
  26. namespace SAGA.MBI.Model
  27. {
  28. [Serializable]
  29. public class MFileBase : BaseViewModelStub
  30. {
  31. public MFileBase()
  32. {
  33. PropertyChanged += MFileBase_PropertyChanged;
  34. State = false;
  35. }
  36. #region CloudProperty
  37. private string m_Id;
  38. public string Id
  39. {
  40. get { return m_Id; }
  41. set
  42. {
  43. m_Id = value;
  44. NotifyPropertyChanged("Id");
  45. }
  46. }
  47. private string m_Name;
  48. public string Name
  49. {
  50. get { return m_Name; }
  51. set
  52. {
  53. m_Name = value;
  54. NotifyPropertyChanged("Name");
  55. }
  56. }
  57. private string m_LocalId;
  58. public string LocalId
  59. {
  60. get { return m_LocalId; }
  61. set
  62. {
  63. m_LocalId = value;
  64. NotifyPropertyChanged("LocalId");
  65. State = true;
  66. }
  67. }
  68. private string m_LocalName;
  69. public string LocalName
  70. {
  71. get { return m_LocalName; }
  72. set
  73. {
  74. m_LocalName = value;
  75. NotifyPropertyChanged("LocalName");
  76. }
  77. }
  78. #endregion Model
  79. #region AttatchProperty
  80. private string _BeCopyPath;
  81. /// <summary>
  82. /// 准备copy revit文件的名称
  83. /// </summary>
  84. public string BeCopyPath
  85. {
  86. set
  87. {
  88. _BeCopyPath = value;
  89. NotifyPropertyChanged(nameof(BeCopyPath));
  90. }
  91. get { return _BeCopyPath; }
  92. }
  93. private bool _state;
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. public bool State
  98. {
  99. set
  100. {
  101. _state = value;
  102. NotifyPropertyChanged("State");
  103. }
  104. get { return _state; }
  105. }
  106. /// <summary>
  107. /// 模型文件存放的文件夹
  108. /// </summary>
  109. public string LocalPath => GetLocalPath();
  110. private string _RelativePath;
  111. /// <summary>
  112. /// 相对路径
  113. /// </summary>
  114. public string RelativePath
  115. {
  116. set
  117. {
  118. _RelativePath = value;
  119. }
  120. get { return _RelativePath; }
  121. }
  122. private string _PathDisplay;
  123. /// <summary>
  124. /// 显示的revit文件的名称
  125. /// </summary>
  126. public string PathDisplay
  127. {
  128. set
  129. {
  130. _PathDisplay = value;
  131. NotifyPropertyChanged("PathDisplay");
  132. }
  133. get { return _PathDisplay; }
  134. }
  135. #endregion
  136. #region UserControl
  137. [NonSerialized]
  138. [XmlIgnore]
  139. private UserControl m_UserControlUp;
  140. [XmlIgnore]
  141. public UserControl UserControlUp
  142. {
  143. get { return m_UserControlUp; }
  144. set { m_UserControlUp = value; }
  145. }
  146. [NonSerialized]
  147. [XmlIgnore]
  148. public UserControl m_UserControlDown;
  149. [XmlIgnore]
  150. public UserControl UserControlDown
  151. {
  152. get { return m_UserControlDown; }
  153. set { m_UserControlDown = value; }
  154. }
  155. #endregion
  156. #region Method
  157. private void MFileBase_PropertyChanged(object sender, PropertyChangedEventArgs e)
  158. {
  159. string name = e.PropertyName;
  160. //忽略通知的属性列表
  161. List<string> list = new List<string>();
  162. list.Add(nameof(State));
  163. list.Add(nameof(TvItemName));
  164. list.Add("IsMissFile");
  165. list.Add(nameof(PathDisplay));
  166. list.Add("TipLabelText");
  167. list.Add("FloorLock");
  168. if (list.Contains(name)) return;
  169. this.State = true;
  170. }
  171. private string m_TvItemName;
  172. public string TvItemName
  173. {
  174. get
  175. {
  176. string str = null;
  177. if (this is MFloor floor)
  178. {
  179. str = floor.ToStringCheckLock;
  180. }
  181. else
  182. {
  183. str = ToString();
  184. }
  185. return str;
  186. }
  187. set
  188. {
  189. m_TvItemName = value;
  190. NotifyPropertyChanged("TvItemName");
  191. }
  192. }
  193. public override string ToString()
  194. {
  195. var instanceInfo = this;
  196. string result = instanceInfo.LocalName;
  197. if (result.IsNotNullEmpty())
  198. return result;
  199. result = instanceInfo.Name;
  200. if (result.IsNotNullEmpty())
  201. return result;
  202. return "未知";
  203. }
  204. protected object m_MFileBaseBak;
  205. /// <summary>
  206. /// 类备份,用于取消更改
  207. /// </summary>
  208. /// <returns></returns>
  209. public virtual void Clone()
  210. {
  211. //FileOperateBase.Serialize(MBIConst.MBITempSettingPath, "CloneFile.xml", this);
  212. //Thread.Sleep(100);
  213. //m_MFileBaseBak = FileOperateBase.Deserialize<MFileBase>(MBIConst.MBITempSettingPath, "CloneFile.xml");
  214. //BinaryFormatter bf = new BinaryFormatter();
  215. //MemoryStream ms = new MemoryStream();
  216. //bf.Serialize(ms, this); //复制到流中
  217. //ms.Position = 0;
  218. //m_MFileBaseBak = (bf.Deserialize(ms));
  219. m_MFileBaseBak = SerializerByXml.Clone<MFileBase>(this);
  220. }
  221. /// <summary>
  222. /// 放弃修改
  223. /// </summary>
  224. /// <param name="bakBase"></param>
  225. private void ReleaseClone()
  226. {
  227. if (m_MFileBaseBak == null) return;
  228. var properties = m_MFileBaseBak.GetType().GetProperties();
  229. List<string> ignoreList = new List<string>();
  230. ignoreList.Add(nameof(UserControlUp));
  231. ignoreList.Add(nameof(UserControlDown));
  232. foreach (PropertyInfo info in properties)
  233. {
  234. var descriptionAttribute = info.GetCustomAttribute(typeof(XmlIgnoreAttribute));
  235. if (descriptionAttribute != null) continue;
  236. if (ignoreList.Contains(info.Name)) continue;
  237. if (info.GetSetMethod() == null) continue;
  238. info.SetValue(this, info.GetValue(m_MFileBaseBak));
  239. }
  240. this.State = false;
  241. }
  242. /// <summary>
  243. /// 文件路径改变了
  244. /// </summary>
  245. /// <returns></returns>
  246. public bool IsPathChanged()
  247. {
  248. if (!State || m_MFileBaseBak == null) return false;
  249. var bakFileBase = (MFileBase)m_MFileBaseBak;
  250. return bakFileBase.BeCopyPath.IsNotNullEmpty();
  251. }
  252. /// <summary>
  253. /// 属性改变了
  254. /// </summary>
  255. /// <returns></returns>
  256. public bool IsPropertyChanged()
  257. {
  258. bool result = false;
  259. if (!State || m_MFileBaseBak == null) return false;
  260. var properties = m_MFileBaseBak.GetType().GetProperties();
  261. foreach (PropertyInfo info in properties)
  262. {
  263. if (info.Name == nameof(PathDisplay) || info.Name == nameof(State)) continue;
  264. var bakValue = info.GetValue(m_MFileBaseBak);
  265. var value = info.GetValue(this);
  266. if (bakValue == null && value == null) continue;
  267. if (bakValue != null && value != null)
  268. {
  269. if (!bakValue.ToString().Equals(value.ToString()))
  270. {
  271. result = true;
  272. break;
  273. }
  274. }
  275. else
  276. {
  277. result = true;
  278. break;
  279. }
  280. }
  281. return result;
  282. }
  283. /// <summary>
  284. /// 获取更新的字符串
  285. /// </summary>
  286. /// <returns></returns>
  287. public string GetUpdateDTEJson(string datatime)
  288. {
  289. JObject idJObject = new JObject();
  290. idJObject.Add("id", this.Id);
  291. idJObject.Add("infos", this.GetJsonProperty());
  292. JArray idJArray = new JArray(idJObject);
  293. JObject updateJObject = new JObject();
  294. updateJObject.Add("criterias", idJArray);
  295. return updateJObject.ToString();
  296. }
  297. #endregion
  298. #region Virtual Method
  299. public virtual string GetLocalPath()
  300. {
  301. return Path.Combine(MBIControl.ProjectBasePath, RelativePath);
  302. }
  303. public virtual bool Save()
  304. {
  305. try
  306. {
  307. //if (IsPropertyChanged())
  308. {
  309. CommonConvert.UpdateInfosSigle(Id, GetJsonProperty());
  310. }
  311. BeCopyPath = "";
  312. State = false;
  313. this.Clone();
  314. }
  315. catch (Exception e)
  316. {
  317. Console.WriteLine(e);
  318. return false;
  319. }
  320. return true;
  321. }
  322. public virtual bool Cancel()
  323. {
  324. try
  325. {
  326. ReleaseClone();
  327. }
  328. catch (Exception e)
  329. {
  330. Console.WriteLine(e);
  331. return false;
  332. }
  333. return true;
  334. }
  335. /// <summary>
  336. /// 从云平台读取数据,对本地数据赋值
  337. /// </summary>
  338. /// <param name="json"></param>
  339. public virtual void SetJsonProperty(string json)
  340. {
  341. this.ReflectSetPropertyValue(json, (values, key) =>
  342. {
  343. JObject jObject = JObject.Parse(values.ToString());
  344. return jObject.GetValueEx(key);
  345. });
  346. }
  347. /// <summary>
  348. /// 将本地数据整理是json格式,待上传
  349. /// </summary>
  350. /// <returns></returns>
  351. public virtual JObject GetJsonProperty()
  352. {
  353. JObject jObject = new JObject();
  354. foreach (PropertyInfo info in this.GetType().GetProperties())
  355. {
  356. var attributes = info.GetCustomAttributes(typeof(DescriptionAttribute));
  357. var att = attributes.FirstOrDefault();
  358. if (att is DescriptionAttribute descriptionAttribute)
  359. {
  360. var description = descriptionAttribute.Description;
  361. if (description.IsNotNullEmpty())
  362. {
  363. object value = info.GetValue(this);
  364. if (info.PropertyType == typeof(int))
  365. value = ((int)value).ToString();
  366. if (value != null && (string)value != "")
  367. {
  368. JArray jArray = CommonTool.GetPropertyJArray((string)value);
  369. jObject.Add(description, jArray);
  370. }
  371. }
  372. }
  373. }
  374. return jObject;
  375. }
  376. #endregion
  377. }
  378. }