PageRecipeManage.cs 9.8 KB

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