EditStandDetailCfg.xaml.cs 15 KB

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