123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using MeterVision.db;
- using MeterVision.model;
- using MeterVision.Util;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace MeterVision.Dlg
- {
- /// <summary>
- /// EditStationDlg2.xaml 的交互逻辑
- /// </summary>
- public partial class EditStandDetailCfg : Window
- {
- //public StationItem mStationItem { get; private set; }
- //private TStandDetail _standDetail;
- public StandDetailItem mStandDetail { get; private set; }
- private bool _can_mark;
- public EditStandDetailCfg(StandDetailItem standDetail)
- {
- InitializeComponent();
- cmbMeterType.ItemsSource = StationItem.MeterTypeList;
- cmbFlowRate.ItemsSource = StationItem.FlowRateList;
- cmbLastUnit.ItemsSource = StationItem.UnitList;
- if (standDetail.MeterType <= 0)
- {
- btnDeleteCfg.Visibility = Visibility.Collapsed;
- }
- else
- {
- btnDeleteCfg.Visibility = Visibility.Visible;
- }
- mStandDetail = standDetail;
- txtStationId.IsEnabled = false;
- Init_Load(standDetail);
- Load_image();
- }
- private void Load_image()
- {
- if (File.Exists(mStandDetail.SrcImage))
- {
- ucMark.Visibility = Visibility.Visible;
- ucMark.SetImagePath(mStandDetail.SrcImage);
- ucMark.MeterRegion_MarkFinished += UcMark_MeterRegion_MarkFinished;
- ucMark.FeatureRegion_MarkFinished += UcMark_FeatureRegion_MarkFinished;
- _can_mark = true;
- }
- else
- {
- ucMark.Visibility = Visibility.Hidden;
- _can_mark = false;
- }
- if (_can_mark)
- {
- if (cmbMeterType.SelectedValue != null)
- {
- ucMark.MeterType = (int)cmbMeterType.SelectedValue;
- }
- else
- {
- ucMark.MeterType = -1;
- }
- }
- }
- //加载数据
- private void Init_Load(StandDetailItem standDetail)
- {
- int meterType = standDetail.MeterType;
- txtStationId.Text = standDetail.StationId;
- if (meterType <= 0)
- {
- //获取小于这条数据并且MeterType>0的数据,stand_id,staion_id相同的数据
- TStandDetail _standDetail = DBStand.GetPreConfigValidStandDetail(standDetail.StandId, standDetail.StationId,standDetail.SampleTime);
- //沿用上一次的信息
- if(_standDetail != null)
- {
- this.ChangeInfoByMeterType(_standDetail.MeterType);
- cmbMeterType.SelectedValue = _standDetail.MeterType;
- cmbMeterType.SelectedValue = _standDetail.MeterType;
- cmbFlowRate.Text = _standDetail.FlowRate.ToString();
- txtBrightVal.Text = _standDetail.BrightVal.ToString();
- txtNumCount.Text = _standDetail.NumCount.ToString();
- txtIndCount.Text = _standDetail.NumCount.ToString();
- cmbLastUnit.SelectedValue = _standDetail.LastUnit;
- txtLastValue.Text = standDetail.LastValue.ToString();
- txtLastTime.Text = standDetail.LastTime.ToString();
- }
- else
- {
- this.ChangeInfoByMeterType(0);
- cmbMeterType.SelectedValue = 0;
- txtNumCount.Text = standDetail.ENumCount.ToString();
- txtIndCount.Text = "0";
- if (StandDetailItem.LastUnitMapping.TryGetValue(standDetail.ELastUnit, out double lastUnit))
- {
- cmbLastUnit.SelectedValue = standDetail.ELastUnit;
- }
- cmbFlowRate.Text = "0";
- txtBrightVal.Text = "1.2";
- }
- txtFeatureRegion.Text = string.Empty;
- txtDialRegion.Text = string.Empty;
- if (int.TryParse(standDetail.StandValue, out int iStandValue))
- {
- txtLastValue.Text = iStandValue.ToString();
- }
- txtLastTime.Text = standDetail.SampleTime;
- txtFeatureRegion.Text = string.Empty;
- txtDialRegion.Text = string.Empty;
- }
- else
- {
- this.ChangeInfoByMeterType(standDetail.MeterType);
- cmbMeterType.SelectedValue = standDetail.MeterType;
- cmbMeterType.SelectedValue = standDetail.MeterType;
- cmbFlowRate.Text = standDetail.FlowRate.ToString();
- txtBrightVal.Text = standDetail.BrightVal.ToString();
- txtNumCount.Text = standDetail.NumCount.ToString();
- txtIndCount.Text = standDetail.IndCount.ToString();
- cmbLastUnit.SelectedValue = standDetail.LastUnit;
- txtFeatureRegion.Text = standDetail.FeatureRegion;
- txtDialRegion.Text = standDetail.DialRegion;
- txtLastValue.Text = standDetail.LastValue.ToString();
- txtLastTime.Text = standDetail.LastTime.ToString();
- }
- }
- private void UcMark_FeatureRegion_MarkFinished(string regions, int meterType)
- {
- //throw new NotImplementedException();
- this.txtFeatureRegion.Text = regions;
- }
- private void UcMark_MeterRegion_MarkFinished(string regions, int meterType)
- {
- //throw new NotImplementedException();
- this.txtDialRegion.Text = regions;
- }
- private bool Get_Load()
- {
- bool trySuccess = false;
- string stationId = txtStationId.Text.Trim();
- if (string.IsNullOrEmpty(stationId))
- {
- MessageBox.Show("请输入站点ID!");
- return false;
- }
- //string stationName = txtStationName.Text.Trim();
- int meterType = -1;
- if (cmbMeterType.SelectedItem != null)
- {
- meterType = (int)cmbMeterType.SelectedValue;
- //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
- }
- if (meterType == 0)
- {
- MessageBox.Show("请选择正确的表类型");
- return false;
- }
- int flowRate = -1;
- if (cmbFlowRate.SelectedItem != null)
- {
- flowRate = (int)cmbFlowRate.SelectedValue;
- //((KeyValuePair<int, string>)cmbFlowRate.SelectedItem).Key;
- }
- else
- {
- trySuccess = int.TryParse(cmbFlowRate.Text.Trim(), out flowRate);
- }
- if (flowRate == 0)
- {
- MessageBox.Show("请输入每小时最大流量");
- return false;
- }
- double brightVal = -1;
- trySuccess = double.TryParse(txtBrightVal.Text.Trim(), out brightVal);
- if (brightVal == 0)
- {
- brightVal = 1.2;
- }
- int numCount = -1;
- trySuccess = int.TryParse(txtNumCount.Text.Trim(), out numCount);
- if ((meterType == 1 || meterType == 3) && numCount < 4)
- {
- MessageBox.Show("请输入正确的数字个数");
- return false;
- }
- int indCount = -1;
- trySuccess = int.TryParse(txtIndCount.Text.Trim(), out indCount);
- if (meterType == 2 && indCount < 5)
- {
- MessageBox.Show("请输入正确的指针个数");
- return false;
- }
- double lastUnit = 0;
- if (cmbLastUnit.SelectedValue == null)
- {
- MessageBox.Show("请选择尾数单位");
- return false;
- }
- lastUnit = (double)cmbLastUnit.SelectedValue;
- string dialRegion = txtDialRegion.Text.Trim();
- if (string.IsNullOrEmpty(dialRegion))
- {
- MessageBox.Show("请输入表盘坐标");
- return false;
- }
- string featureRegion = txtFeatureRegion.Text.Trim();
- if ((meterType == 1 || meterType == 3) && string.IsNullOrEmpty(featureRegion))
- {
- MessageBox.Show("请输入数字区域 \\ 首位指针坐标");
- return false;
- }
- double lastValue;
- trySuccess = double.TryParse(txtLastValue.Text.Trim(), out lastValue);
- string lastTime = txtLastTime.Text.Trim();
- mStandDetail.StationId = stationId;
- mStandDetail.MeterType = meterType;
- mStandDetail.FlowRate = flowRate;
- mStandDetail.BrightVal = brightVal;
- mStandDetail.NumCount = numCount;
- mStandDetail.IndCount = indCount;
- mStandDetail.LastUnit = 0;
- mStandDetail.FeatureRegion = "";
- if (meterType == 1 || meterType == 3 || meterType == 2)
- {
- mStandDetail.LastUnit = lastUnit;
- mStandDetail.FeatureRegion = featureRegion;
- }
- mStandDetail.DialRegion = dialRegion;
- mStandDetail.LastValue = lastValue;
- mStandDetail.LastTime = lastTime;
- return true;
- }
- private void BtnOK_Click(object sender, RoutedEventArgs e)
- {
- bool blGet = Get_Load();
- if (!blGet)
- {
- return;
- }
- TStandDetail tStandDetail = new TStandDetail();
- ObjectHelper.CopyMatchingFields(mStandDetail, tStandDetail);
- try
- {
- bool updateSuccess = DBStand.UpdateTStandDetailByConfig(tStandDetail);
- if (updateSuccess)
- {
- DialogResult = true;
- }
- else
- {
- MessageBox.Show(Application.Current.MainWindow, "修改失败", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- private void BtnClose_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- this.Close();
- }
- private void CmbMeterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- int meterType = -1; //= int.Parse(cmbMeterType.SelectedValue.ToString());
- //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
- //int.TryParse(cmbMeterType.SelectedValue.ToString(),out meterType);
- if (cmbMeterType.SelectedValue != null)
- {
- meterType = (int)cmbMeterType.SelectedValue;
- }
- this.ChangeInfoByMeterType(meterType);
- //改变仪表的类型
- ucMark.MeterType = meterType;
- }
- private void ChangeInfoByMeterType(int meterType)
- {
- if (meterType == 1 || meterType == 3)
- {
- txtLastUnitTitle.Text = "最后一位数字的单位:";
- txtFeatureRegionTitle.Text = "数字区域坐标:";
- if (meterType == 1)
- {
- grdIndCount.Width = new GridLength(1, GridUnitType.Star);
- grdNumCount.Width = new GridLength(1, GridUnitType.Star);
- }
- else
- {
- grdIndCount.Width = new GridLength(0, GridUnitType.Pixel);
- grdNumCount.Width = new GridLength(1, GridUnitType.Star);
- }
- }
- else if (meterType == 2)
- {
- txtLastUnitTitle.Text = "最后一个指针的单位:";
- txtFeatureRegionTitle.Text = "首位指针同刻度坐标:";
- grdNumCount.Width = new GridLength(0, GridUnitType.Pixel);
- grdIndCount.Width = new GridLength(1, GridUnitType.Star);
- }
- }
- private void CmbFlowRate_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- }
- private void CmbFlowRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- // 只允许数字输入
- e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
- }
- private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- // 如果输入法启用(例如中文输入法),则阻止输入
- //if (InputMethod.GetIsInputMethodEnabled)
- //{
- // e.Handled = true;
- // return;
- //}
- // 只允许数字输入
- //e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
- // 判断当前输入的字符是否是数字
- if (e.Text.All(char.IsDigit))
- {
- e.Handled = false; // 如果是数字,允许输入
- }
- else
- {
- // 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入
- e.Handled = true;
- }
- }
- private void BtnWholeMeter_Click(object sender, RoutedEventArgs e)
- {
- txtDialRegion.Text = "0,0 319,239";
- }
- private void BtnDeleteCfg_Click(object sender, RoutedEventArgs e)
- {
- //删除配置
- mStandDetail.MeterType = -1;
- TStandDetail tStandDetail = new TStandDetail();
- ObjectHelper.CopyMatchingFields(mStandDetail, tStandDetail);
- try
- {
- bool updateSuccess = DBStand.UpdateTStandDetailByConfig(tStandDetail);
- if (updateSuccess)
- {
- DialogResult = true;
- }
- else
- {
- MessageBox.Show(Application.Current.MainWindow, "删除失败", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- //DialogResult = true;
- //this.Close();
- }
- //-----------------------------------------------------
- }
- }
|