TsysMessageController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using Castle.Core.Internal;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using NPOI.SS.UserModel;
  5. using NPOI.XSSF.UserModel;
  6. using Ropin.Inspection.Api.Common;
  7. using Ropin.Inspection.Common.Helper;
  8. using Ropin.Inspection.Common;
  9. using Ropin.Inspection.Model;
  10. using Ropin.Inspection.Model.Common;
  11. using Ropin.Inspection.Service;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. namespace Ropin.Inspection.Api.Controllers
  18. {
  19. public class TsysMessageController : BaseController
  20. {
  21. public ILogger<TsysMessageController> _logger { get; }
  22. private readonly ITsysMessageService _TsysMessageService;
  23. /// <summary>
  24. /// 构造函数
  25. /// </summary>
  26. /// <param name="TsysMessageService"></param>
  27. /// <param name="logger"></param>
  28. public TsysMessageController(ITsysMessageService TsysMessageService, ILogger<TsysMessageController> logger)
  29. {
  30. _TsysMessageService = TsysMessageService;
  31. _logger = logger;
  32. }
  33. /// <summary>
  34. /// 通过ID获取消息信息
  35. /// </summary>
  36. /// <param name="id"></param>
  37. /// <returns></returns>
  38. [HttpGet("GetMessageAsync/{id}")]
  39. public async Task<ApiResult> GetMessageAsync(string id)
  40. {
  41. if (string.IsNullOrEmpty(id))
  42. {
  43. return new ApiResult(ReturnCode.GeneralError);
  44. }
  45. try
  46. {
  47. var content = await _TsysMessageService.GetByIdAsync(id);
  48. return new ApiResult<TsysMessageViewModel>(content);
  49. }
  50. catch (Exception ex)
  51. {
  52. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  53. }
  54. }
  55. /// <summary>
  56. /// 获取所有消息
  57. /// </summary>
  58. /// <returns></returns>
  59. [HttpGet("GetMessagesAsync")]
  60. public async Task<ApiResult> GetMessagesAsync()
  61. {
  62. try
  63. {
  64. var contentList = await _TsysMessageService.GetAllAsync();
  65. return new ApiResult<IEnumerable<TsysMessageViewModel>>(contentList);
  66. }
  67. catch (Exception ex)
  68. {
  69. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  70. }
  71. }
  72. /// <summary>
  73. /// 通过消息名称条件查询
  74. /// </summary>
  75. /// <param name="searchModel"></param>
  76. /// <returns></returns>
  77. [HttpPost("GetMessagesByAsync")]
  78. public async Task<ApiResult> GetMessagesByAsync(TsysMessageSearchModel searchModel)
  79. {
  80. if (searchModel == null)
  81. {
  82. return new ApiResult(ReturnCode.ArgsError);
  83. }
  84. try
  85. {
  86. var contentList = await _TsysMessageService.GetConditionAsync(searchModel);
  87. return new ApiResult<PagesModel<TsysMessageViewModel>>(new PagesModel<TsysMessageViewModel>(contentList, searchModel));
  88. }
  89. catch (Exception ex)
  90. {
  91. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  92. }
  93. }
  94. /// <summary>
  95. /// 通过消息名称条件查询【内容实体化】
  96. /// </summary>
  97. /// <param name="searchModel"></param>
  98. /// <returns></returns>
  99. [HttpPost("GetConditionScreenAsync")]
  100. public async Task<ApiResult> GetConditionScreenAsync(TsysMessageSearchModel searchModel)
  101. {
  102. if (searchModel == null)
  103. {
  104. return new ApiResult(ReturnCode.ArgsError);
  105. }
  106. try
  107. {
  108. var contentList = await _TsysMessageService.GetConditionScreenAsync(searchModel);
  109. return new ApiResult<PagesModel<TsysMessageModel>>(new PagesModel<TsysMessageModel>(contentList, searchModel));
  110. }
  111. catch (Exception ex)
  112. {
  113. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  114. }
  115. }
  116. /// <summary>
  117. /// 创建消息
  118. /// </summary>
  119. /// <param name="content"></param>
  120. /// <returns></returns>
  121. [HttpPost("CreateMessageAsync")]
  122. public async Task<ApiResult> CreateMessageAsync(TsysMessageViewModel content)
  123. {
  124. if (content == null)
  125. {
  126. return new ApiResult(ReturnCode.ArgsError);
  127. }
  128. try
  129. {
  130. await _TsysMessageService.CreateOneAsync(content);
  131. }
  132. catch (Exception ex)
  133. {
  134. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  135. }
  136. return new ApiResult(ReturnCode.Success);
  137. }
  138. /// <summary>
  139. /// 删除消息
  140. /// </summary>
  141. /// <param name="id"></param>
  142. /// <returns></returns>
  143. [HttpDelete("DeleteMessageAsync/{id}")]
  144. public async Task<ApiResult> DeleteMessageAsync(string id)
  145. {
  146. if (string.IsNullOrEmpty(id))
  147. {
  148. return new ApiResult(ReturnCode.GeneralError);
  149. }
  150. try
  151. {
  152. await _TsysMessageService.DeleteAsync(id);
  153. }
  154. catch (Exception ex)
  155. {
  156. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  157. }
  158. return new ApiResult(ReturnCode.Success);
  159. }
  160. /// <summary>
  161. /// 更新消息
  162. /// </summary>
  163. /// <param name="id"></param>
  164. /// <param name="updateModel"></param>
  165. /// <returns></returns>
  166. [HttpPut("UpdateMessageAsync/{id}")]
  167. public async Task<ApiResult> UpdateMessageAsync(string id, TsysMessageUpdateModel updateModel)
  168. {
  169. if (string.IsNullOrEmpty(id))
  170. {
  171. return new ApiResult(ReturnCode.GeneralError);
  172. }
  173. try
  174. {
  175. await _TsysMessageService.UpdateAsync(id, updateModel);
  176. }
  177. catch (Exception ex)
  178. {
  179. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  180. }
  181. return new ApiResult(ReturnCode.Success);
  182. }
  183. /// <summary>
  184. /// 更新消息状态
  185. /// </summary>
  186. /// <param name="id"></param>
  187. /// <param name="msgStstus"></param>
  188. /// <returns></returns>
  189. [HttpGet("UpdateMessageAsync/{id}/{msgStstus}")]
  190. public async Task<ApiResult> UpdateMsgStuats(string id, int msgStstus)
  191. {
  192. if (string.IsNullOrEmpty(id))
  193. {
  194. return new ApiResult(ReturnCode.GeneralError);
  195. }
  196. try
  197. {
  198. await _TsysMessageService.UpdateMsgStatusAsync(id, msgStstus);
  199. }
  200. catch (Exception ex)
  201. {
  202. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  203. }
  204. return new ApiResult(ReturnCode.Success);
  205. }
  206. /// <summary>
  207. /// 发送邮件(附件为报警记录)
  208. /// </summary>
  209. /// <param name="searchModel"></param>
  210. /// <returns></returns>
  211. [HttpPost("SendMessageToEmail")]
  212. public async Task SendMessageToEmail(SendMessageToEmailModel searchModel)
  213. {
  214. if (searchModel == null)
  215. {
  216. return;
  217. }
  218. try
  219. {
  220. TsysMessageSearchModel tsysMessage = new TsysMessageSearchModel
  221. {
  222. IsPagination = false,
  223. C_PushMsgToCode= searchModel.C_PushMsgToCode,
  224. C_StoreCode = searchModel.C_StoreCode,
  225. C_DevCode = searchModel.C_DevCode,
  226. C_MsgTypeCode = searchModel.C_MsgTypeCode,
  227. MsgTypeList = searchModel.MsgTypeList,
  228. C_Content = searchModel.C_Content,
  229. I_GenerationType = searchModel.I_GenerationType,
  230. MsgStatus = searchModel.MsgStatus,
  231. C_Status = searchModel.C_Status,
  232. BeginTime = searchModel.BeginTime,
  233. EndTime = searchModel.EndTime,
  234. };
  235. var contentList = await _TsysMessageService.GetConditionScreenAsync(tsysMessage);
  236. var pushMsg = contentList.Select(x => x.PushMsg).ToList();
  237. IWorkbook workbook = new XSSFWorkbook();
  238. ISheet sheet = workbook.CreateSheet("sheet1");
  239. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 6));
  240. var titleRow = sheet.CreateRow(0);
  241. titleRow.Height = 20 * 25;
  242. ICell titleCell = titleRow.CreateCell(0);
  243. titleCell.SetCellValue("设备报警记录");
  244. //第一行字体样式
  245. IFont font = workbook.CreateFont();
  246. font.IsBold = true;
  247. font.FontHeightInPoints = 16;
  248. font.FontName = "宋体";
  249. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  250. titleCellStyle.SetFont(font);
  251. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  252. //边框
  253. titleCellStyle.BorderBottom = BorderStyle.Thin;
  254. titleCellStyle.BorderLeft = BorderStyle.Thin;
  255. titleCellStyle.BorderRight = BorderStyle.Thin;
  256. titleCellStyle.BorderTop = BorderStyle.Thin;
  257. titleCell.CellStyle = titleCellStyle;
  258. var headRow = sheet.CreateRow(1);
  259. headRow.CreateCell(0).SetCellValue("设备名称");
  260. headRow.CreateCell(1).SetCellValue("设备编号");
  261. headRow.CreateCell(2).SetCellValue("报警时间");
  262. headRow.CreateCell(3).SetCellValue("报警类型");
  263. headRow.CreateCell(4).SetCellValue("报警内容");
  264. headRow.CreateCell(5).SetCellValue("报警来源");
  265. //第二行,列名
  266. IFont font1 = workbook.CreateFont();
  267. font1.IsBold = true;
  268. font1.FontHeightInPoints = 12;
  269. font1.FontName = "宋体";
  270. ICellStyle headCellStyle = workbook.CreateCellStyle();
  271. headCellStyle.SetFont(font1);
  272. //边框
  273. headCellStyle.BorderBottom = BorderStyle.Thin;
  274. headCellStyle.BorderLeft = BorderStyle.Thin;
  275. headCellStyle.BorderRight = BorderStyle.Thin;
  276. headCellStyle.BorderTop = BorderStyle.Thin;
  277. foreach (var item in headRow.Cells)
  278. {
  279. item.CellStyle = headCellStyle;
  280. }
  281. int start = 2;
  282. IFont font3 = workbook.CreateFont();
  283. font3.FontHeightInPoints = 9;
  284. font3.FontName = "宋体";
  285. ICellStyle contentCellStyle = workbook.CreateCellStyle();
  286. contentCellStyle.SetFont(font3);
  287. //边框
  288. contentCellStyle.BorderBottom = BorderStyle.Thin;
  289. contentCellStyle.BorderLeft = BorderStyle.Thin;
  290. contentCellStyle.BorderRight = BorderStyle.Thin;
  291. contentCellStyle.BorderTop = BorderStyle.Thin;
  292. foreach (var item in pushMsg)
  293. {
  294. var row = sheet.CreateRow(start);
  295. row.CreateCell(0).SetCellValue(item.DevName);
  296. row.CreateCell(1).SetCellValue(item.DevNumber);
  297. row.CreateCell(2).SetCellValue(item.CreateOn);
  298. row.CreateCell(3).SetCellValue(item.Subject);
  299. row.CreateCell(4).SetCellValue(item.Msg);
  300. row.CreateCell(5).SetCellValue(!string.IsNullOrEmpty(item.UserName) ? item.UserName : "");
  301. start++;
  302. foreach (var cell in row.Cells)
  303. {
  304. cell.CellStyle = contentCellStyle;
  305. }
  306. }
  307. // 自适应单元格
  308. for (int i = 0; i < sheet.LastRowNum; i++)
  309. {
  310. sheet.AutoSizeRow(i);
  311. }
  312. for (int i = 0; i < 7; i++)
  313. {
  314. sheet.AutoSizeColumn(i, true);
  315. }
  316. using (var stream = new NpoiMemoryStream())
  317. {
  318. workbook.Write(stream);
  319. stream.Seek(0, SeekOrigin.Begin);
  320. string emailName = $"{searchModel.DevName} 设备报警记录报表 {searchModel.BeginTime?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.EndTime?.ToString("yyyy-MM-dd HH:mm:ss")}";
  321. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. throw;
  327. }
  328. }
  329. }
  330. }