You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
20 KiB
C#

using CompressorXN.Untils;
using CompressorXN_Common;
using CompressorXN_Communication.TuMos;
using CompressorXN_ControlLib;
using CompressorXN_Model;
using CompressorXN_Model.Enums;
using CompressorXN_Model.ViewModel.Response;
using CompressorXN_Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace CompressorXN
{
public partial class FrmRecipe : Form
{
private static readonly ProductTypeService _productTypeService = new ProductTypeService();
private static readonly AgreementMsgService _agreementMsgService = new AgreementMsgService();
private static readonly ParaService _paraService = new ParaService();
private static EventParaVM eventParaVM = new EventParaVM();
public FrmRecipe()
{
InitializeComponent();
//锁定控件
LockControl();
}
private void FrmRecipe_Load(object sender, EventArgs e)
{
Loading.ShowWaitForm();
BindGrid();
//加载测试项数据
LoadParaItem();
Loading.CloseWaitForm();
}
/// <summary>
/// Grid绑定数据源
/// </summary>
private void BindGrid()
{
var list = _productTypeService.QueryProductTypes().OrderBy(m => m.ProductTypeIndex).ToList();
this.grid_Receipe.DataSource = list;
//取消GirdView默认选中第一行
gridView_Receipe.FocusInvalidRow();
//使用中机种
var model = list.FirstOrDefault(m => m.IsUsed);
if (model != null)
{
txt_UsingRecipeName.Text = model.ProductTypeName;
txt_RecipeName.Text = model.ProductTypeName;
lbl_Id.Text = model.Id;
lbl_IsUsing.Text = model.IsUsed.ToString();
ComboBox cmbStartType = myParaItem.Controls.Find("cmb_StartType", true)[0] as ComboBox;
cmbStartType.SelectedItem = model.StartType;
ComboBox cmbAgreement = myParaItem.Controls.Find("cmb_Agreement", true)[0] as ComboBox;
cmbAgreement.SelectedValue = model.AgreementName;
}
}
/// <summary>
/// 添加机种
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_AddRecipe_Click(object sender, EventArgs e)
{
string recipeName = txt_RecipeName.Text;
if (string.IsNullOrEmpty(recipeName))
{
new FrmDialog("机种名称不能为空!").ShowDialog();
return;
}
if (!_productTypeService.CheckIsExistProductTypeByProductName(recipeName))
{
new FrmDialog("机种已存在!").ShowDialog();
return;
}
ComboBox cmbStartType = myParaItem.Controls.Find("cmb_StartType", true)[0] as ComboBox;
ComboBox cmbAgreement = myParaItem.Controls.Find("cmb_Agreement", true)[0] as ComboBox;
string startType = cmbStartType.SelectedItem.ToString();
string agreementName = cmbAgreement.SelectedItem.ToString();
if (string.IsNullOrEmpty(startType))
{
new FrmDialog("请选择启动方式!").ShowDialog();
return;
}
if (string.IsNullOrEmpty(agreementName))
{
new FrmDialog("请选择控制协议!").ShowDialog();
return;
}
if (new FrmAckDialog($"确认添加【{recipeName}】机种?").ShowDialog() == DialogResult.OK)
{
Loading.ShowWaitForm();
int maxSortIndex = _productTypeService.GetMaxSortIndex();
T_ProductType productType = new T_ProductType
{
ProductTypeName = txt_RecipeName.Text,
StartType = startType,
AgreementName = agreementName,
IsUsed = false,
ProductTypeIndex = maxSortIndex + 1
};
int row = _productTypeService.AddProductType(productType);
bool saveResult = SaveParaItem();
//生成启动压缩机的报文
bool generateResult = AgteementHelper.GenerateAgreement(recipeName, agreementName);
if (row > 0 && saveResult && generateResult)
{
//重新加载数据
BindGrid();
//加载测试项数据
LoadParaItem();
Loading.CloseWaitForm();
new FrmDialog("操作成功!").ShowDialog();
//if (sendFormNameEvent != null)
//{
// eventParaVM.formNameEnum = FormNameEnum.协议配置;
// eventParaVM.AgreementName = productType.AgreementName;
// sendFormNameEvent(eventParaVM);
//}
}
else
{
Loading.CloseWaitForm();
new FrmDialog("操作失败!").ShowDialog();
}
}
}
/// <summary>
/// 修改机种
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_ModifyRecipe_Click(object sender, EventArgs e)
{
string id = lbl_Id.Text;
if (string.IsNullOrEmpty(id))
{
new FrmDialog("请选择要修改的记录行!").ShowDialog();
return;
}
string recipeName = txt_RecipeName.Text;
if (string.IsNullOrEmpty(recipeName))
{
new FrmDialog("机种名称不能为空!").ShowDialog();
return;
}
if (!_productTypeService.CheckIsExistProductTypeByProductName(recipeName, id))
{
new FrmDialog("机种已存在!").ShowDialog();
return;
}
ComboBox cmbStartType = myParaItem.Controls.Find("cmb_StartType", true)[0] as ComboBox;
ComboBox cmbAgreement = myParaItem.Controls.Find("cmb_Agreement", true)[0] as ComboBox;
string startType = cmbStartType.SelectedItem.ToString();
string agreementName = cmbAgreement.SelectedItem.ToString();
if (string.IsNullOrEmpty(startType))
{
new FrmDialog("请选择启动方式!").ShowDialog();
return;
}
if (string.IsNullOrEmpty(agreementName))
{
new FrmDialog("请选择控制协议!").ShowDialog();
return;
}
if (new FrmAckDialog($"确认修改【{recipeName}】机种?").ShowDialog() == DialogResult.OK)
{
Loading.ShowWaitForm();
bool updateResult = _productTypeService.UpdateProductTypeById(id, recipeName, startType, agreementName);
bool saveResult = SaveParaItem();
//生成启动压缩机的报文
bool generateResult = AgteementHelper.GenerateAgreement(recipeName, agreementName);
if (updateResult && saveResult && generateResult)
{
//重新加载数据
BindGrid();
//加载测试项数据
LoadParaItem();
Loading.CloseWaitForm();
new FrmDialog("操作成功!").ShowDialog();
//if (sendFormNameEvent != null)
//{
// eventParaVM.formNameEnum = FormNameEnum.协议配置;
// eventParaVM.AgreementName = agreementName;
// sendFormNameEvent(eventParaVM);
//}
}
else
{
Loading.CloseWaitForm();
new FrmDialog("操作失败!").ShowDialog();
}
}
}
/// <summary>
/// 复制参数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_CopyPara_Click(object sender, EventArgs e)
{
FrmCopyPara frm = new FrmCopyPara();
DialogResult dialogResult = frm.ShowDialog();
if (dialogResult == DialogResult.Yes)
{
new FrmDialog("操作成功!").ShowDialog();
}
else if (dialogResult == DialogResult.No)
{
new FrmDialog("操作失败!").ShowDialog();
}
}
/// <summary>
/// 删除机种
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_DelRecipe_Click(object sender, EventArgs e)
{
string id = lbl_Id.Text;
if (string.IsNullOrEmpty(id))
{
new FrmDialog("请选择要修改的记录行!").ShowDialog();
return;
}
if (new FrmAckDialog("确认删除选中机种?").ShowDialog() == DialogResult.OK)
{
Loading.ShowWaitForm();
var delResult = _productTypeService.DelProductTypeByProductTypeName(txt_RecipeName.Text);
if (delResult)
{
//重新加载数据
BindGrid();
Loading.CloseWaitForm();
new FrmDialog("操作成功,请重新启动上位机软件!").ShowDialog();
}
else
{
Loading.CloseWaitForm();
new FrmDialog("操作失败!").ShowDialog();
}
}
}
/// <summary>
/// 点击解锁按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Unlock_Click(object sender, EventArgs e)
{
FrmUnlock frmUnlock = new FrmUnlock();
DialogResult dialogResult = frmUnlock.ShowDialog();
if (dialogResult == DialogResult.OK)
{
LockControl(false);
}
}
public delegate void SenFormName(EventParaVM eventParaVM);
public event SenFormName sendFormNameEvent;
/// <summary>
/// 应用机种
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_UseRecipe_Click(object sender, EventArgs e)
{
string id = lbl_Id.Text;
if (string.IsNullOrEmpty(id))
{
new FrmDialog("请选择要应用的记录行!").ShowDialog();
return;
}
string productTypeName = txt_RecipeName.Text;
if (new FrmAckDialog($"确认应用【{productTypeName}】机种?").ShowDialog() == DialogResult.OK)
{
Loading.ShowWaitForm();
bool updateResult = _productTypeService.UpdateProductTypeIsUsedById(id);
if (!updateResult)
{
Loading.CloseWaitForm();
new FrmDialog("操作失败!").ShowDialog();
return;
}
ComboBox cmbAgreement = myParaItem.Controls.Find("cmb_Agreement", true)[0] as ComboBox;
string agreementName = cmbAgreement.SelectedItem.ToString();
//生成启动压缩机的报文
bool generateResult = AgteementHelper.GenerateAgreement(productTypeName, agreementName);
if (!generateResult)
{
Loading.CloseWaitForm();
new FrmDialog($"生成【{productTypeName}】机种的压缩机的报文失败!").ShowDialog();
return;
}
T_ProductType t_ProductType = _productTypeService.QueryUsedProductType();
txt_UsingRecipeName.Text = t_ProductType?.ProductTypeName;
GlobalVar.ProductTypeName = t_ProductType?.ProductTypeName;
GlobalVar.ProductTypeIndex = t_ProductType == null ? 0 : t_ProductType.ProductTypeIndex;
GlobalVar.agreementMsgVM = _agreementMsgService.GetAgreementMsgByProductTypeName(GlobalVar.ProductTypeName);
GlobalVar.StartType = GlobalVar.agreementMsgVM.StartType;
//参数写入PLC
var (isOk, msg) = WriteToPlcHelper.WriteParaToPlc(GlobalVar.ProductTypeName);
if (!isOk)
{
Loading.CloseWaitForm();
new FrmDialog(msg).ShowDialog();
return;
}
Loading.CloseWaitForm();
new FrmDialog("操作成功!").ShowDialog();
switch (t_ProductType.StartType)
{
case "CAN":
TMSCANHelper.CanInit((uint)GlobalVar.agreementMsgVM.Bps);
break;
case "LIN":
TMSLINHelper.LinInit(GlobalVar.agreementMsgVM.Bps);
break;
case "CANFD":
TMSCANFDHelper.CANFDInit(5000000, (uint)GlobalVar.agreementMsgVM.Bps);
break;
case "PWM":
break;
}
if (sendFormNameEvent != null)
{
//eventParaVM.formNameEnum = FormNameEnum.协议配置;
//eventParaVM.AgreementName = t_ProductType.AgreementName;
//sendFormNameEvent(eventParaVM);
EventParaVM eventParaVM = new EventParaVM
{
formNameEnum = FormNameEnum.
};
sendFormNameEvent(eventParaVM);
}
}
}
/// <summary>
/// Grid行点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView_Receipe_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
Loading.ShowWaitForm();
var model = gridView_Receipe.GetRow(e.RowHandle) as T_ProductType;
if (model != null)
{
lbl_Id.Text = model.Id;
txt_RecipeName.Text = model.ProductTypeName;
ComboBox cmbStartType = myParaItem.Controls.Find("cmb_StartType", true)[0] as ComboBox;
cmbStartType.SelectedItem = model.StartType;
ComboBox cmbAgreement = myParaItem.Controls.Find("cmb_Agreement", true)[0] as ComboBox;
if (string.IsNullOrEmpty(model.AgreementName))
{
cmbAgreement.SelectedValue = "";
}
else
{
cmbAgreement.SelectedValue = model.AgreementName;
}
}
//加载测试项数据
LoadParaItem();
Loading.CloseWaitForm();
}
/// <summary>
/// 加载测试项数据
/// </summary>
private void LoadParaItem()
{
ClearControlVal();
List<T_Para> paraList = _paraService.QueryParaByProductTypeName(txt_RecipeName.Text).ToList();
if (paraList != null && paraList.Count > 0)
{
List<GroupBox> groupBoxList = myParaItem.Controls.OfType<GroupBox>().ToList();
for (int i = 0; i < groupBoxList.Count; i++)
{
//查询当前类型的测试项
List<T_Para> myParaList = paraList.Where(m => m.ParaType == groupBoxList[i].Text).ToList();
List<MyParaControl> myParaControlList = groupBoxList[i].Controls.OfType<MyParaControl>().ToList();
for (int j = 0; j < myParaControlList.Count; j++)
{
T_Para para = myParaList.FirstOrDefault(m => m.ParaName == myParaControlList[j].LblItemText);
if (para != null)
{
myParaControlList[j].TargetText = para.TargetVal?.ToString();
myParaControlList[j].MinText = para.MinVal?.ToString();
myParaControlList[j].MaxText = para.MaxVal?.ToString();
myParaControlList[j].CkIsActiveChecked = para.IsCheck;
}
}
}
}
}
/// <summary>
/// 保存测试项
/// </summary>
private bool SaveParaItem()
{
List<T_Para> paraList = new List<T_Para>();
string productTypeName = txt_RecipeName.Text;
List<GroupBox> groupBoxList = myParaItem.Controls.OfType<GroupBox>().ToList();
for (int i = 0; i < groupBoxList.Count; i++)
{
List<MyParaControl> myParaControlList = groupBoxList[i].Controls.OfType<MyParaControl>().ToList();
for (int j = 0; j < myParaControlList.Count; j++)
{
T_Para t_Para = new T_Para
{
ParaType = groupBoxList[i].Text,
ProductTypeName = productTypeName,
ParaName = myParaControlList[j].LblItemText
};
string targetText = myParaControlList[j].TargetText;
if (!string.IsNullOrEmpty(targetText))
{
decimal.TryParse(targetText, out decimal targetVal);
t_Para.TargetVal = targetVal;
}
string minText = myParaControlList[j].MinText;
if (!string.IsNullOrEmpty(minText))
{
decimal.TryParse(minText, out decimal minVal);
t_Para.MinVal = minVal;
}
string maxText = myParaControlList[j].MaxText;
if (!string.IsNullOrEmpty(maxText))
{
decimal.TryParse(maxText, out decimal maxVal);
t_Para.MaxVal = maxVal;
}
t_Para.IsCheck = myParaControlList[j].CkIsActiveChecked;
paraList.Add(t_Para);
}
}
return _paraService.SavePara(paraList, productTypeName);
}
/// <summary>
/// 锁定控件
/// </summary>
/// <param name="isReadOnly">是否只读</param>
private void LockControl(bool isReadOnly = true)
{
btn_AddRecipe.Enabled = !isReadOnly;
btn_ModifyRecipe.Enabled = !isReadOnly;
btn_DelRecipe.Enabled = !isReadOnly;
btn_CopyPara.Enabled = !isReadOnly;
myParaItem.IsReadOnly = isReadOnly;
}
/// <summary>
/// 清空控件值
/// </summary>
private void ClearControlVal()
{
List<GroupBox> groupBoxList = myParaItem.Controls.OfType<GroupBox>().ToList();
for (int i = 0; i < groupBoxList.Count; i++)
{
List<MyParaControl> myParaControlList = groupBoxList[i].Controls.OfType<MyParaControl>().ToList();
for (int j = 0; j < myParaControlList.Count; j++)
{
myParaControlList[j].TargetText = string.Empty;
myParaControlList[j].MinText = string.Empty;
myParaControlList[j].MaxText = string.Empty;
myParaControlList[j].CkIsActiveChecked = false;
}
}
}
}
}