MacRecipeController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using AutoMapper;
  2. using Cksoft.Data;
  3. using Cksoft.Data.Repository;
  4. using Cksoft.Unity;
  5. using DllEapCommon.NPOI;
  6. using DllEapDal.OFILM;
  7. using DllEapEntity;
  8. using DllEapEntity.Dtos;
  9. using DllEapEntity.Dtos.Export;
  10. using DllEapEntity.OFILM;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Microsoft.AspNetCore.Mvc;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace DllEapBll.OFILM
  20. {
  21. /// <summary>
  22. /// 机台上线维护
  23. /// </summary>
  24. [Authorize]
  25. [ApiController]
  26. [Route("eap/api/[controller]/[action]")]
  27. public class MacRecipeController : ControllerBase
  28. {
  29. private readonly string connStr = "eapslave";
  30. private IMapper _mapper;
  31. public MacRecipeController(IMapper mapper)
  32. {
  33. _mapper = mapper;
  34. }
  35. /// <summary>
  36. /// 列表
  37. /// </summary>
  38. /// <param name="filter"></param>
  39. /// <param name="pageIndex"></param>
  40. /// <param name="pageSize"></param>
  41. /// <param name="sortField"></param>
  42. /// <param name="sortOrder"></param>
  43. /// <returns></returns>
  44. [HttpGet]
  45. public LayuiModel<MacRecipeDto> Get(string filter, int pageIndex = 1, int pageSize = 10, string sortField = "macCode", string sortOrder = "ascend")
  46. {
  47. if (sortOrder == "descend")
  48. {
  49. sortOrder = "desc";
  50. }
  51. else
  52. {
  53. sortOrder = "asc";
  54. }
  55. int start, end;
  56. start = (pageIndex - 1) * pageSize + 1;
  57. end = start + pageSize;
  58. int total;
  59. using (IDatabase db = DbFactory.Base(connStr))
  60. {
  61. db.BeginTrans();
  62. var dal = new MacRecipeDal(db);
  63. string errorinfo = string.Empty;
  64. var roles = dal.GetMacRecipeDtos(filter, start, pageSize, sortField, sortOrder, out total, ref errorinfo);
  65. return new LayuiModel<MacRecipeDto>
  66. {
  67. code = 1,
  68. count = total,
  69. data = roles,
  70. msg = ""
  71. };
  72. }
  73. }
  74. /// <summary>
  75. /// 列表
  76. /// </summary>
  77. /// <param name="filter"></param>
  78. /// <param name="pageIndex"></param>
  79. /// <param name="pageSize"></param>
  80. /// <param name="sortField"></param>
  81. /// <param name="sortOrder"></param>
  82. /// <returns></returns>
  83. [HttpGet]
  84. public LayuiModel<MachineInfo> GetMachineInfoes(string filter, int pageIndex = 1, int pageSize = 10, string sortField = "macCode", string sortOrder = "ascend")
  85. {
  86. if (sortOrder == "descend")
  87. {
  88. sortOrder = "desc";
  89. }
  90. else
  91. {
  92. sortOrder = "asc";
  93. }
  94. int start, end;
  95. start = (pageIndex - 1) * pageSize + 1;
  96. end = start + pageSize;
  97. int total;
  98. using (IDatabase db = DbFactory.Base(connStr))
  99. {
  100. db.BeginTrans();
  101. var dal = new MacRecipeDal(db);
  102. string errorinfo = string.Empty;
  103. var roles = dal.GetMachineInfoes(filter, start, pageSize, sortField, sortOrder, out total, ref errorinfo);
  104. return new LayuiModel<MachineInfo>
  105. {
  106. code = 1,
  107. count = total,
  108. data = roles,
  109. msg = ""
  110. };
  111. }
  112. }
  113. /// <summary>
  114. /// 详情
  115. /// </summary>
  116. /// <param name="macId"></param>
  117. /// <returns></returns>
  118. [HttpGet]
  119. public MacRecipeDto GetMachineInfoById(int macId)
  120. {
  121. int total;
  122. using (IDatabase db = DbFactory.Base(connStr))
  123. {
  124. db.BeginTrans();
  125. var dal = new MacRecipeDal(db);
  126. string errorinfo = string.Empty;
  127. var role = dal.GetMachineInfoes($" and a.id={macId}", 1, 1, "macid", "asc", out total, ref errorinfo)
  128. .FirstOrDefault();
  129. return role;
  130. }
  131. }
  132. /// <summary>
  133. /// 获取机台运行历史
  134. /// </summary>
  135. /// <param name="filter"></param>
  136. /// <param name="pageIndex"></param>
  137. /// <param name="pageSize"></param>
  138. /// <param name="sortField"></param>
  139. /// <param name="sortOrder"></param>
  140. /// <returns></returns>
  141. [HttpGet]
  142. public LayuiModel<MacRecipeDto> GetMacStatus(string filter, int pageIndex = 1, int pageSize = 10, string sortField = "stime", string sortOrder = "descend")
  143. {
  144. if (sortOrder == "descend")
  145. {
  146. sortOrder = "desc";
  147. }
  148. else
  149. {
  150. sortOrder = "asc";
  151. }
  152. int start, end;
  153. start = (pageIndex - 1) * pageSize + 1;
  154. end = start + pageSize;
  155. int total;
  156. using (IDatabase db = DbFactory.Base(connStr))
  157. {
  158. db.BeginTrans();
  159. var dal = new MacRecipeDal(db);
  160. string errorinfo = string.Empty;
  161. var roles = dal.GetMacStatusDtos(filter, start, pageSize, sortField, sortOrder, out total, ref errorinfo);
  162. return new LayuiModel<MacRecipeDto>
  163. {
  164. code = 1,
  165. count = total,
  166. data = roles,
  167. msg = ""
  168. };
  169. }
  170. }
  171. /// <summary>
  172. /// 机台运行历史导出
  173. /// </summary>
  174. /// <param name="filterInfo"></param>
  175. /// <returns></returns>
  176. [HttpPost]
  177. public async Task<IActionResult> ExportMacStatus(IDictionary<string, string> filterInfo)
  178. {
  179. string filter=string.Empty; int pageIndex = 1; int pageSize = 100000; string sortField = "stime"; string sortOrder = "descend";
  180. if (filterInfo.ContainsKey("filter"))
  181. {
  182. filter = filterInfo["filter"];
  183. }
  184. if (filterInfo.ContainsKey("pageIndex"))
  185. {
  186. pageIndex =Convert.ToInt32( filterInfo["pageIndex"]);
  187. }
  188. if (filterInfo.ContainsKey("pageSize"))
  189. {
  190. pageSize = Convert.ToInt32(filterInfo["pageSize"]);
  191. }
  192. if (filterInfo.ContainsKey("sortField"))
  193. {
  194. sortField = filterInfo["sortField"];
  195. }
  196. if (filterInfo.ContainsKey("sortOrder"))
  197. {
  198. sortOrder = filterInfo["sortOrder"];
  199. }
  200. var dto = GetMacStatus(filter, pageIndex, pageSize, sortField, sortOrder).data;
  201. var list = _mapper.Map<IEnumerable<ExportMacRecipeDto>>(dto).ToList();
  202. var book = DataExportHelper.EntityToExcel(list);
  203. MemoryStream ms = new MemoryStream();
  204. ms.Position = 0;
  205. book.Write(ms);
  206. ms.Dispose();
  207. ms.Close();
  208. await Task.CompletedTask;
  209. return File(ms.ToArray(), "application/octet-stream");
  210. }
  211. /// <summary>
  212. /// 导入数据
  213. /// </summary>
  214. /// <param name="importDto"></param>
  215. /// <returns></returns>
  216. [HttpPost]
  217. public EapResponse Import([FromBody] ImportDto importDto)
  218. {
  219. var res = new EapResponse { Code = 1, Msg = string.Empty };
  220. if (importDto.MacInfoes == null || importDto.MacInfoes.Count() <= 0)
  221. {
  222. res.Code = -1;
  223. res.Msg = "待导入的数据为空";
  224. return res;
  225. }
  226. using (IDatabase db = DbFactory.Base("eap"))
  227. {
  228. string errorinfo = string.Empty;
  229. var dal = new MacRecipeDal(db);
  230. db.BeginTrans();
  231. IList<string> existCodes = new List<string>();
  232. if (dal.ImportMachines(importDto.MacInfoes.ToArray(), Request.Headers["usercode"], existCodes, ref errorinfo) < 0)
  233. {
  234. db.Rollback();
  235. res.Code = -1;
  236. res.Msg = errorinfo;
  237. return res;
  238. }
  239. db.Commit();
  240. if (existCodes != null && existCodes.Count > 0)
  241. {
  242. res.Data = existCodes;
  243. res.Msg = $"机台[{string.Join(",", existCodes)}]在系统中已存在";
  244. }
  245. return res;
  246. }
  247. }
  248. /// <summary>
  249. /// 导入数据
  250. /// </summary>
  251. /// <param name="importDto"></param>
  252. /// <returns></returns>
  253. [HttpPost]
  254. public EapResponse ImportRest([FromBody] ImportDto importDto)
  255. {
  256. var res = new EapResponse { Code = 1, Msg = string.Empty };
  257. if (importDto.MacInfoes == null || importDto.MacInfoes.Count() <= 0)
  258. {
  259. res.Code = -1;
  260. res.Msg = "待导入的数据为空";
  261. return res;
  262. }
  263. using (IDatabase db = DbFactory.Base("eap"))
  264. {
  265. string errorinfo = string.Empty;
  266. var dal = new MacRecipeDal(db);
  267. db.BeginTrans();
  268. if (dal.UpdateMachines(importDto.MacInfoes.ToArray(), Request.Headers["usercode"], ref errorinfo) < 0)
  269. {
  270. db.Rollback();
  271. res.Code = -1;
  272. res.Msg = errorinfo;
  273. return res;
  274. }
  275. db.Commit();
  276. return res;
  277. }
  278. }
  279. /// <summary>
  280. /// 机台信息导出
  281. /// </summary>
  282. /// <param name="filterInfo"></param>
  283. /// <returns></returns>
  284. [HttpPost]
  285. public async Task<IActionResult> Export(IDictionary<string, string> filterInfo)
  286. {
  287. var filter = string.Empty;
  288. if (filterInfo.ContainsKey("filter"))
  289. filter = filterInfo["filter"];
  290. using (IDatabase db = DbFactory.Base(connStr))
  291. {
  292. db.BeginTrans();
  293. var dal = new MacRecipeDal(db);
  294. string errorinfo = string.Empty;
  295. int total;
  296. var roles = dal.GetMachineInfoes(filter, 1, 100000, "MacId", "asc", out total, ref errorinfo);
  297. var list = _mapper.Map<IEnumerable<MachineRecipeExportDto>>(roles).ToList();
  298. var book = DataExportHelper.EntityToExcel(list);
  299. MemoryStream ms = new MemoryStream();
  300. ms.Position = 0;
  301. book.Write(ms);
  302. ms.Dispose();
  303. ms.Close();
  304. await Task.CompletedTask;
  305. return File(ms.ToArray(), "application/octet-stream");
  306. }
  307. }
  308. /// <summary>
  309. /// 机台IP软件版本导出
  310. /// </summary>
  311. /// <param name="filterInfo"></param>
  312. /// <returns></returns>
  313. [HttpPost]
  314. public async Task<IActionResult> ExportIP(IDictionary<string, string> filterInfo)
  315. {
  316. var filter = string.Empty;
  317. if (filterInfo.ContainsKey("filter"))
  318. filter = filterInfo["filter"];
  319. var roles = Get(filter, 1, 10000).data;
  320. var list = _mapper.Map<IEnumerable<ExportIP>>(roles).ToList();
  321. var book = DataExportHelper.EntityToExcel(list);
  322. MemoryStream ms = new MemoryStream();
  323. ms.Position = 0;
  324. book.Write(ms);
  325. ms.Dispose();
  326. ms.Close();
  327. await Task.CompletedTask;
  328. return File(ms.ToArray(), "application/octet-stream");
  329. }
  330. }
  331. }