前端代码——动态数据加载
修改日志查询报表
修改日志查询报表
后端代码
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using ReportSystem.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace ReportSystem.Controllers
{
public class LogController : Controller
{
OraHelper oracle = new OraHelper();
OraHelper oraSecondary = new OraHelper("secondary"); //创建备库的链接对象(用于查询数据)
// GET: Log
public ActionResult QueryLogReport()
{
return View();
}
//查询日志报表
public string getLogReport()
{
try
{
int page = (Convert.ToInt32(Request.QueryString["page"].ToString()) - 1) * Convert.ToInt32(Request.QueryString["limit"].ToString());
string limit = Request.QueryString["limit"].ToString();
string model = Request.QueryString["model"].ToString();
string tableName = Request.QueryString["tableName"].ToString();
string modifier = Request.QueryString["modifier"].ToString();
string startTime = Request.QueryString["startTime"].ToString();
string endTime = Request.QueryString["endTime"].ToString();
string guid = Request.QueryString["guid"].ToString();
StringBuilder condition0 = new StringBuilder();
condition0.Append(MyStringBuilder.toString(model, $@" and MODELTYPE='{model}' "));
condition0.Append(MyStringBuilder.toString(tableName, $@" and TABLENAME='{tableName}' "));
StringBuilder condition = new StringBuilder();
//condition.Append(MyStringBuilder.toString(model, $@" and MODELTYPE='{model}' "));
//condition.Append(MyStringBuilder.toString(tableName, $@" and TABLENAME='{tableName}' "));
condition.Append(MyStringBuilder.toString(modifier, $@" and MODIFY_USER='{modifier}' "));
condition.Append(MyStringBuilder.toString(startTime, $@" and MODIFY_DATE between to_date('{startTime}','yyyy-mm-dd hh24:mi:ss') and to_date('{endTime}','yyyy-mm-dd hh24:mi:ss') "));
//表头
string[] sheetHeader = new string[] { };
List sheetHeader_list = new List(sheetHeader);
//常规查询
if (guid.Equals("undefined"))
{
//查询表明和列明
string tableSql = $@"select logtable,columnzh from log_table_detail where 1 = 1 {condition0.ToString()}";
DataSet ds0 = oracle.GetDataSet(tableSql);
foreach (string col in ds0.Tables[0].Rows[0]["COLUMNZH"].ToString().Split(','))
{
sheetHeader_list.Add(col);
}
sheetHeader = sheetHeader_list.ToArray();
string rawSql = $@"select * from {ds0.Tables[0].Rows[0]["LOGTABLE"].ToString()} where 1 = 1 {condition.ToString()}";
//获取总数
string count = oracle.GetDataScalar($@"SELECT COUNT(*) NUMS FROM ({rawSql})");
//获取需要的数据
DataSet ds = oracle.GetDataSet($@"SELECT * FROM ({rawSql} ORDER BY MODIFY_DATE) OFFSET {page} ROWS FETCH FIRST {limit} ROWS WITH TIES");
JObject json = new JObject();
json.Add("code", "0");
json.Add("msg", "");
json.Add("count", count);
//如果导出的数量超过3万,则使用服务器导出,否则使用浏览器导出
string filePath = string.Empty;
JArray jArray = new JArray();
if (ds.Tables[0].Rows.Count > 30000)
{
filePath = myNpoi.DataTableToExcel(ds.Tables[0], sheetHeader);
}
else
{
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
jArray = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ds.Tables[0], Formatting.Indented, timeFormat));
}
json.Add("filePath", filePath);
json.Add("data", jArray);
json.Add(new JProperty("sheetHeader", sheetHeader));
return json.ToString();
}
else
{
JObject json = new JObject();
json.Add("code", "1");
json.Add("count", "0");
return json.ToString();
}
}
catch (Exception ex)
{
JObject json = new JObject();
json.Add("code", "1");
json.Add("count", "0");
return json.ToString();
}
}
//得到报表的表头
public string getZhColumnName()
{
string model = Request.QueryString["model"].ToString();
string tableName = Request.QueryString["tableName"].ToString();
StringBuilder condition = new StringBuilder();
condition.Append(MyStringBuilder.toString(model, $@" and MODELTYPE='{model}' "));
condition.Append(MyStringBuilder.toString(tableName, $@" and TABLENAME='{tableName}' "));
string colSql = $@"select columnzh,columnen from log_table_detail where 1 = 1 {condition.ToString()}";
string count = oraSecondary.GetDataScalar($@"SELECT COUNT(*) NUMS FROM ({colSql})");
DataSet ds = oraSecondary.GetDataSet($@"SELECT * FROM ({colSql})");
JObject json = new JObject();
json.Add("code", "0");
json.Add("count", count);
JArray jArray = new JArray();
jArray = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ds.Tables[0]));
json.Add("data", jArray);
return json.ToString();
}
//得到模块和表类型
public string getTypes()
{
string typeSql = $@"select distinct modeltype from log_table_detail";
string count = oracle.GetDataScalar($@"SELECT COUNT(*) NUMS FROM ({typeSql})");
DataSet ds = oracle.GetDataSet($@"SELECT * FROM ({typeSql})");
string typeSql2 = $@"select distinct tablename from log_table_detail";
string count2 = oracle.GetDataScalar($@"SELECT COUNT(*) NUMS FROM ({typeSql2})");
DataSet ds2 = oracle.GetDataSet($@"SELECT * FROM ({typeSql2})");
JObject json = new JObject();
json.Add("code", "0");
json.Add("count", count);
JArray jArray = new JArray();
jArray.Add((JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ds.Tables[0])));
jArray.Add((JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ds2.Tables[0])));
json.Add("data", jArray);
return json.ToString();
}
public string getTableTypes(string model = "")
{
string typeSql = $@"select distinct tablename from log_table_detail where modeltype = '{model}'";
string count = oracle.GetDataScalar($@"SELECT COUNT(*) NUMS FROM ({typeSql})");
DataSet ds = oracle.GetDataSet($@"SELECT * FROM ({typeSql})");
JObject json = new JObject();
json.Add("code", "0");
json.Add("count", count);
JArray jArray = new JArray();
jArray.Add((JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ds.Tables[0])));
json.Add("data", jArray);
return json.ToString();
}
}
}
_ViewStart.cshtml
@{
//Layout = "~/Views/Shared/_Layout.cshtml";
}