90 lines
3.1 KiB
C#
90 lines
3.1 KiB
C#
using System.Collections.Generic;
|
||
using System.IO;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Logging;
|
||
using ReportService.Database;
|
||
|
||
namespace ReportService.Controllers.api
|
||
{
|
||
[Area("api")]
|
||
[Route("api/[Controller]/[Action]")]
|
||
public class ReportApiController:Controller
|
||
{
|
||
private readonly ILogger<HomeController> _logger;
|
||
|
||
public IWebHostEnvironment Env { get; private set; }
|
||
public RSDbContext Db { get; set; }
|
||
|
||
/// <summary>
|
||
/// 报表存放位置
|
||
/// </summary>
|
||
public string ReportPath {
|
||
get {
|
||
return Path.Combine(this.Env.ContentRootPath,"wwwroot/report");
|
||
}
|
||
}
|
||
|
||
public ReportApiController(ILogger<HomeController> logger,IWebHostEnvironment env,RSDbContext db) {
|
||
this._logger = logger;
|
||
this.Env = env;
|
||
this.Db = db;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取页面模板列表
|
||
/// </summary>
|
||
/// <returns>名称列表</returns>
|
||
public List<string> GetReportFiles() {
|
||
var files = Directory.GetFiles(this.ReportPath,"*.html");
|
||
var result = new List<string>();
|
||
foreach(var f in files) {
|
||
var file = new System.IO.FileInfo(f);
|
||
if(file.Name.ToLower().EndsWith(".rpt.html")) {
|
||
continue;
|
||
}
|
||
result.Add(file.Name.Substring(0,file.Name.IndexOf('.')));
|
||
}
|
||
return result;
|
||
}
|
||
/// <summary>
|
||
/// 获取模板内容显示模板页面
|
||
/// </summary>
|
||
/// <param name="fileName">模板文件名</param>
|
||
/// <returns>模板内容</returns>
|
||
public IActionResult GetHtml(string fileName) {
|
||
var htmlFile = Path.Combine(this.ReportPath,fileName + ".html");
|
||
using(var fr = System.IO.File.OpenRead(htmlFile)) {
|
||
using(var sr = new StreamReader(fr)) {
|
||
return Content(sr.ReadToEnd());
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取模板内容显示模板页面
|
||
/// </summary>
|
||
/// <param name="fileName">模板文件名</param>
|
||
/// <returns>模板内容</returns>
|
||
public IActionResult GetPrint(string fileName) {
|
||
var htmlFile = Path.Combine(this.ReportPath,fileName + ".rpt.html");
|
||
using(var fr = System.IO.File.OpenRead(htmlFile)) {
|
||
using(var sr = new StreamReader(fr)) {
|
||
return Content(sr.ReadToEnd(),"text/html; charset=utf-8");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用database执行sql语句,将结果封装为json对象返回
|
||
/// </summary>
|
||
/// <param name="sql">要执行的sql语句</param>
|
||
/// <returns>json对象</returns>
|
||
public object GetResult(string sql) {
|
||
var result = this.Db.SqlJsonQuery(sql);
|
||
return Content(result,"application/json; charset=utf-8");
|
||
}
|
||
|
||
}
|
||
}
|