TmtnDevOpsRecordController.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. using FluentEmail.Core;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
  6. using Microsoft.Extensions.Logging;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.HSSF.Util;
  9. using NPOI.POIFS.Crypt.Dsig;
  10. using NPOI.SS.Formula;
  11. using NPOI.SS.UserModel;
  12. using NPOI.SS.Util;
  13. using NPOI.Util;
  14. using NPOI.XSSF.UserModel;
  15. using NPOI.XWPF.UserModel;
  16. using Ropin.Inspection.Api.Common;
  17. using Ropin.Inspection.Api.Controllers;
  18. using Ropin.Inspection.Common.Helper;
  19. using Ropin.Inspection.Model;
  20. using Ropin.Inspection.Model.Common;
  21. using Ropin.Inspection.Service;
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Drawing;
  25. using System.IO;
  26. using System.IO.Compression;
  27. using System.Linq;
  28. using System.Net.Http;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31. using System.Web;
  32. namespace Ropin.Inspection.Api
  33. {
  34. public class TmtnDevOpsRecordController : BaseController
  35. {
  36. public ILogger<TmtnDevOpsRecordController> _logger { get; }
  37. private readonly ITmtnDevOpsRecordService _TmtnDevOpsRecordService;
  38. private readonly ITpntStoreService _TpntStoreService;
  39. /// <summary>
  40. /// 构造函数
  41. /// </summary>
  42. /// <param name="TmtnDevOpsRecordService"></param>
  43. /// <param name="logger"></param>
  44. public TmtnDevOpsRecordController(ITmtnDevOpsRecordService TmtnDevOpsRecordService, ILogger<TmtnDevOpsRecordController> logger, ITpntStoreService TpntStoreService)
  45. {
  46. _TmtnDevOpsRecordService = TmtnDevOpsRecordService;
  47. _logger = logger;
  48. _TpntStoreService = TpntStoreService;
  49. }
  50. /// <summary>
  51. /// 通过id获取运维记录信息
  52. /// </summary>
  53. /// <param name="id"></param>
  54. /// <returns></returns>
  55. [HttpGet("GetDevOpsRecordAsync/{id}")]
  56. public async Task<ApiResult> GetDevOpsRecordAsync(string id)
  57. {
  58. if (string.IsNullOrEmpty(id))
  59. {
  60. return new ApiResult(ReturnCode.GeneralError);
  61. }
  62. try
  63. {
  64. var content = await _TmtnDevOpsRecordService.GetConditionAsync(new TmtnDevOpsRecordSearchModel { C_ID = id });
  65. return new ApiResult<TmtnDevOpsRecordViewModel>(content.FirstOrDefault());
  66. }
  67. catch (Exception ex)
  68. {
  69. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  70. }
  71. }
  72. /// <summary>
  73. /// 获取所有运维记录
  74. /// </summary>
  75. /// <returns></returns>
  76. [HttpGet("GetDevOpsRecordsAsync")]
  77. public async Task<ApiResult> GetDevOpsRecordsAsync()
  78. {
  79. try
  80. {
  81. var contentList = await _TmtnDevOpsRecordService.GetAllAsync();
  82. return new ApiResult<IEnumerable<TmtnDevOpsRecordViewModel>>(contentList);
  83. }
  84. catch (Exception ex)
  85. {
  86. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  87. }
  88. }
  89. /// <summary>
  90. /// 通过运维记录名称条件查询
  91. /// </summary>
  92. /// <param name="searchModel"></param>
  93. /// <returns></returns>
  94. [HttpPost("GetDevOpsRecordsByAsync")]
  95. public async Task<ApiResult> GetDevOpsRecordsByAsync(TmtnDevOpsRecordSearchModel searchModel)
  96. {
  97. if (searchModel == null)
  98. {
  99. return new ApiResult(ReturnCode.ArgsError);
  100. }
  101. searchModel.IsPagination = false;
  102. try
  103. {
  104. var contentList = await _TmtnDevOpsRecordService.GetConditionAsync(searchModel);
  105. return new ApiResult<PagesModel<TmtnDevOpsRecordViewModel>>(new PagesModel<TmtnDevOpsRecordViewModel>(contentList, searchModel));
  106. }
  107. catch (Exception ex)
  108. {
  109. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  110. }
  111. }
  112. /// <summary>
  113. /// 创建运维记录
  114. /// </summary>
  115. /// <param name="content"></param>
  116. /// <returns></returns>
  117. [HttpPost("CreateDevOpsRecordAsync")]
  118. public async Task<ApiResult> CreateDevOpsRecordAsync(TmtnDevOpsRecordViewModel content)
  119. {
  120. if (content == null)
  121. {
  122. return new ApiResult(ReturnCode.ArgsError);
  123. }
  124. try
  125. {
  126. await _TmtnDevOpsRecordService.CreateOneAsync(content);
  127. }
  128. catch (Exception ex)
  129. {
  130. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  131. }
  132. return new ApiResult(ReturnCode.Success);
  133. }
  134. /// <summary>
  135. /// 创建运维记录,一起提交
  136. /// </summary>
  137. /// <param name="record"></param>
  138. /// <returns></returns>
  139. [HttpPost("CreateDevOpsRecordListAsync")]
  140. public async Task<ApiResult> CreateDevOpsRecordListAsync(TmtnDevOpsRecordListCreateModel record)
  141. {
  142. if (record == null || record.TmtnRecordItemList == null || !record.TmtnRecordItemList.Any())
  143. {
  144. return new ApiResult(ReturnCode.ArgsError);
  145. }
  146. try
  147. {
  148. await _TmtnDevOpsRecordService.CreateDevOpsRecordListAsync(record);
  149. }
  150. catch (Exception ex)
  151. {
  152. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  153. }
  154. return new ApiResult(ReturnCode.Success);
  155. }
  156. /// <summary>
  157. /// 更新运维记录,一起提交
  158. /// </summary>
  159. /// <param name="record"></param>
  160. /// <returns></returns>
  161. [HttpPost("UpdataDevOpsRecordListAsync")]
  162. public async Task<ApiResult> UpdataDevOpsRecordListAsync(TmtnDevOpsRecordListUpdataModel record)
  163. {
  164. if (record == null || record.TmtnRecordItemList == null || !record.TmtnRecordItemList.Any())
  165. {
  166. return new ApiResult(ReturnCode.ArgsError);
  167. }
  168. try
  169. {
  170. await _TmtnDevOpsRecordService.UpdataDevOpsRecordListAsync(record);
  171. }
  172. catch (Exception ex)
  173. {
  174. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  175. }
  176. return new ApiResult(ReturnCode.Success);
  177. }
  178. /// <summary>
  179. /// 删除运维记录
  180. /// </summary>
  181. /// <param name="id"></param>
  182. /// <returns></returns>
  183. [HttpDelete("DeleteDevOpsRecordAsync/{id}")]
  184. public async Task<ApiResult> DeleteDevOpsRecordAsync(string id)
  185. {
  186. if (string.IsNullOrEmpty(id))
  187. {
  188. return new ApiResult(ReturnCode.GeneralError);
  189. }
  190. try
  191. {
  192. await _TmtnDevOpsRecordService.DeleteAsync(id);
  193. }
  194. catch (Exception ex)
  195. {
  196. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  197. }
  198. return new ApiResult(ReturnCode.Success);
  199. }
  200. /// <summary>
  201. /// 更新运维记录
  202. /// </summary>
  203. /// <param name="id"></param>
  204. /// <param name="updateModel"></param>
  205. /// <returns></returns>
  206. [HttpPut("UpdateDevOpsRecordAsync/{id}")]
  207. public async Task<ApiResult> UpdateDevOpsRecordAsync(string id, TmtnDevOpsRecordUpdateModel updateModel)
  208. {
  209. if (string.IsNullOrEmpty(id))
  210. {
  211. return new ApiResult(ReturnCode.GeneralError);
  212. }
  213. try
  214. {
  215. await _TmtnDevOpsRecordService.UpdateAsync(id, updateModel);
  216. }
  217. catch (Exception ex)
  218. {
  219. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  220. }
  221. return new ApiResult(ReturnCode.Success);
  222. }
  223. /// <summary>
  224. /// 条件获取运维记录
  225. /// </summary>
  226. /// <param name="searchModel"></param>
  227. /// <returns></returns>
  228. [HttpPost("GetRecordsConditionAsync")]
  229. public async Task<ApiResult> GetRecordsConditionAsync(TmtnDevOpsRecordDetailSearchModel searchModel)
  230. {
  231. if (searchModel == null)
  232. {
  233. return new ApiResult(ReturnCode.ArgsError);
  234. }
  235. try
  236. {
  237. var recordList = await _TmtnDevOpsRecordService.GetRecordsConditionAsync(searchModel);
  238. return new ApiResult<PagesModel<TmtnDevOpsRecordDetailViewModel>>(new PagesModel<TmtnDevOpsRecordDetailViewModel>(recordList?.ToList(), searchModel));
  239. }
  240. catch (Exception ex)
  241. {
  242. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  243. }
  244. }
  245. /// <summary>
  246. /// 条件获取运维工单记录
  247. /// </summary>
  248. /// <param name="searchModel"></param>
  249. /// <returns></returns>
  250. [HttpPost("GetDevOpsAsync")]
  251. public async Task<ApiResult> GetDevOpsAsync(TmtnDevOpsDetailSearchModel searchModel)
  252. {
  253. if (searchModel == null || string.IsNullOrEmpty(searchModel.C_StoreCode))
  254. {
  255. return new ApiResult(ReturnCode.ArgsError);
  256. }
  257. try
  258. {
  259. var recordList = await _TmtnDevOpsRecordService.GetDevOpsAsync(searchModel);
  260. return new ApiResult<PagesModel<TmtnDevOpsDetailViewModel>>(new PagesModel<TmtnDevOpsDetailViewModel>(recordList?.ToList(), searchModel));
  261. }
  262. catch (Exception ex)
  263. {
  264. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  265. }
  266. }
  267. /// <summary>
  268. /// 通过维保记录ID获取巡检详细信息,通过内容分组
  269. /// </summary>
  270. /// <param name="recordId"></param>
  271. /// <returns></returns>
  272. [HttpGet("GetRecordItemsGroupByRecordIdAsync/{recordId}")]
  273. [AllowAnonymous]
  274. public async Task<ApiResult> GetRecordItemsGroupByRecordIdAsync(string recordId)
  275. {
  276. if (string.IsNullOrEmpty(recordId))
  277. {
  278. return new ApiResult(ReturnCode.ArgsError);
  279. }
  280. try
  281. {
  282. List<List<TmtnDevOpsRecordDetailWithImageViewModel>> recordItems = await _TmtnDevOpsRecordService.GetRecordItemsGroupByRecordIdAsync(recordId);
  283. return new ApiResult<List<List<TmtnDevOpsRecordDetailWithImageViewModel>>>(recordItems);
  284. }
  285. catch (Exception ex)
  286. {
  287. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  288. }
  289. }
  290. /// <summary>
  291. /// 通过维保记录ID获取巡检详细信息
  292. /// </summary>
  293. /// <param name="recordId"></param>
  294. /// <returns></returns>
  295. [HttpGet("GetRecordItemsByRecordIdAsync/{recordId}")]
  296. [AllowAnonymous]
  297. public async Task<ApiResult> GetRecordItemsByRecordIdAsync(string recordId)
  298. {
  299. if (string.IsNullOrEmpty(recordId))
  300. {
  301. return new ApiResult(ReturnCode.ArgsError);
  302. }
  303. try
  304. {
  305. IEnumerable<TmtnDevOpsRecordDetailWithImageViewModel> recordItems = await _TmtnDevOpsRecordService.GetRecordItemsByRecordIdAsync(recordId);
  306. return new ApiResult<List<TmtnDevOpsRecordDetailWithImageViewModel>>(recordItems.ToList());
  307. }
  308. catch (Exception ex)
  309. {
  310. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  311. }
  312. }
  313. /// <summary>
  314. /// 发送邮件(附件为维保记录)
  315. /// </summary>
  316. /// <param name="searchModel"></param>
  317. /// <returns></returns>
  318. [HttpPost("SendDevOpsToEmail")]
  319. public async Task SendDevOpsToEmail(SendDevOpsToEmailModel searchModel)
  320. {
  321. if (searchModel == null || string.IsNullOrEmpty(searchModel.C_StoreCode))
  322. {
  323. return;
  324. }
  325. try
  326. {
  327. var recordList = await _TmtnDevOpsRecordService.GetDevOpsAsync(searchModel);
  328. string zip = @"wwwroot\\ZipFile";
  329. if (!System.IO.Directory.Exists(zip))
  330. {
  331. Directory.CreateDirectory(zip);
  332. }
  333. string zipPaht = zip + "\\维保记录下载_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip";
  334. #region Excel
  335. IWorkbook workbook = new XSSFWorkbook();
  336. ISheet sheet = workbook.CreateSheet("sheet1");
  337. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 6));
  338. var titleRow = sheet.CreateRow(0);
  339. titleRow.Height = 20 * 25;
  340. NPOI.SS.UserModel.ICell titleCell = titleRow.CreateCell(0);
  341. titleCell.SetCellValue("设备维保记录");
  342. //第一行字体样式
  343. IFont font = workbook.CreateFont();
  344. font.IsBold = true;
  345. font.FontHeightInPoints = 16;
  346. font.FontName = "宋体";
  347. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  348. titleCellStyle.SetFont(font);
  349. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  350. //边框
  351. titleCellStyle.BorderBottom = BorderStyle.Thin;
  352. titleCellStyle.BorderLeft = BorderStyle.Thin;
  353. titleCellStyle.BorderRight = BorderStyle.Thin;
  354. titleCellStyle.BorderTop = BorderStyle.Thin;
  355. titleCell.CellStyle = titleCellStyle;
  356. var headRow = sheet.CreateRow(1);
  357. //headRow.CreateCell(0).SetCellValue("序 号");
  358. headRow.CreateCell(0).SetCellValue("设备名称");
  359. headRow.CreateCell(1).SetCellValue("维保位置");
  360. headRow.CreateCell(2).SetCellValue("维保人员 ");
  361. headRow.CreateCell(3).SetCellValue("维保时间");
  362. headRow.CreateCell(4).SetCellValue("维保状态");
  363. headRow.CreateCell(5).SetCellValue("备注");
  364. headRow.CreateCell(6).SetCellValue("记录查看");
  365. //第二行,列名
  366. IFont font1 = workbook.CreateFont();
  367. font1.IsBold = true;
  368. font1.FontHeightInPoints = 12;
  369. font1.FontName = "宋体";
  370. ICellStyle headCellStyle = workbook.CreateCellStyle();
  371. headCellStyle.SetFont(font1);
  372. //边框
  373. headCellStyle.BorderBottom = BorderStyle.Thin;
  374. headCellStyle.BorderLeft = BorderStyle.Thin;
  375. headCellStyle.BorderRight = BorderStyle.Thin;
  376. headCellStyle.BorderTop = BorderStyle.Thin;
  377. foreach (var item in headRow.Cells)
  378. {
  379. item.CellStyle = headCellStyle;
  380. }
  381. #region 超链接
  382. // 创建样式
  383. ICellStyle styleLInk = workbook.CreateCellStyle();
  384. // 设置字体颜色为蓝色
  385. IFont fontLink = workbook.CreateFont();
  386. fontLink.IsBold = false;
  387. fontLink.FontHeightInPoints = 12;
  388. fontLink.FontName = "宋体";
  389. fontLink.Color = IndexedColors.Green.Index;
  390. styleLInk.SetFont(fontLink);
  391. // 设置下划线
  392. fontLink.Underline = FontUnderlineType.Single;
  393. styleLInk.SetFont(fontLink);
  394. styleLInk.Alignment = HorizontalAlignment.Center; //字体居中
  395. //边框
  396. styleLInk.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  397. styleLInk.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  398. styleLInk.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  399. styleLInk.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  400. styleLInk.WrapText = true;
  401. #endregion
  402. int start = 2;
  403. IFont font3 = workbook.CreateFont();
  404. font3.FontHeightInPoints = 9;
  405. font3.FontName = "宋体";
  406. ICellStyle contentCellStyle = workbook.CreateCellStyle();
  407. contentCellStyle.SetFont(font3);
  408. //边框
  409. contentCellStyle.BorderBottom = BorderStyle.Thin;
  410. contentCellStyle.BorderLeft = BorderStyle.Thin;
  411. contentCellStyle.BorderRight = BorderStyle.Thin;
  412. contentCellStyle.BorderTop = BorderStyle.Thin;
  413. foreach (var item in recordList)
  414. {
  415. if (item!=null)
  416. {
  417. var row = sheet.CreateRow(start);
  418. row.CreateCell(0).SetCellValue(item.DevStoreName);
  419. row.CreateCell(1).SetCellValue(item.C_Name);
  420. row.CreateCell(2).SetCellValue(item.C_CreateByName);
  421. row.CreateCell(3).SetCellValue(item.D_CreateOn.ToString("yyyy-MM-dd hh:mm:ss"));
  422. switch (item.C_Status)
  423. {
  424. case "1":
  425. row.CreateCell(4).SetCellValue("维保上报");
  426. break;
  427. case "2":
  428. row.CreateCell(4).SetCellValue("维保确认");
  429. break;
  430. case "3":
  431. row.CreateCell(4).SetCellValue("正在维保");
  432. break;
  433. case "4":
  434. row.CreateCell(4).SetCellValue("维保完成");
  435. break;
  436. case "5":
  437. row.CreateCell(4).SetCellValue("维保取消");
  438. break;
  439. case "6":
  440. row.CreateCell(4).SetCellValue("维保返工");
  441. break;
  442. case "7":
  443. row.CreateCell(4).SetCellValue("维保完成确认");
  444. break;
  445. default:
  446. row.CreateCell(4).SetCellValue("未知状态");
  447. break;
  448. }
  449. row.CreateCell(5).SetCellValue("");
  450. //超链接
  451. NPOI.SS.UserModel.ICell cellLink = row.CreateCell(6);
  452. cellLink.SetCellValue("附件");
  453. IHyperlink link = new XSSFHyperlink(HyperlinkType.Url);
  454. link.Address = ("维保记录/" + item.C_DevOpsCode + ".docx");
  455. cellLink.Hyperlink = link;
  456. start++;
  457. foreach (var cell in row.Cells)
  458. {
  459. cell.CellStyle = contentCellStyle;
  460. }
  461. cellLink.CellStyle = styleLInk;
  462. }
  463. }
  464. // 自适应单元格
  465. for (int i = 0; i < sheet.LastRowNum; i++)
  466. {
  467. sheet.AutoSizeRow(i);
  468. }
  469. for (int i = 0; i < 7; i++)
  470. {
  471. sheet.AutoSizeColumn(i, true);
  472. }
  473. #endregion
  474. using (var zipFile = new FileStream(zipPaht, FileMode.Create))
  475. {
  476. // 列表Excel
  477. using (var memoryStream = new NpoiMemoryStream())
  478. {
  479. workbook.Write(memoryStream);
  480. byte[] excelData = memoryStream.ToArray();
  481. using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
  482. {
  483. archive.CreateEntry("维保记录报表.xlsx").Open().Write(excelData, 0, excelData.Length);
  484. }
  485. }
  486. }
  487. //内容word
  488. foreach (var cell in recordList.ToList())
  489. {
  490. if (cell != null)
  491. {
  492. List<List<TmtnDevOpsRecordDetailWithImageViewModel>> recordItems = await _TmtnDevOpsRecordService.GetRecordItemsGroupByRecordIdAsync(cell.C_DevOpsCode);
  493. if (recordItems.Count > 0)
  494. {
  495. XWPFDocument doc = new XWPFDocument();
  496. var paragraph = doc.CreateParagraph();
  497. var run1 = paragraph.CreateRun();
  498. run1.FontSize = 18;
  499. run1.SetText("维保主题:" + cell.C_Name);
  500. int serialNum = 1;
  501. foreach (var recordItem in recordItems)
  502. {
  503. if (recordItem==null) { continue; }
  504. var p0 = doc.CreateParagraph();
  505. var run0 = p0.CreateRun();
  506. run0.FontSize = 16;
  507. run0.SetText(" " + serialNum.ToString() + "、" + recordItem.FirstOrDefault()?.C_Name);
  508. int serialNum1 = 1;
  509. foreach (var item in recordItem)
  510. {
  511. if (item == null) { continue; }
  512. var p1 = doc.CreateParagraph();
  513. var run = p1.CreateRun();
  514. run.FontSize = 14;
  515. string status = string.Empty;
  516. switch (item.C_Status)
  517. {
  518. case "1":
  519. item.C_Status = "维保上报";
  520. break;
  521. case "2":
  522. item.C_Status = "维保确认";
  523. break;
  524. case "3":
  525. item.C_Status = "正在维保";
  526. break;
  527. case "4":
  528. item.C_Status = "维保完成";
  529. break;
  530. case "5":
  531. item.C_Status = "维保取消";
  532. break;
  533. case "6":
  534. item.C_Status = "维保返工";
  535. break;
  536. case "7":
  537. item.C_Status = "维保完成确认";
  538. break;
  539. }
  540. run.SetText(" " + serialNum.ToString() + "." + serialNum1.ToString() + "、" + item.C_Status);
  541. run.AddCarriageReturn();
  542. run.AppendText(" 操作时间:" + item.D_CreateOn);
  543. run.AddCarriageReturn();
  544. run.AppendText(" 状态:" + item.C_Status);
  545. run.AddCarriageReturn();
  546. run.AppendText(" 处理人员:" + item.ReportUserName);
  547. run.AddCarriageReturn();
  548. run.AppendText(" 现场描述:" + item.C_Remark);
  549. run.AddCarriageReturn();
  550. run.AppendText(" 现场记录:" + item.C_Record);
  551. if (item.RecordImageList != null && item.RecordImageList.Count() > 0)
  552. {
  553. run.AddCarriageReturn();
  554. run.AppendText(" 现场拍照:");
  555. foreach (var path in item.RecordImageList)
  556. {
  557. string pathFile = path.C_ImageURL;
  558. if (pathFile[0].ToString() == @"/")
  559. {
  560. pathFile = pathFile.Substring(1);
  561. }
  562. if (!System.IO.File.Exists(pathFile))
  563. {
  564. pathFile = @"wwwroot/error.png";
  565. }
  566. FileStream fileStream = new FileStream(pathFile, FileMode.Open, FileAccess.Read);
  567. run.AddCarriageReturn();
  568. run.AddPicture(fileStream, 6, pathFile, Units.ToEMU(100), Units.ToEMU(100));
  569. }
  570. }
  571. serialNum1++;
  572. }
  573. serialNum++;
  574. }
  575. using (var zipFile = new FileStream(zipPaht, FileMode.OpenOrCreate))
  576. {
  577. using (var memoryStream = new NpoiMemoryStream())
  578. {
  579. doc.Write(memoryStream);
  580. byte[] excelData = memoryStream.ToArray();
  581. using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
  582. {
  583. archive.CreateEntry("维保记录/" + cell.C_DevOpsCode + ".docx").Open().Write(excelData, 0, excelData.Length);
  584. }
  585. }
  586. }
  587. }
  588. }
  589. }
  590. var zipFile1 = new FileStream(zipPaht, FileMode.Open);
  591. //using var stream = new NpoiMemoryStream();
  592. //workbook.Write(stream);
  593. //stream.Seek(0, SeekOrigin.Begin);
  594. string emailName = $"{searchModel.DevName}设备维保记录{searchModel.Start.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.End.ToString("yyyy-MM-dd HH:mm:ss")}";
  595. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.zip", "application/zip", zipFile1);
  596. //EmailHelper.SendEmail(searchModel.Mails, $"{searchModel.Start}-{searchModel.End}设备维保记录报表", "", "报表见附件", $"{searchModel.Start}-{searchModel.End}设备维保记录报表.xlsx", "application/vnd.ms-excel", stream);
  597. System.IO.File.Delete(zipPaht);
  598. }
  599. catch (Exception ex)
  600. {
  601. throw;
  602. }
  603. }
  604. /// <summary>
  605. /// 导出维保记录压缩包
  606. /// </summary>
  607. /// <param name="searchModel"></param>
  608. /// <returns></returns>
  609. [HttpPost("ExportZip")]
  610. [AllowAnonymous]
  611. public async Task<IActionResult> ExportZip(TmtnDevOpsDetailSearchModel searchModel)
  612. {
  613. var recordList = await _TmtnDevOpsRecordService.GetDevOpsAsync(searchModel);
  614. string zip = @"wwwroot\\ZipFile";
  615. if (!System.IO.Directory.Exists(zip))
  616. {
  617. Directory.CreateDirectory(zip);
  618. }
  619. string zipPaht = zip + "\\维保记录下载.zip";
  620. System.IO.File.Delete(zipPaht);
  621. TpntStoreViewModel content = null;
  622. if (!string.IsNullOrEmpty(searchModel.C_StoreCode))
  623. {
  624. Guid guid = Guid.Parse(searchModel.C_StoreCode);
  625. content = await _TpntStoreService.GetByIdAsync(guid);
  626. }
  627. IWorkbook workbook = new XSSFWorkbook();
  628. ISheet sheet = workbook.CreateSheet();
  629. #region
  630. //标题加粗居中.
  631. IFont font = workbook.CreateFont();
  632. font.IsBold = true;
  633. font.FontHeightInPoints = 25;
  634. font.FontName = "宋体";
  635. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  636. titleCellStyle.SetFont(font);
  637. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  638. //边框
  639. titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  640. titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  641. titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  642. titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  643. titleCellStyle.WrapText = true;
  644. #endregion
  645. #region
  646. //表头加粗居中.
  647. IFont font1 = workbook.CreateFont();
  648. font1.IsBold = true;
  649. font1.FontHeightInPoints = 14;
  650. font1.FontName = "宋体";
  651. ICellStyle headCellStyle = workbook.CreateCellStyle();
  652. headCellStyle.SetFont(font1);
  653. headCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  654. //边框
  655. headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  656. headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  657. headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  658. headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  659. headCellStyle.WrapText = true;
  660. #endregion
  661. #region
  662. //行.
  663. IFont font2 = workbook.CreateFont();
  664. font2.IsBold = false;
  665. font2.FontHeightInPoints = 12;
  666. font2.FontName = "宋体";
  667. ICellStyle rowCellStyle = workbook.CreateCellStyle();
  668. rowCellStyle.SetFont(font2);
  669. rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  670. //边框
  671. rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  672. rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  673. rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  674. rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  675. rowCellStyle.WrapText = true;
  676. #endregion
  677. #region 超链接
  678. // 创建样式
  679. ICellStyle styleLInk = workbook.CreateCellStyle();
  680. // 设置字体颜色为蓝色
  681. IFont fontLink = workbook.CreateFont();
  682. fontLink.IsBold = false;
  683. fontLink.FontHeightInPoints = 12;
  684. fontLink.FontName = "宋体";
  685. fontLink.Color = IndexedColors.Green.Index;
  686. styleLInk.SetFont(fontLink);
  687. // 设置下划线
  688. fontLink.Underline = FontUnderlineType.Single;
  689. styleLInk.SetFont(fontLink);
  690. styleLInk.Alignment = HorizontalAlignment.Center; //字体居中
  691. //边框
  692. styleLInk.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  693. styleLInk.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  694. styleLInk.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  695. styleLInk.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  696. styleLInk.WrapText = true;
  697. #endregion
  698. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 7));
  699. IRow title= sheet.CreateRow(0);
  700. title.Height = 30 * 25;
  701. title.CreateCell(0).SetCellValue("维保记录报表"+ (content == null ? "" : "(" + content.C_Name + ")"));
  702. foreach (var item in title.Cells)
  703. {
  704. item.CellStyle = titleCellStyle;
  705. }
  706. IRow head= sheet.CreateRow(1);
  707. head.CreateCell(0).SetCellValue("序号");
  708. head.CreateCell(1).SetCellValue("维保单编号");
  709. head.CreateCell(2).SetCellValue("维保位置");
  710. head.CreateCell(3).SetCellValue("设备名称");
  711. head.CreateCell(4).SetCellValue("申报人员");
  712. head.CreateCell(5).SetCellValue("维保时间");
  713. head.CreateCell(6).SetCellValue("位置编号");
  714. head.CreateCell(7).SetCellValue("记录查看");
  715. foreach (var item in head.Cells)
  716. {
  717. item.CellStyle = headCellStyle;
  718. }
  719. if (content != null)
  720. {
  721. byte[] imageBytes = null;
  722. string imagePath = content.C_Logo; // 指定图片的路径
  723. if (!System.IO.File.Exists(imagePath))
  724. {
  725. imagePath = @"./wwwroot/null.jpeg";
  726. }
  727. using (FileStream fs = new FileStream(imagePath, FileMode.Open))
  728. {
  729. imageBytes = new byte[fs.Length];
  730. fs.Read(imageBytes, 0, (int)fs.Length);
  731. }
  732. int pictureIdx = workbook.AddPicture(imageBytes, NPOI.SS.UserModel.PictureType.JPEG); // 将图像数据添加为图片资源
  733. IDrawing drawingPatriarch = sheet.CreateDrawingPatriarch(); // 创建绘制对象
  734. IClientAnchor anchor = workbook.GetCreationHelper().CreateClientAnchor();
  735. anchor.Row1 = 0;
  736. anchor.Col1 = 8;
  737. anchor.Row2 = 3;
  738. anchor.Col2 = 10;
  739. IPicture picture = drawingPatriarch.CreatePicture(anchor, pictureIdx); // 创建图片对象
  740. }
  741. int rowNumber = 2; int number = 1;
  742. if (recordList.Any() && recordList.FirstOrDefault() != null)
  743. {
  744. recordList.ForEach(x =>
  745. {
  746. IRow content = sheet.CreateRow(rowNumber);
  747. content.CreateCell(0).SetCellValue(number);
  748. content.CreateCell(1).SetCellValue(x.C_DevOpsCode);
  749. content.CreateCell(2).SetCellValue(x.C_Name);
  750. content.CreateCell(3).SetCellValue(x.DevStoreName);
  751. content.CreateCell(4).SetCellValue(x.C_CreateByName);
  752. content.CreateCell(5).SetCellValue(x.D_CreateOn.ToString("yyyy-MM-dd HH:mm"));
  753. content.CreateCell(6).SetCellValue(x.C_Number);
  754. //超链接
  755. NPOI.SS.UserModel.ICell cell = content.CreateCell(7);
  756. cell.SetCellValue("附件");
  757. IHyperlink link = new XSSFHyperlink(HyperlinkType.Url);
  758. link.Address = ("维保记录/" + x.C_DevOpsCode + ".docx");
  759. cell.Hyperlink = link;
  760. foreach (var item in content.Cells)
  761. {
  762. item.CellStyle = rowCellStyle;
  763. }
  764. cell.CellStyle = styleLInk;
  765. rowNumber++; number++;
  766. });
  767. }
  768. else
  769. {
  770. IRow content1 = sheet.CreateRow(rowNumber);
  771. content1.CreateCell(0).SetCellValue("暂无数据");
  772. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 0, 7));
  773. }
  774. for (int i = 0; i <= 7; i++)
  775. {
  776. if (i == 0)
  777. {
  778. sheet.SetColumnWidth(i, 2000);
  779. }
  780. else if (i == 1)
  781. {
  782. sheet.SetColumnWidth(i, 7000);
  783. }
  784. else
  785. {
  786. sheet.SetColumnWidth(i, 4500);
  787. }
  788. }
  789. using (var zipFile = new FileStream(zipPaht, FileMode.Create))
  790. {
  791. // 列表Excel
  792. using (var memoryStream = new NpoiMemoryStream())
  793. {
  794. workbook.Write(memoryStream);
  795. byte[] excelData = memoryStream.ToArray();
  796. using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
  797. {
  798. archive.CreateEntry("维保记录报表.xlsx").Open().Write(excelData, 0, excelData.Length);
  799. }
  800. }
  801. }
  802. //内容word
  803. foreach (var cell in recordList.ToList())
  804. {
  805. if (cell!=null)
  806. {
  807. List<List<TmtnDevOpsRecordDetailWithImageViewModel>> recordItems = await _TmtnDevOpsRecordService.GetRecordItemsGroupByRecordIdAsync(cell.C_DevOpsCode);
  808. if (recordItems.Count > 0)
  809. {
  810. XWPFDocument doc = new XWPFDocument();
  811. var paragraph = doc.CreateParagraph();
  812. var run1 = paragraph.CreateRun();
  813. run1.FontSize = 18;
  814. run1.SetText("维保主题:" + cell.C_Name);
  815. int serialNum = 1;
  816. foreach (var recordItem in recordItems)
  817. {
  818. if(recordItem==null) { continue; }
  819. var p0 = doc.CreateParagraph();
  820. var run0 = p0.CreateRun();
  821. run0.FontSize = 16;
  822. run0.SetText(" " + serialNum.ToString() + "、" + recordItem.FirstOrDefault()?.C_Name);
  823. int serialNum1 = 1;
  824. foreach (var item in recordItem)
  825. {
  826. if(item==null) continue;
  827. var p1 = doc.CreateParagraph();
  828. var run = p1.CreateRun();
  829. run.FontSize = 14;
  830. string status = string.Empty;
  831. switch (item.C_Status)
  832. {
  833. case "1":
  834. item.C_Status = "维保上报";
  835. break;
  836. case "2":
  837. item.C_Status = "维保确认";
  838. break;
  839. case "3":
  840. item.C_Status = "正在维保";
  841. break;
  842. case "4":
  843. item.C_Status = "维保完成";
  844. break;
  845. case "5":
  846. item.C_Status = "维保取消";
  847. break;
  848. case "6":
  849. item.C_Status = "维保返工";
  850. break;
  851. case "7":
  852. item.C_Status = "维保完成确认";
  853. break;
  854. }
  855. run.SetText(" " + serialNum.ToString() + "." + serialNum1.ToString());
  856. run.AddCarriageReturn();
  857. run.AppendText(" 操作时间:" + item.D_CreateOn);
  858. run.AddCarriageReturn();
  859. run.AppendText(" 状态:" + item.C_Status);
  860. run.AddCarriageReturn();
  861. run.AppendText(" 处理人员:" + item.ReportUserName);
  862. run.AddCarriageReturn();
  863. run.AppendText(" 现场描述:" + item.C_Remark);
  864. run.AddCarriageReturn();
  865. run.AppendText(" 现场记录:" + item.C_Record);
  866. if (item.RecordImageList != null && item.RecordImageList.Count() > 0)
  867. {
  868. run.AddCarriageReturn();
  869. run.AppendText(" 现场拍照:");
  870. foreach (var path in item.RecordImageList)
  871. {
  872. string pathFile = path.C_ImageURL;
  873. if (pathFile[0].ToString() == @"/")
  874. {
  875. pathFile = pathFile.Substring(1);
  876. }
  877. if (!System.IO.File.Exists(pathFile))
  878. {
  879. pathFile = @"wwwroot/error.png";
  880. }
  881. FileStream fileStream = new FileStream(pathFile, FileMode.Open, FileAccess.Read);
  882. run.AddCarriageReturn();
  883. run.AddPicture(fileStream, 6, pathFile, Units.ToEMU(100), Units.ToEMU(100));
  884. }
  885. }
  886. serialNum1++;
  887. }
  888. serialNum++;
  889. }
  890. using (var zipFile = new FileStream(zipPaht, FileMode.OpenOrCreate))
  891. {
  892. using (var memoryStream = new NpoiMemoryStream())
  893. {
  894. doc.Write(memoryStream);
  895. byte[] excelData = memoryStream.ToArray();
  896. using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
  897. {
  898. archive.CreateEntry("维保记录/" + cell.C_DevOpsCode + ".docx").Open().Write(excelData, 0, excelData.Length);
  899. }
  900. }
  901. }
  902. }
  903. }
  904. }
  905. var zipFile1 = new FileStream(zipPaht, FileMode.Open);
  906. // 将压缩后的数据作为响应返回给客户端(这里假设你使用的是HTTP GET请求)
  907. return File(zipFile1, "application/zip", "维保记录报表.zip"); // 这里假设你的文件类型是.xlsx的GZip压缩文件。你可以根据需要调整MIME类型和文件扩展名。
  908. }
  909. /// <summary>
  910. /// 发送邮件(附件为维保详情)
  911. /// </summary>
  912. /// <param name="searchModel"></param>
  913. /// <returns></returns>
  914. [HttpPost("SendDevOpsItemToEmail")]
  915. public async Task<ApiResult> SendDevOpsItemToEmail(SendDevOpsToEmailModel searchModel)
  916. {
  917. if (searchModel == null && string.IsNullOrEmpty(searchModel.C_DevOpsCode))
  918. {
  919. return new ApiResult(ReturnCode.ArgsError, "请输入参数");
  920. }
  921. try
  922. {
  923. List<List<TmtnDevOpsRecordDetailWithImageViewModel>> recordItems = await _TmtnDevOpsRecordService.GetRecordItemsGroupByRecordIdAsync(searchModel.C_DevOpsCode);
  924. string zip = @"wwwroot\\ZipFile";
  925. if (!System.IO.Directory.Exists(zip))
  926. {
  927. Directory.CreateDirectory(zip);
  928. }
  929. string zipPaht = zip + $"\\{searchModel.C_DevOpsCode}维保详情下载.zip";
  930. if (recordItems.Count > 0)
  931. {
  932. XWPFDocument doc = new XWPFDocument();
  933. var paragraph = doc.CreateParagraph();
  934. var run1 = paragraph.CreateRun();
  935. run1.FontSize = 18;
  936. run1.SetText(searchModel.DevName);
  937. int serialNum = 1;
  938. foreach (var recordItem in recordItems)
  939. {
  940. if (recordItem==null)
  941. {
  942. continue;
  943. }
  944. var p0 = doc.CreateParagraph();
  945. var run0 = p0.CreateRun();
  946. run0.FontSize = 16;
  947. run0.SetText(" " + serialNum.ToString() + "、" + recordItem.FirstOrDefault()?.C_Name);
  948. int serialNum1 = 1;
  949. foreach (var item in recordItem)
  950. {
  951. if (item==null)
  952. {
  953. continue;
  954. }
  955. var p1 = doc.CreateParagraph();
  956. var run = p1.CreateRun();
  957. run.FontSize = 14;
  958. string status = string.Empty;
  959. switch (item.C_Status)
  960. {
  961. case "1":
  962. item.C_Status = "维保上报";
  963. break;
  964. case "2":
  965. item.C_Status = "维保确认";
  966. break;
  967. case "3":
  968. item.C_Status = "正在维保";
  969. break;
  970. case "4":
  971. item.C_Status = "维保完成";
  972. break;
  973. case "5":
  974. item.C_Status = "维保取消";
  975. break;
  976. case "6":
  977. item.C_Status = "维保返工";
  978. break;
  979. case "7":
  980. item.C_Status = "维保完成确认";
  981. break;
  982. }
  983. run.SetText(" " + serialNum.ToString() + "." + serialNum1.ToString() + "、" + item.C_Status);
  984. run.AddCarriageReturn();
  985. run.AppendText(" 操作时间:" + item.D_CreateOn);
  986. run.AddCarriageReturn();
  987. run.AppendText(" 状态:" + item.C_Status);
  988. run.AddCarriageReturn();
  989. run.AppendText(" 处理人员:" + item.ReportUserName);
  990. run.AddCarriageReturn();
  991. run.AppendText(" 现场描述:" + item.C_Remark);
  992. run.AddCarriageReturn();
  993. run.AppendText(" 现场记录:" + item.C_Record);
  994. if (item.RecordImageList != null && item.RecordImageList.Count() > 0)
  995. {
  996. run.AddCarriageReturn();
  997. run.AppendText(" 现场拍照:");
  998. foreach (var path in item.RecordImageList)
  999. {
  1000. string pathFile = path.C_ImageURL;
  1001. if (pathFile[0].ToString() == @"/")
  1002. {
  1003. pathFile = pathFile.Substring(1);
  1004. }
  1005. if (!System.IO.File.Exists(pathFile))
  1006. {
  1007. pathFile = @"wwwroot/error.png";
  1008. }
  1009. FileStream fileStream = new FileStream(pathFile, FileMode.Open, FileAccess.Read);
  1010. run.AddCarriageReturn();
  1011. run.AddPicture(fileStream, 6, pathFile, Units.ToEMU(100), Units.ToEMU(100));
  1012. }
  1013. }
  1014. serialNum1++;
  1015. }
  1016. serialNum++;
  1017. }
  1018. using (var zipFile = new FileStream(zipPaht, FileMode.Create))
  1019. {
  1020. using (var memoryStream = new NpoiMemoryStream())
  1021. {
  1022. doc.Write(memoryStream);
  1023. byte[] excelData = memoryStream.ToArray();
  1024. using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Update))
  1025. {
  1026. archive.CreateEntry($"{searchModel.C_DevOpsCode}维保详情.docx").Open().Write(excelData, 0, excelData.Length);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. string emailName = $"{searchModel.DevName}-维保详情";
  1032. using (var zipFile1 = new FileStream(zipPaht, FileMode.Open))
  1033. {
  1034. await Task.Run(() =>
  1035. {
  1036. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "详情见附件", $"{emailName}.zip", "application/zip", zipFile1);
  1037. });
  1038. }
  1039. System.IO.File.Delete(zipPaht);
  1040. return new ApiResult(ReturnCode.Success);
  1041. }
  1042. catch (Exception ex)
  1043. {
  1044. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  1045. }
  1046. }
  1047. }
  1048. }