devHandController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using Ropin.Inspection.Api.Common;
  6. using Ropin.Inspection.Model;
  7. using Ropin.Inspection.Service;
  8. using Ropin.Inspection.Service.DEV.Interface;
  9. using System.Threading.Tasks;
  10. using System;
  11. using Ropin.Inspection.Model.ViewModel.DEV;
  12. using NPOI.SS.UserModel;
  13. using System.IO;
  14. using NPOI.Util;
  15. using NPOI.XWPF.UserModel;
  16. using NPOI.SS.Formula;
  17. using Newtonsoft.Json;
  18. namespace Ropin.Inspection.Api.Controllers.DEV
  19. {
  20. public class devHandController : BaseController
  21. {
  22. public ILogger<devHandController> _logger { get; }
  23. private readonly IDev_HandService _repository;
  24. /// <summary>
  25. /// 构造函数
  26. /// </summary>
  27. /// <param name="logger"></param>
  28. /// <param name="repository"></param>
  29. public devHandController( ILogger<devHandController> logger, IDev_HandService repository)
  30. {
  31. _logger = logger;
  32. _repository = repository;
  33. }
  34. /// <summary>
  35. /// 创建设备手持信息
  36. /// </summary>
  37. /// <param name="content"></param>
  38. /// <returns></returns>
  39. [HttpPost("CreatedevHandAsync")]
  40. public async Task<ApiResult> CreatedevHandAsync(dev_HandModel content)
  41. {
  42. if (content == null)
  43. {
  44. return new ApiResult(ReturnCode.ArgsError);
  45. }
  46. try
  47. {
  48. await _repository.CreateOneAsync(content);
  49. }
  50. catch (Exception ex)
  51. {
  52. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  53. }
  54. return new ApiResult(ReturnCode.Success);
  55. }
  56. /// <summary>
  57. /// 删除设备手持信息
  58. /// </summary>
  59. /// <param name="id"></param>
  60. /// <returns></returns>
  61. [HttpDelete("DeletedevHandAsync/{id}")]
  62. public async Task<ApiResult> DeletedevHandAsync(Guid id)
  63. {
  64. if (id==Guid.Empty)
  65. {
  66. return new ApiResult(ReturnCode.GeneralError);
  67. }
  68. try
  69. {
  70. await _repository.DeleteAsync(id);
  71. }
  72. catch (Exception ex)
  73. {
  74. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  75. }
  76. return new ApiResult(ReturnCode.Success);
  77. }
  78. /// <summary>
  79. /// 更新设备手持信息
  80. /// </summary>
  81. /// <param name="id"></param>
  82. /// <param name="updateModel"></param>
  83. /// <returns></returns>
  84. [HttpPut("UpdatedevHandAsync/{id}")]
  85. [AllowAnonymous]
  86. public async Task<ApiResult> UpdatedevHandAsync(Guid id, dev_HandModel updateModel)
  87. {
  88. if (id==Guid.Empty)
  89. {
  90. return new ApiResult(ReturnCode.GeneralError);
  91. }
  92. try
  93. {
  94. await _repository.UpdateAsync(id, updateModel);
  95. }
  96. catch (Exception ex)
  97. {
  98. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  99. }
  100. return new ApiResult(ReturnCode.Success);
  101. }
  102. /// <summary>
  103. /// 获取设备手持信息
  104. /// </summary>
  105. /// <returns></returns>
  106. [HttpPost("GetdevHandAsync")]
  107. [AllowAnonymous]
  108. public async Task<ApiResult> GetdevHandAsync(TdevHandSearchModel searchModel)
  109. {
  110. if (searchModel == null)
  111. {
  112. return new ApiResult(ReturnCode.ArgsError);
  113. }
  114. try
  115. {
  116. var contentList = await _repository.GetConditionAsync(searchModel);
  117. return new ApiResult<PagesModel<devHandViewModel>>(new PagesModel<devHandViewModel>(contentList, searchModel));
  118. }
  119. catch (Exception ex)
  120. {
  121. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  122. }
  123. }
  124. /// <summary>
  125. /// 通过id获取设备手持信息
  126. /// </summary>
  127. /// <returns></returns>
  128. [HttpPost("GetdevHandByIDAsync")]
  129. [AllowAnonymous]
  130. public async Task<ApiResult> GetdevHandByIDAsync(Guid Id)
  131. {
  132. if (Id==Guid.Empty)
  133. {
  134. return new ApiResult(ReturnCode.ArgsError);
  135. }
  136. try
  137. {
  138. devHandViewModel data = new devHandViewModel();
  139. data = await _repository.GetConditionByIdAsync(Id);
  140. return new ApiResult<devHandViewModel>(data);
  141. }
  142. catch (Exception ex)
  143. {
  144. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  145. }
  146. }
  147. /// <summary>
  148. /// 通过id获取设备手持信息返回word文件
  149. /// </summary>
  150. /// <returns></returns>
  151. [HttpPost("GetdevHandFileByIDAsync")]
  152. [AllowAnonymous]
  153. public async Task<IActionResult> GetdevHandFileByIDAsync(Guid Id)
  154. {
  155. try
  156. {
  157. devHandViewModel data = new devHandViewModel();
  158. data = await _repository.GetConditionByIdAsync(Id);
  159. XWPFDocument doc = new XWPFDocument();
  160. var paragraph = doc.CreateParagraph();
  161. var run = paragraph.CreateRun();
  162. run.FontSize = 16;
  163. string fileName = "null.docx";
  164. if (data!=null)
  165. {
  166. string json = JsonConvert.SerializeObject(data);
  167. run.SetText(json);
  168. fileName = data.C_ID + ".docx";
  169. }
  170. else
  171. {
  172. run.SetText("null");
  173. }
  174. using (MemoryStream stream = new MemoryStream())
  175. {
  176. doc.Write(stream);
  177. byte[] bytes = stream.ToArray();
  178. string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
  179. return File(bytes, contentType, fileName);
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. throw;
  185. }
  186. }
  187. }
  188. }