add report

This commit is contained in:
kamonwan taengsuk
2023-01-23 21:59:21 +07:00
parent 4417115ce8
commit 4dfecc8a9b
9 changed files with 739 additions and 1 deletions

View File

@@ -0,0 +1,233 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using FastReport;
using FastReport.Export.Csv;
using FastReport.Export.Mht;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;
namespace rmutr_report.Controllers
{
[SwaggerTag("สำหรับรายงานนักศึกษาเข้าใหม่")]
public class StudentNews : Controller
{
readonly Setting _setting;
public StudentNews(Setting setting)
{
this._setting = setting;
}
[HttpPost, Route("reports/student_news_faculty/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetStd1Report([FromRoute] string type,
[FromBody] student_news_faculty _student_news_faculty)
{
var std = _student_news_faculty.data;
var a1 = _student_news_faculty.data.Where(d => d.bachelors_degree == d.bachelors_degree)
.Sum(o => o.bachelors_degree);
foreach (var data in std)
{
data.total1 = a1;
if (data.bachelors_degree != 0 )
{
data.sum_percentage = (data.bachelors_degree*100)/a1;
}
var a2 = _student_news_faculty.data.Where(d => d.sum_percentage == d.sum_percentage)
.Sum(o => o.sum_percentage);
data.total2 = a2;
}
var student_news_facultys = new List<student_news_faculty>() {_student_news_faculty};
Report report = new Report();
report.Load(_setting.report_path + "student_news_faculty.frx");
report.RegisterData(student_news_facultys, "student_news_faculty");
report.Prepare();
MemoryStream stream = new MemoryStream();
switch (type)
{
case "pdf":
PDFExport pdf = new PDFExport();
report.Export(pdf, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf");
case "xls":
case "xlsx":
Excel2007Export excel = new Excel2007Export();
report.Export(excel, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel");
break;
case "mht":
MHTExport mht = new MHTExport();
report.Export(mht, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "multipart/related");
break;
case "csv":
CSVExport csv = new CSVExport();
report.Export(csv, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "text/csv");
break;
}
return Ok();
}
[HttpPost, Route("reports/student_news_area/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetStd2Report([FromRoute] string type,
[FromBody] student_news_area _student_news_faculty)
{
var std = _student_news_faculty.data;
var a1 = _student_news_faculty.data.Where(d => d.bachelors_degree_science == d.bachelors_degree_science)
.Sum(o => o.bachelors_degree_science);
var a2 = _student_news_faculty.data.Where(d => d.bachelors_degree_social == d.bachelors_degree_social)
.Sum(o => o.bachelors_degree_social);
foreach (var data in std)
{
data.bachelors_degree_total = data.bachelors_degree_science + data.bachelors_degree_social;
data.total1 = a1;
if ( data.bachelors_degree_total != 0)
{
data.sum_percentage_science = (data.bachelors_degree_science*100)/data.bachelors_degree_total;
}
if ( data.bachelors_degree_total != 0)
{
data.sum_percentage_social = (data.bachelors_degree_social*100)/data.bachelors_degree_total;
}
var a3 = _student_news_faculty.data.Where(d => d.bachelors_degree_total == d.bachelors_degree_total)
.Sum(o => o.bachelors_degree_total);
var a4 = _student_news_faculty.data.Where(d => d.sum_percentage_science == d.sum_percentage_science)
.Sum(o => o.sum_percentage_science);
var a5 = _student_news_faculty.data.Where(d => d.sum_percentage_social == d.sum_percentage_social)
.Sum(o => o.sum_percentage_social);
data.total2 = a2;
data.total3 = a3;
data.total4 = (data.total1*100)/data.total3;
data.total5 = (data.total2*100)/data.total3;
}
var student_news_facultys = new List<student_news_area>() {_student_news_faculty};
Report report = new Report();
report.Load(_setting.report_path + "student_news_area.frx");
report.RegisterData(student_news_facultys, "student_news_area");
report.Prepare();
MemoryStream stream = new MemoryStream();
switch (type)
{
case "pdf":
PDFExport pdf = new PDFExport();
report.Export(pdf, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf");
case "xls":
case "xlsx":
Excel2007Export excel = new Excel2007Export();
report.Export(excel, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel");
break;
case "mht":
MHTExport mht = new MHTExport();
report.Export(mht, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "multipart/related");
break;
case "csv":
CSVExport csv = new CSVExport();
report.Export(csv, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "text/csv");
break;
}
return Ok();
}
[HttpPost, Route("reports/student_news_area_major/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetStd3Report([FromRoute] string type,
[FromBody] student_news_area _student_news_faculty)
{
var std = _student_news_faculty.data;
var a1 = _student_news_faculty.data.Where(d => d.bachelors_degree_science == d.bachelors_degree_science)
.Sum(o => o.bachelors_degree_science);
var a2 = _student_news_faculty.data.Where(d => d.bachelors_degree_social == d.bachelors_degree_social)
.Sum(o => o.bachelors_degree_social);
foreach (var data in std)
{
data.bachelors_degree_total = data.bachelors_degree_science + data.bachelors_degree_social;
data.total1 = a1;
if ( data.bachelors_degree_total != 0)
{
data.sum_percentage_science = (data.bachelors_degree_science*100)/data.bachelors_degree_total;
}
if ( data.bachelors_degree_total != 0)
{
data.sum_percentage_social = (data.bachelors_degree_social*100)/data.bachelors_degree_total;
}
var a3 = _student_news_faculty.data.Where(d => d.bachelors_degree_total == d.bachelors_degree_total)
.Sum(o => o.bachelors_degree_total);
var a4 = _student_news_faculty.data.Where(d => d.sum_percentage_science == d.sum_percentage_science)
.Sum(o => o.sum_percentage_science);
var a5 = _student_news_faculty.data.Where(d => d.sum_percentage_social == d.sum_percentage_social)
.Sum(o => o.sum_percentage_social);
data.total2 = a2;
data.total3 = a3;
data.total4 = (data.total1*100)/data.total3;
data.total5 = (data.total2*100)/data.total3;
}
var student_news_facultys = new List<student_news_area>() {_student_news_faculty};
Report report = new Report();
report.Load(_setting.report_path + "student_news_area_major.frx");
report.RegisterData(student_news_facultys, "student_news_area");
report.Prepare();
MemoryStream stream = new MemoryStream();
switch (type)
{
case "pdf":
PDFExport pdf = new PDFExport();
report.Export(pdf, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf");
case "xls":
case "xlsx":
Excel2007Export excel = new Excel2007Export();
report.Export(excel, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel");
break;
case "mht":
MHTExport mht = new MHTExport();
report.Export(mht, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "multipart/related");
break;
case "csv":
CSVExport csv = new CSVExport();
report.Export(csv, stream);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "text/csv");
break;
}
return Ok();
}
}
}