TmtnRepairOrderController.cs 42 KB

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