RecipeStepTimeViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Aitex.Core.RT.SCCore;
  2. using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig;
  3. using MECF.Framework.UI.Client.CenterViews.Dialogs;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Text.RegularExpressions;
  12. using System.Windows.Media;
  13. using MECF.Framework.UI.Client.ClientBase;
  14. namespace MECF.Framework.UI.Client.CenterViews.Editors.Recipe
  15. {
  16. public class RecipeStepTimeViewModel : ModuleUiViewModelBase
  17. {
  18. public Dictionary<string, string> RecipeStepTime = new Dictionary<string, string>();
  19. public bool IsSave { get; set; } = false;
  20. private string _selectTime;
  21. public string SelectTime
  22. {
  23. get => _selectTime;
  24. set
  25. {
  26. _selectTime = value;
  27. if (!string.IsNullOrEmpty(value) && !value.Contains(" ") && value.Contains(":"))
  28. {
  29. SelectValueTime = value;
  30. SelectValueTimeH = value.Split(':')[0];
  31. SelectValueTimeM = value.Split(':')[1];
  32. SelectValueTimeS = value.Split(':')[2];
  33. }
  34. else if (!string.IsNullOrEmpty(value) && RecipeStepTime.ContainsKey(value))
  35. {
  36. SelectValueTime = RecipeStepTime[value];
  37. SelectValueTimeH = SelectValueTime.Split(':')[0];
  38. SelectValueTimeM = SelectValueTime.Split(':')[1];
  39. SelectValueTimeS = SelectValueTime.Split(':')[2];
  40. }
  41. }
  42. }
  43. private string _selectValueTime = "00:00:10.0";
  44. public string SelectValueTime
  45. {
  46. get
  47. {
  48. return _selectValueTime;
  49. }
  50. set
  51. {
  52. _selectValueTime = value;
  53. NotifyOfPropertyChange(SelectValueTime);
  54. }
  55. }
  56. private string _selectValueTimeH = "00";
  57. public string SelectValueTimeH
  58. {
  59. get
  60. {
  61. return _selectValueTimeH;
  62. }
  63. set
  64. {
  65. _selectValueTimeH = value;
  66. NotifyOfPropertyChange(SelectValueTimeH);
  67. }
  68. }
  69. private string _selectValueTimeM = "00";
  70. public string SelectValueTimeM
  71. {
  72. get
  73. {
  74. return _selectValueTimeM;
  75. }
  76. set
  77. {
  78. _selectValueTimeM = value;
  79. NotifyOfPropertyChange(SelectValueTimeM);
  80. }
  81. }
  82. private string _selectValueTimeS = "10";
  83. public string SelectValueTimeS
  84. {
  85. get
  86. {
  87. return _selectValueTimeS;
  88. }
  89. set
  90. {
  91. _selectValueTimeS = value;
  92. NotifyOfPropertyChange(SelectValueTimeS);
  93. }
  94. }
  95. private string _TimeModel;
  96. public string TimeModel { get { return _TimeModel; } set { _TimeModel = value; NotifyOfPropertyChange(nameof(TimeModel)); } }
  97. public string TimeParam { get; set; }
  98. public bool IsEnable => CGlobal.RecipeProcessEditViewEnable;//是否是View模式
  99. private void GetDataOfConfigItems()
  100. {
  101. if (SelectTime != null && SelectTime.Length > 1 && SelectTime.Split(':').Length > 2)
  102. {
  103. SelectValueTimeH = SelectTime.Split(':')[0];
  104. SelectValueTimeM = SelectTime.Split(':')[1];
  105. SelectValueTimeS = SelectTime.Split(':')[2];
  106. }
  107. var temp = generate();
  108. foreach (var item in temp)
  109. {
  110. RecipeStepTime[item] = SystemConfigProvider.Instance.GetValueByName($"PM1.RecipeEditParameter.{TimeParam}.{item}");
  111. }
  112. }
  113. public RecipeStepTimeViewModel(string timeParam)
  114. {
  115. TimeParam = timeParam;
  116. Initialize();
  117. }
  118. protected override void OnInitialize()
  119. {
  120. base.OnInitialize();
  121. InitTime();
  122. }
  123. protected override void OnViewLoaded(object view)
  124. {
  125. GetDataOfConfigItems();
  126. base.OnViewLoaded(view);
  127. foreach (var item in RecipeStepTime.Keys)
  128. {
  129. object Txt = ((RecipeStepTimeView)(((Window)GetView()).Content)).FindName("Txt" + item);
  130. if (Txt is TextBlock)
  131. {
  132. ((TextBlock)Txt).Text = RecipeStepTime[item];
  133. }
  134. }
  135. }
  136. public void SetValue(object sender, string isFullKeyboard = "")
  137. {
  138. string value = ((TextBox)sender).Text;
  139. if (!string.IsNullOrEmpty(isFullKeyboard))
  140. {
  141. FullKeyboard fullKeyboard1 = new FullKeyboard("", value);
  142. if ((bool)fullKeyboard1.ShowDialog())
  143. {
  144. ((TextBox)sender).Text = fullKeyboard1.ValueString;
  145. }
  146. }
  147. else
  148. {
  149. NumberKeyboard fullKeyboard = new NumberKeyboard("", value);
  150. if ((bool)fullKeyboard.ShowDialog())
  151. {
  152. ((TextBox)sender).Text = fullKeyboard.ValueString;
  153. }
  154. }
  155. }
  156. public void InitTime()
  157. {
  158. if (string.IsNullOrEmpty(SelectValueTime))
  159. {
  160. return;
  161. }
  162. var timeList = SelectValueTime.Split(':').ToList();
  163. var first = timeList[0];
  164. string pattern = "^[0-9]*$";
  165. Regex rex = new Regex(pattern);
  166. if (!rex.IsMatch(first))
  167. {
  168. timeList.Remove(first);
  169. }
  170. SelectValueTimeH = timeList.FirstOrDefault();
  171. SelectValueTimeM = timeList.Skip(1).Take(1).FirstOrDefault();
  172. SelectValueTimeS = timeList.LastOrDefault();
  173. SetTimeValue("Value");
  174. }
  175. public void Initialize()
  176. {
  177. var temp = generate();
  178. foreach (var item in temp)
  179. {
  180. RecipeStepTime.Add(item, "00:00:00");
  181. }
  182. }
  183. IEnumerable<string> generate()
  184. {
  185. for (char c = 'A'; c <= 'Z'; c++)
  186. yield return new string(c, 1);
  187. }
  188. public void SetTimeValue(string nameCmd)
  189. {
  190. SelectTime = nameCmd;
  191. TimeModel = nameCmd;
  192. if (TimeModel != "Value")
  193. {
  194. object Txt = ((RecipeStepTimeView)(((Window)GetView()).Content)).FindName("TxtTime");
  195. if (Txt is TextBox)
  196. {
  197. ((TextBox)Txt).Foreground = new SolidColorBrush(Colors.Black);
  198. }
  199. }
  200. }
  201. public void SetTimeCmd(string nameCmd)
  202. {
  203. SelectValueTime = SelectValueTimeH + ":" + SelectValueTimeM + ":" + SelectValueTimeS;
  204. switch (nameCmd)
  205. {
  206. case "Save":
  207. if (!CheckTimeFormat(SelectValueTimeH, SelectValueTimeM, SelectValueTimeS)) return;
  208. IsSave = true;
  209. break;
  210. case "Close":
  211. IsSave = false;
  212. break;
  213. default:
  214. break;
  215. }
  216. ((Window)GetView()).Close();
  217. }
  218. private bool CheckTimeFormat(string strTimeH, string strTimeM, string strTimeS)
  219. {
  220. bool bResult = true, bTimeH = true, bTimeM = true, bTimeS = true;
  221. string pattern = "^[0-9]*$";
  222. Regex rex = new Regex(pattern);
  223. //秒需要设置小数
  224. Regex reg = new Regex(@"^\d+(\.\d+)?$");
  225. object TxtH = ((RecipeStepTimeView)(((Window)GetView()).Content)).FindName("TxtTimeH");
  226. object TxtM = ((RecipeStepTimeView)(((Window)GetView()).Content)).FindName("TxtTimeM");
  227. object TxtS = ((RecipeStepTimeView)(((Window)GetView()).Content)).FindName("TxtTimeS");
  228. if (!rex.IsMatch(strTimeH))
  229. {
  230. bTimeH = false;
  231. }
  232. if (!bTimeH)
  233. {
  234. if (TxtH is TextBox)((TextBox)TxtH).Foreground = new SolidColorBrush(Colors.Red);
  235. }
  236. else
  237. {
  238. if (TxtH is TextBox) ((TextBox)TxtH).Foreground = new SolidColorBrush(Colors.Black);
  239. }
  240. if (!rex.IsMatch(strTimeM)|| double.Parse(strTimeM) > 59)
  241. {
  242. bTimeM = false;
  243. }
  244. if (!bTimeM)
  245. {
  246. if (TxtM is TextBox)((TextBox)TxtM).Foreground = new SolidColorBrush(Colors.Red);
  247. }
  248. else
  249. {
  250. if (TxtM is TextBox) ((TextBox)TxtM).Foreground = new SolidColorBrush(Colors.Black);
  251. }
  252. double value;
  253. if (!double.TryParse(strTimeS, out value))
  254. {
  255. bTimeS = false;
  256. }
  257. //if (Convert.ToDouble(value) < 0)
  258. //{
  259. // bTimeS = false;
  260. //}
  261. if (!reg.IsMatch(strTimeS) || double.Parse(strTimeS) >= 60)
  262. {
  263. bTimeS = false;
  264. }
  265. if (!bTimeS)
  266. {
  267. if (TxtS is TextBox) ((TextBox)TxtS).Foreground = new SolidColorBrush(Colors.Red);
  268. }
  269. else
  270. {
  271. if (TxtS is TextBox) ((TextBox)TxtS).Foreground = new SolidColorBrush(Colors.Black);
  272. }
  273. bResult = bTimeH && bTimeM && bTimeS;
  274. return bResult;
  275. }
  276. }
  277. }