TmtnDevOpsContentController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.XSSF.UserModel;
  7. using Renci.SshNet;
  8. using Ropin.Inspection.Api.Common;
  9. using Ropin.Inspection.Api.Controllers;
  10. using Ropin.Inspection.Model;
  11. using Ropin.Inspection.Model.ViewModel.DEV;
  12. using Ropin.Inspection.Service;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel.DataAnnotations;
  16. using System.Linq;
  17. using System.Security.Claims;
  18. using System.Threading.Tasks;
  19. namespace Ropin.Inspection.Api
  20. {
  21. public class TmtnDevOpsContentController : BaseController
  22. {
  23. public ILogger<TmtnDevOpsContentController> _logger { get; }
  24. private readonly ITmtnDevOpsContentService _TmtnDevOpsContentService;
  25. /// <summary>
  26. /// 构造函数
  27. /// </summary>
  28. /// <param name="TmtnDevOpsContentService"></param>
  29. /// <param name="logger"></param>
  30. public TmtnDevOpsContentController(ITmtnDevOpsContentService TmtnDevOpsContentService, ILogger<TmtnDevOpsContentController> logger)
  31. {
  32. _TmtnDevOpsContentService = TmtnDevOpsContentService;
  33. _logger = logger;
  34. }
  35. /// <summary>
  36. /// 通过id获取运维内容信息
  37. /// </summary>
  38. /// <param name="id"></param>
  39. /// <returns></returns>
  40. [HttpGet("GetDevOpsContentAsync/{id}")]
  41. public async Task<ApiResult> GetDevOpsContentAsync(string id)
  42. {
  43. if (string.IsNullOrEmpty(id))
  44. {
  45. return new ApiResult(ReturnCode.GeneralError);
  46. }
  47. try
  48. {
  49. var content = await _TmtnDevOpsContentService.GetConditionAsync(new TmtnDevOpsContentSearchModel { C_ID = id });
  50. return new ApiResult<TmtnDevOpsContentViewModel>(content.FirstOrDefault());
  51. }
  52. catch (Exception ex)
  53. {
  54. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  55. }
  56. }
  57. /// <summary>
  58. /// 获取所有运维内容
  59. /// </summary>
  60. /// <returns></returns>
  61. [HttpGet("GetDevOpsContentsAsync")]
  62. public async Task<ApiResult> GetDevOpsContentsAsync()
  63. {
  64. try
  65. {
  66. var contentList = await _TmtnDevOpsContentService.GetAllAsync();
  67. return new ApiResult<IEnumerable<TmtnDevOpsContentViewModel>>(contentList);
  68. }
  69. catch (Exception ex)
  70. {
  71. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  72. }
  73. }
  74. /// <summary>
  75. /// 通过运维内容名称条件查询
  76. /// </summary>
  77. /// <param name="searchModel"></param>
  78. /// <returns></returns>
  79. [HttpPost("GetDevOpsContentsByAsync")]
  80. public async Task<ApiResult> GetDevOpsContentsByAsync(TmtnDevOpsContentSearchModel searchModel)
  81. {
  82. if (searchModel == null)
  83. {
  84. return new ApiResult(ReturnCode.ArgsError);
  85. }
  86. searchModel.IsPagination = false;
  87. try
  88. {
  89. var contentList = await _TmtnDevOpsContentService.GetConditionAsync(searchModel);
  90. return new ApiResult<PagesModel<TmtnDevOpsContentViewModel>>(new PagesModel<TmtnDevOpsContentViewModel>(contentList, searchModel));
  91. }
  92. catch (Exception ex)
  93. {
  94. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  95. }
  96. }
  97. /// <summary>
  98. /// 创建运维内容
  99. /// </summary>
  100. /// <param name="content"></param>
  101. /// <returns></returns>
  102. [HttpPost("CreateDevOpsContentAsync")]
  103. public async Task<ApiResult> CreateDevOpsContentAsync(TmtnDevOpsContentViewModel content)
  104. {
  105. if (content == null)
  106. {
  107. return new ApiResult(ReturnCode.ArgsError);
  108. }
  109. try
  110. {
  111. await _TmtnDevOpsContentService.CreateOneAsync(content);
  112. }
  113. catch (Exception ex)
  114. {
  115. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  116. }
  117. return new ApiResult(ReturnCode.Success);
  118. }
  119. /// <summary>
  120. /// 删除运维内容
  121. /// </summary>
  122. /// <param name="id"></param>
  123. /// <returns></returns>
  124. [HttpDelete("DeleteDevOpsContentAsync/{id}")]
  125. public async Task<ApiResult> DeleteDevOpsContentAsync(string id)
  126. {
  127. if (string.IsNullOrEmpty(id))
  128. {
  129. return new ApiResult(ReturnCode.GeneralError);
  130. }
  131. try
  132. {
  133. await _TmtnDevOpsContentService.DeleteAsync(id);
  134. }
  135. catch (Exception ex)
  136. {
  137. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  138. }
  139. return new ApiResult(ReturnCode.Success);
  140. }
  141. /// <summary>
  142. /// 更新运维内容
  143. /// </summary>
  144. /// <param name="id"></param>
  145. /// <param name="updateModel"></param>
  146. /// <returns></returns>
  147. [HttpPut("UpdateDevOpsContentAsync/{id}")]
  148. public async Task<ApiResult> UpdateDevOpsContentAsync(string id, TmtnDevOpsContentUpdateModel updateModel)
  149. {
  150. if (string.IsNullOrEmpty(id))
  151. {
  152. return new ApiResult(ReturnCode.GeneralError);
  153. }
  154. try
  155. {
  156. await _TmtnDevOpsContentService.UpdateAsync(id, updateModel);
  157. }
  158. catch (Exception ex)
  159. {
  160. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  161. }
  162. return new ApiResult(ReturnCode.Success);
  163. }
  164. [HttpPost("ImportExcel")]
  165. public async Task<ApiResult> ImportExcel([Required]string storeCode, [Required] IFormFile file)
  166. {
  167. try
  168. {
  169. var exName = file.FileName.Split('.').Last().ToLower();
  170. if (exName != "xlsx" && exName != "xls")
  171. {
  172. throw new Exception(":不是Excel文件");
  173. }
  174. IWorkbook wk = null;
  175. if (exName == "xlsx")
  176. {
  177. wk = new XSSFWorkbook(file.OpenReadStream());
  178. }
  179. else
  180. {
  181. wk = new HSSFWorkbook(file.OpenReadStream());
  182. }
  183. DevOpsContentImportModel importModel = new DevOpsContentImportModel();
  184. importModel.DevOpsContents = new List<TmtnDevOpsContentViewModel>();
  185. //获取第一个sheet
  186. ISheet sheet = wk.GetSheetAt(0);
  187. for (int i = 1; i <= sheet.LastRowNum; i++)
  188. {
  189. var row = sheet.GetRow(i);
  190. importModel.DevOpsContents.Add(new TmtnDevOpsContentViewModel
  191. {
  192. C_ID = Guid.NewGuid().ToString(),
  193. C_Number = row.Cells[0].ToString(),
  194. I_Sort = Convert.ToInt32(row.Cells[1].ToString()),
  195. C_Name = row.Cells[2].ToString(),
  196. C_AlarmLevel = row.Cells[3].ToString(),
  197. C_Status = row.Cells[4].ToString(),
  198. C_Remark = row.Cells[5].ToString(),
  199. C_CreateBy = GetUserId(User),
  200. D_CreateOn = DateTime.Now,
  201. C_StoreCode = storeCode
  202. });
  203. }
  204. await _TmtnDevOpsContentService.ImportExecl(importModel);
  205. return new ApiResult(ReturnCode.Success);
  206. }
  207. catch (Exception ex)
  208. {
  209. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  210. }
  211. }
  212. }
  213. }