MFileBase.cs 12 KB

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