TmtnPushMsgResultController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using Microsoft.AspNetCore.Authorization;
  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.Api.Controllers;
  8. using Ropin.Inspection.Common;
  9. using Ropin.Inspection.Common.Helper;
  10. using Ropin.Inspection.Model;
  11. using Ropin.Inspection.Model.Common;
  12. using Ropin.Inspection.Service;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net.Http;
  18. using System.Threading.Tasks;
  19. namespace Ropin.Inspection.Api
  20. {
  21. public class TmtnPushMsgResultController : BaseController
  22. {
  23. public ILogger<TmtnPushMsgResultController> _logger { get; }
  24. private readonly ITmtnPushMsgResultService _TmtnPushMsgResultService;
  25. private readonly IHttpClientFactory _httpClientFactory;
  26. private readonly IPushMsgService _pushMsgService;
  27. /// <summary>
  28. /// 构造函数
  29. /// </summary>
  30. /// <param name="TmtnPushMsgResultService"></param>
  31. /// <param name="logger"></param>
  32. public TmtnPushMsgResultController(ITmtnPushMsgResultService TmtnPushMsgResultService, IPushMsgService pushMsgService, IHttpClientFactory httpClientFactory, ILogger<TmtnPushMsgResultController> logger)
  33. {
  34. _TmtnPushMsgResultService = TmtnPushMsgResultService;
  35. _logger = logger;
  36. _httpClientFactory = httpClientFactory;
  37. _pushMsgService = pushMsgService;
  38. }
  39. /// <summary>
  40. /// 通过id获取推送消息结果信息
  41. /// </summary>
  42. /// <param name="id"></param>
  43. /// <returns></returns>
  44. [HttpGet("GetPushMsgResultAsync/{id}")]
  45. public async Task<ApiResult> GetPushMsgResultAsync(string id)
  46. {
  47. if (string.IsNullOrEmpty(id))
  48. {
  49. return new ApiResult(ReturnCode.GeneralError);
  50. }
  51. try
  52. {
  53. var content = await _TmtnPushMsgResultService.GetConditionAsync(new TmtnPushMsgResultSearchModel { C_ID = id });
  54. return new ApiResult<TmtnPushMsgResultViewModel>(content.FirstOrDefault());
  55. }
  56. catch (Exception ex)
  57. {
  58. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  59. }
  60. }
  61. /// <summary>
  62. /// 获取所有推送消息结果
  63. /// </summary>
  64. /// <returns></returns>
  65. [HttpGet("GetPushMsgResultsAsync")]
  66. public async Task<ApiResult> GetPushMsgResultsAsync()
  67. {
  68. try
  69. {
  70. var contentList = await _TmtnPushMsgResultService.GetAllAsync();
  71. return new ApiResult<IEnumerable<TmtnPushMsgResultViewModel>>(contentList);
  72. }
  73. catch (Exception ex)
  74. {
  75. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  76. }
  77. }
  78. /// <summary>
  79. /// 通过推送消息结果名称条件查询
  80. /// </summary>
  81. /// <param name="searchModel"></param>
  82. /// <returns></returns>
  83. [HttpPost("GetPushMsgResultsByAsync")]
  84. public async Task<ApiResult> GetPushMsgResultsByAsync(TmtnPushMsgResultSearchModel searchModel)
  85. {
  86. if (searchModel == null)
  87. {
  88. return new ApiResult(ReturnCode.ArgsError);
  89. }
  90. //searchModel.IsPagination = false;
  91. try
  92. {
  93. var contentList = await _TmtnPushMsgResultService.GetConditionAsync(searchModel);
  94. return new ApiResult<PagesModel<TmtnPushMsgResultViewModel>>(new PagesModel<TmtnPushMsgResultViewModel>(contentList, searchModel));
  95. }
  96. catch (Exception ex)
  97. {
  98. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  99. }
  100. }
  101. /// <summary>
  102. /// 通过设备ID取报警信息
  103. /// </summary>
  104. /// <param name="id"></param>
  105. /// <param type="type"></param>
  106. /// <returns></returns>
  107. [HttpGet("GetPushMsgResultByDevStoreIdAsync/{id}/{type}")]
  108. [AllowAnonymous]
  109. public async Task<IEnumerable<TpushMsgModel>> GetPushMsgResultByDevStoreIdAsync(string id,string type)
  110. {
  111. if (string.IsNullOrEmpty(id))
  112. {
  113. return await Task.FromResult<IEnumerable<TpushMsgModel>>(null);
  114. }
  115. try
  116. {
  117. IEnumerable<TmtnPushMsgResultViewModel> contentList = await _TmtnPushMsgResultService.GetConditionAsync(new TmtnPushMsgResultSearchModel { C_DevStoreCode = id });
  118. if (type == "0")
  119. return contentList.Where(x => x.PushMsg.C_MsgTypeCode == "MSG_TYPE_012" || x.PushMsg.C_MsgTypeCode == "MSG_TYPE_013").Select(y=>y.PushMsg);
  120. else if (type == "1")
  121. return contentList.Where(x => x.PushMsg.C_MsgTypeCode != "MSG_TYPE_012" && x.PushMsg.C_MsgTypeCode != "MSG_TYPE_013").Select(y => y.PushMsg);
  122. else
  123. return await Task.FromResult<IEnumerable<TpushMsgModel>>(null);
  124. }
  125. catch (Exception ex)
  126. {
  127. return await Task.FromResult<IEnumerable<TpushMsgModel>>(null);
  128. }
  129. }
  130. /// <summary>
  131. /// 创建推送消息结果
  132. /// </summary>
  133. /// <param name="content"></param>
  134. /// <returns></returns>
  135. [HttpPost("CreatePushMsgResultAsync")]
  136. public async Task<ApiResult> CreatePushMsgResultAsync(TmtnPushMsgResultViewModel content)
  137. {
  138. if (content == null)
  139. {
  140. return new ApiResult(ReturnCode.ArgsError);
  141. }
  142. try
  143. {
  144. await _TmtnPushMsgResultService.CreateOneAsync(content);
  145. }
  146. catch (Exception ex)
  147. {
  148. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  149. }
  150. return new ApiResult(ReturnCode.Success);
  151. }
  152. /// <summary>
  153. /// 删除推送消息结果
  154. /// </summary>
  155. /// <param name="id"></param>
  156. /// <returns></returns>
  157. [HttpDelete("DeletePushMsgResultAsync/{id}")]
  158. public async Task<ApiResult> DeletePushMsgResultAsync(string id)
  159. {
  160. if (string.IsNullOrEmpty(id))
  161. {
  162. return new ApiResult(ReturnCode.GeneralError);
  163. }
  164. try
  165. {
  166. await _TmtnPushMsgResultService.DeleteAsync(id);
  167. }
  168. catch (Exception ex)
  169. {
  170. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  171. }
  172. return new ApiResult(ReturnCode.Success);
  173. }
  174. /// <summary>
  175. /// 更新推送消息结果
  176. /// </summary>
  177. /// <param name="id"></param>
  178. /// <param name="updateModel"></param>
  179. /// <returns></returns>
  180. [HttpPut("UpdatePushMsgResultAsync/{id}")]
  181. public async Task<ApiResult> UpdatePushMsgResultAsync(string id, TmtnPushMsgResultUpdateModel updateModel)
  182. {
  183. if (string.IsNullOrEmpty(id))
  184. {
  185. return new ApiResult(ReturnCode.GeneralError);
  186. }
  187. try
  188. {
  189. await _TmtnPushMsgResultService.UpdateAsync(id, updateModel);
  190. }
  191. catch (Exception ex)
  192. {
  193. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  194. }
  195. return new ApiResult(ReturnCode.Success);
  196. }
  197. /// <summary>
  198. /// 测试推送消息
  199. /// </summary>
  200. /// <returns></returns>
  201. [HttpGet("PushMsgTestAsync")]
  202. [AllowAnonymous]
  203. public ApiResult PushMsgTestAsync()
  204. {
  205. try
  206. {
  207. //EmailHelper.SendEmail("154817501@qq.com", "环保数字","测试主题", "测试推送消息");
  208. _pushMsgService.PushAlarmMsgAsync(new TpushMsgModel {
  209. C_DevStoreCode = "f59ce998-6fd7-4f19-99eb-4fe567433d48",
  210. C_MsgTypeCode = "MSG_TYPE_001",
  211. Msg ="压力表要维修",
  212. Subject = "压力表维修通知",
  213. DevNumber="20222214510",
  214. DevName = "Dovc氨氮处理器",
  215. }, "压力表维修通知");
  216. return new ApiResult(ReturnCode.Success, "chenggong");
  217. }
  218. catch (Exception ex)
  219. {
  220. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  221. }
  222. }
  223. /// <summary>
  224. /// 发送邮件(附件为报警记录)
  225. /// </summary>
  226. /// <param name="searchModel"></param>
  227. /// <returns></returns>
  228. [HttpPost("SendPushMsgResultsToEmail")]
  229. public async Task SendPushMsgResultsToEmail(SendPushMsgResultsToEmailModel searchModel)
  230. {
  231. if (searchModel == null)
  232. {
  233. return;
  234. }
  235. //searchModel.IsPagination = false;
  236. try
  237. {
  238. var contentList = await _TmtnPushMsgResultService.GetConditionAsync(searchModel);
  239. var pushMsg = contentList.Select(x => x.PushMsg).ToList();
  240. IWorkbook workbook = new XSSFWorkbook();
  241. ISheet sheet = workbook.CreateSheet("sheet1");
  242. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 6));
  243. var titleRow = sheet.CreateRow(0);
  244. titleRow.Height = 20 * 25;
  245. ICell titleCell = titleRow.CreateCell(0);
  246. titleCell.SetCellValue("设备报警记录");
  247. //第一行字体样式
  248. IFont font = workbook.CreateFont();
  249. font.IsBold = true;
  250. font.FontHeightInPoints = 16;
  251. font.FontName = "宋体";
  252. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  253. titleCellStyle.SetFont(font);
  254. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  255. //边框
  256. titleCellStyle.BorderBottom = BorderStyle.Thin;
  257. titleCellStyle.BorderLeft = BorderStyle.Thin;
  258. titleCellStyle.BorderRight = BorderStyle.Thin;
  259. titleCellStyle.BorderTop = BorderStyle.Thin;
  260. titleCell.CellStyle = titleCellStyle;
  261. var headRow = sheet.CreateRow(1);
  262. headRow.CreateCell(0).SetCellValue("设备名称");
  263. headRow.CreateCell(1).SetCellValue("设备编号");
  264. headRow.CreateCell(2).SetCellValue("报警日期");
  265. headRow.CreateCell(3).SetCellValue("报警主题");
  266. headRow.CreateCell(4).SetCellValue("报警消息");
  267. headRow.CreateCell(5).SetCellValue("消息类型");
  268. headRow.CreateCell(6).SetCellValue("报警来源");
  269. //第二行,列名
  270. IFont font1 = workbook.CreateFont();
  271. font1.IsBold = true;
  272. font1.FontHeightInPoints = 12;
  273. font1.FontName = "宋体";
  274. ICellStyle headCellStyle = workbook.CreateCellStyle();
  275. headCellStyle.SetFont(font1);
  276. //边框
  277. headCellStyle.BorderBottom = BorderStyle.Thin;
  278. headCellStyle.BorderLeft = BorderStyle.Thin;
  279. headCellStyle.BorderRight = BorderStyle.Thin;
  280. headCellStyle.BorderTop = BorderStyle.Thin;
  281. foreach (var item in headRow.Cells)
  282. {
  283. item.CellStyle = headCellStyle;
  284. }
  285. int start = 2;
  286. IFont font3 = workbook.CreateFont();
  287. font3.FontHeightInPoints = 9;
  288. font3.FontName = "宋体";
  289. ICellStyle contentCellStyle = workbook.CreateCellStyle();
  290. contentCellStyle.SetFont(font3);
  291. //边框
  292. contentCellStyle.BorderBottom = BorderStyle.Thin;
  293. contentCellStyle.BorderLeft = BorderStyle.Thin;
  294. contentCellStyle.BorderRight = BorderStyle.Thin;
  295. contentCellStyle.BorderTop = BorderStyle.Thin;
  296. foreach (var item in pushMsg)
  297. {
  298. var row = sheet.CreateRow(start);
  299. row.CreateCell(0).SetCellValue(item.DevName);
  300. row.CreateCell(1).SetCellValue(item.DevNumber);
  301. row.CreateCell(2).SetCellValue(item.CreateOn);
  302. row.CreateCell(3).SetCellValue(item.Subject);
  303. row.CreateCell(4).SetCellValue(item.Msg);
  304. string msgVal = DataDictionaryHelper.GetValByCode(item.C_MsgTypeCode);
  305. row.CreateCell(5).SetCellValue(msgVal);
  306. row.CreateCell(6).SetCellValue(!string.IsNullOrEmpty(item.UserName) ? $"{item.UserName}({item.UserMobile})" : "");
  307. start++;
  308. foreach (var cell in row.Cells)
  309. {
  310. cell.CellStyle = contentCellStyle;
  311. }
  312. }
  313. // 自适应单元格
  314. for (int i = 0; i < sheet.LastRowNum; i++)
  315. {
  316. sheet.AutoSizeRow(i);
  317. }
  318. for (int i = 0; i < 7; i++)
  319. {
  320. sheet.AutoSizeColumn(i, true);
  321. }
  322. using (var stream = new NpoiMemoryStream())
  323. {
  324. workbook.Write(stream);
  325. stream.Seek(0, SeekOrigin.Begin);
  326. string emailName = $"{searchModel.DevName} 设备报警记录报表 {searchModel.D_Start?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.D_End?.ToString("yyyy-MM-dd HH:mm:ss")}";
  327. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
  328. }
  329. }
  330. catch (Exception ex)
  331. {
  332. throw;
  333. }
  334. }
  335. /// <summary>
  336. ///消息报警天数计量
  337. /// </summary>
  338. /// <param name="searchModel"></param>
  339. /// <returns></returns>
  340. //[HttpPost("GetPushMsgResultContentAsync")]
  341. //public async Task<ApiResult> GetPushMsgResultContentAsync(TmtnPushMsgResultSearchModel searchModel)
  342. //{
  343. // if (searchModel == null)
  344. // {
  345. // return new ApiResult(ReturnCode.GeneralError);
  346. // }
  347. // try
  348. // {
  349. // DevAlarmCount data = await _TmtnPushMsgResultService.GetPushMsgResultContentAsync(searchModel);
  350. // return new ApiResult<DevAlarmCount>(data);
  351. // }
  352. // catch (Exception ex)
  353. // {
  354. // return new ApiResult(ReturnCode.GeneralError, ex.Message);
  355. // }
  356. //}
  357. }
  358. }