VmMaintainerProjectAsset.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. 
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //文件名称: VmMaintainerProjectAsset.cs
  4. //文件描述: 维修商项目资产
  5. //创 建 者: xls
  6. //创建日期: 2018/5/24 22:10:36
  7. //版 本 号:1.0.0.0
  8. ////////////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using CEFSharpWpf;
  17. using Com.FirmLib.Bll;
  18. using Com.FirmLib.Entity;
  19. using Com.FirmLib.UI.BllCommon;
  20. using Com.FirmLib.UI.Common;
  21. using Com.FirmLib.UI.Common.Model;
  22. using FWindSoft.DataFramework;
  23. using FWindSoft.MVVM;
  24. using FWindSoft.Wpf;
  25. using FWindSoft.Wpf.Common;
  26. using FWindSoft.Wpf.Controls;
  27. namespace Com.FirmLib.UI.Maintainer
  28. {
  29. public class VmMaintainerProjectAsset : BaseViewModel
  30. {
  31. public VmMaintainerProjectAsset()
  32. {
  33. Init();
  34. }
  35. private void Init()
  36. {
  37. ContactItem = new ContactInformationItem();
  38. }
  39. public void Init(LoadParameter parameter)
  40. {
  41. MaintainerId = parameter.Parameter as string;
  42. }
  43. #region 属性相关
  44. /// <summary>
  45. /// 维修商Id
  46. /// </summary>
  47. public string MaintainerId { get; private set; }
  48. private string m_EndDate;
  49. /// <summary>
  50. /// 截止日期
  51. /// </summary>
  52. public string EndDate
  53. {
  54. get { return this.m_EndDate; }
  55. set
  56. {
  57. this.m_EndDate = value;
  58. RaisePropertyChanged(() => this.EndDate);
  59. }
  60. }
  61. /// <summary>
  62. /// 有效的项目编号
  63. /// </summary>
  64. public bool ValidateProject { get; set; }
  65. private string m_ProjectNo;
  66. /// <summary>
  67. /// 项目编号
  68. /// </summary>
  69. public string ProjectNo
  70. {
  71. get { return this.m_ProjectNo; }
  72. set
  73. {
  74. this.m_ProjectNo = value;
  75. if (!IsInEditing)
  76. {
  77. LoadProjectItem(SystemProjectId);
  78. LoadEquipmentItems(SystemProjectId, MaintainerId);
  79. }
  80. RaisePropertyChanged(() => this.ProjectNo);
  81. }
  82. }
  83. /// <summary>
  84. /// 系统项目Id
  85. /// </summary>
  86. public string SystemProjectId
  87. {
  88. get { return ProjectIdManager.GetSystemProjectId(ProjectNo); }
  89. }
  90. private string m_LocalProjectName;
  91. /// <summary>
  92. /// 项目本地名称
  93. /// </summary>
  94. public string LocalProjectName
  95. {
  96. get { return this.m_LocalProjectName; }
  97. set
  98. {
  99. this.m_LocalProjectName = value;
  100. RaisePropertyChanged(() => this.LocalProjectName);
  101. }
  102. }
  103. private ContactInformationItem m_ContactItem;
  104. /// <summary>
  105. /// 联系人信息
  106. /// </summary>
  107. public ContactInformationItem ContactItem
  108. {
  109. get { return this.m_ContactItem; }
  110. set
  111. {
  112. this.m_ContactItem = value;
  113. RaisePropertyChanged(() => this.ContactItem);
  114. }
  115. }
  116. private ObservableCollection<AssetShowItem> m_RefAssetItems;
  117. /// <summary>
  118. /// 关联资产信息
  119. /// </summary>
  120. public ObservableCollection<AssetShowItem> RefAssetItems
  121. {
  122. get { return this.m_RefAssetItems; }
  123. set
  124. {
  125. this.m_RefAssetItems = value;
  126. RaisePropertyChanged(nameof(this.RefAssetItems));
  127. }
  128. }
  129. #endregion
  130. #region 命令相关
  131. /// <summary>
  132. /// 保存按钮
  133. /// </summary>
  134. /// <param name="parameter"></param>
  135. [Command]
  136. public void SaveCommand(object parameter)
  137. {
  138. if (CreateProjectInfo()&&CreateRefEquipment())
  139. {
  140. AfterSuccessSave();
  141. MessageTip.Show(this.GetWindow(), "保存成功");
  142. }
  143. else
  144. {
  145. MessageTip.Show(this.GetWindow(), "保存失败");
  146. }
  147. }
  148. public bool CanSaveCommand(object parameter)
  149. {
  150. return IsValidated&&ValidateProject;;
  151. }
  152. /// <summary>
  153. /// 选择设备
  154. /// </summary>
  155. /// <param name="parameter"></param>
  156. [Command]
  157. public void SelectAssetCommand(object parameter)
  158. {
  159. WinMaintainerAssetSelector win = new WinMaintainerAssetSelector();
  160. win.Owner = this.GetWindow();
  161. SelectAssetContext context = new SelectAssetContext();
  162. context.ProjectId = SystemProjectId;
  163. context.VenderId = MaintainerId;
  164. context.ContainIds = new List<string>(RefAssetItems.Select(p=>p.Id));
  165. LoadParameter.StartParameter(win, new LoadParameter(context));
  166. if (win.ShowDialog() == true)
  167. {
  168. if (RefAssetItems == null)
  169. {
  170. RefAssetItems = new ObservableCollection<AssetShowItem>();
  171. }
  172. var addRelations = context.AddedItems;
  173. foreach (var item in addRelations)
  174. {
  175. if (RefAssetItems.Any(c => c.Id == item.Id))
  176. return;
  177. RefAssetItems.Add(item);
  178. }
  179. }
  180. }
  181. public bool CanSelectAssetCommand(object parameter)
  182. {
  183. return ValidateProject;
  184. }
  185. /// <summary>
  186. /// 删除在保资产
  187. /// </summary>
  188. /// <param name="parameter"></param>
  189. [Command]
  190. public void DeleteCommand(object parameter)
  191. {
  192. AssetShowItem showItem = parameter as AssetShowItem;
  193. if (showItem == null)
  194. return;
  195. var result = MessageTip.Show(this.GetWindow(), "是否要删除", "提示", MessageBoxButton.YesNo);
  196. if (result == MessageBoxResult.Yes)
  197. {
  198. bool flag = false;
  199. if (showItem.MaintainerId == MaintainerId)
  200. {
  201. flag = AssetManager.DeleteMaintainerEquipments(SystemProjectId, new List<string>() { showItem.Id });
  202. }
  203. else
  204. {
  205. flag = true;
  206. }
  207. if (flag)
  208. {
  209. this.RefAssetItems.Remove(showItem);
  210. }
  211. else
  212. {
  213. MessageTip.Show(this.GetWindow(), "删除失败");
  214. }
  215. }
  216. }
  217. public bool CanDeleteCommand(object parameter)
  218. {
  219. return true;
  220. }
  221. /// <summary>
  222. /// 查看资产详情
  223. /// </summary>
  224. /// <param name="parameter"></param>
  225. [Command]
  226. public void ScanCommand(object parameter)
  227. {
  228. AssetShowItem showItem = parameter as AssetShowItem;
  229. if (showItem == null)
  230. {
  231. return;
  232. }
  233. AssetItem parItem = new AssetItem();
  234. parItem.Id = showItem.Id;
  235. parItem.ProjectId = showItem.Id;
  236. //获取三位编码
  237. parItem.FamilyCode = showItem.FamilyCode.Substring(2);
  238. string url = BllFactory<AssetBll>.Instance.GetEquipmentUrl(parItem);
  239. WinBrowser.Instance.Show(url, "设备信息");
  240. }
  241. public bool CanScanCommand(object parameter)
  242. {
  243. return true;
  244. }
  245. #endregion
  246. #region 私有方法
  247. /// <summary>
  248. /// 创建项目信息
  249. /// </summary>
  250. /// <returns></returns>
  251. private bool CreateProjectInfo()
  252. {
  253. LinkerItem item = new LinkerItem();
  254. item.VenderId = MaintainerId;
  255. item.ProjectId = SystemProjectId;
  256. item.Name = ContactItem.People;
  257. item.Phone = ContactItem.Telephone;
  258. item.Fax = ContactItem.Fax;
  259. item.Email = ContactItem.Email;
  260. var result = BllFactory<MaintainerBll>.Instance.UpdateLinker(item);
  261. if (!result)
  262. {
  263. MessageBox.Show(BllFactory<MaintainerBll>.Instance.ErrorMessage ?? string.Empty);
  264. }
  265. return result;
  266. }
  267. /// <summary>
  268. /// 通过项目编号加载项目信息
  269. /// </summary>
  270. /// <param name="projectNo"></param>
  271. private void LoadProjectItem(string projectNo)
  272. {
  273. ProjectItem item = null;
  274. try
  275. {
  276. ValidateProject = false;
  277. if (string.IsNullOrEmpty(projectNo))
  278. return;
  279. item = BllFactory<ProjectBll>.Instance.GetProjectItem(projectNo);
  280. ValidateProject = item!=null;
  281. }
  282. finally
  283. {
  284. if (ValidateProject)
  285. {
  286. LocalProjectName = item.Name;
  287. LoadLinkerItem(MaintainerId, projectNo);
  288. }
  289. else
  290. {
  291. LocalProjectName = string.Empty;
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// 加载联系人信息
  297. /// </summary>
  298. /// <param name="venderId">厂商库id</param>
  299. /// <param name="projectId">项目Id</param>
  300. private void LoadLinkerItem(string venderId, string projectId)
  301. {
  302. // return;
  303. if (string.IsNullOrEmpty(projectId) || string.IsNullOrEmpty(venderId))
  304. return;
  305. var linkerItem = BllFactory<MaintainerBll>.Instance.GetLinker(venderId, projectId);
  306. if (linkerItem != null)
  307. {
  308. this.ContactItem.Telephone = linkerItem.Phone;
  309. this.ContactItem.People = linkerItem.Name;
  310. this.ContactItem.Fax = linkerItem.Fax;
  311. this.ContactItem.Email = linkerItem.Email;
  312. }
  313. }
  314. /// <summary>
  315. /// 根据厂商获取项目设备信息
  316. /// </summary>
  317. /// <param name="projectId"></param>
  318. /// <param name="venderId"></param>
  319. private void LoadEquipmentItems(string projectId, string venderId)
  320. {
  321. RefAssetItems = new ObservableCollection<AssetShowItem>();
  322. var dataItems = BllFactory<MaintainerBll>.Instance.GetCurrentProjectAssetItems(venderId, projectId);
  323. foreach (var dataItem in dataItems)
  324. {
  325. var tempItem = MaintainerDataConverter.ConvertShowItem(dataItem);
  326. tempItem.ProjectId = projectId;
  327. RefAssetItems.Add(tempItem);
  328. }
  329. }
  330. #endregion
  331. #region 保存资产相关
  332. private bool CreateRefEquipment()
  333. {
  334. if (this.RefAssetItems == null || !this.RefAssetItems.Any())
  335. return true;
  336. #region 漏填的产品确认
  337. List<ProductTypeShowItem> typeItems = GetLossTypes();
  338. if (typeItems.Any())
  339. {
  340. WinMaintainerSaveEnsure win = new WinMaintainerSaveEnsure();
  341. win.Owner = this.GetWindow();
  342. LoadParameter.StartParameter(win, new LoadParameter(new List<ProductTypeShowItem>(typeItems)));
  343. if (win.ShowDialog() == true)
  344. {
  345. var checkedItems = win.GetRealResult<List<ProductTypeShowItem>>();
  346. if (checkedItems != null)
  347. {
  348. var useIds = checkedItems.Select(c => c.ProductTypeId);
  349. if(!useIds.Any())
  350. {
  351. var flag = BllFactory<ProductDirectoryBll>.Instance.Add(MaintainerId, new List<string>(useIds));
  352. }
  353. }
  354. }
  355. }
  356. #endregion
  357. #region 资产变更信息点确认
  358. List<AssetShowItem> changedItems = new List<AssetShowItem>();
  359. foreach (var item in this.RefAssetItems)
  360. {
  361. var parameters = GetChangeParameters(item);
  362. if (!parameters.IsEmpty)
  363. {
  364. item.CacheParameters.Merge(parameters);
  365. changedItems.Add(item);
  366. }
  367. }
  368. if (changedItems.Any())
  369. {
  370. AssetSaveContext context = new AssetSaveContext();
  371. context.ChangedItems = changedItems;
  372. WinMaintainerSaveEnsure win = new WinMaintainerSaveEnsure();
  373. win.Owner = this.GetWindow();
  374. LoadParameter.StartParameter(win, new LoadParameter(context));
  375. if (win.ShowDialog() == true)
  376. {
  377. context = win.GetRealResult<AssetSaveContext>();
  378. if (context != null)
  379. {
  380. var items = context.ChangedItems ?? new List<AssetShowItem>();
  381. foreach (var item in items)
  382. {
  383. if (context.EditType == EditType.Correct)
  384. {
  385. item.CacheParameters.SetAllParameterTime(context.EffectiveTime);
  386. }
  387. }
  388. }
  389. }
  390. }
  391. #endregion
  392. #region 数据整理
  393. RefAssetItems.ToList().ForEach(assetItem =>
  394. {
  395. assetItem.MaintainerId = MaintainerId;
  396. assetItem.MaintainerEndDate = this.EndDate??string.Empty;
  397. assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.DPMaintainerID)));
  398. assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.MaintainDeadline)));
  399. assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.MaintainPeriod)));
  400. });
  401. return AssetManager.UpdateEqItems(SystemProjectId, RefAssetItems.ToList());
  402. #endregion
  403. }
  404. /// <summary>
  405. /// 初始化变化参数
  406. /// </summary>
  407. /// <param name="item"></param>
  408. private WatchParameters GetChangeParameters(AssetShowItem item)
  409. {
  410. WatchParameters parameters = new WatchParameters();
  411. if (!string.IsNullOrEmpty(item.MaintainerId) && item.MaintainerId != MaintainerId)
  412. {
  413. parameters.Add(new WatchParameter(MBIBuiltInParameter.DPMaintainerID));
  414. }
  415. return parameters;
  416. }
  417. /// <summary>
  418. /// 获取漏掉的类型信息
  419. /// </summary>
  420. /// <returns></returns>
  421. private List<ProductTypeShowItem> GetLossTypes()
  422. {
  423. List<ProductTypeShowItem> items = new List<ProductTypeShowItem>();
  424. var containItems = BllFactory<ProductDirectoryBll>.Instance.GetAllItems(MaintainerId);
  425. var assets = new List<AssetShowItem>(this.RefAssetItems ?? new ObservableCollection<AssetShowItem>());
  426. items.AddRange(AssetShowItemUtils.GetLossTypes(assets, containItems.Select(c => c.ProductTypeId).ToList()));
  427. return items;
  428. }
  429. #endregion
  430. /// <summary>
  431. /// 成功保存后处理
  432. /// </summary>
  433. private void AfterSuccessSave()
  434. {
  435. ProjectManager.SyncMaintainerProject(SystemProjectId, MaintainerId);
  436. LoadEquipmentItems(SystemProjectId, MaintainerId);
  437. if (this.GetNavigationBar() is NavigationBar bar)
  438. {
  439. bar.RefreshPreWinddow();
  440. }
  441. }
  442. }
  443. }