SampleEditor.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using DW5S.Entity;
  2. using DW5S.Repostory;
  3. using ExtensionsDev;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Windows.Forms;
  11. namespace DW5S.App.EditForms
  12. {
  13. public partial class SampleEditor : DevExpress.XtraEditors.XtraForm
  14. {
  15. public SampleInfo info;
  16. private List<SampleInfo> infos;
  17. public SampleEditor()
  18. {
  19. InitializeComponent();
  20. this.layoutControl1.UseDefault();
  21. txtSampleName.UseDoubleClickToSelectAll();
  22. txtSampleFile.UseDoubleClickToSelectAll();
  23. this.Text = "添加样本";
  24. info = new SampleInfo();
  25. this.StartPosition = FormStartPosition.CenterParent;
  26. }
  27. public SampleEditor(SampleInfo info)
  28. : this()
  29. {
  30. this.Text = "编辑样本";
  31. this.info = info;
  32. }
  33. private async void SampleEditor_Load(object sender, EventArgs e)
  34. {
  35. var reftxs = new List<TxInfo>();
  36. var refsigs = new List<SigInfo>();
  37. var unitOfWork = IocContainer.UnitOfWork;
  38. var res = await unitOfWork.Of<TxInfo>().FindAsync(t=>t.TxType== EnumTxType.Ref);
  39. reftxs.AddRange(res);
  40. var refsigList = await unitOfWork.Of<SigInfo>().FindAsync(s => s.SigType == EnumSigType.Ref);
  41. refsigs.AddRange(refsigList);
  42. this.searchLookRef.UseDefault().SetData(reftxs, displayField: nameof(TxInfo.Name)).UseDoubleClickToSelectAll();
  43. this.txtRefSig.UseDefault().SetData(refsigs, displayField: nameof(SigInfo.FreqDownDis)).UseDoubleClickToSelectAll();
  44. if (this.Text == "编辑样本" && info != null)
  45. {
  46. this.txtSampleName.EditValue = info.SampleName;
  47. this.txtFsHz.EditValue = info.Fs;
  48. this.txtStartSec.EditValue = info.StartSec;
  49. this.txtEndSec.EditValue = info.EndSec;
  50. this.searchLookRef.EditValue = reftxs.FirstOrDefault(r => r.Id == info.RefTxInfoID);
  51. this.txtRefSig.EditValue = refsigs.FirstOrDefault(s => s.Id == info.SigInfoID);
  52. this.txtSampleFile.EditValue = Path.Combine(info.SamplePath, info.SampleFileName);
  53. this.txtRmark.Text = info.Remark;
  54. }
  55. var repsSample = unitOfWork.Of<SampleInfo>();
  56. var sampleres = await repsSample.GetAllAsync();
  57. infos.AddRange(sampleres);
  58. }
  59. private void btnCancel_Click(object sender, EventArgs e)
  60. {
  61. this.DialogResult = DialogResult.Cancel;
  62. }
  63. private async void btnOk_Click(object sender, EventArgs e)
  64. {
  65. try
  66. {
  67. dxErrorProvider.ClearErrors();
  68. string sampleName = txtSampleName.Text;
  69. if (string.IsNullOrWhiteSpace(sampleName))
  70. {
  71. dxErrorProvider.SetError(txtSampleName, "请输入样本名称");
  72. return;
  73. }
  74. if (searchLookRef.EditValue == null)
  75. {
  76. dxErrorProvider.SetError(searchLookRef, "请选择参考站");
  77. return;
  78. }
  79. if (txtRefSig.EditValue == null)
  80. {
  81. dxErrorProvider.SetError(txtRefSig, "请选择参考信号");
  82. return;
  83. }
  84. if (!double.TryParse(txtFsHz.Text, out double fsHz))
  85. {
  86. dxErrorProvider.SetError(txtFsHz, "采样率格式错误");
  87. return;
  88. }
  89. if (!int.TryParse(txtStartSec.Text, out int startSec))
  90. {
  91. dxErrorProvider.SetError(txtStartSec, "开始秒数格式错误");
  92. return;
  93. }
  94. if (!int.TryParse(txtEndSec.Text, out int endSec))
  95. {
  96. dxErrorProvider.SetError(txtEndSec, "结束秒数格式错误");
  97. return;
  98. }
  99. if (endSec <= startSec)
  100. {
  101. dxErrorProvider.SetError(txtEndSec, "结束秒数大于开始秒数");
  102. return;
  103. }
  104. var unitOfWork = IocContainer.UnitOfWork;
  105. var settings =await unitOfWork.Of<SysSetings>().FirstOrDefaultAsync();
  106. if (string.IsNullOrEmpty(settings.SampleDirectory))
  107. {
  108. DxHelper.MsgBoxHelper.ShowError($"系统设置中输入样本存储目录");
  109. return;
  110. }
  111. if (!Directory.Exists(settings.SampleDirectory))
  112. {
  113. DxHelper.MsgBoxHelper.ShowError($"系统设置中的样本存储目录不存在");
  114. return;
  115. }
  116. string sampleFile = txtSampleFile.Text;
  117. if (string.IsNullOrEmpty(sampleFile))
  118. {
  119. dxErrorProvider.SetError(txtSampleFile, "请输入样本文件");
  120. return;
  121. }
  122. if (!File.Exists(sampleFile))
  123. {
  124. dxErrorProvider.SetError(txtSampleFile, "样本文件不存在");
  125. return;
  126. }
  127. if (infos.Any(i => i.Id != info.Id && i.SampleName == sampleName))
  128. {
  129. DxHelper.MsgBoxHelper.ShowError($"样本名称[{sampleName}]已经存在!");
  130. return;
  131. }
  132. string fileName = Path.GetFileName(sampleFile);
  133. try
  134. {
  135. string destfile = Path.Combine(settings.SampleDirectory, fileName);
  136. if (!sampleFile.Equals(destfile))
  137. {
  138. if (File.Exists(destfile))
  139. {
  140. File.Delete(destfile);
  141. }
  142. File.Copy(sampleFile, destfile);
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. IocContainer.Logger.Error(ex,$"移动样本文件到样本存储目录{settings.SampleDirectory}失败");
  148. DxHelper.MsgBoxHelper.ShowError($"移动样本文件到样本存储目录{settings.SampleDirectory}失败");
  149. return;
  150. }
  151. var refinfo = (searchLookRef.EditValue as TxInfo);
  152. var siginfo = (txtRefSig.EditValue as SigInfo);
  153. info.SampleName = sampleName;
  154. info.Fs = fsHz;
  155. info.StartSec = startSec;
  156. info.EndSec = endSec;
  157. info.RefTxInfoID = refinfo.Id;
  158. info.RefTxInfo = refinfo;
  159. info.SigInfoID = siginfo.Id;
  160. info.SigInfo = siginfo;
  161. info.SampleFileName = fileName;
  162. info.SamplePath = settings.SampleDirectory;
  163. info.Remark = txtRmark.Text;
  164. this.DialogResult = DialogResult.OK;
  165. }
  166. catch (Exception ex)
  167. {
  168. IocContainer.Logger.Error(ex,"编辑样本信息出错");
  169. DxHelper.MsgBoxHelper.ShowError("编辑样本信息出错");
  170. }
  171. }
  172. private void btnSampleFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  173. {
  174. OpenFileDialog dlg = new OpenFileDialog();
  175. dlg.Title = "文件选择";
  176. dlg.Multiselect = false;
  177. if (dlg.ShowDialog() == DialogResult.OK)
  178. {
  179. txtSampleFile.Text = dlg.FileName;
  180. }
  181. }
  182. }
  183. }