Commit 1fa36210 by lcx

返回数据

parent a2aef2fd
...@@ -228,7 +228,7 @@ public ApiResponse MenuReport([CustomizeValidator(RuleSet = "Menu"), FromBody] R ...@@ -228,7 +228,7 @@ public ApiResponse MenuReport([CustomizeValidator(RuleSet = "Menu"), FromBody] R
[HttpPost] [HttpPost]
public ApiResponse Operation([ FromBody] ReportRequest request) public ApiResponse Operation([ FromBody] ReportRequest request)
{ {
var list = reportService.Test(request); var list = reportService.Operation(request);
return new ApiResponse(ResponseType.OK, "", list); return new ApiResponse(ResponseType.OK, "", list);
} }
} }
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace Performance.Services namespace Performance.Services
{ {
...@@ -403,46 +401,194 @@ public void ExecProc(string execsql, object param) ...@@ -403,46 +401,194 @@ public void ExecProc(string execsql, object param)
} }
} }
public List<PerReport> Test(ReportRequest request) public SheetExportResponse Operation(ReportRequest request)
{ {
string where = ""; SheetExportResponse sheet = new SheetExportResponse();
if (!string.IsNullOrEmpty(request.Year))
#region data
IEnumerable<int> years = new int[] { };
if (string.IsNullOrEmpty(request.Year))
{ {
where += $" and year in ({request.Year}) "; string getYearsSql = "select max(`year`) `year` from view_test_data where hospitalid = @hospitalid;";
years = perforReportRepository.DapperQuery<int?>(getYearsSql, new { hospitalid = request.HospitalId })?.Select(t => t ?? 0);
} }
if (!string.IsNullOrEmpty(request.Month)) else
{ {
where += $" and month in ({request.Month}) "; years = Array.ConvertAll(request.Year.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries), t => ConvertHelper.To<int>(t));
} }
var getDate = $"select distinct year, month from view_test_data where hospitalid = {request.HospitalId} {where}"; if (years == null || !years.Any()) return sheet;
var dateList = perforReportRepository.DapperQuery<per_allot>(getDate, new { });
if (dateList == null || !dateList.Any()) return new List<PerReport>();
if (string.IsNullOrEmpty(request.Year)) string sql = $"select * from view_test_data where hospitalid = @hospitalid and year in @year;";
var data = perforReportRepository.DapperQuery<ViewTestData>(sql, new { hospitalId = request.HospitalId, year = years })?.ToList();
#endregion
years = data.Select(t => t.Year).Distinct().OrderByDescending(t => t);
int index = 0;
#region header
var cells = new List<Cell>
{ {
int year = dateList.Max(m => m.Year); new Cell{ CellType = "header", CellValue = "经济指标", PointCell = 0, MergeRow = 1, MergeCell = 3 },
dateList = dateList.Where(t => t.Year == year).ToList(); };
}
StringBuilder fields = new StringBuilder(string.Empty); for (int i = 1; i < 13; i++)
foreach (var date in dateList.OrderBy(t => t.Year).ThenBy(t => t.Month))
{ {
fields.Append($" sum(case when year = {date.Year} and month = {date.Month} then if(ifnull(value, '')<>'', value, 0) else 0 end) `{date.Year}{date.Month}月`,"); cells.Add(new Cell { CellType = "header", CellValue = $"{i}月", PointCell = i + 2, MergeRow = 1, MergeCell = 1 });
} }
string columns = fields.ToString(); //foreach (var year in years)
if (!string.IsNullOrEmpty(columns) && columns.Length > 1) //{
columns = "," + columns.Substring(0, columns.Length - 1); // for (int i = 1; i < 13; i++)
// {
// cells.Add(new Cell { CellType = "header", CellValue = $"{i}月", PointCell = i + 2, MergeRow = 1, MergeCell = 1 });
// }
//}
string sql = $"select source, category, itemname {columns} from view_test_data where hospitalid = @hospitalid and year in @year and month in @month group by source, category, itemname;"; sheet.Header = new List<Row>
{
new Row(0){ Data = cells }
};
#endregion
var data = perforReportRepository.DapperQuery<object>(sql, new { hospitalId = request.HospitalId, year = dateList.Select(t => t.Year), month = dateList.Select(t => t.Month) })?.ToList();
if (data == null || !data.Any()) if (data == null || !data.Any())
return new List<PerReport>(); return sheet;
var sourcetypes = data.Select(t => t.SourceType).Distinct();
#region body
Dictionary<int, Func<ViewTestData, object>> dict = new Dictionary<int, Func<ViewTestData, object>>
{
{ 1, t => t.January },
{ 2, t => t.February },
{ 3, t => t.March },
{ 4, t => t.April },
{ 5, t => t.May },
{ 6, t => t.June },
{ 7, t => t.July },
{ 8, t => t.August },
{ 9, t => t.September },
{ 10, t => t.October },
{ 11, t => t.November },
{ 12, t => t.December },
};
int rownumber = 0;
int maxPointIndex = 0;
List<Row> rows = new List<Row>();
foreach (var sourcetype in sourcetypes.OrderBy(t => t))
{
int inittype = 0; // 0加载sourcetype、category; 1加载category; 2不加载sourcetype、category
var sourcetypeData = data.Where(t => t.SourceType == sourcetype);
if (sourcetypeData == null || !sourcetypeData.Any()) continue;
return new List<PerReport>(); foreach (var category in sourcetypeData.Select(t => t.Category).Distinct().OrderBy(t => t))
{
var categoryData = sourcetypeData.Where(t => t.Category == category);
if (categoryData == null || !categoryData.Any()) continue;
foreach (var itemname in categoryData.Select(t => t.ItemName).Distinct().OrderBy(t => t))
{
var itemnameData = categoryData.Where(t => t.ItemName == itemname);
if (itemnameData == null || !itemnameData.Any()) continue;
var rowcells = new List<Cell>();
if (inittype == 0)
{
rowcells = new List<Cell>
{
new Cell { CellType = "body", CellValue = sourcetype, PointCell = 0, MergeRow = sourcetypeData.Count(), MergeCell = 1 },
new Cell { CellType = "body", CellValue = category, PointCell = 1, MergeRow = categoryData.Count(), MergeCell = 1 },
};
} }
else if (inittype == 1)
{
rowcells.Add(new Cell { CellType = "body", CellValue = category, PointCell = 0, MergeRow = categoryData.Count(), MergeCell = 1 });
}
int itemnameIndex = inittype == 0 ? 2 : inittype == 1 ? 1 : 0;
rowcells.Add(new Cell { CellType = "body", CellValue = itemname, PointCell = itemnameIndex, MergeRow = 1, MergeCell = 1 });
index = 0;
foreach (var year in years)
{
var thisdata = itemnameData.FirstOrDefault(t => t.Year == year);
if (thisdata == null)
{
index++;
continue;
}
int point = itemnameIndex + 1 + index * 12;
for (int i = 1; i < 13; i++)
{
rowcells.Add(new Cell { CellType = "body", CellValue = dict[i].Invoke(thisdata), PointCell = point, MergeRow = 1, MergeCell = 1 });
point++;
}
index++;
}
if (inittype == 0) maxPointIndex = rowcells.Max(t => t.PointCell);
inittype = 2;
rows.Add(new Row(rownumber) { Data = rowcells });
rownumber++;
}
inittype = 1;
}
}
sheet.Row = rows;
#endregion
return sheet;
}
}
public class ViewTestData
{
public int HospitalId { get; set; }
public int Year { get; set; }
public string SourceType { get; set; }
public string Category { get; set; }
public string ItemName { get; set; }
public decimal? January { get; set; }
public decimal? February { get; set; }
public decimal? March { get; set; }
public decimal? April { get; set; }
public decimal? May { get; set; }
public decimal? June { get; set; }
public decimal? July { get; set; }
public decimal? August { get; set; }
public decimal? September { get; set; }
public decimal? October { get; set; }
public decimal? November { get; set; }
public decimal? December { get; set; }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment