123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using BLL.Dto.RecipeDto;
- using BLL.Manager;
- using Helper;
- using HZY.Framework.DependencyInjection;
- using IoTClient.Enums;
- using Mapster;
- using MiniExcelLibs;
- using SqlSugar;
- using Sunny.UI;
- namespace Scada.Page
- {
- public partial class PageRecipeManage : UIPage, ISingletonSelfDependency
- {
- private readonly RecipeManager _recipeManager;
- public PageRecipeManage(RecipeManager recipeManager)
- {
- InitializeComponent();
- _recipeManager = recipeManager;
- dgv_Recipe.AutoGenerateColumns = false;
- this.Load += PageRecipeManage_Load;
- }
- private async void PageRecipeManage_Load(object sender, EventArgs e)
- {
- await LoadRecipe();
- }
- private async Task LoadRecipe()
- {
- var res = await _recipeManager.GetRecipeListAsync();
- if (res.Result == SystemEnums.Result.Success)
- {
- this.dgv_Recipe.DataSource = null;
- this.dgv_Recipe.DataSource = res.Data;
- }
- else
- {
- LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
- }
- }
- private async void btn_AddRecipe_Click(object sender, EventArgs e)
- {
- if (this.txt_ProductType.Text.IsNullOrEmpty())
- {
- UIMessageTip.ShowWarning("请输入产品类型");
- return;
- }
- AddRecipeDto addDto = new AddRecipeDto()
- {
- 产品类型 = this.txt_ProductType.Text
- };
- foreach (var item in this.uiTitlePanel1.Controls)
- {
- if (item is UserSetValue usv)
- {
- addDto.GetType().GetProperty(usv.VariableName).SetValue(addDto, usv.VarValue);
- }
- }
- var res = await _recipeManager.AddRecipeAsync(addDto);
- if (res.Result == SystemEnums.Result.Success)
- {
- UIMessageTip.ShowOk("添加配方成功");
- await LoadRecipe();
- }
- else
- {
- LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
- UIMessageTip.ShowError(res.Msg);
- }
- }
- private async void btn_DelRecipe_Click(object sender, EventArgs e)
- {
- if (this.dgv_Recipe.CurrentRow == null)
- {
- UIMessageTip.ShowWarning("请选择一行");
- return;
- }
- var id = this.dgv_Recipe.CurrentRow.Cells["Id"].Value.ToString().ToInt();
- var res = await _recipeManager.DeleteRecipeAsync(new DelRecipeDto() { Id = id });
- if (res.Result == SystemEnums.Result.Success)
- {
- UIMessageTip.ShowOk("删除配方成功");
- await LoadRecipe();
- }
- else
- {
- LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
- UIMessageTip.ShowError(res.Msg);
- }
- }
- private async void btn_UpdateRecipe_Click(object sender, EventArgs e)
- {
- if (this.dgv_Recipe.CurrentRow == null)
- {
- UIMessageTip.ShowWarning("请选择一行");
- return;
- }
- UpdateRecipeDto updateDto = new UpdateRecipeDto();
- var id = this.dgv_Recipe.CurrentRow.Cells["Id"].Value.ToString().ToInt();
- updateDto.Id = id;
- updateDto.产品类型 = this.txt_ProductType.Text;
- foreach (var item in this.uiTitlePanel1.Controls)
- {
- if (item is UserSetValue usv)
- {
- updateDto.GetType().GetProperty(usv.VariableName).SetValue(updateDto, usv.VarValue);
- }
- }
- var res = await _recipeManager.UpdateRecipeAsync(updateDto);
- if (res.Result == SystemEnums.Result.Success)
- {
- UIMessageTip.ShowOk("修改配方成功");
- await LoadRecipe();
- }
- else
- {
- UIMessageTip.ShowError(res.Msg);
- LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
- }
- }
- private void btn_ExportRecipe_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- var rows = this.dgv_Recipe.DataSource as List<QueryRecipeResultDto>;
- var filePath = saveFileDialog.FileName;
- MiniExcel.SaveAs(filePath, rows);
- }
- catch (Exception ex)
- {
- UIMessageTip.ShowError("导出失败");
- LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
- }
- }
- }
- private async void btn_ImportRecipe_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- var fileName = openFileDialog.FileName;
- var rows = MiniExcel.Query<QueryRecipeResultDto>(fileName).ToList();
- var res = await _recipeManager.GetRecipeListAsync();
- if (res.Result == SystemEnums.Result.Success)
- {
- var existingRows = res.Data;
- foreach (var item in rows)
- {
- var dbRows = existingRows.FirstOrDefault(c => c.产品类型 == item.产品类型);
- if (dbRows != null)
- {
- var updateDto = item.Adapt<UpdateRecipeDto>();
- updateDto.Id = dbRows.Id;
- var res1 = await _recipeManager.UpdateRecipeAsync(updateDto);
- if (res1.Result != SystemEnums.Result.Success)
- {
- UIMessageTip.ShowError("导入失败:" + res1.Msg);
- return;
- }
- }
- else
- {
- //添加
- var addDto = item.Adapt<AddRecipeDto>();
- var res2 = await _recipeManager.AddRecipeAsync(addDto);
- if (res2.Result != SystemEnums.Result.Success)
- {
- UIMessageTip.ShowError("导入失败:" + res2.Msg);
- return;
- }
- }
- }
- UIMessageTip.ShowOk("导入成功");
- await LoadRecipe();
- }
- else
- {
- UIMessageTip.ShowError("导入失败:" + res.Msg);
- }
- }
- catch (Exception ex)
- {
- UIMessageTip.ShowError("导入失败");
- }
- }
- }
- private void btn_DownloadRecipe_Click(object sender, EventArgs e)
- {
- if (!Globals.SiemensClient.Connected)
- {
- UIMessageTip.ShowWarning("请先连接西门子客户端");
- return;
- }
- var res = UIMessageBox.ShowAsk("是否要下载到PLC");
- if (res)
- {
- foreach (var item in this.uiTitlePanel1.Controls)
- {
- if (item is UserSetValue usv)
- {
- if (usv.VarValue == string.Empty)
- {
- usv.VarValue = "0";
- }
- if (!Globals.PlcWrite(usv.VariableName, usv.VarValue, DataTypeEnum.Float))
- {
- new UIMessageForm().ShowErrorDialog($"{usv.VariableName}下载失败");
- return;
- }
- }
- }
- UIMessageTip.ShowOk("下载成功");
- }
- }
- private async void dgv_Recipe_SelectIndexChangeAsync(object sender, int index)
- {
- var id = this.dgv_Recipe.Rows[index].Cells["Id"].Value.ToString().ToInt();
- var res = await _recipeManager.GetRecipeAsyncById(new GetRecipeByIdDto() { Id = id });
- if (res.Result == SystemEnums.Result.Success)
- {
- foreach (var item in this.uiTitlePanel1.Controls)
- {
- if (item is UserSetValue usv)
- {
- var value = res.Data[0].GetType().GetProperty(usv.VariableName).GetValue(res.Data[0]);
- usv.VarValue = value.ToString();
- }
- }
- this.txt_ProductType.Text = res.Data[0].产品类型;
- }
- }
- private async void btn_QueryRecipe_Click(object sender, EventArgs e)
- {
- await LoadRecipe();
- }
- }
- }
|