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
{
///
/// EditStationDlg2.xaml 的交互逻辑
///
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;
cmbNumCount.ItemsSource = StationItem.NumList;
cmbIndCount.ItemsSource = StationItem.NumList;
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();
txtNumInUpper.Text = _standDetail.NumInUpper.ToString();
cmbNumCount.Text = _standDetail.NumCount.ToString();
cmbIndCount.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;
cmbNumCount.Text = standDetail.ENumCount.ToString();
cmbIndCount.Text = "0";
if (StandDetailItem.LastUnitMapping.TryGetValue(standDetail.ELastUnit, out double lastUnit))
{
cmbLastUnit.SelectedValue = standDetail.ELastUnit;
}
cmbFlowRate.Text = "0";
//txtBrightVal.Text = "1.2";
txtNumInUpper.Text = "1";
}
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();
txtNumInUpper.Text = standDetail.NumInUpper.ToString();
cmbNumCount.Text = standDetail.NumCount.ToString();
cmbIndCount.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)cmbMeterType.SelectedItem).Key;
}
if (meterType == 0)
{
MessageBox.Show("请选择正确的表类型");
return false;
}
int flowRate = -1;
if (cmbFlowRate.SelectedItem != null)
{
flowRate = (int)cmbFlowRate.SelectedValue;
//((KeyValuePair)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)
//{
double brightVal = 1.2;
//}
int numCount = -1;
trySuccess = int.TryParse(cmbNumCount.Text.Trim(), out numCount);
if ((meterType == 1 || meterType == 3) && numCount < 4)
{
MessageBox.Show("请输入正确的数字个数");
return false;
}
int indCount = -1;
trySuccess = int.TryParse(cmbIndCount.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;
int numInUpper = -1;
if (meterType == 1 || meterType == 3)
{
if (!int.TryParse(txtNumInUpper.Text.Trim(), out numInUpper) || (numInUpper != 0 && numInUpper != 1))
{
MessageBox.Show("请输入正确的数字在上的值");
return false;
}
}
else
{
numInUpper = 1; //这个应该是无所谓的
}
//表盘坐标与特征坐标可以为空
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.NumInUpper = numInUpper;
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)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);
}
grdFlowRate.Width = new GridLength(1, GridUnitType.Star);
grdNumInUpper.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);
grdFlowRate.Width = new GridLength(1, GridUnitType.Star);
grdNumInUpper.Width = new GridLength(0, GridUnitType.Pixel);
}
}
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();
}
//-----------------------------------------------------
}
}