Files
kou_zhaoxielou_shandong/口罩泄露定制款/Form/frm_AddNewExperiment.cs
2026-01-16 20:53:33 +08:00

894 lines
29 KiB
C#

using DevExpress.Entity.Model.Metadata;
using DevExpress.Internal;
using DevExpress.XtraEditors.Mask;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using ;
using .Form;
using static DevExpress.XtraPrinting.Export.Pdf.PdfSignature;
namespace _定制款.
{
public partial class frm_AddNewExperiment : UIForm
{
// 本底浓度采集列表
List<float> benDiList = new List<float>() { };
short index = 0;
ExperData experData = new ExperData();
private const string MaskConfigFilePath = "MaskTypeConfig.json"; // 面罩类型配置文件路径
private const string ParticleConfigFilePath = "ParticleTypeConfig.json"; // 颗粒物类型配置文件路径
frm_Main main = new frm_Main();
private MaskTypeConfig maskTypeConfig;
private ParticleTypeConfig particleTypeConfig;
private ParticleThresholdConfig particleThresholdConfig;
public frm_AddNewExperiment()
{
InitializeComponent();
InitComboTreeViewData();
InitComboTreeViewData2();
//uiRadioButtonGroup1.SelectedIndex = 0;
//tb_benDi.Text = experData.BenDiNongDu.ToString("F3");
}
public frm_AddNewExperiment(frm_Main mainForm)
{
InitializeComponent();
InitComboTreeViewData();
InitComboTreeViewData2();
//uiRadioButtonGroup1.SelectedIndex = 0;
//tb_benDi.Text = experData.BenDiNongDu.ToString("F3");
main = mainForm; // 赋值主窗体引用
//tb_ShiYanBianHao.Text = IDGenerator.GenerateID();
//tb_benDi.Text = experData.BenDiNongDu.ToString("F3");
dp_ShiYanRiQi.Text = DateTime.Now.ToString("yyyy-MM-dd");
}
private void frm_AddNewExperiment_Load(object sender, EventArgs e)
{
string maskType = cbb_面罩类型.Text;
maskTypeConfig = new MaskTypeConfig(); // 初始化面罩配置
LoadMaskTypes(); // 加载面罩类型
particleTypeConfig = new ParticleTypeConfig(); // 初始化颗粒物配置
particleThresholdConfig = new ParticleThresholdConfig();
LoadParticleTypes(); // 加载颗粒物类型
LoadUserData();
if (maskType.Contains("KN90") || maskType.Contains("KP90"))
{
uiTextBox1.Text = "10.0";
uiTextBox1.Enabled = false;
}
else if (maskType.Contains("KN95") || maskType.Contains("KP95"))
{
uiTextBox1.Text = "8.0";
uiTextBox1.Enabled = false;
}
else if (maskType.Contains("KN100") || maskType.Contains("KP100"))
{
uiTextBox1.Text = "2.0";
uiTextBox1.Enabled = false;
}
else
{
uiTextBox1.Enabled = true;
}
uiComboBox1.Items.Add("篇一");
uiComboBox1.Items.Add("篇二");
uiComboBox1.Items.Add("篇三");
uiComboBox1.Items.Add("自定义");
}
// 初始化树形下拉框的节点数据
private void InitComboTreeViewData()
{
cbb_面罩类型.Nodes.Clear();
// 预设面罩类型
TreeNode root1 = new TreeNode("随弃式面罩TIL");
root1.Nodes.Add(new TreeNode("KN90"));
root1.Nodes.Add(new TreeNode("KN95"));
root1.Nodes.Add(new TreeNode("KN100"));
root1.Nodes.Add(new TreeNode("KP90"));
root1.Nodes.Add(new TreeNode("KP95"));
root1.Nodes.Add(new TreeNode("KP100"));
TreeNode root2 = new TreeNode("可更换式半面罩IL");
root2.Nodes.Add(new TreeNode("硅胶半面罩"));
root2.Nodes.Add(new TreeNode("橡胶半面罩"));
root2.Nodes.Add(new TreeNode("其他材质半面罩"));
TreeNode root3 = new TreeNode("全面罩IL");
root3.Nodes.Add(new TreeNode("防毒全面罩"));
root3.Nodes.Add(new TreeNode("防尘全面罩"));
TreeNode root4 = new TreeNode("自定义");
cbb_面罩类型.Nodes.Add(root1);
cbb_面罩类型.Nodes.Add(root2);
cbb_面罩类型.Nodes.Add(root3);
cbb_面罩类型.Nodes.Add(root4);
if (cbb_面罩类型.Nodes.Count > 0)
{
cbb_面罩类型.Nodes[0].Expand();
if (cbb_面罩类型.Nodes[0].Nodes.Count > 0)
{
cbb_面罩类型.SelectedNode = cbb_面罩类型.Nodes[0].Nodes[0];
}
else
{
cbb_面罩类型.SelectedNode = cbb_面罩类型.Nodes[0];
}
}
}
// 初始化树形下拉框的节点数据
private void InitComboTreeViewData2()
{
cbb_Yangpin.Nodes.Clear();
// 根节点:颗粒物
TreeNode root = new TreeNode("样品分类");
// NaCl
TreeNode nodeNaCl = new TreeNode("NaCl");
nodeNaCl.Nodes.Add(new TreeNode("NaCl浓度"));
nodeNaCl.Nodes.Add(new TreeNode("NaCl粒径"));
// 油类
TreeNode nodeOil = new TreeNode("油类");
// 油类-DOP
TreeNode nodeDOP = new TreeNode("DOP");
nodeDOP.Nodes.Add(new TreeNode("油类DOP浓度"));
nodeDOP.Nodes.Add(new TreeNode("油类DOP粒径"));
// 油类-自定义
TreeNode nodeOilCustom = new TreeNode("自定义");
nodeOilCustom.Nodes.Add(new TreeNode("油类自定义浓度"));
nodeOilCustom.Nodes.Add(new TreeNode("油类自定义粒径"));
nodeOil.Nodes.Add(nodeDOP);
nodeOil.Nodes.Add(nodeOilCustom);
// 顶层自定义
TreeNode nodeCustom = new TreeNode("自定义");
nodeCustom.Nodes.Add(new TreeNode("自定义浓度"));
nodeCustom.Nodes.Add(new TreeNode("自定义粒径"));
// 组装
root.Nodes.Add(nodeNaCl);
root.Nodes.Add(nodeOil);
root.Nodes.Add(nodeCustom);
cbb_Yangpin.Nodes.Add(root);
// 默认展开
root.Expand();
if (root.Nodes.Count > 0 && root.Nodes[0].Nodes.Count > 0)
{
cbb_Yangpin.SelectedNode = root.Nodes[0].Nodes[0];
}
else
{
cbb_Yangpin.SelectedNode = root;
}
}
private void LoadMaskTypes()
{
if (File.Exists(MaskConfigFilePath))
{
string json = File.ReadAllText(MaskConfigFilePath);
maskTypeConfig = JsonConvert.DeserializeObject<MaskTypeConfig>(json);
foreach (var maskType in maskTypeConfig.MaskTypes)
{
TreeNode customNode = new TreeNode(maskType);
cbb_面罩类型.Nodes[3].Nodes.Add(customNode); // 添加到自定义节点
}
}
}
private void LoadParticleTypes()
{
if (File.Exists(ParticleConfigFilePath))
{
string json = File.ReadAllText(ParticleConfigFilePath);
particleTypeConfig = JsonConvert.DeserializeObject<ParticleTypeConfig>(json);
// 遍历树,找到所有自定义节点
List<TreeNode> customNodes = new List<TreeNode>();
FindAllCustomNodes(cbb_Yangpin.Nodes, customNodes);
foreach (var customNode in customNodes)
{
foreach (var particleType in particleTypeConfig.ParticleTypes)
{
bool exists = false;
foreach (TreeNode node in customNode.Nodes)
{
if (node.Text == particleType)
{
exists = true;
break;
}
}
if (!exists)
{
customNode.Nodes.Add(new TreeNode(particleType));
}
}
customNode.Expand();
}
}
}
private void FindAllCustomNodes(TreeNodeCollection nodes, List<TreeNode> result)
{
foreach (TreeNode node in nodes)
{
if (node.Text == "自定义")
{
result.Add(node);
}
if (node.Nodes.Count > 0)
{
FindAllCustomNodes(node.Nodes, result);
}
}
}
private void SaveMaskTypes()
{
if (maskTypeConfig != null)
{
string json = JsonConvert.SerializeObject(maskTypeConfig, Formatting.Indented);
File.WriteAllText(MaskConfigFilePath, json);
}
}
private void SaveParticleTypes()
{
if (particleTypeConfig != null)
{
string json = JsonConvert.SerializeObject(particleTypeConfig, Formatting.Indented);
File.WriteAllText(ParticleConfigFilePath, json);
}
}
private void btn_AddMaskType_Click(object sender, EventArgs e)
{
string newMaskType = uiTextBoxMaskType.Text.Trim();
if (!string.IsNullOrEmpty(newMaskType))
{
TreeNode newNode = new TreeNode(newMaskType);
cbb_面罩类型.Nodes[3].Nodes.Add(newNode); // 添加到自定义节点
maskTypeConfig.MaskTypes.Add(newMaskType); // 更新配置
SaveMaskTypes(); // 保存到文件
uiTextBoxMaskType.Clear(); // 清空输入框
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("请输入有效的面罩类型!");
}
}
private void btn_DeleteMaskType_Click(object sender, EventArgs e)
{
TreeNode selectedNode = cbb_面罩类型.SelectedNode;
if (selectedNode != null && selectedNode.Parent != null && selectedNode.Parent.Index == 3) // 判断是否为自定义节点
{
// 从树形控件中删除选中的节点
cbb_面罩类型.Nodes[3].Nodes.Remove(selectedNode);
// 从配置中删除该面罩类型
maskTypeConfig.MaskTypes.Remove(selectedNode.Text);
// 保存更新后的配置
SaveMaskTypes();
InitComboTreeViewData(); // 重新加载树形控件的数据
LoadMaskTypes();
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("请选择一个有效的自定义面罩类型进行删除!");
}
}
private void btn_AddParticleType_Click(object sender, EventArgs e)
{
string newParticleType = uiTextBoxParticleType.Text.Trim();
if (!string.IsNullOrEmpty(newParticleType))
{
TreeNode selectedNode = cbb_Yangpin.SelectedNode;
if (selectedNode == null)
{
MessageBox.Show("请先选择一个自定义节点!");
return;
}
TreeNode customNode = selectedNode;
while (customNode != null && customNode.Text != "自定义")
{
customNode = customNode.Parent;
}
if (customNode != null && customNode.Text == "自定义")
{
TreeNode newNode = new TreeNode(newParticleType);
customNode.Nodes.Add(newNode);
customNode.Expand();
if (!particleTypeConfig.ParticleTypes.Contains(newParticleType))
particleTypeConfig.ParticleTypes.Add(newParticleType);
SaveParticleTypes();
uiTextBoxParticleType.Clear();
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("请选择一个自定义节点进行添加!");
}
}
else
{
MessageBox.Show("请输入有效的颗粒物类型!");
}
}
private void btn_DeleteParticleType_Click(object sender, EventArgs e)
{
TreeNode selectedNode = cbb_Yangpin.SelectedNode;
if (selectedNode != null && selectedNode.Parent != null && selectedNode.Parent.Text == "自定义")
{
// 从树形控件中删除选中的节点
selectedNode.Parent.Nodes.Remove(selectedNode);
// 从配置中删除该颗粒物类型(如有需要可区分不同自定义节点,这里简单移除)
particleTypeConfig.ParticleTypes.Remove(selectedNode.Text);
// 保存更新后的配置
SaveParticleTypes();
InitComboTreeViewData2();
LoadParticleTypes();
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("请选择一个有效的自定义颗粒物类型进行删除!");
}
}
private void btn_Jiaozhun_Click(object sender, EventArgs e)
{
benDiList.Clear();
index = 0;
btn_Jiaozhun.Text = "采集中";
lb_time.Visible = true;
if (!timer_CaiJi.Enabled)
{
main.master?.WriteSingleCoil(1, 15, true);
timer_CaiJi.Start();
}
else
MessageBox.Show("请先停止采集!");
}
private void timer_CaiJi_Tick(object sender, EventArgs e)
{
benDiList.Add(experData.Mask_TSINongDu);
index++;
if (index >= 120)
{
float sumAll = 0;
btn_Jiaozhun.Text = "校准本底浓度";
for (int i = 20; i < 120; i++)
{
sumAll += benDiList[i];
}
float agv = sumAll / 100;
tb_benDi.Text = agv.ToString("F3");
lb_time.Text = "采集完成";
main.master?.WriteSingleCoil(1, 15, false);
timer_CaiJi.Stop();
}
else
{
lb_time.Text = "采集中" + index.ToString();
}
}
private void btn_Save_Click(object sender, EventArgs e)
{
experData.ExperName = tb_shiYanYuan.Text;
experData.ExperDate = dp_ShiYanRiQi.Text;
experData.ExperNum = tb_ShiYanBianHao.Text;
experData.BenDiNongDu = tb_benDi.Text.ToFloat();
experData.ExperType = cbb_Yangpin.Text;
experData.MaskType = cbb_面罩类型.Text;
experData.ExperMaskType = uiTextBox1.Text.ToFloat();
experData.BenDiNongDu = tb_benDi.Text.ToFloat();
//if (uiRadioButtonGroup1.SelectedIndex == 0)
//{
// experData.YangPinXiShu = 1.0f;
//}
//else if (uiRadioButtonGroup1.SelectedIndex == 1)
//{
// experData.YangPinXiShu = 1.7f;
//}
SaveUserData();
MessageBox.Show("保存成功!");
this.Close();
// 保存后调用主窗体的刷新方法
main.RefreshMainData(); // 新增的刷新方法
}
Random rd = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
experData.BenDiNongDu = rd.Next();
}
private void uiComboTreeView_面罩类型_SelectedNodeChanged(object sender, TreeNode node)
{
TreeNode selectedNode = cbb_面罩类型.SelectedNode;
if (selectedNode == null) return;
TreeNode rootNode = selectedNode;
while (rootNode.Parent != null)
{
rootNode = rootNode.Parent;
}
//if (maskType.Contains("KN90") || maskType.Contains("KP90"))
//{
// return (13.0f, 10.0f);
//}
//else if (maskType.Contains("KN95") || maskType.Contains("KP95"))
//{
// return (11.0f, 8.0f);
//}
//else if (maskType.Contains("KN100") || maskType.Contains("KP100"))
//{
// return (5.0f, 2.0f);
//}
string maskType = selectedNode.Text;
if (maskType.Contains("KN90") || maskType.Contains("KP90"))
{
uiTextBox1.Text = "10.0";
//uiLabel9.Visible = false;
//uiTextBox1.Visible = false;
//uiLabel7.Visible = false;
uiTextBox1.Enabled = false;
}
else if (maskType.Contains("KN95") || maskType.Contains("KP95"))
{
uiTextBox1.Text = "8.0";
uiTextBox1.Enabled = false;
}
else if (maskType.Contains("KN100") || maskType.Contains("KP100"))
{
uiTextBox1.Text = "2.0";
uiTextBox1.Enabled = false;
}
else
{
uiTextBox1.Enabled = true;
}
}
private const string UserDataFilePath = "UserData.json"; // JSON 文件路径
private void SaveUserData()
{
UserData userData;
if (File.Exists(UserDataFilePath))
{
string json = File.ReadAllText(UserDataFilePath);
userData = JsonConvert.DeserializeObject<UserData>(json) ?? new UserData();
}
else
{
userData = new UserData();
}
// 基本信息赋值
userData.High = highTxt.Text;
userData.Weight = weightTxt.Text;
userData.FaceWidth = faceWidthTxt.Text;
userData.FaceLength = faceLongTxt.Text;
userData.EyesDistance = eyesDistanceTxt.Text;
userData.Customer = customerTxt.Text;
userData.IsNeedRun = needrun_chk.Checked;
userData.MaskType = cbb_面罩类型.Text;
TreeNode selectedNode = cbb_面罩类型.SelectedNode;
if (selectedNode != null)
{
userData.FullMaskType = selectedNode.Parent.Text + "-" + cbb_面罩类型.Text;
}
userData.Category = cbb_Yangpin.Text;
userData.Threshold = uiTextBox1.Text;
userData.Concentration = uiTextBox2.Text;
userData.ParticleSize = uiTextBox3.Text;
userData.ContentString = uiComboBox1.Text;
userData.Name = uiTextBox9.Text;
userData.Sex = uiTextBox7.Text;
userData.Age = uiTextBox5.Text;
userData.IdNumber = uiTextBox8.Text;
userData.Company = uiTextBox6.Text;
userData.CompanyAge = uiTextBox4.Text;
userData.No = tb_ShiYanBianHao.Text;
if (string.IsNullOrEmpty(tb_benDi.Text))
{
userData.BenDiNongdu = 0;
}
else
{
userData.BenDiNongdu = float.Parse(tb_benDi.Text);
}
if (uiComboBox1.Text == "自定义")
{
userData.CustomScreenValue = uiRichTextBox1.Text;
}
else
{
userData.ScreenValue = uiRichTextBox1.Text;
}
string jsonOut = JsonConvert.SerializeObject(userData, Formatting.Indented);
File.WriteAllText(UserDataFilePath, jsonOut);
}
private void LoadUserData()
{
if (File.Exists(UserDataFilePath))
{
string json = File.ReadAllText(UserDataFilePath);
var userData = JsonConvert.DeserializeObject<UserData>(json);
highTxt.Text = userData.High;
weightTxt.Text = userData.Weight;
faceWidthTxt.Text = userData.FaceWidth;
faceLongTxt.Text = userData.FaceLength;
eyesDistanceTxt.Text = userData.EyesDistance;
customerTxt.Text = userData.Customer;
needrun_chk.Checked = userData.IsNeedRun;
cbb_面罩类型.Text = userData.MaskType;
cbb_Yangpin.Text = userData.Category;
uiTextBox1.Text = userData.Threshold;
uiTextBox2.Text = userData.Concentration;
uiTextBox3.Text = userData.ParticleSize;
uiComboBox1.Text = userData.ContentString;
uiRichTextBox1.Text = userData.ScreenValue;
uiTextBox9.Text = userData.Name;
uiTextBox7.Text = userData.Sex;
uiTextBox5.Text = userData.Age;
uiTextBox8.Text = userData.IdNumber;
uiTextBox6.Text = userData.Company;
uiTextBox4.Text = userData.CompanyAge;
tb_benDi.Text = userData.BenDiNongdu.ToString("F3");
if (userData.ContentString == "自定义")
{
uiRichTextBox1.Text = userData.CustomScreenValue ?? "";
uiRichTextBox1.ReadOnly = false;
}
else
{
uiRichTextBox1.Text = userData.ScreenValue ?? "";
uiRichTextBox1.ReadOnly = true;
}
}
string value = cbb_Yangpin.Text;
if (value != null && value.Contains("浓度"))
{
uiLabel16.Visible = true;
if (value.Contains("NaCl"))
{
uiLabel16.Text = "NaCl浓度";
}
else if (value.Contains("油类"))
{
uiLabel16.Text = "油类浓度";
}
else
{
uiLabel16.Text = "浓度";
}
uiTextBox2.Visible = true;
uiLabel14.Visible = true;
//uiLabel19.Visible = true;
//uiTextBox3.Visible = true;
//uiLabel18.Visible = true;
}
else if (value.Contains("粒径"))
{
uiLabel16.Visible = true;
if (value.Contains("NaCl"))
{
uiLabel16.Text = "盐类粒径";
}
else if (value.Contains("油类"))
{
uiLabel16.Text = "油类粒径";
}
else
{
uiLabel16.Text = "粒径";
}
uiTextBox2.Visible = true;
uiLabel14.Visible = true;
//uiLabel19.Visible = false;
//uiTextBox3.Visible = false;
//uiLabel18.Visible = false;
}
else
{
uiLabel16.Visible = false;
uiTextBox2.Visible = false;
uiLabel14.Visible = false;
}
}
private void cbb_Yangpin_NodeSelected(object sender, TreeNode node)
{
TreeNode selectedNode = cbb_Yangpin.SelectedNode;
if (selectedNode == null) return;
TreeNode rootNode = selectedNode;
while (rootNode.Parent != null)
{
rootNode = rootNode.Parent;
}
string value = cbb_Yangpin.Text;
if (value != null && (value.Contains("浓度")))
{
if (value.Contains("NaCl"))
{
uiLabel16.Text = "NaCl浓度";
}
else if (value.Contains("油类"))
{
uiLabel16.Text = "油类浓度";
}
else
{
uiLabel16.Text = "浓度";
}
uiLabel16.Visible = true;
uiTextBox2.Visible = true;
uiLabel14.Visible = true;
//uiLabel19.Visible = true;
//uiTextBox3.Visible = true;
//uiLabel18.Visible = true;
}
else if (value.Contains("粒径"))
{
uiLabel16.Visible = true;
if (value.Contains("NaCl"))
{
uiLabel16.Text = "盐类粒径";
}
else if (value.Contains("油类"))
{
uiLabel16.Text = "油类粒径";
}
else
{
uiLabel16.Text = "粒径";
}
uiTextBox2.Visible = true;
uiLabel14.Visible = true;
//uiLabel19.Visible = false;
//uiTextBox3.Visible = false;
//uiLabel18.Visible = false;
}
else
{
uiLabel16.Visible = false;
uiTextBox2.Visible = false;
uiLabel14.Visible = false;
}
}
private void uiLabel14_Click(object sender, EventArgs e)
{
}
private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedItem = uiComboBox1.SelectedItem?.ToString();
if (selectedItem != "自定义")
{
if (particleThresholdConfig.predefinedContent.ContainsKey(selectedItem))
{
uiRichTextBox1.Text = particleThresholdConfig.predefinedContent[selectedItem];
}
else
{
uiRichTextBox1.Text = "";
}
uiRichTextBox1.ReadOnly = true;
}
else
{
// 只加载自定义内容
if (File.Exists(UserDataFilePath))
{
string json = File.ReadAllText(UserDataFilePath);
var userData = JsonConvert.DeserializeObject<UserData>(json);
uiRichTextBox1.Text = userData?.CustomScreenValue ?? "";
}
else
{
uiRichTextBox1.Text = "";
}
uiRichTextBox1.ReadOnly = false;
}
}
}
public class UserData
{
/// <summary>
/// 身高
/// </summary>
public string High { get; set; }
/// <summary>
/// 体重
/// </summary>
public string Weight { get; set; }
/// <summary>
/// 面宽
/// </summary>
public string FaceWidth { get; set; }
/// <summary>
/// 面长
/// </summary>
public string FaceLength { get; set; }
/// <summary>
/// 瞳距
/// </summary>
public string EyesDistance { get; set; }
/// <summary>
/// 自定义
/// </summary>
public string Customer { get; set; }
/// <summary>
/// 是否需要运行
/// </summary>
public bool IsNeedRun { get; set; }
/// <summary>口罩类型
/// </summary>
public string MaskType { get; set; }
/// <summary>
/// 样品分类
/// </summary>
public string Category { get; set; }
/// <summary>
/// 泄露判定阈值
/// </summary>
public string Threshold { get; set; }
/// <summary>
/// 浓度
/// </summary>
public string Concentration { get; set; }
/// <summary>
/// 粒径
/// </summary>
public string ParticleSize { get; set; }
/// <summary>
/// 朗读内容类型(篇一篇二等)
/// </summary>
public string ContentString { get; set; }
/// <summary>
/// 朗读内容文本
/// </summary>
public string ScreenValue { get; set; }
/// <summary>
/// 朗读内容文本
/// </summary>
public string CustomScreenValue { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
public string IdNumber { get; set; }
public string Company { get; set; }
public string CompanyAge { get; set; }
public string No { get; set; }
/// <summary>口罩类型
/// </summary>
public string FullMaskType { get; set; }
public float BenDiNongdu { get; set; }
}
}