ReportController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using Autofac.Core;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using Newtonsoft.Json;
  6. using NPOI.SS.UserModel;
  7. using NPOI.SS.Util;
  8. using NPOI.XSSF.UserModel;
  9. using Ropin.Inspection.Api.Common;
  10. using Ropin.Inspection.Model;
  11. using Ropin.Inspection.Model.SearchModel;
  12. using Ropin.Inspection.Model.ViewModel;
  13. using Ropin.Inspection.Model.ViewModel.DEV;
  14. using Ropin.Inspection.Service.Interface;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Threading.Tasks;
  20. namespace Ropin.Inspection.Api.Controllers
  21. {
  22. public class ReportController : BaseController
  23. {
  24. public ILogger<ReportController> _logger { get; }
  25. private readonly IReportService _service;
  26. /// <summary>
  27. /// 构造函数
  28. /// </summary>
  29. /// <param name="service"></param>
  30. /// <param name="logger"></param>
  31. public ReportController(IReportService service, ILogger<ReportController> logger)
  32. {
  33. _service = service;
  34. _logger = logger;
  35. }
  36. /// <summary>
  37. /// 通过ID获取报表信息
  38. /// </summary>
  39. /// <param name="id"></param>
  40. /// <returns></returns>
  41. [HttpGet("GetReportDataAsync/{id}")]
  42. public async Task<ApiResult> GetReportDataAsync(Guid id)
  43. {
  44. if (Guid.Empty == id)
  45. {
  46. return new ApiResult(ReturnCode.GeneralError);
  47. }
  48. try
  49. {
  50. object Report = await _service.GetReportDataByIdAsync(id);
  51. return new ApiResult<object>(Report);
  52. }
  53. catch (Exception ex)
  54. {
  55. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  56. }
  57. }
  58. /// <summary>
  59. /// 条件获取报表记录,不包含报表数据,带分页
  60. /// </summary>
  61. /// <param name="searchModel"></param>
  62. /// <returns></returns>
  63. [HttpPost("GetWithoutReportData")]
  64. public async Task<ApiResult> GetWithoutReportData(ReportSearchModel searchModel)
  65. {
  66. if (searchModel == null)
  67. {
  68. return new ApiResult(ReturnCode.ArgsError);
  69. }
  70. try
  71. {
  72. var reportList = await _service.GetWithoutReportData(searchModel);
  73. return new ApiResult<PagesModel<ReportViewModel>>(new PagesModel<ReportViewModel>(reportList?.ToList(), searchModel));
  74. }
  75. catch (Exception ex)
  76. {
  77. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  78. }
  79. }
  80. /// <summary>
  81. /// 更新巡检记录
  82. /// </summary>
  83. /// <param name="id"></param>
  84. /// <param name="updateModel"></param>
  85. /// <returns></returns>
  86. [HttpPut("UpdateReportAsync/{id}")]
  87. public async Task<ApiResult> UpdateReportAsync(Guid id, ReportUpdateViewModel updateModel)
  88. {
  89. if (Guid.Empty == id|| updateModel == null)
  90. {
  91. return new ApiResult(ReturnCode.GeneralError);
  92. }
  93. try
  94. {
  95. await _service.UpdateAsync(id, updateModel);
  96. }
  97. catch (Exception ex)
  98. {
  99. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  100. }
  101. return new ApiResult(ReturnCode.Success);
  102. }
  103. /// <summary>
  104. /// 删除
  105. /// </summary>
  106. /// <param name="id"></param>
  107. /// <returns></returns>
  108. [HttpDelete("DeleteReportAsync/{id}")]
  109. public async Task<ApiResult> DeleteReportAsync(Guid id)
  110. {
  111. if (Guid.Empty == id)
  112. {
  113. return new ApiResult(ReturnCode.GeneralError);
  114. }
  115. try
  116. {
  117. await _service.DeleteAsync(id);
  118. }
  119. catch (Exception ex)
  120. {
  121. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  122. }
  123. return new ApiResult(ReturnCode.Success);
  124. }
  125. /// <summary>
  126. /// 导出报表-Excel
  127. /// </summary>
  128. /// <param name="id"></param>
  129. /// <param name="OrgName"></param>
  130. /// <param name="StoreName"></param>
  131. /// <returns></returns>
  132. [HttpPost("ReportExportExcel")]
  133. [AllowAnonymous]
  134. public async Task<IActionResult> ReportExportExcel(Guid id,string OrgName,string StoreName)
  135. {
  136. if (Guid.Empty == id)
  137. {
  138. throw new Exception("参数不能为空值");
  139. }
  140. try
  141. {
  142. ReportViewModel model = await _service.GetByIdAsync(id);
  143. if (model == null)
  144. {
  145. throw new Exception("没有此报表");
  146. }
  147. object Report= await _service.GetReportDataByIdAsync(id);
  148. IWorkbook workbook = new XSSFWorkbook();
  149. ISheet sheet = workbook.CreateSheet();
  150. #region 标题-第一行
  151. //标题加粗居中.
  152. IFont font = workbook.CreateFont();
  153. font.IsBold = true;
  154. font.FontHeightInPoints = 22;
  155. font.FontName = "宋体";
  156. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  157. titleCellStyle.SetFont(font);
  158. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  159. titleCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  160. //边框
  161. titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  162. titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  163. titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  164. titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  165. titleCellStyle.WrapText = true;
  166. #endregion
  167. #region 说明 -
  168. //标题加粗居中.
  169. IFont font0 = workbook.CreateFont();
  170. font0.IsBold = false;
  171. font0.FontHeightInPoints = 14;
  172. font0.FontName = "宋体";
  173. ICellStyle headCellStyle1 = workbook.CreateCellStyle();
  174. headCellStyle1.SetFont(font0);
  175. headCellStyle1.Alignment = HorizontalAlignment.Left; //字体左右居左
  176. headCellStyle1.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  177. //边框
  178. headCellStyle1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  179. headCellStyle1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  180. headCellStyle1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  181. headCellStyle1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  182. headCellStyle1.WrapText = true;
  183. #endregion
  184. #region 表头 -
  185. //标题加粗居中.
  186. IFont font1 = workbook.CreateFont();
  187. font1.IsBold = true;
  188. font1.FontHeightInPoints = 14;
  189. font1.FontName = "宋体";
  190. ICellStyle headCellStyle = workbook.CreateCellStyle();
  191. headCellStyle.SetFont(font1);
  192. headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
  193. headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  194. //边框
  195. headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  196. headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  197. headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  198. headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  199. headCellStyle.WrapText = true;
  200. #endregion
  201. #region 内容 -
  202. //标题加粗居中.
  203. IFont font2 = workbook.CreateFont();
  204. font2.IsBold = false;
  205. font2.FontHeightInPoints = 12;
  206. font2.FontName = "宋体";
  207. ICellStyle rowCellStyle = workbook.CreateCellStyle();
  208. rowCellStyle.SetFont(font2);
  209. rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  210. rowCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  211. //边框
  212. rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  213. rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  214. rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  215. rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  216. rowCellStyle.WrapText = true;
  217. #endregion
  218. string TitelName = model.C_DevName+"点检工单记录表"+(string.IsNullOrEmpty(model.C_DevNumber)?"":"(设备编号:"+model.C_DevNumber+" )");
  219. if (model.I_Type==5)
  220. {
  221. IRow title = sheet.CreateRow(0);
  222. title.Height = 30 * 25;
  223. title.CreateCell(0).SetCellValue(TitelName);
  224. foreach (var item in title.Cells)
  225. {
  226. item.CellStyle = titleCellStyle;
  227. }
  228. // 合并列示例,合并第 1 行的第 0 列到第 15 列
  229. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4));
  230. IRow row3 = sheet.CreateRow(3);
  231. row3.CreateCell(0).SetCellValue("序号");
  232. row3.CreateCell(1).SetCellValue("设备和设施");
  233. row3.CreateCell(2).SetCellValue("巡视检查内容");
  234. row3.CreateCell(3).SetCellValue("巡检状态");
  235. row3.CreateCell(4).SetCellValue("维护维修记录");
  236. foreach (var item in row3.Cells)
  237. {
  238. item.CellStyle = headCellStyle;
  239. }
  240. string UserName = "";
  241. IEnumerable<InspectionWorkOrderModel> Report3 =(IEnumerable<InspectionWorkOrderModel>)Report;
  242. if (Report3 == null || Report3.Count() == 0)
  243. {
  244. IRow row4 = sheet.CreateRow(4);
  245. row4.CreateCell(0).SetCellValue("暂无数据");
  246. row4.CreateCell(1).SetCellValue("");
  247. row4.CreateCell(2).SetCellValue("");
  248. row4.CreateCell(3).SetCellValue("");
  249. row4.CreateCell(4).SetCellValue("");
  250. foreach (var item in row4.Cells)
  251. {
  252. item.CellStyle = rowCellStyle;
  253. }
  254. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(4, 4, 0, 4));
  255. }
  256. else
  257. {
  258. int seriaNum = 1;int rowsNumber = 4;
  259. List<string> nameList= new List<string>();
  260. foreach (var item in Report3)
  261. {
  262. if (item.ContentRecordList != null&&item.ContentRecordList.Count()>0)
  263. {
  264. int mergeRowNum = 0;
  265. foreach (var index in item.ContentRecordList)
  266. {
  267. if (index != null)
  268. {
  269. nameList.AddRange(index.UserName);
  270. IRow rows = sheet.CreateRow(rowsNumber);
  271. if (mergeRowNum == 0)
  272. {
  273. rows.CreateCell(0).SetCellValue(seriaNum);
  274. rows.CreateCell(1).SetCellValue(item.PatrolItem);
  275. rows.CreateCell(4).SetCellValue(item.Remark);
  276. }
  277. else
  278. {
  279. rows.CreateCell(0).SetCellValue("");
  280. rows.CreateCell(1).SetCellValue("");
  281. rows.CreateCell(4).SetCellValue("");
  282. }
  283. rows.CreateCell(2).SetCellValue(index.Name);
  284. //string tick = "\u2713"; // 对勾的Unicode编码 "✓"; // 对勾
  285. //string cross = "\u2718"; // 叉号的Unicode编码 "✘"; // 叉号
  286. string FuHao = "\u25EF"; // 输出:◯
  287. switch (index.CheckStatus)
  288. {
  289. case "3": FuHao = "\u2717"; break;
  290. case "0":
  291. case "1":
  292. case "2":
  293. case "4": FuHao = "✓"; break;
  294. default: FuHao = "\u25EF"; break;
  295. }
  296. rows.CreateCell(3).SetCellValue(FuHao);
  297. foreach (var cel in rows.Cells)
  298. {
  299. cel.CellStyle = rowCellStyle;
  300. }
  301. rowsNumber++;
  302. mergeRowNum++;
  303. }
  304. }
  305. if (mergeRowNum > 1)
  306. {
  307. sheet.AddMergedRegion(new CellRangeAddress(rowsNumber - mergeRowNum, rowsNumber - 1, 0, 0));
  308. sheet.AddMergedRegion(new CellRangeAddress(rowsNumber - mergeRowNum, rowsNumber - 1, 1, 1));
  309. sheet.AddMergedRegion(new CellRangeAddress(rowsNumber - mergeRowNum, rowsNumber - 1, 4, 4));
  310. }
  311. seriaNum++;
  312. }
  313. }
  314. UserName = string.Join(";", nameList.Distinct());
  315. IRow rown = sheet.CreateRow(rowsNumber);
  316. rown.Height = 30 * 20;
  317. rown.CreateCell(0).SetCellValue("使用部门确认:");
  318. rown.CreateCell(1).SetCellValue("");
  319. rown.CreateCell(2).SetCellValue("");
  320. rown.CreateCell(3).SetCellValue("日期: 年 月 日");
  321. rown.CreateCell(4).SetCellValue("");
  322. foreach (var item in rown.Cells)
  323. {
  324. item.CellStyle = headCellStyle1;
  325. }
  326. // 合并列示例,合并第 2 行的第 0 列到第 2 列
  327. sheet.AddMergedRegion(new CellRangeAddress(rowsNumber, rowsNumber, 0, 2));
  328. // 合并列示例,合并第 2 行的第 3 列到第 4 列
  329. sheet.AddMergedRegion(new CellRangeAddress(rowsNumber, rowsNumber, 3, 4));
  330. IRow rown1 = sheet.CreateRow(rowsNumber+1);
  331. rown1.CreateCell(0).SetCellValue("巡检状态说明: ✓ = 已检查且正常; ✘ = 已检查且异常; ◯ = 未检查");
  332. // 合并列示例,合并第 2 行的第 0 列到第 2 列
  333. sheet.AddMergedRegion(new CellRangeAddress(rowsNumber+1, rowsNumber+1, 0, 4));
  334. }
  335. IRow row1 = sheet.CreateRow(1);
  336. row1.Height = 30 * 15;
  337. row1.CreateCell(0).SetCellValue("使用部门:" + StoreName);
  338. row1.CreateCell(1).SetCellValue("");
  339. row1.CreateCell(2).SetCellValue("");
  340. row1.CreateCell(3).SetCellValue("维保时间:" + model.D_CreateTime.ToString("yyyy年MM月dd日"));
  341. row1.CreateCell(4).SetCellValue("");
  342. IRow row2 = sheet.CreateRow(2);
  343. row2.Height = 30 * 15;
  344. row2.CreateCell(0).SetCellValue("服务公司:" + OrgName);
  345. row2.CreateCell(1).SetCellValue("");
  346. row2.CreateCell(2).SetCellValue("");
  347. row2.CreateCell(3).SetCellValue("维保人员:"+ UserName);
  348. row2.CreateCell(4).SetCellValue("");
  349. foreach (var item in row1.Cells)
  350. {
  351. item.CellStyle = headCellStyle1;
  352. }
  353. foreach (var item in row2.Cells)
  354. {
  355. item.CellStyle = headCellStyle1;
  356. }
  357. // 合并列示例,合并第 2 行的第 0 列到第 2 列
  358. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 2));
  359. // 合并列示例,合并第 2 行的第 3 列到第 4 列
  360. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 3, 4));
  361. // 合并列示例,合并第 3 行的第 0 列到第 2 列
  362. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 2));
  363. // 合并列示例,合并第 3 行的第 3 列到第 4 列
  364. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 3, 4));
  365. }
  366. // 自适应单元格
  367. //for (int i = 4; i < sheet.LastRowNum; i++)
  368. //{
  369. // sheet.AutoSizeRow(i);
  370. //}
  371. for (int i = 0; i < 5; i++)
  372. {
  373. if (i == 0)
  374. {
  375. sheet.SetColumnWidth(i, 256 * 10);
  376. }
  377. else if(i==2)
  378. {
  379. sheet.SetColumnWidth(i, 256 * 55);
  380. }
  381. else if (i == 4)
  382. {
  383. sheet.SetColumnWidth(i, 256 * 35);
  384. }
  385. else
  386. {
  387. sheet.SetColumnWidth(i, 256 * 22);
  388. }
  389. }
  390. using (MemoryStream stream = new MemoryStream())
  391. {
  392. workbook.Write(stream);
  393. byte[] data = stream.ToArray();
  394. return File(data, "application/vnd.ms-excel", $"{model.C_Name}.xlsx");
  395. }
  396. }
  397. catch (Exception ex)
  398. {
  399. throw new Exception("导出Excel数据异常:"+ex.Message);
  400. }
  401. }
  402. }
  403. }