EditStandDetailCfg.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using MeterVision.db;
  2. using MeterVision.model;
  3. using MeterVision.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. namespace MeterVision.Dlg
  20. {
  21. /// <summary>
  22. /// EditStationDlg2.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class EditStandDetailCfg : Window
  25. {
  26. //public StationItem mStationItem { get; private set; }
  27. //private TStandDetail _standDetail;
  28. public StandDetailItem mStandDetail { get; private set; }
  29. private bool _can_mark;
  30. public EditStandDetailCfg(StandDetailItem standDetail)
  31. {
  32. InitializeComponent();
  33. cmbMeterType.ItemsSource = StationItem.MeterTypeList;
  34. cmbFlowRate.ItemsSource = StationItem.FlowRateList;
  35. cmbLastUnit.ItemsSource = StationItem.UnitList;
  36. cmbNumCount.ItemsSource = StationItem.NumList;
  37. cmbIndCount.ItemsSource = StationItem.NumList;
  38. if (standDetail.MeterType <= 0)
  39. {
  40. btnDeleteCfg.Visibility = Visibility.Collapsed;
  41. }
  42. else
  43. {
  44. btnDeleteCfg.Visibility = Visibility.Visible;
  45. }
  46. mStandDetail = standDetail;
  47. txtStationId.IsEnabled = false;
  48. Init_Load(standDetail);
  49. Load_image();
  50. }
  51. private void Load_image()
  52. {
  53. if (File.Exists(mStandDetail.SrcImage))
  54. {
  55. ucMark.Visibility = Visibility.Visible;
  56. ucMark.SetImagePath(mStandDetail.SrcImage);
  57. ucMark.MeterRegion_MarkFinished += UcMark_MeterRegion_MarkFinished;
  58. ucMark.FeatureRegion_MarkFinished += UcMark_FeatureRegion_MarkFinished;
  59. _can_mark = true;
  60. }
  61. else
  62. {
  63. ucMark.Visibility = Visibility.Hidden;
  64. _can_mark = false;
  65. }
  66. if (_can_mark)
  67. {
  68. if (cmbMeterType.SelectedValue != null)
  69. {
  70. ucMark.MeterType = (int)cmbMeterType.SelectedValue;
  71. }
  72. else
  73. {
  74. ucMark.MeterType = -1;
  75. }
  76. }
  77. }
  78. //加载数据
  79. private void Init_Load(StandDetailItem standDetail)
  80. {
  81. int meterType = standDetail.MeterType;
  82. txtStationId.Text = standDetail.StationId;
  83. if (meterType <= 0)
  84. {
  85. //获取小于这条数据并且MeterType>0的数据,stand_id,staion_id相同的数据
  86. TStandDetail _standDetail = DBStand.GetPreConfigValidStandDetail(standDetail.StandId, standDetail.StationId,standDetail.SampleTime);
  87. //沿用上一次的信息
  88. if(_standDetail != null)
  89. {
  90. this.ChangeInfoByMeterType(_standDetail.MeterType);
  91. cmbMeterType.SelectedValue = _standDetail.MeterType;
  92. cmbMeterType.SelectedValue = _standDetail.MeterType;
  93. cmbFlowRate.Text = _standDetail.FlowRate.ToString();
  94. //txtBrightVal.Text = _standDetail.BrightVal.ToString();
  95. txtNumInUpper.Text = _standDetail.NumInUpper.ToString();
  96. cmbNumCount.Text = _standDetail.NumCount.ToString();
  97. cmbIndCount.Text = _standDetail.NumCount.ToString();
  98. cmbLastUnit.SelectedValue = _standDetail.LastUnit;
  99. txtLastValue.Text = standDetail.LastValue.ToString();
  100. txtLastTime.Text = standDetail.LastTime.ToString();
  101. }
  102. else
  103. {
  104. this.ChangeInfoByMeterType(0);
  105. cmbMeterType.SelectedValue = 0;
  106. cmbNumCount.Text = standDetail.ENumCount.ToString();
  107. cmbIndCount.Text = "0";
  108. if (StandDetailItem.LastUnitMapping.TryGetValue(standDetail.ELastUnit, out double lastUnit))
  109. {
  110. cmbLastUnit.SelectedValue = standDetail.ELastUnit;
  111. }
  112. cmbFlowRate.Text = "0";
  113. //txtBrightVal.Text = "1.2";
  114. txtNumInUpper.Text = "1";
  115. }
  116. txtFeatureRegion.Text = string.Empty;
  117. txtDialRegion.Text = string.Empty;
  118. if (int.TryParse(standDetail.StandValue, out int iStandValue))
  119. {
  120. txtLastValue.Text = iStandValue.ToString();
  121. }
  122. txtLastTime.Text = standDetail.SampleTime;
  123. txtFeatureRegion.Text = string.Empty;
  124. txtDialRegion.Text = string.Empty;
  125. }
  126. else
  127. {
  128. this.ChangeInfoByMeterType(standDetail.MeterType);
  129. cmbMeterType.SelectedValue = standDetail.MeterType;
  130. cmbMeterType.SelectedValue = standDetail.MeterType;
  131. cmbFlowRate.Text = standDetail.FlowRate.ToString();
  132. //txtBrightVal.Text = standDetail.BrightVal.ToString();
  133. txtNumInUpper.Text = standDetail.NumInUpper.ToString();
  134. cmbNumCount.Text = standDetail.NumCount.ToString();
  135. cmbIndCount.Text = standDetail.IndCount.ToString();
  136. cmbLastUnit.SelectedValue = standDetail.LastUnit;
  137. txtFeatureRegion.Text = standDetail.FeatureRegion;
  138. txtDialRegion.Text = standDetail.DialRegion;
  139. txtLastValue.Text = standDetail.LastValue.ToString();
  140. txtLastTime.Text = standDetail.LastTime.ToString();
  141. }
  142. }
  143. private void UcMark_FeatureRegion_MarkFinished(string regions, int meterType)
  144. {
  145. //throw new NotImplementedException();
  146. this.txtFeatureRegion.Text = regions;
  147. }
  148. private void UcMark_MeterRegion_MarkFinished(string regions, int meterType)
  149. {
  150. //throw new NotImplementedException();
  151. this.txtDialRegion.Text = regions;
  152. }
  153. private bool Get_Load()
  154. {
  155. bool trySuccess = false;
  156. string stationId = txtStationId.Text.Trim();
  157. if (string.IsNullOrEmpty(stationId))
  158. {
  159. MessageBox.Show("请输入站点ID!");
  160. return false;
  161. }
  162. //string stationName = txtStationName.Text.Trim();
  163. int meterType = -1;
  164. if (cmbMeterType.SelectedItem != null)
  165. {
  166. meterType = (int)cmbMeterType.SelectedValue;
  167. //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
  168. }
  169. if (meterType == 0)
  170. {
  171. MessageBox.Show("请选择正确的表类型");
  172. return false;
  173. }
  174. int flowRate = -1;
  175. if (cmbFlowRate.SelectedItem != null)
  176. {
  177. flowRate = (int)cmbFlowRate.SelectedValue;
  178. //((KeyValuePair<int, string>)cmbFlowRate.SelectedItem).Key;
  179. }
  180. else
  181. {
  182. trySuccess = int.TryParse(cmbFlowRate.Text.Trim(), out flowRate);
  183. }
  184. if (flowRate == 0)
  185. {
  186. MessageBox.Show("请输入每小时最大流量");
  187. return false;
  188. }
  189. //double brightVal = -1;
  190. //trySuccess = double.TryParse(txtBrightVal.Text.Trim(), out brightVal);
  191. //if (brightVal == 0)
  192. //{
  193. double brightVal = 1.2;
  194. //}
  195. int numCount = -1;
  196. trySuccess = int.TryParse(cmbNumCount.Text.Trim(), out numCount);
  197. if ((meterType == 1 || meterType == 3) && numCount < 4)
  198. {
  199. MessageBox.Show("请输入正确的数字个数");
  200. return false;
  201. }
  202. int indCount = -1;
  203. trySuccess = int.TryParse(cmbIndCount.Text.Trim(), out indCount);
  204. if (meterType == 2 && indCount < 5)
  205. {
  206. MessageBox.Show("请输入正确的指针个数");
  207. return false;
  208. }
  209. double lastUnit = 0;
  210. if (cmbLastUnit.SelectedValue == null)
  211. {
  212. MessageBox.Show("请选择尾数单位");
  213. return false;
  214. }
  215. lastUnit = (double)cmbLastUnit.SelectedValue;
  216. int numInUpper = -1;
  217. if (meterType == 1 || meterType == 3)
  218. {
  219. if (!int.TryParse(txtNumInUpper.Text.Trim(), out numInUpper) || (numInUpper != 0 && numInUpper != 1))
  220. {
  221. MessageBox.Show("请输入正确的数字在上的值");
  222. return false;
  223. }
  224. }
  225. else
  226. {
  227. numInUpper = 1; //这个应该是无所谓的
  228. }
  229. //表盘坐标与特征坐标可以为空
  230. string dialRegion = txtDialRegion.Text.Trim();
  231. //if (string.IsNullOrEmpty(dialRegion))
  232. //{
  233. // MessageBox.Show("请输入表盘坐标");
  234. // return false;
  235. //}
  236. string featureRegion = txtFeatureRegion.Text.Trim();
  237. //if ((meterType == 1 || meterType == 3) && string.IsNullOrEmpty(featureRegion))
  238. //{
  239. // MessageBox.Show("请输入数字区域 \\ 首位指针坐标");
  240. // return false;
  241. //}
  242. double lastValue;
  243. trySuccess = double.TryParse(txtLastValue.Text.Trim(), out lastValue);
  244. string lastTime = txtLastTime.Text.Trim();
  245. mStandDetail.StationId = stationId;
  246. mStandDetail.MeterType = meterType;
  247. mStandDetail.FlowRate = flowRate;
  248. mStandDetail.BrightVal = brightVal;
  249. mStandDetail.NumCount = numCount;
  250. mStandDetail.IndCount = indCount;
  251. mStandDetail.LastUnit = 0;
  252. mStandDetail.FeatureRegion = "";
  253. if (meterType == 1 || meterType == 3 || meterType == 2)
  254. {
  255. mStandDetail.LastUnit = lastUnit;
  256. mStandDetail.FeatureRegion = featureRegion;
  257. }
  258. mStandDetail.NumInUpper = numInUpper;
  259. mStandDetail.DialRegion = dialRegion;
  260. mStandDetail.LastValue = lastValue;
  261. mStandDetail.LastTime = lastTime;
  262. return true;
  263. }
  264. private void BtnOK_Click(object sender, RoutedEventArgs e)
  265. {
  266. bool blGet = Get_Load();
  267. if (!blGet)
  268. {
  269. return;
  270. }
  271. TStandDetail tStandDetail = new TStandDetail();
  272. ObjectHelper.CopyMatchingFields(mStandDetail, tStandDetail);
  273. try
  274. {
  275. bool updateSuccess = DBStand.UpdateTStandDetailByConfig(tStandDetail);
  276. if (updateSuccess)
  277. {
  278. DialogResult = true;
  279. }
  280. else
  281. {
  282. MessageBox.Show(Application.Current.MainWindow, "修改失败", "错误",
  283. MessageBoxButton.OK, MessageBoxImage.Error);
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
  289. MessageBoxButton.OK, MessageBoxImage.Error);
  290. }
  291. }
  292. private void BtnClose_Click(object sender, RoutedEventArgs e)
  293. {
  294. DialogResult = false;
  295. this.Close();
  296. }
  297. private void CmbMeterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  298. {
  299. int meterType = -1; //= int.Parse(cmbMeterType.SelectedValue.ToString());
  300. //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
  301. //int.TryParse(cmbMeterType.SelectedValue.ToString(),out meterType);
  302. if (cmbMeterType.SelectedValue != null)
  303. {
  304. meterType = (int)cmbMeterType.SelectedValue;
  305. }
  306. this.ChangeInfoByMeterType(meterType);
  307. //改变仪表的类型
  308. ucMark.MeterType = meterType;
  309. }
  310. private void ChangeInfoByMeterType(int meterType)
  311. {
  312. if (meterType == 1 || meterType == 3)
  313. {
  314. txtLastUnitTitle.Text = "最后一位数字的单位:";
  315. txtFeatureRegionTitle.Text = "数字区域坐标:";
  316. if (meterType == 1)
  317. {
  318. grdIndCount.Width = new GridLength(1, GridUnitType.Star);
  319. grdNumCount.Width = new GridLength(1, GridUnitType.Star);
  320. }
  321. else
  322. {
  323. grdIndCount.Width = new GridLength(0, GridUnitType.Pixel);
  324. grdNumCount.Width = new GridLength(1, GridUnitType.Star);
  325. }
  326. grdFlowRate.Width = new GridLength(1, GridUnitType.Star);
  327. grdNumInUpper.Width = new GridLength(1, GridUnitType.Star);
  328. }
  329. else if (meterType == 2)
  330. {
  331. txtLastUnitTitle.Text = "最后一个指针的单位:";
  332. txtFeatureRegionTitle.Text = "首位指针同刻度坐标:";
  333. grdNumCount.Width = new GridLength(0, GridUnitType.Pixel);
  334. grdIndCount.Width = new GridLength(1, GridUnitType.Star);
  335. grdFlowRate.Width = new GridLength(1, GridUnitType.Star);
  336. grdNumInUpper.Width = new GridLength(0, GridUnitType.Pixel);
  337. }
  338. }
  339. private void CmbFlowRate_SelectionChanged(object sender, SelectionChangedEventArgs e)
  340. {
  341. }
  342. private void CmbFlowRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
  343. {
  344. // 只允许数字输入
  345. e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
  346. }
  347. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  348. {
  349. // 如果输入法启用(例如中文输入法),则阻止输入
  350. //if (InputMethod.GetIsInputMethodEnabled)
  351. //{
  352. // e.Handled = true;
  353. // return;
  354. //}
  355. // 只允许数字输入
  356. //e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
  357. // 判断当前输入的字符是否是数字
  358. if (e.Text.All(char.IsDigit))
  359. {
  360. e.Handled = false; // 如果是数字,允许输入
  361. }
  362. else
  363. {
  364. // 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入
  365. e.Handled = true;
  366. }
  367. }
  368. private void BtnWholeMeter_Click(object sender, RoutedEventArgs e)
  369. {
  370. txtDialRegion.Text = "0,0 319,239";
  371. }
  372. private void BtnDeleteCfg_Click(object sender, RoutedEventArgs e)
  373. {
  374. //删除配置
  375. mStandDetail.MeterType = -1;
  376. TStandDetail tStandDetail = new TStandDetail();
  377. ObjectHelper.CopyMatchingFields(mStandDetail, tStandDetail);
  378. try
  379. {
  380. bool updateSuccess = DBStand.UpdateTStandDetailByConfig(tStandDetail);
  381. if (updateSuccess)
  382. {
  383. DialogResult = true;
  384. }
  385. else
  386. {
  387. MessageBox.Show(Application.Current.MainWindow, "删除失败", "错误",
  388. MessageBoxButton.OK, MessageBoxImage.Error);
  389. }
  390. }
  391. catch (Exception ex)
  392. {
  393. MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
  394. MessageBoxButton.OK, MessageBoxImage.Error);
  395. }
  396. //DialogResult = true;
  397. //this.Close();
  398. }
  399. //-----------------------------------------------------
  400. }
  401. }