| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- /*
- * 自定义查询
- */
- using System;
- using System.Web;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Data;
- using System.Threading.Tasks;
- using System.Text.RegularExpressions;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using MySystem.Models;
- using Library;
- using LitJson;
- using MySystemLib;
- namespace MySystem.Areas.Admin.Controllers
- {
- [Area("Admin")]
- [Route("Admin/[controller]/[action]")]
- public class CustomQueryDoController : BaseController
- {
- public CustomQueryDoController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
- {
- OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
- }
- #region 自定义查询列表
- /// <summary>
- /// 根据条件查询自定义查询列表
- /// </summary>
- /// <returns></returns>
- public IActionResult Index(int Id, string right)
- {
- ViewBag.RightInfo = RightInfo;
- ViewBag.right = right;
- ViewBag.Id = Id.ToString();
- Dictionary<string, string> query = new Dictionary<string, string>();
- CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
- if(SysUserName != "admin")
- {
- string checkString = "," + item.AdminNames + ",";
- if(!checkString.Contains("," + SysUserName + ","))
- {
- return Content("无权限");
- }
- }
- string sql = item.SqlContent;
- MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
- foreach(Match m in mc)
- {
- string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- string fieldName = fieldData[0];
- string Title = fieldData[1];
- if(!query.ContainsKey(fieldName))
- {
- query.Add(fieldName, Title);
- }
- }
- List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
- foreach(CustomQuerySub sub in subItems)
- {
- sql = sub.SqlContent;
- mc = Regex.Matches(sql, @"\$\{.*?\}\$");
- foreach(Match m in mc)
- {
- string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- string fieldName = fieldData[0];
- string Title = fieldData[1];
- if(!query.ContainsKey(fieldName))
- {
- query.Add(fieldName, Title);
- }
- }
- }
- ViewBag.query = query;
- string[] fieldList = item.SeoDescription.Split(',');
- string cols = "";
- foreach(string Title in fieldList)
- {
- cols += ",{field:'" + Title + "', width: 200, title:'" + Title + "'}";
- }
- ViewBag.cols = cols;
- return View();
- }
- #endregion
- #region 根据条件查询自定义查询列表
- /// <summary>
- /// 自定义查询列表
- /// </summary>
- /// <returns></returns>
- public JsonResult IndexData(int Id, int page = 1, int limit = 30)
- {
- Dictionary<string, string> dicVals = new Dictionary<string, string>();
- Dictionary<string, Dictionary<string, string>> dicValsJson = new Dictionary<string, Dictionary<string, string>>();
- string ConnectStr = "";
- List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
- foreach(CustomQuerySub sub in subItems)
- {
- ConnectStr = sub.DatabaseConnect;
- if(!string.IsNullOrEmpty(ConnectStr))
- {
- ConnectStr = GetConnectStr(sub.DatabaseConnect);
- }
- else
- {
- ConnectStr = MysqlConn.ReadSqlConnStr;
- }
- string sqlstr = sub.SqlContent;
- MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
- foreach(Match m in mcsub)
- {
- string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- string fieldName = fieldData[0];
- sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
- }
- mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
- foreach(Match m in mcsub)
- {
- string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- if(dicVals.ContainsKey(fieldName))
- {
- sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
- }
- else
- {
- sqlstr = sqlstr.Replace(m.Value, "");
- }
- }
- DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
- if(dtsub.Rows.Count > 0)
- {
- if(dtsub.Rows.Count > 1)
- {
- if(!string.IsNullOrEmpty(sub.DataKey))
- {
- string[] TextFields = sub.DataText.Split(',');
- Dictionary<string, string> vals = new Dictionary<string, string>();
- foreach(DataRow dr in dtsub.Rows)
- {
- foreach(string key in TextFields)
- {
- vals.Add(key, dr[key].ToString());
- }
- }
- dicValsJson.Add(sub.Alias + "." + sub.DataKey, vals);
- }
- else
- {
- string val = "";
- foreach(DataRow dr in dtsub.Rows)
- {
- val += dr[0].ToString() + ",";
- }
- val = val.TrimEnd(',');
- dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
- }
- }
- else
- {
- DataRow dr = dtsub.Rows[0];
- foreach(DataColumn dc in dtsub.Columns)
- {
- dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
- }
- }
- }
- }
- CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
- ConnectStr = item.DatabaseConnect;
- if(!string.IsNullOrEmpty(ConnectStr))
- {
- ConnectStr = GetConnectStr(item.DatabaseConnect);
- }
- else
- {
- if(item.ExcuteFlag == 2 || item.ExcuteFlag == 3)
- {
- ConnectStr = MysqlConn.RedisConnStr;
- }
- else
- {
- ConnectStr = MysqlConn.ReadSqlConnStr;
- }
- }
- string sql = item.SqlContent;
- MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
- foreach(Match m in mc)
- {
- string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- string fieldName = fieldData[0];
- sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
- }
- mc = Regex.Matches(sql, @"\#\{.*?\}\#");
- foreach(Match m in mc)
- {
- string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- if(dicVals.ContainsKey(fieldName))
- {
- sql = sql.Replace(m.Value, dicVals[fieldName]);
- }
- else
- {
- sql = sql.Replace(m.Value, "");
- }
- }
- mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
- foreach(Match m in mc)
- {
- string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
- string str = fieldData[0];
- string oldStr = fieldData[1];
- string newStr = fieldData[2];
- str = str.Replace(oldStr, newStr);
- sql = sql.Replace(m.Value, str);
- }
- List<Dictionary<string, string>> diclist = new List<Dictionary<string, string>>();
- if(item.ExcuteFlag == 0)
- {
- if(page > 1)
- {
- int skip = (page - 1) * limit;
- sql += " limit " + skip + "," + limit;
- }
- else
- {
- sql += " limit " + limit;
- }
- DataTable dt = CustomerSqlConn.dtable(sql, ConnectStr);
- foreach(DataRow dr in dt.Rows)
- {
- Dictionary<string, string> row = new Dictionary<string, string>();
- foreach(DataColumn dc in dt.Columns)
- {
- Match m = Regex.Match(dr[dc.ColumnName].ToString(), @"\#\{.*?\}\#");
- if(m.Success)
- {
- string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- Dictionary<string, string> fieldVals = dicValsJson[fieldName];
- foreach(string field in fieldVals.Keys)
- {
- row.Add(dc.ColumnName + field, fieldVals[field]);
- }
- }
- else
- {
- if (dc.DataType == typeof(DateTime))
- {
- row.Add(dc.ColumnName, string.IsNullOrEmpty(dr[dc.ColumnName].ToString()) ? "" : DateTime.Parse(dr[dc.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
- }
- else
- {
- row.Add(dc.ColumnName, dr[dc.ColumnName].ToString());
- }
- }
- }
- diclist.Add(row);
- }
- }
- else if(item.ExcuteFlag == 2)
- {
- string redisCommand = sql;
- string[] redisCommandList = redisCommand.Split(' ');
- string kind = redisCommandList[0];
- string key = redisCommandList[1];
- if(kind == "lrange_json")
- {
- List<Dictionary<string, string>> list = new CustomRedisDbconn(ConnectStr).GetList<Dictionary<string, string>>(key, page, limit);
- foreach(Dictionary<string, string> dr in list)
- {
- Dictionary<string, string> row = new Dictionary<string, string>();
- foreach(string ColumnName in dr.Keys)
- {
- Match m = Regex.Match(dr[ColumnName], @"\#\{.*?\}\#");
- if(m.Success)
- {
- string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- Dictionary<string, string> fieldVals = dicValsJson[fieldName];
- foreach(string field in fieldVals.Keys)
- {
- row.Add(ColumnName + field, fieldVals[field]);
- }
- }
- else
- {
- row.Add(ColumnName, dr[ColumnName]);
- }
- }
- diclist.Add(row);
- }
- }
- else if(kind == "lrange_str")
- {
- List<string> list = new CustomRedisDbconn(ConnectStr).GetList<string>(key, page, limit);
- foreach(string val in list)
- {
- Dictionary<string, string> row = new Dictionary<string, string>();
- Match m = Regex.Match(val, @"\#\{.*?\}\#");
- if(m.Success)
- {
- string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- Dictionary<string, string> fieldVals = dicValsJson[fieldName];
- foreach(string field in fieldVals.Keys)
- {
- row.Add(field, fieldVals[field]);
- }
- }
- else
- {
- row.Add("值", val);
- }
- diclist.Add(row);
- }
- }
- else if (kind == "get")
- {
- Dictionary<string, string> row = new Dictionary<string, string>();
- row.Add("值", new CustomRedisDbconn(ConnectStr).Get<string>(key));
- diclist.Add(row);
- }
- }
- Dictionary<string, object> obj = new Dictionary<string, object>();
- obj.Add("code", 0);
- obj.Add("msg", "");
- obj.Add("count", 300);
- obj.Add("data", diclist);
- return Json(obj);
- }
- #endregion
- #region 导出Excel
- /// <summary>
- /// 导出Excel
- /// </summary>
- /// <returns></returns>
- public string ExportExcel(int Id)
- {
- var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.AdminName == SysUserName && m.Status > -1);
- Dictionary<string, string> dicVals = new Dictionary<string, string>();
- List<Dictionary<string, string>> subJson = new List<Dictionary<string, string>>();
- List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
- foreach(CustomQuerySub sub in subItems)
- {
- Dictionary<string, string> subJsonItem = new Dictionary<string, string>();
- string ConnectStr = sub.DatabaseConnect;
- if(!string.IsNullOrEmpty(ConnectStr))
- {
- ConnectStr = GetConnectStr(sub.DatabaseConnect);
- }
- else
- {
- ConnectStr = MysqlConn.ReadSqlConnStr;
- }
- string sqlstr = sub.SqlContent;
- MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
- foreach(Match m in mcsub)
- {
- string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- string fieldName = fieldData[0];
- sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
- }
- mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
- foreach(Match m in mcsub)
- {
- string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- if(dicVals.ContainsKey(fieldName))
- {
- sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
- }
- }
- DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
- if(dtsub.Rows.Count > 0)
- {
- if(dtsub.Rows.Count > 1)
- {
- string val = "";
- foreach(DataRow dr in dtsub.Rows)
- {
- val += dr[0].ToString() + ",";
- }
- val = val.TrimEnd(',');
- dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
- }
- else
- {
- DataRow dr = dtsub.Rows[0];
- foreach(DataColumn dc in dtsub.Columns)
- {
- dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
- }
- }
- }
- subJsonItem.Add("DatabaseConnect", sub.DatabaseConnect);
- subJsonItem.Add("SqlContent", sqlstr);
- subJsonItem.Add("DataKey", sub.DataKey);
- subJsonItem.Add("DataText", sub.DataText);
- subJsonItem.Add("Alias", sub.Alias);
- subJson.Add(subJsonItem);
- }
- CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
- string sql = item.SqlContent;
- MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
- foreach(Match m in mc)
- {
- string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- string fieldName = fieldData[0];
- sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
- }
- mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
- foreach(Match m in mc)
- {
- string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
- string str = fieldData[0];
- string oldStr = fieldData[1];
- string newStr = fieldData[2];
- str = str.Replace(oldStr, newStr);
- sql = sql.Replace(m.Value, str);
- }
- var FileName = item.Title + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- string SubSqlString = "";
- if(subJson.Count > 0)
- {
- SubSqlString = ",\"SubSqlString\":" + Newtonsoft.Json.JsonConvert.SerializeObject(subJson);
- }
- string SendData = "{\"Operater\":\"" + sysAdmin.Id + "\",\"SqlString\":\"" + sql + "\",\"FileName\":\"" + FileName + "\",\"MaxCount\":\"0\"" + SubSqlString + "}";
- RedisDbconn.Instance.AddList("ExportQueue", SendData);
- return "success";
- }
- #endregion
- #region 执行
- /// <summary>
- /// 执行
- /// </summary>
- /// <returns></returns>
- // public string Excute(int Id)
- // {
- // var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.AdminName == SysUserName && m.Status > -1);
- // string ConnectStr = "";
- // Dictionary<string, string> dicVals = new Dictionary<string, string>();
- // List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
- // foreach(CustomQuerySub sub in subItems)
- // {
- // ConnectStr = sub.DatabaseConnect;
- // if(!string.IsNullOrEmpty(ConnectStr))
- // {
- // ConnectStr = GetConnectStr(sub.DatabaseConnect);
- // }
- // else
- // {
- // ConnectStr = MysqlConn.ReadSqlConnStr;
- // }
- // string sqlstr = sub.SqlContent;
- // MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
- // foreach(Match m in mcsub)
- // {
- // string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- // string fieldName = fieldData[0];
- // sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
- // }
- // mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
- // foreach(Match m in mcsub)
- // {
- // string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
- // if(dicVals.ContainsKey(fieldName))
- // {
- // sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
- // }
- // }
- // DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
- // if(dtsub.Rows.Count > 0)
- // {
- // if(dtsub.Rows.Count > 1)
- // {
- // string val = "";
- // foreach(DataRow dr in dtsub.Rows)
- // {
- // val += dr[0].ToString() + ",";
- // }
- // val = val.TrimEnd(',');
- // dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
- // }
- // else
- // {
- // DataRow dr = dtsub.Rows[0];
- // foreach(DataColumn dc in dtsub.Columns)
- // {
- // dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
- // }
- // }
- // }
- // }
- // CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
- // ConnectStr = item.DatabaseConnect;
- // if(!string.IsNullOrEmpty(ConnectStr))
- // {
- // ConnectStr = GetConnectStr(item.DatabaseConnect);
- // }
- // else
- // {
- // ConnectStr = MysqlConn.ReadSqlConnStr;
- // }
- // string sql = item.SqlContent;
- // MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
- // foreach(Match m in mc)
- // {
- // string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
- // string fieldName = fieldData[0];
- // sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
- // }
- // mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
- // foreach(Match m in mc)
- // {
- // string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
- // string str = fieldData[0];
- // string oldStr = fieldData[1];
- // string newStr = fieldData[2];
- // str = str.Replace(oldStr, newStr);
- // sql = sql.Replace(m.Value, str);
- // }
- // var FileName = item.Title + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- // string SendData = "{\"Operater\":\"" + sysAdmin.Id + "\",\"SqlString\":\"" + sql + "\",\"FileName\":\"执行\",\"MaxCount\":\"" + item.ExcuteFlag + "\",\"ConnectStr\":\"" + ConnectStr + "\"}";
- // RedisDbconn.Instance.AddList("ExportQueue", SendData);
- // return "success";
- // }
- #endregion
- #region 获取链接数据库字符串
- public string GetConnectStr(string key)
- {
- return Library.ConfigurationManager.AppSettings[key].ToString();
- }
- #endregion
- }
- }
|