150 lines
5.7 KiB
C#
150 lines
5.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Logging;
|
||
using ReportService.Database;
|
||
using ReportService.Models;
|
||
using ReportService.Models.ReportApi;
|
||
|
||
namespace ReportService.Controllers.api
|
||
{
|
||
[Area("api")]
|
||
[Route("api/[Controller]/[Action]")]
|
||
public class ReportApiController:Controller
|
||
{
|
||
public IWebHostEnvironment Env { get; private set; }
|
||
public ILogger Logger { get; set; }
|
||
public DbContextFactory DbFactory { get; set; }
|
||
|
||
/// <summary>
|
||
/// 报表存放位置
|
||
/// </summary>
|
||
public string ReportPath {
|
||
get {
|
||
return Path.Combine(this.Env.ContentRootPath,"wwwroot\\report");
|
||
}
|
||
}
|
||
|
||
public ReportApiController(ILogger<ReportApiController> logger,IWebHostEnvironment env,DbContextFactory dbFactory) {
|
||
this.Logger = logger;
|
||
this.Env = env;
|
||
this.DbFactory = dbFactory;
|
||
}
|
||
|
||
/// <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('.')));
|
||
}
|
||
this.Logger.LogInformation($"GetReportFiles:{string.Join(',',result.ToArray())}");
|
||
return result;
|
||
}
|
||
/// <summary>
|
||
/// 获取树状架构的报表列表
|
||
/// </summary>
|
||
/// <returns>报表结构</returns>
|
||
public ReportListModel GetReportList(string rootPath = null) {
|
||
var result = new ReportListModel();
|
||
var cPath = Path.Combine(this.ReportPath,rootPath ?? "");
|
||
var dirs = Directory.GetDirectories(cPath);
|
||
foreach(var d in dirs) {
|
||
if(d.Trim().ToLower().EndsWith("\\hide")) {
|
||
continue;
|
||
}
|
||
var sd = new ReportItem {
|
||
Name = d.Substring(d.LastIndexOf('\\') + 1),
|
||
Path = rootPath ?? "",
|
||
Type = ItemType.group,
|
||
};
|
||
result.Sub.Add(sd);
|
||
sd.Sub.AddRange(GetReportList(Path.Combine(sd.Path,sd.Name)).Sub);
|
||
}
|
||
var files = Directory.GetFiles(cPath,"*.html");
|
||
foreach(var f in files) {
|
||
var file = new System.IO.FileInfo(f);
|
||
if(file.Name.ToLower().EndsWith(".rpt.html")) {
|
||
continue;
|
||
}
|
||
result.Sub.Add(new ReportItem {
|
||
Name = file.Name.Substring(0,file.Name.IndexOf('.')),
|
||
Path = rootPath,
|
||
Type = ItemType.file,
|
||
});
|
||
}
|
||
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)) {
|
||
this.Logger.LogInformation($"GetHtml:{fileName}");
|
||
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)) {
|
||
this.Logger.LogInformation($"GetPrint:{fileName}");
|
||
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) {
|
||
return GetData(new GetDataData {
|
||
DbName = "ReportService",
|
||
Sql = sql,
|
||
});
|
||
}
|
||
/// <summary>
|
||
/// 从数据请求数据。
|
||
/// </summary>
|
||
/// <param name="data">请求数据模型</param>
|
||
/// <returns>返回json格式数据</returns>
|
||
public object GetData([FromBody]GetDataData data) {
|
||
var result = this.DbFactory.RunRawSql(data.DbName,data.Sql);
|
||
this.Logger.LogInformation($"GetData:\n{data.Sql}\n{result}");
|
||
return Content(result,"application/json; charset=utf-8");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取系统时间
|
||
/// </summary>
|
||
/// <param name="format">时间格式</param>
|
||
/// <returns>时间字符串</returns>
|
||
public object GetServerTime(string format) {
|
||
format ??= "yyyyMMdd HH:mm:ss";
|
||
return new { str = DateTimeOffset.Now.ToString(format) };
|
||
}
|
||
}
|
||
}
|