diff --git a/OpenAuth.App/ServiceApp/DataAnalysisApp.cs b/OpenAuth.App/ServiceApp/DataAnalysisApp.cs index 701414d..1b03deb 100644 --- a/OpenAuth.App/ServiceApp/DataAnalysisApp.cs +++ b/OpenAuth.App/ServiceApp/DataAnalysisApp.cs @@ -1,5 +1,8 @@ using Infrastructure; using Microsoft.Extensions.Configuration; +using NPOI.HSSF.UserModel; +using NPOI.SS.UserModel; +using NPOI.SS.Util; using OpenAuth.App.BaseApp.Base; using OpenAuth.App.Interface; using OpenAuth.App.ServiceApp.Response; @@ -243,35 +246,35 @@ namespace OpenAuth.App.ServiceApp return new Response>(); // 默认返回空列表 } - public async Task>> GetAiShpDataExpenseAccountingTableOld(DateTime beginTime, DateTime endTime) - { - using (var db = base.UnitWork.CreateContext()) - { - var aiShpList = db.InsAishp.AsQueryable() - .WhereIF(beginTime.Year != 0001, r => r.CreateTime >= beginTime && r.CreateTime <= endTime); - //总面积 - var totalTifArea = aiShpList.Sum(r => r.AreaNum); - //个数 - var totalTifCount = aiShpList.Count(); + //public async Task>> GetAiShpDataExpenseAccountingTableOld(DateTime beginTime, DateTime endTime) + //{ + // using (var db = base.UnitWork.CreateContext()) + // { + // var aiShpList = db.InsAishp.AsQueryable() + // .WhereIF(beginTime.Year != 0001, r => r.CreateTime >= beginTime && r.CreateTime <= endTime); + // //总面积 + // var totalTifArea = aiShpList.Sum(r => r.AreaNum); + // //个数 + // var totalTifCount = aiShpList.Count(); - var endData = await aiShpList.GroupBy(r => r.ShpDate.Date).Select(r => new AiShpDataExpenseAccountingTableResp - { - ShapDate = r.ShpDate.Date.ToString("yyyyMMdd"), - AreaNum = SqlFunc.AggregateSum(r.AreaNum), - ShpCount = SqlFunc.AggregateSum(r.ShpCount), - }).ToListAsync(); - return new Response>() - { - Result = endData - }; - } - } - public async Task>> GetAiShpDataExpenseAccountingTable(DateTime beginTime, DateTime endTime) + // var endData = await aiShpList.GroupBy(r => r.ShpDate.Date).Select(r => new AiShpDataExpenseAccountingTableResp + // { + // ShapDate = r.ShpDate.Date.ToString("yyyyMMdd"), + // AreaNum = SqlFunc.AggregateSum(r.AreaNum), + // ShpCount = SqlFunc.AggregateSum(r.ShpCount), + // }).ToListAsync(); + // return new Response>() + // { + // Result = endData + // }; + // } + //} + public async Task> GetAiShpDataExpenseAccountingTable1(DateTime beginTime, DateTime endTime) { using (var db = base.UnitWork.CreateContext()) { List dateList = new List(); - List dataList = new List(); + List dataList = new List(); if (beginTime > endTime) { throw new ArgumentException("开始时间需要小于结束时间"); @@ -285,27 +288,320 @@ namespace OpenAuth.App.ServiceApp var taskList = db.InsTask.AsQueryable().WhereIF(beginTime.Year != 0001, t => t.CreateTime >= beginTime && t.CreateTime <= endTime); for (int i = 0; i < dateList.Count; i++) { - AiShpDataExpenseAccountingTableResp aiShpDataExpenseAccountingTableResp = new AiShpDataExpenseAccountingTableResp(); - var aiShpListDay = db.InsAishp.AsQueryable().Where(r => r.ShpDate >= dateList[i]); - aiShpDataExpenseAccountingTableResp.ShapDate = dateList[i].ToString("yyyyMMdd"); - //提取个数 - aiShpDataExpenseAccountingTableResp.AreaNum = await aiShpListDay.SumAsync(r => r.AreaNum); - aiShpDataExpenseAccountingTableResp.ShpCount = await aiShpListDay.SumAsync(r => r.ShpCount); - //下发数量 - aiShpDataExpenseAccountingTableResp.issuedCount = await taskList.Where(t => aiShpListDay.Select(r => r.Id).ToList().Contains(t.ShpId)).CountAsync(); - - } + AiShpDataExpenseAccountingResp aiShpDataExpenseAccountingResp = new AiShpDataExpenseAccountingResp(); + var aiShpListDay = db.InsAishp.AsQueryable().Where(r => r.ShpDate >= dateList[i]); + aiShpDataExpenseAccountingResp.Number = i + 1; + aiShpDataExpenseAccountingResp.ShapDate = dateList[i].ToString("yyyyMMdd"); + //提取个数 + aiShpDataExpenseAccountingResp.AreaNum = await aiShpListDay.SumAsync(r => r.AreaNum); + aiShpDataExpenseAccountingResp.ShpCount = await aiShpListDay.SumAsync(r => r.ShpCount); + //下发数量 + aiShpDataExpenseAccountingResp.IssuedCount = await taskList.Where(t => aiShpListDay.Select(r => r.Id).ToList().Contains(t.ShpId)).CountAsync(); + + } + AiShpDataExpenseAccountingTableResp aiShpDataExpenseAccountingTableResp = new AiShpDataExpenseAccountingTableResp(); + aiShpDataExpenseAccountingTableResp.RespData = dataList; //总面积 - var totalTifArea = aiShpList.Sum(r => r.AreaNum); + aiShpDataExpenseAccountingTableResp.TotalShpArea = aiShpList.Sum(r => r.AreaNum); //个数 - var totalTifCount = aiShpList.Count(); - return new Response>() + aiShpDataExpenseAccountingTableResp.TotalShpCount = aiShpList.Count(); + return new Response() { - Result = dataList + Result = aiShpDataExpenseAccountingTableResp }; } } + public async Task> GetAiShpDataExpenseAccountingTable(DateTime beginTime, DateTime endTime) + { + using (var db = base.UnitWork.CreateContext()) + { + if (beginTime > endTime) + { + throw new ArgumentException("开始时间需要小于结束时间"); + } + + var dateList = Enumerable.Range(0, (endTime.Date - beginTime.Date).Days + 1) + .Select(offset => beginTime.Date.AddDays(offset)) + .ToList(); + + var aiShpList = await db.InsAishp.AsQueryable() + .Where(r => r.ShpDate >= beginTime && r.ShpDate <= endTime) + .ToListAsync(); + + var taskList = await db.InsTask.AsQueryable() + .Where(t => t.CreateTime >= beginTime && t.CreateTime <= endTime) + .ToListAsync(); + int TotalIssuedCount = 0; + var dataList = dateList.Select(date => + { + var aiShpForDay = aiShpList.Where(r => r.ShpDate.Date == date).ToList(); + + var areaNum = aiShpForDay.Sum(r => r.AreaNum); + var shpCount = aiShpForDay.Sum(r => r.ShpCount ?? 0); + + var issuedCount = taskList.Count(t => aiShpForDay.Any(r => r.Id == t.ShpId)); + TotalIssuedCount += issuedCount; + //文件夹 + var folderPath = string.Join(",", aiShpForDay.Select(r => + { + var directory = Path.GetDirectoryName(r.ShpPath); // 获取目录部分 + return directory != null ? Path.GetFileName(directory) : string.Empty; // 获取目录名 + }).ToList()); + + return new AiShpDataExpenseAccountingResp + { + Number = dateList.IndexOf(date) + 1, + ShapDate = date.ToString("yyyyMMdd"), + AreaNum = areaNum, + ShpCount = shpCount, + IssuedCount = issuedCount, + FolderPath = folderPath, + }; + }).ToList(); + + var totalShpArea = aiShpList.Sum(r => r.AreaNum); + var totalShpCount = aiShpList.Count(); + + var aiShpDataExpenseAccountingTableResp = new AiShpDataExpenseAccountingTableResp + { + RespData = dataList, + TotalShpArea = totalShpArea, + TotalShpCount = totalShpCount, + TotalShpProduction = 0, + TotalShpInterpretation = 0, + TotalJudgment = 0, + TotalPush = 0, + TotalCost = 0, + TotalIssuedCount = TotalIssuedCount + }; + + return new Response + { + Result = aiShpDataExpenseAccountingTableResp + }; + } + } + //导出 + public Response CaseOfMineralsToExcelNew(DateTime startTime, DateTime endTime) + { + Response response = new Response(); + try + { + var list = GetAiShpDataExpenseAccountingTable(startTime, endTime).Result; + HSSFWorkbook workbook = new HSSFWorkbook(); + + #region 样式设置 + + ICellStyle CreateCellStyle(HSSFWorkbook wb, short fontHeight, string fontName, HorizontalAlignment hAlign, + VerticalAlignment vAlign, bool bold = false) + { + IFont font = wb.CreateFont(); + font.FontName = fontName; + font.FontHeightInPoints = fontHeight; + font.IsBold = bold; + + ICellStyle style = wb.CreateCellStyle(); + style.BorderBottom = BorderStyle.Thin; + style.BorderLeft = BorderStyle.Thin; + style.BorderRight = BorderStyle.Thin; + style.BorderTop = BorderStyle.Thin; + style.Alignment = hAlign; + style.VerticalAlignment = vAlign; + style.SetFont(font); + style.WrapText = true; + + return style; + } + + ICellStyle CreateCellStyle2(HSSFWorkbook wb, short fontHeight, string fontName, HorizontalAlignment hAlign, + VerticalAlignment vAlign, bool bold = false) + { + IFont font = wb.CreateFont(); + font.FontName = fontName; + font.FontHeightInPoints = fontHeight; + font.IsBold = bold; + + ICellStyle style = wb.CreateCellStyle(); + style.Alignment = hAlign; + style.VerticalAlignment = vAlign; + style.SetFont(font); + style.WrapText = true; + + return style; + } + + // 创建右对齐样式 + ICellStyle rightAlignStyle = workbook.CreateCellStyle(); + rightAlignStyle.Alignment = HorizontalAlignment.Right; // 设置右对齐 + rightAlignStyle.VerticalAlignment = VerticalAlignment.Center; + IFont font = workbook.CreateFont(); + font.FontName = "宋体"; + font.FontHeightInPoints = 12; + rightAlignStyle.SetFont(font); + rightAlignStyle.WrapText = true; + //第二个sheet右对齐样式 + ICellStyle rightAlignStyle2 = workbook.CreateCellStyle(); + rightAlignStyle2.Alignment = HorizontalAlignment.Right; // 设置右对齐 + rightAlignStyle2.VerticalAlignment = VerticalAlignment.Center; + IFont font1 = workbook.CreateFont(); + font1.FontName = "宋体"; + font1.FontHeightInPoints = 9; + rightAlignStyle2.SetFont(font1); + rightAlignStyle2.WrapText = true; + + ICellStyle contentStyle = + CreateCellStyle(workbook, 16, "宋体", HorizontalAlignment.Center, VerticalAlignment.Center); + ICellStyle titleStyle = + CreateCellStyle2(workbook, 10, "宋体", HorizontalAlignment.Left, VerticalAlignment.Center); + ICellStyle headerStyle1 = CreateCellStyle2(workbook, 18, "方正小标宋简体", HorizontalAlignment.Center, + VerticalAlignment.Center); + ICellStyle headerStyle2 = + CreateCellStyle(workbook, 16, "宋体", HorizontalAlignment.Right, VerticalAlignment.Center); + ICellStyle headerStyle3 = + CreateCellStyle(workbook, 16, "宋体", HorizontalAlignment.Center, VerticalAlignment.Center); + //第二个sheet + ICellStyle contentStyle22 = + CreateCellStyle(workbook, 12, "宋体", HorizontalAlignment.Center, VerticalAlignment.Center); + ICellStyle titleStyle2 = + CreateCellStyle2(workbook, 9, "宋体", HorizontalAlignment.Left, VerticalAlignment.Center); + ICellStyle headerStyle21 = CreateCellStyle2(workbook, 18, "方正小标宋简体", HorizontalAlignment.Center, + VerticalAlignment.Center); + ICellStyle headerStyle23 = + CreateCellStyle(workbook, 12, "黑体", HorizontalAlignment.Center, VerticalAlignment.Center); + ICellStyle beizhuStyle21 = + CreateCellStyle2(workbook, 9, "宋体", HorizontalAlignment.Left, VerticalAlignment.Center); + + #endregion + + #region 第一个sheet + + #region 创建表头 + + IRow CreateRowWithHeight(ISheet sht, int rowIndex, float heightInPoints) + { + IRow row = sht.CreateRow(rowIndex); + row.HeightInPoints = heightInPoints; + return row; + } + + void SetCellStyle(IRow row, ICellStyle style) + { + foreach (NPOI.SS.UserModel.ICell cell in row.Cells) + { + cell.CellStyle = style; + } + } + + #endregion + + #endregion + + + #region 第二个sheet + + ISheet sheet2 = workbook.CreateSheet("Sheet2"); + + #region 创建表头 + + IRow row20 = CreateRowWithHeight(sheet2, 0, 42); + row20.CreateCell(0).SetCellValue("汇总表"); + + IRow row22 = CreateRowWithHeight(sheet2, 1, 24); + + row22.CreateCell(0).SetCellValue("县区"); + row22.CreateCell(1).SetCellValue("下发线索总数(个)"); + row22.CreateCell(2).SetCellValue("总数"); + row22.CreateCell(3); + row22.CreateCell(4); + row22.CreateCell(5).SetCellValue("定性"); + row22.CreateCell(6); + row22.CreateCell(7); + row22.CreateCell(8); + row22.CreateCell(9).SetCellValue("分类"); + row22.CreateCell(10); + row22.CreateCell(11).SetCellValue("超期未报(个)"); + + IRow row23 = CreateRowWithHeight(sheet2, 2, 69); + + row23.CreateCell(0); + row23.CreateCell(1); + row23.CreateCell(2).SetCellValue("已完成(个)"); + row23.CreateCell(3).SetCellValue("未完成(个)"); + row23.CreateCell(4).SetCellValue("完成率(%)"); + row23.CreateCell(5).SetCellValue("合法(个)"); + row23.CreateCell(6).SetCellValue("违法(个)"); + row23.CreateCell(7).SetCellValue("其他(个)"); + row23.CreateCell(8).SetCellValue("未定性(个)"); + row23.CreateCell(9).SetCellValue("非法开采(个)"); + row23.CreateCell(10).SetCellValue("非法加工(个)"); + row23.CreateCell(11); + + // 设置样式 + SetCellStyle(row20, headerStyle21); + + SetCellStyle(row22, headerStyle23); + SetCellStyle(row23, headerStyle23); + + + // 合并单元格 + sheet2.AddMergedRegion(new CellRangeAddress(0, 0, 0, 11)); // 合并标题行 + sheet2.AddMergedRegion(new CellRangeAddress(1, 2, 0, 0)); + sheet2.AddMergedRegion(new CellRangeAddress(1, 2, 1, 1)); + sheet2.AddMergedRegion(new CellRangeAddress(1, 1, 2, 4)); + sheet2.AddMergedRegion(new CellRangeAddress(1, 1, 5, 8)); + sheet2.AddMergedRegion(new CellRangeAddress(1, 1, 9, 10)); + sheet2.AddMergedRegion(new CellRangeAddress(1, 2, 11, 11)); + + + + // 设置列宽 + int[] normalColumns2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + foreach (var index in normalColumns2) sheet2.SetColumnWidth(index, 9 * 256); // 设置列宽 + int[] normalColumns1 = { 9, 10, 11 }; + foreach (var index in normalColumns1) sheet2.SetColumnWidth(index, 12 * 256); // 设置列宽 + + + #endregion + + #region 填充数据 + for (int i = 0; i < list.Result.RespData.Count - 1; i++) + { + var rowIndex = i + 3; // 数据从第5行开始 + var row = CreateRowWithHeight(sheet2, rowIndex, 34); + row.CreateCell(0).SetCellValue(list.Result.RespData[i].Number); + row.CreateCell(1).SetCellValue(list.Result.RespData[i].ShpProduction); + row.CreateCell(2).SetCellValue(list.Result.RespData[i].ShpInterpretation); + row.CreateCell(3).SetCellValue(list.Result.RespData[i].Judgment); + row.CreateCell(4).SetCellValue(list.Result.RespData[i].Push); + row.CreateCell(5).SetCellValue(list.Result.RespData[i].Cost); + row.CreateCell(6).SetCellValue(list.Result.RespData[i].ShpCount.ToString()); + row.CreateCell(7).SetCellValue(list.Result.RespData[i].IssuedCount); + row.CreateCell(8).SetCellValue(list.Result.RespData[i].FolderPath); + row.CreateCell(9).SetCellValue(list.Result.RespData[i].Format); + row.CreateCell(10).SetCellValue(list.Result.RespData[i].Remark); + SetCellStyle(row, contentStyle22); + } + #endregion + + #endregion + + response.Result = new MemoryStream(); + workbook.Write(response.Result); + response.Result.Position = 0; + workbook.Close(); + + response.Code = 200; + response.Message = "获取成功"; + } + catch (Exception ex) + { + response.Code = 500; + response.Message = ex.Message; + } + + return response; + } #endregion } } diff --git a/OpenAuth.App/ServiceApp/Response/AiShpDataExpenseAccountingTableResp.cs b/OpenAuth.App/ServiceApp/Response/AiShpDataExpenseAccountingTableResp.cs index c3e260e..ffc2da0 100644 --- a/OpenAuth.App/ServiceApp/Response/AiShpDataExpenseAccountingTableResp.cs +++ b/OpenAuth.App/ServiceApp/Response/AiShpDataExpenseAccountingTableResp.cs @@ -6,11 +6,74 @@ using System.Threading.Tasks; namespace OpenAuth.App.ServiceApp.Response { + public class AiShpDataExpenseAccountingResp + { + /// + /// 序号 + /// + public int Number { get; set; } + /// + /// 日期 + /// + public string ShapDate { get; set; } + /// + /// 面积 + /// + public decimal AreaNum { get; set; } + /// + /// 数量 + /// + public int? ShpCount { get; set; } + /// + /// 下发数量 + /// + public int IssuedCount { get; set; } + /// + /// 文件夹 + /// + public string FolderPath { get; set; } + /// + /// 影像生产 + /// + public string ShpProduction { get; set; } + /// + /// 解译 + /// + public string ShpInterpretation { get; set; } + /// + /// 研判分析 + /// + public string Judgment { get; set; } + /// + /// 图斑推送 + /// + public string Push { get; set; } + /// + /// 费用 + /// + public string Cost { get; set; } + /// + /// 格式 + /// + public string Format { get; set; } + /// + /// 备注 + /// + public string Remark { get; set; } + + } public class AiShpDataExpenseAccountingTableResp { - public string ShapDate { get; set; } - public decimal AreaNum { get; set; } - public int? ShpCount { get; set; } - public int issuedCount { get; set; } + public List RespData { get; set; } + public int TotalShpCount { get; set; } + public decimal TotalShpArea { get; set; } + public int TotalShpProduction { get; set; } + public int TotalShpInterpretation { get; set; } + public int TotalJudgment { get; set; } + public int TotalPush { get; set; } + public int TotalCost { get; set; } + public int TotalIssuedCount { get; set; } + } + } diff --git a/OpenAuth.WebApi/Controllers/ServiceControllers/DataAnalysisController.cs b/OpenAuth.WebApi/Controllers/ServiceControllers/DataAnalysisController.cs index b363202..b32f458 100644 --- a/OpenAuth.WebApi/Controllers/ServiceControllers/DataAnalysisController.cs +++ b/OpenAuth.WebApi/Controllers/ServiceControllers/DataAnalysisController.cs @@ -48,9 +48,9 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers /// [HttpGet] [AllowAnonymous] - public async Task>> GetAiShpDataExpenseAccountingTable(DateTime beginTime, DateTime endTime) + public async Task> GetAiShpDataExpenseAccountingTable(DateTime beginTime, DateTime endTime) { - Response> response = new Response>(); + Response response = new Response(); try { return await _app.GetAiShpDataExpenseAccountingTable(beginTime, endTime);