VmSupplyAssetCreater.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. 
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //文件名称: VmSupplyAssetCreater.cs
  4. //文件描述: VmSupplyAssetCreater.cs
  5. //创 建 者: xls
  6. //创建日期: 2018/9/12 22:30:38
  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.Windows;
  14. using Com.FirmLib.Bll;
  15. using Com.FirmLib.Entity;
  16. using Com.FirmLib.UI.BllCommon;
  17. using Com.FirmLib.UI.Common;
  18. using Com.FirmLib.UI.Common.Model;
  19. using FWindSoft.DataFramework;
  20. using FWindSoft.MVVM;
  21. using FWindSoft.Wpf;
  22. using FWindSoft.Wpf.Common;
  23. namespace Com.FirmLib.UI.Seller
  24. {
  25. public class VmSupplyAssetCreater : BaseViewModel
  26. {
  27. public VmSupplyAssetCreater()
  28. {
  29. Init();
  30. }
  31. private void Init()
  32. {
  33. AssetItems = new ObservableCollection<AssetShowItem>() { };
  34. this.AssetCount = 1;
  35. this.Price = 1000;
  36. this.MaintenancePeriod = 3;
  37. }
  38. public void Init(LoadParameter parameter)
  39. {
  40. SelectAssetContext context = parameter.Parameter as SelectAssetContext;
  41. if (context == null)
  42. return;
  43. Context = context;
  44. }
  45. SelectAssetContext Context { get; set; }
  46. #region 属性相关
  47. private int m_AssetCount;
  48. /// <summary>
  49. /// 资产数量
  50. /// </summary>
  51. public int AssetCount
  52. {
  53. get { return this.m_AssetCount; }
  54. set
  55. {
  56. this.m_AssetCount = value;
  57. RaisePropertyChanged(nameof(this.AssetCount));
  58. //更新资产
  59. UpdateAssetItems();
  60. }
  61. }
  62. private double m_Price;
  63. /// <summary>
  64. /// 采购价格
  65. /// </summary>
  66. public double Price
  67. {
  68. get { return this.m_Price; }
  69. set
  70. {
  71. this.m_Price = value;
  72. RaisePropertyChanged(nameof(this.Price));
  73. }
  74. }
  75. private double m_MaintenancePeriod;
  76. /// <summary>
  77. /// 保修期
  78. /// </summary>
  79. public double MaintenancePeriod
  80. {
  81. get { return this.m_MaintenancePeriod; }
  82. set
  83. {
  84. this.m_MaintenancePeriod = value;
  85. RaisePropertyChanged(nameof(this.MaintenancePeriod));
  86. }
  87. }
  88. private ProductTypeShowItem m_CurrentProductType;
  89. /// <summary>
  90. /// 当前选中类型
  91. /// </summary>
  92. public ProductTypeShowItem CurrentProductType
  93. {
  94. get { return this.m_CurrentProductType; }
  95. set
  96. {
  97. this.m_CurrentProductType = value;
  98. RaisePropertyChanged(nameof(this.CurrentProductType));
  99. }
  100. }
  101. private ObservableCollection<AssetShowItem> m_AssetItems;
  102. /// <summary>
  103. /// 创建资产
  104. /// </summary>
  105. public ObservableCollection<AssetShowItem> AssetItems
  106. {
  107. get { return this.m_AssetItems; }
  108. set
  109. {
  110. this.m_AssetItems = value;
  111. RaisePropertyChanged(nameof(this.AssetItems));
  112. }
  113. }
  114. #endregion
  115. #region 命令相关
  116. /// <summary>
  117. /// 选择型号
  118. /// </summary>
  119. /// <param name="parameter"></param>
  120. [Command]
  121. public void SelectProductType(object parameter)
  122. {
  123. try
  124. {
  125. WinSingleProductTypeSelector win = new WinSingleProductTypeSelector();
  126. win.Owner = this.GetWindow();
  127. LoadParameter.StartParameter(win, new LoadParameter(CurrentProductType?.Clone()));
  128. if (win.ShowDialog() == true)
  129. {
  130. ProductTypeShowItem item = win.GetRealResult<ProductTypeShowItem>();
  131. this.CurrentProductType = item;
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. MessageTip.Show(ex.Message);
  137. }
  138. }
  139. public bool CanSelectProductType(object parameter)
  140. {
  141. return true;
  142. }
  143. /// <summary>
  144. /// 生成资产项目
  145. /// </summary>
  146. /// <param name="parameter"></param>
  147. [Command]
  148. public void CreateAssetItem(object parameter)
  149. {
  150. try
  151. {
  152. CreateAssetItems(parameter as Window);
  153. }
  154. catch (Exception ex)
  155. {
  156. MessageTip.Show(ex.ToString());
  157. }
  158. }
  159. public bool CanCreateAssetItem(object parameter)
  160. {
  161. return true;
  162. }
  163. #endregion
  164. #region 内部方法
  165. /// <summary>
  166. /// 控制资产数据设置
  167. /// </summary>
  168. private void UpdateAssetItems()
  169. {
  170. var currentCount = this.AssetItems.Count;
  171. //控件绑定的Value值,与控件显示不同步的情况;
  172. //不使用ValueChange事件,只能这样去处理
  173. var uiCount = Math.Min(Math.Max(1, this.AssetCount), 100);
  174. if (currentCount == uiCount)
  175. return;
  176. if (currentCount > uiCount)
  177. {
  178. for (int i = currentCount - 1; i >= uiCount; i--)
  179. {
  180. this.AssetItems.RemoveAt(i);
  181. }
  182. }
  183. else
  184. {
  185. for (int i = currentCount; i < uiCount; i++)
  186. {
  187. this.AssetItems.Add(new AssetShowItem() { LocalName = i.ToString(),ProductionDate=DateTime.Today.ToString("yyyyMMdd") });
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 整理显示对象数据
  193. /// </summary>
  194. /// <returns></returns>
  195. private List<AssetShowItem> GetShowItems()
  196. {
  197. foreach (var asset in this.AssetItems)
  198. {
  199. asset.CacheParameters.Clear();
  200. asset.ProjectId = Context.ProjectId;
  201. if (this.CurrentProductType != null)
  202. {
  203. asset.Brand = this.CurrentProductType.BrandName;
  204. asset.Product = this.CurrentProductType.ProductName;
  205. asset.Type = this.CurrentProductType.ProductTypeName;
  206. asset.TypeId = this.CurrentProductType.ProductTypeId;
  207. asset.FamilyCode = this.CurrentProductType.FamilyCode;
  208. }
  209. asset.ProcurementPrice = this.Price;
  210. asset.Warranty = this.MaintenancePeriod;
  211. #region 增加修改参数
  212. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.AssetID));
  213. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.DPSpecificationID));
  214. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.ProductDate));
  215. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.EquipLocalName));
  216. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.EquipLocalID));
  217. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.SerialNum));
  218. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.PurchasePrice));
  219. asset.CacheParameters.Add(new WatchParameter(MBIBuiltInParameter.Warranty));
  220. #endregion
  221. }
  222. return new List<AssetShowItem>(this.AssetItems);
  223. }
  224. /// <summary>
  225. /// 在项目中创建资产
  226. /// </summary>
  227. private void CreateAssetItems(Window window)
  228. {
  229. if (Context == null | string.IsNullOrEmpty(Context.ProjectId))
  230. {
  231. MessageTip.Show("项目id,不能为空");
  232. }
  233. var items = GetShowItems();
  234. var createItems = items.Select(c => ConcertTdItem(c)).ToList();
  235. BatchResult<List<string>> result=BllFactory<PlatformBll>.Instance.CreateAssetItems(new ProjectSetting(Context.ProjectId), createItems);
  236. if (result.Result == BatchResultType.Successed)
  237. {
  238. MessageTip.Show(this.GetWindow(), "创建资产成功");
  239. var ids = result.AttachInfo ?? new List<string>();
  240. for (int i = 0; i < ids.Count; i++)
  241. {
  242. if (items.Count > i)
  243. {
  244. items[i].Id = ids[i];
  245. }
  246. }
  247. if (Context != null)
  248. {
  249. Context.AddedItems.AddRange(items);
  250. }
  251. window.DialogResult = true;
  252. }
  253. else if(result.Result==BatchResultType.PartSuccessed)
  254. {
  255. MessageTip.Show(this.GetWindow(),"创建资产部分失败,不能直接将新资产关联到相关列表,请去选择列表选择");
  256. window.DialogResult = true;
  257. }
  258. else
  259. {
  260. MessageTip.Show(this.GetWindow(),"创建资产全部失败");
  261. window.DialogResult = false;
  262. }
  263. }
  264. #endregion
  265. #region 创建值转换信息
  266. /// <summary>
  267. /// 显示数据转换成操作数据
  268. /// </summary>
  269. /// <param name="item"></param>
  270. /// <returns></returns>
  271. private TdAssetItem ConcertTdItem(AssetShowItem item)
  272. {
  273. TdAssetItem tdItem = AssetManager.ConvertToAssetItem(item); ;
  274. //tdItem.FamilyCode = item.FamilyCode;
  275. return tdItem;
  276. }
  277. #endregion
  278. }
  279. }