PageRecipeManage.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using BLL.Dto.RecipeDto;
  11. using BLL.Manager;
  12. using Helper;
  13. using HZY.Framework.DependencyInjection;
  14. using IoTClient.Enums;
  15. using Mapster;
  16. using MiniExcelLibs;
  17. using SqlSugar;
  18. using Sunny.UI;
  19. namespace Scada.Page
  20. {
  21. public partial class PageRecipeManage : UIPage, ISingletonSelfDependency
  22. {
  23. private readonly RecipeManager _recipeManager;
  24. public PageRecipeManage(RecipeManager recipeManager)
  25. {
  26. InitializeComponent();
  27. _recipeManager = recipeManager;
  28. dgv_Recipe.AutoGenerateColumns = false;
  29. this.Load += PageRecipeManage_Load;
  30. }
  31. protected override CreateParams CreateParams
  32. {
  33. get
  34. {
  35. CreateParams paras = base.CreateParams;
  36. paras.ExStyle |= 0x02000000;
  37. return paras;
  38. }
  39. }
  40. private async void PageRecipeManage_Load(object sender, EventArgs e)
  41. {
  42. await LoadRecipe();
  43. }
  44. private async Task LoadRecipe()
  45. {
  46. var res = await _recipeManager.GetRecipeListAsync();
  47. if (res.Result == SystemEnums.Result.Success)
  48. {
  49. this.dgv_Recipe.DataSource = null;
  50. this.dgv_Recipe.DataSource = res.Data;
  51. }
  52. else
  53. {
  54. LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
  55. }
  56. }
  57. private async void btn_AddRecipe_Click(object sender, EventArgs e)
  58. {
  59. if (this.txt_ProductType.Text.IsNullOrEmpty())
  60. {
  61. UIMessageTip.ShowWarning("请输入产品类型");
  62. return;
  63. }
  64. AddRecipeDto addDto = new AddRecipeDto()
  65. {
  66. 产品类型 = this.txt_ProductType.Text
  67. };
  68. foreach (var item in this.uiTitlePanel1.Controls)
  69. {
  70. if (item is UserSetValue usv)
  71. {
  72. addDto.GetType().GetProperty(usv.VariableName).SetValue(addDto, usv.VarValue);
  73. }
  74. }
  75. var res = await _recipeManager.AddRecipeAsync(addDto);
  76. if (res.Result == SystemEnums.Result.Success)
  77. {
  78. UIMessageTip.ShowOk("添加配方成功");
  79. await LoadRecipe();
  80. }
  81. else
  82. {
  83. LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
  84. UIMessageTip.ShowError(res.Msg);
  85. }
  86. }
  87. private async void btn_DelRecipe_Click(object sender, EventArgs e)
  88. {
  89. if (this.dgv_Recipe.CurrentRow == null)
  90. {
  91. UIMessageTip.ShowWarning("请选择一行");
  92. return;
  93. }
  94. var id = this.dgv_Recipe.CurrentRow.Cells["Id"].Value.ToString().ToInt();
  95. var res = await _recipeManager.DeleteRecipeAsync(new DelRecipeDto() { Id = id });
  96. if (res.Result == SystemEnums.Result.Success)
  97. {
  98. UIMessageTip.ShowOk("删除配方成功");
  99. await LoadRecipe();
  100. }
  101. else
  102. {
  103. LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
  104. UIMessageTip.ShowError(res.Msg);
  105. }
  106. }
  107. private async void btn_UpdateRecipe_Click(object sender, EventArgs e)
  108. {
  109. if (this.dgv_Recipe.CurrentRow == null)
  110. {
  111. UIMessageTip.ShowWarning("请选择一行");
  112. return;
  113. }
  114. UpdateRecipeDto updateDto = new UpdateRecipeDto();
  115. var id = this.dgv_Recipe.CurrentRow.Cells["Id"].Value.ToString().ToInt();
  116. updateDto.Id = id;
  117. updateDto.产品类型 = this.txt_ProductType.Text;
  118. foreach (var item in this.uiTitlePanel1.Controls)
  119. {
  120. if (item is UserSetValue usv)
  121. {
  122. updateDto.GetType().GetProperty(usv.VariableName).SetValue(updateDto, usv.VarValue);
  123. }
  124. }
  125. var res = await _recipeManager.UpdateRecipeAsync(updateDto);
  126. if (res.Result == SystemEnums.Result.Success)
  127. {
  128. UIMessageTip.ShowOk("修改配方成功");
  129. await LoadRecipe();
  130. }
  131. else
  132. {
  133. UIMessageTip.ShowError(res.Msg);
  134. LogExtension.ShowMessage(res.Msg, Microsoft.Extensions.Logging.LogLevel.Error);
  135. }
  136. }
  137. private void btn_ExportRecipe_Click(object sender, EventArgs e)
  138. {
  139. SaveFileDialog saveFileDialog = new SaveFileDialog();
  140. saveFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
  141. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  142. {
  143. try
  144. {
  145. var rows = this.dgv_Recipe.DataSource as List<QueryRecipeResultDto>;
  146. var filePath = saveFileDialog.FileName;
  147. MiniExcel.SaveAs(filePath, rows);
  148. }
  149. catch (Exception ex)
  150. {
  151. UIMessageTip.ShowError("导出失败");
  152. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  153. }
  154. }
  155. }
  156. private async void btn_ImportRecipe_Click(object sender, EventArgs e)
  157. {
  158. OpenFileDialog openFileDialog = new OpenFileDialog();
  159. openFileDialog.Filter = "Excel files(*.xlsx)|*.xlsx";
  160. if (openFileDialog.ShowDialog() == DialogResult.OK)
  161. {
  162. try
  163. {
  164. var fileName = openFileDialog.FileName;
  165. var rows = MiniExcel.Query<QueryRecipeResultDto>(fileName).ToList();
  166. var res = await _recipeManager.GetRecipeListAsync();
  167. if (res.Result == SystemEnums.Result.Success)
  168. {
  169. var existingRows = res.Data;
  170. foreach (var item in rows)
  171. {
  172. var dbRows = existingRows.FirstOrDefault(c => c.产品类型 == item.产品类型);
  173. if (dbRows != null)
  174. {
  175. var updateDto = item.Adapt<UpdateRecipeDto>();
  176. updateDto.Id = dbRows.Id;
  177. var res1 = await _recipeManager.UpdateRecipeAsync(updateDto);
  178. if (res1.Result != SystemEnums.Result.Success)
  179. {
  180. UIMessageTip.ShowError("导入失败:" + res1.Msg);
  181. return;
  182. }
  183. }
  184. else
  185. {
  186. //添加
  187. var addDto = item.Adapt<AddRecipeDto>();
  188. var res2 = await _recipeManager.AddRecipeAsync(addDto);
  189. if (res2.Result != SystemEnums.Result.Success)
  190. {
  191. UIMessageTip.ShowError("导入失败:" + res2.Msg);
  192. return;
  193. }
  194. }
  195. }
  196. UIMessageTip.ShowOk("导入成功");
  197. await LoadRecipe();
  198. }
  199. else
  200. {
  201. UIMessageTip.ShowError("导入失败:" + res.Msg);
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. UIMessageTip.ShowError("导入失败");
  207. }
  208. }
  209. }
  210. private void btn_DownloadRecipe_Click(object sender, EventArgs e)
  211. {
  212. if (!Globals.SiemensClient.Connected)
  213. {
  214. UIMessageTip.ShowWarning("请先连接西门子客户端");
  215. return;
  216. }
  217. var res = UIMessageBox.ShowAsk("是否要下载到PLC");
  218. if (res)
  219. {
  220. foreach (var item in this.uiTitlePanel1.Controls)
  221. {
  222. if (item is UserSetValue usv)
  223. {
  224. if (usv.VarValue == string.Empty)
  225. {
  226. usv.VarValue = "0";
  227. }
  228. if (!Globals.PlcWrite(usv.VariableName, usv.VarValue, DataTypeEnum.Float))
  229. {
  230. new UIMessageForm().ShowErrorDialog($"{usv.VariableName}下载失败");
  231. return;
  232. }
  233. }
  234. }
  235. UIMessageTip.ShowOk("下载成功");
  236. }
  237. }
  238. private async void dgv_Recipe_SelectIndexChangeAsync(object sender, int index)
  239. {
  240. var id = this.dgv_Recipe.Rows[index].Cells["Id"].Value.ToString().ToInt();
  241. var res = await _recipeManager.GetRecipeAsyncById(new GetRecipeByIdDto() { Id = id });
  242. if (res.Result == SystemEnums.Result.Success)
  243. {
  244. foreach (var item in this.uiTitlePanel1.Controls)
  245. {
  246. if (item is UserSetValue usv)
  247. {
  248. var value = res.Data[0].GetType().GetProperty(usv.VariableName).GetValue(res.Data[0]);
  249. usv.VarValue = value.ToString();
  250. }
  251. }
  252. this.txt_ProductType.Text = res.Data[0].产品类型;
  253. }
  254. }
  255. private async void btn_QueryRecipe_Click(object sender, EventArgs e)
  256. {
  257. await LoadRecipe();
  258. }
  259. }
  260. }