using APT.BaseData.Domain.Entities; using APT.BaseData.Domain.Entities.FM; using APT.BaseData.Domain.Enums; using APT.BaseData.Domain.IServices; using APT.BaseData.Domain.IServices.FM; using APT.Infrastructure.Core; using APT.MS.Domain.Entities.BS; using APT.MS.Domain.Entities.HM; using APT.MS.Domain.Enums; using APT.Utility; using APT.WebApi.Models; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.IO; using APT.Infrastructure.Api; namespace APT.BS.WebApi.Controllers.Api { [Route("api/BS/BSRiskSubmitContentDeal")] public partial class BSRiskSubmitContentDealController : AuthorizeApiController { IFMFlowPermitService MFlowPermitService { get; set; } IFMNotificationTaskService NotificationTaskService { get; set; } IPFCodeRuleService CodeRuleService { get; set; } IPFSysLogService SysLogService { get; set; } public BSRiskSubmitContentDealController(IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFCodeRuleService codeRuleService, IPFSysLogService sysLogService) { NotificationTaskService = notificationTaskService; MFlowPermitService = mFlowPermitService; CodeRuleService = codeRuleService; SysLogService = sysLogService; } /// /// 获得单条实体数据 /// /// 过滤实体 /// [HttpPost, Route("GetAll")] public JsonActionResult GetAll([FromBody] KeywordFilter filter) { return SafeExecute(() => { ICollection Include = new List(); foreach (var item in filter.Include) { if (item.Contains(".Nav_UserSignFiles")) { Include.Add(item.Split(".Nav_UserSignFiles")[0]); } else { Include.Add(item); } } filter.Include = Include; var modelSubConDel = GetEntity(null, filter); if (modelSubConDel == null) { throw new Exception("获取整改单信息失败!"); } List listUserID = new List(); if (modelSubConDel.DEAL_USER_ID.HasValue) listUserID.Add(modelSubConDel.DEAL_USER_ID.Value); if (modelSubConDel.CHECK_USER_ID.HasValue) listUserID.Add(modelSubConDel.CHECK_USER_ID.Value); if (modelSubConDel.Nav_ApproveCheckAudit != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails.Any()) { foreach (var item in modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails) { if (item.APPROVE_USER_ID.HasValue) listUserID.Add(item.APPROVE_USER_ID.Value); } } if (listUserID.Count > 0) { listUserID = listUserID.Distinct().ToList(); var listUsersSign = GetEntities(e => listUserID.Contains(e.USER_ID), null, "Nav_ImgFile"); if (modelSubConDel.DEAL_USER_ID.HasValue) modelSubConDel.Nav_UserDeal.Nav_UserSignFiles = listUsersSign.Where(e => e.USER_ID == modelSubConDel.DEAL_USER_ID.Value).ToList(); if (modelSubConDel.CHECK_USER_ID.HasValue) modelSubConDel.Nav_UserCheck.Nav_UserSignFiles = listUsersSign.Where(e => e.USER_ID == modelSubConDel.CHECK_USER_ID.Value).ToList(); if (modelSubConDel.Nav_ApproveCheckAudit != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails.Any()) { foreach (var item in modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails) { if (item.APPROVE_USER_ID.HasValue) item.Nav_ApproveUser.Nav_UserSignFiles = listUsersSign.Where(e => e.USER_ID == item.APPROVE_USER_ID).ToList(); } } } if (modelSubConDel.RISK_SUBMIT_CONTENT_ID.HasValue) { var applyDetail = GetEntity(e => e.RISK_SUBMIT_CONTENT_ID == modelSubConDel.RISK_SUBMIT_CONTENT_ID.Value); modelSubConDel.Nav_ApplyDetail = applyDetail; } return modelSubConDel; }); } /// /// 获得单条实体数据 /// /// 过滤实体 /// [HttpPost, Route("Get")] public JsonActionResult Get([FromBody] KeywordFilter filter) { filter.IgnoreDataRule = true; return WitEntity(null, filter); } /// /// 生成入参对应整改记录Excel /// /// 整改单IDS /// /// [HttpPost, Route("GetOutPutData")] public JsonActionResult GetOutPutData([FromBody] KeywordFilter filter) { return SafeExecute(() => { if (string.IsNullOrEmpty(filter.Keyword)) { throw new Exception("获取导出信息失败!"); } string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + APT.Infrastructure.Api.AppContext.CurrentSession.UserName + "_" + new Random().Next(100, 999) + ".xlsx"; #region 获取填充信息 搜索 var listIds = filter.Keyword.Split(',', StringSplitOptions.RemoveEmptyEntries); List listID = new List(); foreach (var item in listIds) { listID.Add(new Guid(item)); } var listDealOut = GetEntities(e => listID.Contains(e.ID), null, "Nav_SubmitContent.Nav_Department", "Nav_UserCheck", "Nav_UserDeal", "Nav_UserActualDeal", "Nav_Files.Nav_ImgFile", "Nav_FilesAfter.Nav_ImgFile").ToList(); if (listDealOut == null || !listDealOut.Any()) { listDealOut = GetEntities(e => e.RISK_SUBMIT_ID.HasValue && listID.Contains(e.RISK_SUBMIT_ID.Value), null, "Nav_SubmitContent.Nav_Department", "Nav_UserCheck", "Nav_UserDeal", "Nav_UserActualDeal", "Nav_Files.Nav_ImgFile", "Nav_FilesAfter.Nav_ImgFile").ToList(); } if (listDealOut == null || !listDealOut.Any()) { throw new Exception("未获取到导出信息!"); } #endregion #region 填充数据 处理 与 填充 int rowIndex = 0; IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("隐患整改单"); #region 样式 全局 //表头样式 ICellStyle cellStyleHead = workbook.CreateCellStyle(); // 设置字体 IFont fontHead = workbook.CreateFont(); fontHead.FontName = "Arial"; fontHead.FontHeightInPoints = 20; fontHead.Boldweight = (short)FontBoldWeight.Bold; cellStyleHead.SetFont(fontHead); // 设置对齐方式 cellStyleHead.Alignment = HorizontalAlignment.Center; cellStyleHead.VerticalAlignment = VerticalAlignment.Center; //表头样式 ICellStyle cellStyleNO = workbook.CreateCellStyle(); // 设置字体 IFont fontNO = workbook.CreateFont(); fontNO.FontName = "宋体"; fontNO.FontHeightInPoints = 11; fontNO.Boldweight = (short)FontBoldWeight.Bold; cellStyleNO.Alignment = HorizontalAlignment.Right; cellStyleNO.VerticalAlignment = VerticalAlignment.Center; cellStyleNO.SetFont(fontNO); ICellStyle cellStyleNOC = workbook.CreateCellStyle(); // 设置字体 IFont fontNOC = workbook.CreateFont(); fontNOC.FontName = "宋体"; fontNOC.FontHeightInPoints = 11; fontNOC.Boldweight = (short)FontBoldWeight.Bold; cellStyleNOC.Alignment = HorizontalAlignment.Left; cellStyleNOC.VerticalAlignment = VerticalAlignment.Center; cellStyleNOC.SetFont(fontNO); // 设置对齐方式 cellStyleHead.Alignment = HorizontalAlignment.Center; cellStyleHead.VerticalAlignment = VerticalAlignment.Center; ICellStyle cellStyle = workbook.CreateCellStyle(); // 设置字体 IFont font = workbook.CreateFont(); font.FontName = "宋体"; font.FontHeightInPoints = 11; font.Boldweight = (short)FontBoldWeight.Bold; cellStyle.SetFont(font); // 设置边框 cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.WrapText = true; cellStyle.BottomBorderColor = HSSFColor.Black.Index; cellStyle.LeftBorderColor = HSSFColor.Black.Index; cellStyle.RightBorderColor = HSSFColor.Black.Index; cellStyle.TopBorderColor = HSSFColor.Black.Index; // 设置对齐方式 cellStyle.Alignment = HorizontalAlignment.Center; cellStyle.VerticalAlignment = VerticalAlignment.Center; //检查内容样式 ICellStyle cellStyleRight = workbook.CreateCellStyle(); // 设置字体 IFont fontLeft = workbook.CreateFont(); fontLeft.FontName = "宋体"; fontLeft.FontHeightInPoints = 11; fontLeft.Boldweight = (short)FontBoldWeight.Bold; cellStyleRight.SetFont(fontLeft); // 设置边框 cellStyleRight.BorderBottom = BorderStyle.Thin; cellStyleRight.BorderLeft = BorderStyle.Thin; cellStyleRight.BorderRight = BorderStyle.Thin; cellStyleRight.BorderTop = BorderStyle.Thin; cellStyleRight.WrapText = true; //// 设置背景色 //cellStyle.FillForegroundColor = IndexedColors.LightYellow.Index; //cellStyle.FillPattern = FillPattern.SolidForeground; // 设置对齐方式 cellStyleRight.Alignment = HorizontalAlignment.Right; cellStyleRight.VerticalAlignment = VerticalAlignment.Center; #endregion sheet.SetColumnWidth(0, 15 * 256); sheet.SetColumnWidth(1, 30 * 256); sheet.SetColumnWidth(2, 15 * 256); sheet.SetColumnWidth(3, 30 * 256); sheet.DefaultRowHeight = 330; int fileMaxCount = 0; List listUserID = new List(); foreach (var item in listDealOut) { if (item.CHECK_USER_ID.HasValue && !listUserID.Contains(item.CHECK_USER_ID.Value)) { listUserID.Add(item.CHECK_USER_ID.Value); } if (item.DEAL_USER_ID.HasValue && !listUserID.Contains(item.DEAL_USER_ID.Value)) { listUserID.Add(item.DEAL_USER_ID.Value); } } //Nav_UserCheck //Nav_UserDeal List listUser = null; T_FM_USER modelUser = null; //T_PF_FILE fileInfo = null; T_PF_IMG_FILE imgFile = null; string FILE_PATH = string.Empty; bool isFile = false; bool isFileA = false; if (listUserID != null && listUserID.Any()) { listUser = GetEntities(e => listUserID.Contains(e.ID), null, null).ToList(); } var fileP = ConfigurationManager.AppSettings["Img_Local"];//文件位置 List listFile = null; List listFileA = null; //按信息导出 for (int i = 0; i < listDealOut.Count; i++) { #region 表头第1行 IRow row = sheet.CreateRow(rowIndex); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 3));// 起始行 , 结束行(合并单元格的行数相同,所以是起始行) , 起始列 ,结束列(比想要合并的最后一列多1) ICell cell = row.CreateCell(0); // 在合并区域的左上角单元格写入数据 row.Height = 700; cell.SetCellValue(listDealOut[i].NAME); cell.CellStyle = cellStyleHead; #endregion #region 编号第2行 编号 rowIndex++; IRow rowNO = sheet.CreateRow(rowIndex); ICell cellNOH = rowNO.CreateCell(2); cellNOH.SetCellValue("编号"); cellNOH.CellStyle = cellStyleNO; ICell cellNOC = rowNO.CreateCell(3); cellNOC.SetCellValue(listDealOut[i].CODE); cellNOC.CellStyle = cellStyleNOC; #endregion #region 编号第3行 隐患地点 检查问题 rowIndex++; IRow row3 = sheet.CreateRow(rowIndex); ICell cell30 = row3.CreateCell(0); cell30.SetCellValue("隐患地点"); cell30.CellStyle = cellStyle; ICell cell31 = row3.CreateCell(1); cell31.SetCellValue(listDealOut[i]?.Nav_SubmitContent?.ADDRESS); cell31.CellStyle = cellStyle; ICell cell32 = row3.CreateCell(2); cell32.SetCellValue("检查问题"); cell32.CellStyle = cellStyle; ICell cell33 = row3.CreateCell(3); cell33.SetCellValue(listDealOut[i]?.Nav_SubmitContent?.DESCREPTION); cell33.CellStyle = cellStyle; #endregion #region 编号第4行 隐患上报日期 隐患上报日期 rowIndex++; IRow row4 = sheet.CreateRow(rowIndex); ICell cell40 = row4.CreateCell(0); cell40.SetCellValue("隐患上报日期"); cell40.CellStyle = cellStyle; ICell cell41 = row4.CreateCell(1); cell41.SetCellValue(listDealOut[i]?.Nav_SubmitContent?.CREATE_TIME?.ToString("yyyy-MM-dd")); cell41.CellStyle = cellStyle; ICell cell42 = row4.CreateCell(2); cell42.SetCellValue("隐患上报日期"); cell42.CellStyle = cellStyle; ICell cell43 = row4.CreateCell(3); cell43.SetCellValue(listDealOut[i]?.Nav_SubmitContent?.OKDATE?.ToString("yyyy-MM-dd")); cell43.CellStyle = cellStyle; #endregion #region 编号第4行 整改部门 整改落实人 rowIndex++; IRow row5 = sheet.CreateRow(rowIndex); ICell cell50 = row5.CreateCell(0); cell50.SetCellValue("整改部门"); cell50.CellStyle = cellStyle; ICell cell51 = row5.CreateCell(1); cell51.SetCellValue(listDealOut[i]?.Nav_SubmitContent?.Nav_Department.NAME); cell51.CellStyle = cellStyle; ICell cell52 = row5.CreateCell(2); cell52.SetCellValue("整改落实人"); cell52.CellStyle = cellStyle; ICell cell53 = row5.CreateCell(3); cell53.SetCellValue(listDealOut[i]?.Nav_UserActualDeal?.NAME); cell53.CellStyle = cellStyle; #endregion #region 编号第6行 整改建议 rowIndex++; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 1, 3)); IRow row6 = sheet.CreateRow(rowIndex); ICell cell62 = row6.CreateCell(2); cell62.CellStyle = cellStyle; ICell cell63 = row6.CreateCell(3); cell63.CellStyle = cellStyle; ICell cell60 = row6.CreateCell(0); cell60.SetCellValue("整改建议"); cell60.CellStyle = cellStyle; ICell cell61 = row6.CreateCell(1); cell61.SetCellValue(listDealOut[i]?.Nav_SubmitContent?.DEMAND); cell61.CellStyle = cellStyle; #endregion #region 编号第7行 附件 if (listDealOut[i].Nav_FilesAfter != null && listDealOut[i].Nav_FilesAfter.Any()) { listFileA = listDealOut[i].Nav_FilesAfter.ToList(); fileMaxCount = listFileA.Count; } else { listFileA = null; } if (listDealOut[i].Nav_Files != null && listDealOut[i].Nav_Files.Any()) { listFile = listDealOut[i].Nav_Files.ToList(); fileMaxCount = Math.Max(fileMaxCount, listFile.Count); } else { listFile = null; } if (fileMaxCount == 1) { rowIndex++; IRow row7 = sheet.CreateRow(rowIndex); row7.Height = 3000; ICell cell70 = row7.CreateCell(0); cell70.SetCellValue("附件(整改前)"); cell70.CellStyle = cellStyle; //"Nav_Files" ICell cell71 = row7.CreateCell(1); //isFile FILE_PATH = ""; if (listFile != null && listFile.Any()) { imgFile = listFile?[0].Nav_ImgFile; if (imgFile != null) { if (!string.IsNullOrEmpty(imgFile.FILE_TYPE) && imgFile.FILE_TYPE.StartsWith("image/")) { FILE_PATH = imgFile.FILE_PATH; isFile = true; } else { FILE_PATH = imgFile.FILE_NAME; } } } if (!string.IsNullOrEmpty(FILE_PATH)) { if (isFile) { ShowImg(workbook, sheet, fileP, FILE_PATH, rowIndex, 1); cell71.SetCellValue(""); } else { cell71.SetCellValue(FILE_PATH); } } else { cell71.SetCellValue(""); } cell71.CellStyle = cellStyle; ICell cell72 = row7.CreateCell(2); cell72.SetCellValue("附件(整改后)"); cell72.CellStyle = cellStyle; //, "Nav_FilesAfter" ICell cell73 = row7.CreateCell(3); //cell73.SetCellValue("附件(整改后)文件"); FILE_PATH = ""; if (listFileA != null && listFileA.Any()) { imgFile = listFileA?[0].Nav_ImgFile; if (imgFile != null) { if (!string.IsNullOrEmpty(imgFile.FILE_TYPE) && imgFile.FILE_TYPE.StartsWith("image/")) { FILE_PATH = imgFile.FILE_PATH; isFileA = true; } else { FILE_PATH = imgFile.FILE_NAME; } } } if (!string.IsNullOrEmpty(FILE_PATH)) { if (isFileA) { ShowImg(workbook, sheet, fileP, FILE_PATH, rowIndex, 3); cell73.SetCellValue(""); } else { cell73.SetCellValue(FILE_PATH); } } else { cell73.SetCellValue(""); } cell73.CellStyle = cellStyle; if (!isFileA && !isFile) { row7.Height = 350; } } else { for (int j = 0; j < fileMaxCount; j++) { rowIndex++; IRow row7 = sheet.CreateRow(rowIndex); row7.Height = 3000; isFile = false; isFileA = false; if (j == 0) { sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex + fileMaxCount - 1, 0, 0)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex + fileMaxCount - 1, 2, 2)); ICell cell70 = row7.CreateCell(0); cell70.SetCellValue("附件(整改前)"); cell70.CellStyle = cellStyle; ICell cell72 = row7.CreateCell(2); cell72.SetCellValue("附件(整改后)"); cell72.CellStyle = cellStyle; } else { ICell cellRiskEmpty = row7.CreateCell(0); cellRiskEmpty.CellStyle = cellStyle; } ICell cell71 = row7.CreateCell(1); //isFile FILE_PATH = ""; if (listFile != null && listFile.Any() && listFile.Count >= j + 1) { imgFile = listFile?[j].Nav_ImgFile; if (imgFile != null) { if (!string.IsNullOrEmpty(imgFile.FILE_TYPE) && imgFile.FILE_TYPE.StartsWith("image/")) { FILE_PATH = imgFile.FILE_PATH; isFile = true; } else { FILE_PATH = imgFile.FILE_NAME; } } } if (!string.IsNullOrEmpty(FILE_PATH)) { if (isFile) { ShowImg(workbook, sheet, fileP, FILE_PATH, rowIndex, 1); cell71.SetCellValue(""); } else { cell71.SetCellValue(FILE_PATH); } } else { cell71.SetCellValue(""); } cell71.CellStyle = cellStyle; //, "Nav_FilesAfter" ICell cell73 = row7.CreateCell(3); //cell73.SetCellValue("附件(整改后)文件"); FILE_PATH = ""; if (listFileA != null && listFileA.Any() && listFileA.Count >= j + 1) { imgFile = listFileA?[j].Nav_ImgFile; if (imgFile != null) { if (!string.IsNullOrEmpty(imgFile.FILE_TYPE) && imgFile.FILE_TYPE.StartsWith("image/")) { FILE_PATH = imgFile.FILE_PATH; isFileA = true; } else { FILE_PATH = imgFile.FILE_NAME; } } } if (!string.IsNullOrEmpty(FILE_PATH)) { if (isFileA) { ShowImg(workbook, sheet, fileP, FILE_PATH, rowIndex, 3); cell73.SetCellValue(""); } else { cell73.SetCellValue(FILE_PATH); } } else { cell73.SetCellValue(""); } cell73.CellStyle = cellStyle; if (!isFileA && !isFile) { row7.Height = 350; } } } #endregion #region 编号第8行 整改责任人 验收人 rowIndex++; IRow row8 = sheet.CreateRow(rowIndex); row8.Height = 1000; ICell cell80 = row8.CreateCell(0); cell80.SetCellValue("整改责任人"); cell80.CellStyle = cellStyle; //Nav_UserDeal ICell cell81 = row8.CreateCell(1); //cell81.SetCellValue("Nav_UserDeal"); FILE_PATH = ""; //整改责任人 图片 if (listUser != null && listDealOut[i].DEAL_USER_ID.HasValue && listDealOut[i].ISDEALSIGN) { modelUser = listUser.FirstOrDefault(e => e.ID == listDealOut[i].DEAL_USER_ID.Value); if (modelUser != null && modelUser.FILE_PATH != null && modelUser.FILE_PATH.Length > 0) { FILE_PATH = modelUser.FILE_PATH; } } if (!string.IsNullOrEmpty(FILE_PATH)) { ShowImg(workbook, sheet, fileP, FILE_PATH, rowIndex, 1); cell81.SetCellValue(""); } else { cell81.SetCellValue(""); } cell81.CellStyle = cellStyle; ICell cell82 = row8.CreateCell(2); cell82.SetCellValue("验收人"); cell82.CellStyle = cellStyle; //Nav_UserCheck ICell cell83 = row8.CreateCell(3); //cell83.SetCellValue("Nav_UserCheck"); //验收人 图片 FILE_PATH = ""; if (listUser != null && listDealOut[i].CHECK_USER_ID.HasValue && listDealOut[i].ISCHECKSIGN) { modelUser = listUser.FirstOrDefault(e => e.ID == listDealOut[i].CHECK_USER_ID.Value); if (modelUser != null && modelUser.FILE_PATH != null && modelUser.FILE_PATH.Length > 0) { FILE_PATH = modelUser.FILE_PATH; } } if (!string.IsNullOrEmpty(FILE_PATH)) { ShowImg(workbook, sheet, fileP, FILE_PATH, rowIndex, 3); cell83.SetCellValue(""); } else { cell83.SetCellValue(""); } cell83.CellStyle = cellStyle; #endregion rowIndex += 5; } #endregion #region 表格数据填充 var fileNameP = ConfigurationManager.AppSettings["File_DownLoad"]; var fullFilePart = $"{fileP}{fileNameP}"; var fullFilePath = $"{fileP}{fileNameP}{fileName}"; string Result = $"{fileNameP}{fileName}"; //如果没有文件夹 创建 //如果文件夹下有超过10个文件 按时间 删除多余的 if (!Directory.Exists(fullFilePart)) { Directory.CreateDirectory(fullFilePart); } else { FileInfo[] files = new DirectoryInfo(fullFilePart).GetFiles(); if (files.Length > 10) { var listFilesOrder = files.OrderBy(e => e.CreationTime).ToList(); for (int i = 0; i < files.Length - 10; i++) { listFilesOrder[i].Delete(); } } } // 写入到文件 using (FileStream file = new FileStream(fullFilePath, FileMode.Create, FileAccess.Write)) { workbook.Write(file); } // 关闭工作簿资源 workbook.Close(); #endregion //return $"{fileNameP}{fileName}"; return Result; }); } public void ShowImg(IWorkbook workbook, ISheet sheet, string fileConfig, string filePath, int rowIndex, int colIndex) { try { byte[] imgBytes = System.IO.File.ReadAllBytes(fileConfig + filePath); int pictureIdx = workbook.AddPicture(imgBytes, PictureType.JPEG); // 创建Drawing对象 XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(10, 10, 0, 0, colIndex, rowIndex, colIndex + 1, rowIndex + 1); // 示例位置,请根据需要调整 // 创建图片对象并插入到Excel中 IPicture pict = drawing.CreatePicture(anchor, pictureIdx); pict.Resize(0.9);// 注意:此行代码可能需要根据NPOI版本和具体实现进行调整 } catch { } } } }