EditStationDlg2.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using MeterVision.db;
  2. using MeterVision.model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. namespace MeterVision.Dlg
  18. {
  19. /// <summary>
  20. /// EditStationDlg2.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class EditStationDlg2 : Window
  23. {
  24. private bool mIsAdd;
  25. public StationItem mStationItem { get; private set; }
  26. public EditStationDlg2(StationItem stationItem)
  27. {
  28. InitializeComponent();
  29. cmbMeterType.ItemsSource = StationItem.MeterTypeList;
  30. cmbFlowRate.ItemsSource = StationItem.FlowRateList;
  31. cmbLastUnit.ItemsSource = StationItem.UnitList;
  32. //cmbLastIndUnit.ItemsSource = StationItem.UnitList;
  33. mIsAdd = (stationItem == null);
  34. if(stationItem != null)
  35. {
  36. mStationItem = stationItem;
  37. Init_Load(stationItem);
  38. txtStationId.IsEnabled = false;
  39. }
  40. else
  41. {
  42. mStationItem = new StationItem();
  43. txtStationId.IsEnabled = true;
  44. txtBrightVal.Text = "1.2";
  45. }
  46. }
  47. //加载数据
  48. private void Init_Load(StationItem stationItem)
  49. {
  50. txtStationId.Text = stationItem.StationId;
  51. this.ChangeInfoByMeterType(stationItem.MeterType);
  52. //txtStationName.Text = stationItem.StationName;
  53. cmbMeterType.SelectedValue = stationItem.MeterType;
  54. cmbFlowRate.Text = stationItem.FlowRate.ToString();
  55. txtBrightVal.Text = stationItem.BrightVal.ToString();
  56. txtNumCount.Text = stationItem.NumCount == 0 ? "" : stationItem.NumCount.ToString();
  57. txtIndCount.Text = stationItem.IndCount == 0 ? "" : stationItem.IndCount.ToString();
  58. //txtLastNumUnit.Text = stationItem.LastNumUnit.ToString();
  59. //txtLastIndUnit.Text = stationItem.LastIndUnit.ToString();
  60. txtDialRegion.Text = stationItem.DialRegion;
  61. if (stationItem.MeterType == 1 || stationItem.MeterType == 3 || stationItem.MeterType == 2)
  62. {
  63. cmbLastUnit.SelectedValue = stationItem.LastUnit;
  64. txtFeatureRegion.Text = stationItem.FeatureRegion;
  65. }
  66. txtLastValue.Text = string.IsNullOrEmpty(stationItem.LastTime) ? "" : stationItem.LastValue.ToString();
  67. txtLastTime.Text = stationItem.LastTime;
  68. }
  69. private bool Get_Load()
  70. {
  71. bool trySuccess = false;
  72. string stationId = txtStationId.Text.Trim();
  73. if (string.IsNullOrEmpty(stationId))
  74. {
  75. MessageBox.Show("请输入站点ID!");
  76. return false;
  77. }
  78. //string stationName = txtStationName.Text.Trim();
  79. int meterType = -1;
  80. if (cmbMeterType.SelectedItem != null)
  81. {
  82. meterType = (int)cmbMeterType.SelectedValue;
  83. //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
  84. }
  85. if(meterType == 0)
  86. {
  87. MessageBox.Show("请选择正确的表类型");
  88. return false;
  89. }
  90. int flowRate = -1;
  91. if (cmbFlowRate.SelectedItem != null)
  92. {
  93. flowRate = (int)cmbFlowRate.SelectedValue;
  94. //((KeyValuePair<int, string>)cmbFlowRate.SelectedItem).Key;
  95. }
  96. else
  97. {
  98. trySuccess = int.TryParse(cmbFlowRate.Text.Trim(), out flowRate);
  99. }
  100. if(flowRate == 0)
  101. {
  102. MessageBox.Show("请输入每小时最大流量");
  103. return false;
  104. }
  105. double brightVal = -1;
  106. trySuccess = double.TryParse(txtBrightVal.Text.Trim(),out brightVal);
  107. if(brightVal == 0)
  108. {
  109. brightVal = 1.2;
  110. }
  111. int numCount = -1;
  112. trySuccess = int.TryParse(txtNumCount.Text.Trim(), out numCount);
  113. if((meterType == 1 || meterType == 3) && numCount < 4 )
  114. {
  115. MessageBox.Show("请输入正确的数字个数");
  116. return false;
  117. }
  118. int indCount = -1;
  119. trySuccess = int.TryParse(txtIndCount.Text.Trim(), out indCount);
  120. if(meterType == 2 && indCount < 5)
  121. {
  122. MessageBox.Show("请输入正确的指针个数");
  123. return false;
  124. }
  125. double lastUnit = 0;
  126. if (cmbLastUnit.SelectedValue == null)
  127. {
  128. MessageBox.Show("请选择尾数单位");
  129. return false;
  130. }
  131. lastUnit = (double)cmbLastUnit.SelectedValue;
  132. string dialRegion = txtDialRegion.Text.Trim();
  133. if (string.IsNullOrEmpty(dialRegion))
  134. {
  135. MessageBox.Show("请输入表盘坐标");
  136. return false;
  137. }
  138. string featureRegion = txtFeatureRegion.Text.Trim();
  139. if((meterType == 1 || meterType == 3) && string.IsNullOrEmpty(featureRegion))
  140. {
  141. MessageBox.Show("请输入数字区域 \\ 首位指针坐标");
  142. return false;
  143. }
  144. double lastValue;
  145. trySuccess = double.TryParse(txtLastValue.Text.Trim(), out lastValue);
  146. string lastTime = txtLastTime.Text.Trim();
  147. mStationItem.StationId = stationId;
  148. mStationItem.StationName = ""; // stationName;
  149. mStationItem.MeterType = meterType;
  150. mStationItem.FlowRate = flowRate;
  151. mStationItem.BrightVal = brightVal;
  152. mStationItem.NumCount = numCount;
  153. mStationItem.IndCount = indCount;
  154. mStationItem.LastUnit = 0;
  155. mStationItem.FeatureRegion = "";
  156. if (meterType == 1 || meterType == 3 || meterType == 2)
  157. {
  158. mStationItem.LastUnit = lastUnit;
  159. mStationItem.FeatureRegion = featureRegion;
  160. }
  161. mStationItem.DialRegion = dialRegion;
  162. mStationItem.LastValue = lastValue;
  163. mStationItem.LastTime = lastTime;
  164. if (mIsAdd)
  165. {
  166. mStationItem.CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss();
  167. }
  168. return true;
  169. }
  170. private void BtnOK_Click(object sender, RoutedEventArgs e)
  171. {
  172. bool blGet = Get_Load();
  173. if (!blGet)
  174. {
  175. return;
  176. }
  177. TStation tStation = new TStation(mStationItem);
  178. try
  179. {
  180. if (mIsAdd)
  181. {
  182. bool insertSuccess = DBStation.InsertTStation(tStation);
  183. if (insertSuccess)
  184. {
  185. DialogResult = true;
  186. }
  187. else
  188. {
  189. MessageBox.Show(Application.Current.MainWindow, "新增失败", "错误",
  190. MessageBoxButton.OK, MessageBoxImage.Error);
  191. }
  192. }
  193. else
  194. {
  195. bool updateSuccess = DBStation.UpdateTStation(tStation);
  196. if (updateSuccess)
  197. {
  198. DialogResult = true;
  199. }
  200. else
  201. {
  202. MessageBox.Show(Application.Current.MainWindow, "修改失败", "错误",
  203. MessageBoxButton.OK, MessageBoxImage.Error);
  204. }
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
  210. MessageBoxButton.OK, MessageBoxImage.Error);
  211. }
  212. }
  213. private void BtnClose_Click(object sender, RoutedEventArgs e)
  214. {
  215. DialogResult = false;
  216. this.Close();
  217. }
  218. private void CmbMeterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  219. {
  220. int meterType = -1; //= int.Parse(cmbMeterType.SelectedValue.ToString());
  221. //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
  222. //int.TryParse(cmbMeterType.SelectedValue.ToString(),out meterType);
  223. if (cmbMeterType.SelectedValue != null)
  224. {
  225. meterType = (int)cmbMeterType.SelectedValue;
  226. }
  227. this.ChangeInfoByMeterType(meterType);
  228. }
  229. private void ChangeInfoByMeterType(int meterType)
  230. {
  231. if (meterType == 1 || meterType == 3)
  232. {
  233. txtLastUnitTitle.Text = "最后一位数字的单位:";
  234. txtFeatureRegionTitle.Text = "数字区域坐标:";
  235. if (meterType == 1)
  236. {
  237. grdIndCount.Width = new GridLength(1, GridUnitType.Star);
  238. grdNumCount.Width = new GridLength(1, GridUnitType.Star);
  239. }
  240. else
  241. {
  242. grdIndCount.Width = new GridLength(0, GridUnitType.Pixel);
  243. grdNumCount.Width = new GridLength(1, GridUnitType.Star);
  244. }
  245. }
  246. else if (meterType == 2)
  247. {
  248. txtLastUnitTitle.Text = "最后一个指针的单位:";
  249. txtFeatureRegionTitle.Text = "首位指针同刻度坐标:";
  250. grdNumCount.Width = new GridLength(0, GridUnitType.Pixel);
  251. grdIndCount.Width = new GridLength(1, GridUnitType.Star);
  252. }
  253. }
  254. private void CmbFlowRate_SelectionChanged(object sender, SelectionChangedEventArgs e)
  255. {
  256. }
  257. private void CmbFlowRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
  258. {
  259. // 只允许数字输入
  260. e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
  261. }
  262. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  263. {
  264. // 如果输入法启用(例如中文输入法),则阻止输入
  265. //if (InputMethod.GetIsInputMethodEnabled)
  266. //{
  267. // e.Handled = true;
  268. // return;
  269. //}
  270. // 只允许数字输入
  271. //e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
  272. // 判断当前输入的字符是否是数字
  273. if (e.Text.All(char.IsDigit))
  274. {
  275. e.Handled = false; // 如果是数字,允许输入
  276. }
  277. else
  278. {
  279. // 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入
  280. e.Handled = true;
  281. }
  282. }
  283. //-----------------------------------------------------
  284. }
  285. }