123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using Castle.Core.Internal;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using Ropin.Inspection.Api.Common;
- using Ropin.Inspection.Common.Helper;
- using Ropin.Inspection.Common;
- using Ropin.Inspection.Model;
- using Ropin.Inspection.Model.Common;
- using Ropin.Inspection.Service;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Api.Controllers
- {
- public class TsysMessageController : BaseController
- {
- public ILogger<TsysMessageController> _logger { get; }
- private readonly ITsysMessageService _TsysMessageService;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="TsysMessageService"></param>
- /// <param name="logger"></param>
- public TsysMessageController(ITsysMessageService TsysMessageService, ILogger<TsysMessageController> logger)
- {
- _TsysMessageService = TsysMessageService;
- _logger = logger;
- }
- /// <summary>
- /// 通过ID获取消息信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [HttpGet("GetMessageAsync/{id}")]
- public async Task<ApiResult> GetMessageAsync(string id)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- var content = await _TsysMessageService.GetByIdAsync(id);
- return new ApiResult<TsysMessageViewModel>(content);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 获取所有消息
- /// </summary>
- /// <returns></returns>
- [HttpGet("GetMessagesAsync")]
- public async Task<ApiResult> GetMessagesAsync()
- {
- try
- {
- var contentList = await _TsysMessageService.GetAllAsync();
- return new ApiResult<IEnumerable<TsysMessageViewModel>>(contentList);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 通过消息名称条件查询
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("GetMessagesByAsync")]
- public async Task<ApiResult> GetMessagesByAsync(TsysMessageSearchModel searchModel)
- {
- if (searchModel == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- var contentList = await _TsysMessageService.GetConditionAsync(searchModel);
- return new ApiResult<PagesModel<TsysMessageViewModel>>(new PagesModel<TsysMessageViewModel>(contentList, searchModel));
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 通过消息名称条件查询【内容实体化】
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("GetConditionScreenAsync")]
- public async Task<ApiResult> GetConditionScreenAsync(TsysMessageSearchModel searchModel)
- {
- if (searchModel == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- var contentList = await _TsysMessageService.GetConditionScreenAsync(searchModel);
- return new ApiResult<PagesModel<TsysMessageModel>>(new PagesModel<TsysMessageModel>(contentList, searchModel));
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 创建消息
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- [HttpPost("CreateMessageAsync")]
- public async Task<ApiResult> CreateMessageAsync(TsysMessageViewModel content)
- {
- if (content == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- await _TsysMessageService.CreateOneAsync(content);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 删除消息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [HttpDelete("DeleteMessageAsync/{id}")]
- public async Task<ApiResult> DeleteMessageAsync(string id)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- await _TsysMessageService.DeleteAsync(id);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 更新消息
- /// </summary>
- /// <param name="id"></param>
- /// <param name="updateModel"></param>
- /// <returns></returns>
- [HttpPut("UpdateMessageAsync/{id}")]
- public async Task<ApiResult> UpdateMessageAsync(string id, TsysMessageUpdateModel updateModel)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- await _TsysMessageService.UpdateAsync(id, updateModel);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 更新消息状态
- /// </summary>
- /// <param name="id"></param>
- /// <param name="msgStstus"></param>
- /// <returns></returns>
- [HttpGet("UpdateMessageAsync/{id}/{msgStstus}")]
- public async Task<ApiResult> UpdateMsgStuats(string id, int msgStstus)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- await _TsysMessageService.UpdateMsgStatusAsync(id, msgStstus);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 发送邮件(附件为报警记录)
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("SendMessageToEmail")]
- public async Task SendMessageToEmail(SendMessageToEmailModel searchModel)
- {
- if (searchModel == null)
- {
- return;
- }
- try
- {
- TsysMessageSearchModel tsysMessage = new TsysMessageSearchModel
- {
- IsPagination = false,
- C_PushMsgToCode= searchModel.C_PushMsgToCode,
- C_StoreCode = searchModel.C_StoreCode,
- C_DevCode = searchModel.C_DevCode,
- C_MsgTypeCode = searchModel.C_MsgTypeCode,
- MsgTypeList = searchModel.MsgTypeList,
- C_Content = searchModel.C_Content,
- I_GenerationType = searchModel.I_GenerationType,
- MsgStatus = searchModel.MsgStatus,
- C_Status = searchModel.C_Status,
- BeginTime = searchModel.BeginTime,
- EndTime = searchModel.EndTime,
- };
- var contentList = await _TsysMessageService.GetConditionScreenAsync(tsysMessage);
- var pushMsg = contentList.Select(x => x.PushMsg).ToList();
- IWorkbook workbook = new XSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("sheet1");
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 5));
- var titleRow = sheet.CreateRow(0);
- titleRow.Height = 20 * 25;
- ICell titleCell = titleRow.CreateCell(0);
- titleCell.SetCellValue("设备报警记录");
- //第一行字体样式
- IFont font = workbook.CreateFont();
- font.IsBold = true;
- font.FontHeightInPoints = 16;
- font.FontName = "宋体";
- ICellStyle titleCellStyle = workbook.CreateCellStyle();
- titleCellStyle.SetFont(font);
- titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
- //边框
- titleCellStyle.BorderBottom = BorderStyle.Thin;
- titleCellStyle.BorderLeft = BorderStyle.Thin;
- titleCellStyle.BorderRight = BorderStyle.Thin;
- titleCellStyle.BorderTop = BorderStyle.Thin;
- titleCell.CellStyle = titleCellStyle;
- var headRow = sheet.CreateRow(1);
- headRow.CreateCell(0).SetCellValue("设备名称");
- headRow.CreateCell(1).SetCellValue("设备编号");
- headRow.CreateCell(2).SetCellValue("报警时间");
- headRow.CreateCell(3).SetCellValue("报警类型");
- headRow.CreateCell(4).SetCellValue("报警内容");
- headRow.CreateCell(5).SetCellValue("报警来源");
- //第二行,列名
- IFont font1 = workbook.CreateFont();
- font1.IsBold = true;
- font1.FontHeightInPoints = 12;
- font1.FontName = "宋体";
- ICellStyle headCellStyle = workbook.CreateCellStyle();
- headCellStyle.SetFont(font1);
- //边框
- headCellStyle.BorderBottom = BorderStyle.Thin;
- headCellStyle.BorderLeft = BorderStyle.Thin;
- headCellStyle.BorderRight = BorderStyle.Thin;
- headCellStyle.BorderTop = BorderStyle.Thin;
- foreach (var item in headRow.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- int start = 2;
- IFont font3 = workbook.CreateFont();
- font3.FontHeightInPoints = 9;
- font3.FontName = "宋体";
- ICellStyle contentCellStyle = workbook.CreateCellStyle();
- contentCellStyle.SetFont(font3);
- //边框
- contentCellStyle.BorderBottom = BorderStyle.Thin;
- contentCellStyle.BorderLeft = BorderStyle.Thin;
- contentCellStyle.BorderRight = BorderStyle.Thin;
- contentCellStyle.BorderTop = BorderStyle.Thin;
- foreach (var item in pushMsg)
- {
- var row = sheet.CreateRow(start);
- row.CreateCell(0).SetCellValue(item.DevName);
- row.CreateCell(1).SetCellValue(item.DevNumber);
- row.CreateCell(2).SetCellValue(item.CreateOn);
- row.CreateCell(3).SetCellValue(item.Subject);
- row.CreateCell(4).SetCellValue(item.Msg);
- row.CreateCell(5).SetCellValue(!string.IsNullOrEmpty(item.UserName) ? item.UserName : "");
- start++;
- foreach (var cell in row.Cells)
- {
- cell.CellStyle = contentCellStyle;
- }
- }
- // 自适应单元格
- for (int i = 0; i < sheet.LastRowNum; i++)
- {
- sheet.AutoSizeRow(i);
- }
- for (int i = 0; i < 7; i++)
- {
- sheet.AutoSizeColumn(i, true);
- }
- using (var stream = new NpoiMemoryStream())
- {
- workbook.Write(stream);
- stream.Seek(0, SeekOrigin.Begin);
- string emailName = $"{searchModel.DevName} 设备报警记录报表 {searchModel.BeginTime?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.EndTime?.ToString("yyyy-MM-dd HH:mm:ss")}";
- EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
- }
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- }
- }
|