CustomQueryDoController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * 自定义查询
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Data;
  10. using System.Threading.Tasks;
  11. using System.Text.RegularExpressions;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Options;
  16. using MySystem.Models;
  17. using Library;
  18. using LitJson;
  19. using MySystemLib;
  20. namespace MySystem.Areas.Admin.Controllers
  21. {
  22. [Area("Admin")]
  23. [Route("Admin/[controller]/[action]")]
  24. public class CustomQueryDoController : BaseController
  25. {
  26. public CustomQueryDoController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  27. {
  28. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  29. }
  30. #region 自定义查询列表
  31. /// <summary>
  32. /// 根据条件查询自定义查询列表
  33. /// </summary>
  34. /// <returns></returns>
  35. public IActionResult Index(int Id, string right)
  36. {
  37. ViewBag.RightInfo = RightInfo;
  38. ViewBag.right = right;
  39. ViewBag.Id = Id.ToString();
  40. Dictionary<string, string> query = new Dictionary<string, string>();
  41. CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
  42. if(SysUserName != "admin")
  43. {
  44. string checkString = "," + item.AdminNames + ",";
  45. if(!checkString.Contains("," + SysUserName + ","))
  46. {
  47. return Content("无权限");
  48. }
  49. }
  50. string sql = item.SqlContent;
  51. MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  52. foreach(Match m in mc)
  53. {
  54. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  55. string fieldName = fieldData[0];
  56. string Title = fieldData[1];
  57. if(!query.ContainsKey(fieldName))
  58. {
  59. query.Add(fieldName, Title);
  60. }
  61. }
  62. List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
  63. foreach(CustomQuerySub sub in subItems)
  64. {
  65. sql = sub.SqlContent;
  66. mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  67. foreach(Match m in mc)
  68. {
  69. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  70. string fieldName = fieldData[0];
  71. string Title = fieldData[1];
  72. if(!query.ContainsKey(fieldName))
  73. {
  74. query.Add(fieldName, Title);
  75. }
  76. }
  77. }
  78. ViewBag.query = query;
  79. string[] fieldList = item.SeoDescription.Split(',');
  80. string cols = "";
  81. foreach(string Title in fieldList)
  82. {
  83. cols += ",{field:'" + Title + "', width: 200, title:'" + Title + "'}";
  84. }
  85. ViewBag.cols = cols;
  86. return View();
  87. }
  88. #endregion
  89. #region 根据条件查询自定义查询列表
  90. /// <summary>
  91. /// 自定义查询列表
  92. /// </summary>
  93. /// <returns></returns>
  94. public JsonResult IndexData(int Id, int page = 1, int limit = 30)
  95. {
  96. Dictionary<string, string> dicVals = new Dictionary<string, string>();
  97. Dictionary<string, Dictionary<string, string>> dicValsJson = new Dictionary<string, Dictionary<string, string>>();
  98. string ConnectStr = "";
  99. List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
  100. foreach(CustomQuerySub sub in subItems)
  101. {
  102. ConnectStr = sub.DatabaseConnect;
  103. if(!string.IsNullOrEmpty(ConnectStr))
  104. {
  105. ConnectStr = GetConnectStr(sub.DatabaseConnect);
  106. }
  107. else
  108. {
  109. ConnectStr = MysqlConn.ReadSqlConnStr;
  110. }
  111. string sqlstr = sub.SqlContent;
  112. MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
  113. foreach(Match m in mcsub)
  114. {
  115. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  116. string fieldName = fieldData[0];
  117. sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
  118. }
  119. mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
  120. foreach(Match m in mcsub)
  121. {
  122. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  123. if(dicVals.ContainsKey(fieldName))
  124. {
  125. sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
  126. }
  127. else
  128. {
  129. sqlstr = sqlstr.Replace(m.Value, "");
  130. }
  131. }
  132. DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
  133. if(dtsub.Rows.Count > 0)
  134. {
  135. if(dtsub.Rows.Count > 1)
  136. {
  137. if(!string.IsNullOrEmpty(sub.DataKey))
  138. {
  139. string[] TextFields = sub.DataText.Split(',');
  140. foreach(DataRow dr in dtsub.Rows)
  141. {
  142. Dictionary<string, string> vals = new Dictionary<string, string>();
  143. foreach (string key in TextFields)
  144. {
  145. vals.Add(key, dr[key].ToString());
  146. }
  147. dicValsJson.Add(sub.Alias + "." + dr[sub.DataKey].ToString(), vals);
  148. }
  149. }
  150. else
  151. {
  152. string val = "";
  153. foreach(DataRow dr in dtsub.Rows)
  154. {
  155. val += dr[0].ToString() + ",";
  156. }
  157. val = val.TrimEnd(',');
  158. dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
  159. }
  160. }
  161. else
  162. {
  163. DataRow dr = dtsub.Rows[0];
  164. foreach(DataColumn dc in dtsub.Columns)
  165. {
  166. dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
  167. }
  168. }
  169. }
  170. }
  171. CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
  172. ConnectStr = item.DatabaseConnect;
  173. if(!string.IsNullOrEmpty(ConnectStr))
  174. {
  175. ConnectStr = GetConnectStr(item.DatabaseConnect);
  176. }
  177. else
  178. {
  179. if(item.ExcuteFlag == 2 || item.ExcuteFlag == 3)
  180. {
  181. ConnectStr = MysqlConn.RedisConnStr;
  182. }
  183. else
  184. {
  185. ConnectStr = MysqlConn.ReadSqlConnStr;
  186. }
  187. }
  188. string sql = item.SqlContent;
  189. MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  190. foreach(Match m in mc)
  191. {
  192. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  193. string fieldName = fieldData[0];
  194. sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
  195. }
  196. mc = Regex.Matches(sql, @"\#\{.*?\}\#");
  197. foreach(Match m in mc)
  198. {
  199. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  200. if(!fieldName.Contains("."))
  201. {
  202. if(dicVals.ContainsKey(fieldName))
  203. {
  204. sql = sql.Replace(m.Value, dicVals[fieldName]);
  205. }
  206. else
  207. {
  208. sql = sql.Replace(m.Value, "");
  209. }
  210. }
  211. }
  212. mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
  213. foreach(Match m in mc)
  214. {
  215. string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
  216. string str = fieldData[0];
  217. string oldStr = fieldData[1];
  218. string newStr = fieldData[2];
  219. str = str.Replace(oldStr, newStr);
  220. sql = sql.Replace(m.Value, str);
  221. }
  222. List<Dictionary<string, string>> diclist = new List<Dictionary<string, string>>();
  223. if(item.ExcuteFlag == 0)
  224. {
  225. if(page > 1)
  226. {
  227. int skip = (page - 1) * limit;
  228. sql += " limit " + skip + "," + limit;
  229. }
  230. else
  231. {
  232. sql += " limit " + limit;
  233. }
  234. DataTable dt = CustomerSqlConn.dtable(sql, ConnectStr);
  235. foreach(DataRow dr in dt.Rows)
  236. {
  237. Dictionary<string, string> row = new Dictionary<string, string>();
  238. foreach(DataColumn dc in dt.Columns)
  239. {
  240. Match m = Regex.Match(dr[dc.ColumnName].ToString(), @"\#\{.*?\}\#");
  241. if(m.Success)
  242. {
  243. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  244. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  245. foreach(string field in fieldVals.Keys)
  246. {
  247. row.Add(dc.ColumnName + field, fieldVals[field]);
  248. }
  249. }
  250. else
  251. {
  252. if (dc.DataType == typeof(DateTime))
  253. {
  254. row.Add(dc.ColumnName, string.IsNullOrEmpty(dr[dc.ColumnName].ToString()) ? "" : DateTime.Parse(dr[dc.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
  255. }
  256. else
  257. {
  258. row.Add(dc.ColumnName, dr[dc.ColumnName].ToString());
  259. }
  260. }
  261. }
  262. diclist.Add(row);
  263. }
  264. }
  265. else if(item.ExcuteFlag == 2)
  266. {
  267. string redisCommand = sql;
  268. string[] redisCommandList = redisCommand.Split(' ');
  269. string kind = redisCommandList[0];
  270. string key = redisCommandList[1];
  271. if(kind == "lrange_json")
  272. {
  273. List<Dictionary<string, string>> list = new CustomRedisDbconn(ConnectStr).GetList<Dictionary<string, string>>(key, page, limit);
  274. foreach(Dictionary<string, string> dr in list)
  275. {
  276. Dictionary<string, string> row = new Dictionary<string, string>();
  277. foreach(string ColumnName in dr.Keys)
  278. {
  279. Match m = Regex.Match(function.CheckNull(dr[ColumnName]), @"\#\{.*?\}\#");
  280. if(m.Success)
  281. {
  282. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  283. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  284. foreach(string field in fieldVals.Keys)
  285. {
  286. row.Add(ColumnName + field, fieldVals[field]);
  287. }
  288. }
  289. else
  290. {
  291. row.Add(ColumnName, dr[ColumnName]);
  292. }
  293. }
  294. diclist.Add(row);
  295. }
  296. }
  297. else if(kind == "lrange_str")
  298. {
  299. List<string> list = new CustomRedisDbconn(ConnectStr).GetList<string>(key, page, limit);
  300. foreach(string val in list)
  301. {
  302. Dictionary<string, string> row = new Dictionary<string, string>();
  303. Match m = Regex.Match(val, @"\#\{.*?\}\#");
  304. if(m.Success)
  305. {
  306. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  307. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  308. foreach(string field in fieldVals.Keys)
  309. {
  310. row.Add(field, fieldVals[field]);
  311. }
  312. }
  313. else
  314. {
  315. row.Add("值", val);
  316. }
  317. diclist.Add(row);
  318. }
  319. }
  320. else if (kind == "get")
  321. {
  322. Dictionary<string, string> row = new Dictionary<string, string>();
  323. row.Add("值", function.CheckNull(new CustomRedisDbconn(ConnectStr).Get<string>(key)));
  324. diclist.Add(row);
  325. }
  326. }
  327. Dictionary<string, object> obj = new Dictionary<string, object>();
  328. obj.Add("code", 0);
  329. obj.Add("msg", "");
  330. obj.Add("count", 300);
  331. obj.Add("data", diclist);
  332. return Json(obj);
  333. }
  334. #endregion
  335. #region 导出Excel
  336. /// <summary>
  337. /// 导出Excel
  338. /// </summary>
  339. /// <returns></returns>
  340. public string ExportExcel(int Id)
  341. {
  342. var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.AdminName == SysUserName && m.Status > -1);
  343. Dictionary<string, string> dicVals = new Dictionary<string, string>();
  344. List<Dictionary<string, string>> subJson = new List<Dictionary<string, string>>();
  345. List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
  346. foreach(CustomQuerySub sub in subItems)
  347. {
  348. Dictionary<string, string> subJsonItem = new Dictionary<string, string>();
  349. string ConnectStr = sub.DatabaseConnect;
  350. if(!string.IsNullOrEmpty(ConnectStr))
  351. {
  352. ConnectStr = GetConnectStr(sub.DatabaseConnect);
  353. }
  354. else
  355. {
  356. ConnectStr = MysqlConn.ReadSqlConnStr;
  357. }
  358. string sqlstr = sub.SqlContent;
  359. MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
  360. foreach(Match m in mcsub)
  361. {
  362. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  363. string fieldName = fieldData[0];
  364. sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
  365. }
  366. mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
  367. foreach(Match m in mcsub)
  368. {
  369. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  370. if(dicVals.ContainsKey(fieldName))
  371. {
  372. sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
  373. }
  374. }
  375. DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
  376. if(dtsub.Rows.Count > 0)
  377. {
  378. if(dtsub.Rows.Count > 1)
  379. {
  380. string val = "";
  381. foreach(DataRow dr in dtsub.Rows)
  382. {
  383. val += dr[0].ToString() + ",";
  384. }
  385. val = val.TrimEnd(',');
  386. dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
  387. }
  388. else
  389. {
  390. DataRow dr = dtsub.Rows[0];
  391. foreach(DataColumn dc in dtsub.Columns)
  392. {
  393. dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
  394. }
  395. }
  396. }
  397. subJsonItem.Add("DatabaseConnect", sub.DatabaseConnect);
  398. subJsonItem.Add("SqlContent", sqlstr);
  399. subJsonItem.Add("DataKey", sub.DataKey);
  400. subJsonItem.Add("DataText", sub.DataText);
  401. subJsonItem.Add("Alias", sub.Alias);
  402. subJson.Add(subJsonItem);
  403. }
  404. CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
  405. string sql = item.SqlContent;
  406. MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  407. foreach(Match m in mc)
  408. {
  409. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  410. string fieldName = fieldData[0];
  411. sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
  412. }
  413. mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
  414. foreach(Match m in mc)
  415. {
  416. string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
  417. string str = fieldData[0];
  418. string oldStr = fieldData[1];
  419. string newStr = fieldData[2];
  420. str = str.Replace(oldStr, newStr);
  421. sql = sql.Replace(m.Value, str);
  422. }
  423. var FileName = item.Title + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  424. string SubSqlString = "";
  425. if(subJson.Count > 0)
  426. {
  427. SubSqlString = ",\"SubSqlString\":" + Newtonsoft.Json.JsonConvert.SerializeObject(subJson);
  428. }
  429. string SendData = "{\"Operater\":\"" + sysAdmin.Id + "\",\"SqlString\":\"" + sql + "\",\"ConnectStr\":\"" + item.DatabaseConnect + "\",\"FileName\":\"" + FileName + "\",\"MaxCount\":\"0\"" + SubSqlString + "}";
  430. RedisDbconn.Instance.AddList("ExportQueue", SendData);
  431. return "success";
  432. }
  433. #endregion
  434. #region 执行
  435. /// <summary>
  436. /// 执行
  437. /// </summary>
  438. /// <returns></returns>
  439. // public string Excute(int Id)
  440. // {
  441. // var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.AdminName == SysUserName && m.Status > -1);
  442. // string ConnectStr = "";
  443. // Dictionary<string, string> dicVals = new Dictionary<string, string>();
  444. // List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
  445. // foreach(CustomQuerySub sub in subItems)
  446. // {
  447. // ConnectStr = sub.DatabaseConnect;
  448. // if(!string.IsNullOrEmpty(ConnectStr))
  449. // {
  450. // ConnectStr = GetConnectStr(sub.DatabaseConnect);
  451. // }
  452. // else
  453. // {
  454. // ConnectStr = MysqlConn.ReadSqlConnStr;
  455. // }
  456. // string sqlstr = sub.SqlContent;
  457. // MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
  458. // foreach(Match m in mcsub)
  459. // {
  460. // string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  461. // string fieldName = fieldData[0];
  462. // sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
  463. // }
  464. // mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
  465. // foreach(Match m in mcsub)
  466. // {
  467. // string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  468. // if(dicVals.ContainsKey(fieldName))
  469. // {
  470. // sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
  471. // }
  472. // }
  473. // DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
  474. // if(dtsub.Rows.Count > 0)
  475. // {
  476. // if(dtsub.Rows.Count > 1)
  477. // {
  478. // string val = "";
  479. // foreach(DataRow dr in dtsub.Rows)
  480. // {
  481. // val += dr[0].ToString() + ",";
  482. // }
  483. // val = val.TrimEnd(',');
  484. // dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
  485. // }
  486. // else
  487. // {
  488. // DataRow dr = dtsub.Rows[0];
  489. // foreach(DataColumn dc in dtsub.Columns)
  490. // {
  491. // dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
  492. // }
  493. // }
  494. // }
  495. // }
  496. // CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
  497. // ConnectStr = item.DatabaseConnect;
  498. // if(!string.IsNullOrEmpty(ConnectStr))
  499. // {
  500. // ConnectStr = GetConnectStr(item.DatabaseConnect);
  501. // }
  502. // else
  503. // {
  504. // ConnectStr = MysqlConn.ReadSqlConnStr;
  505. // }
  506. // string sql = item.SqlContent;
  507. // MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  508. // foreach(Match m in mc)
  509. // {
  510. // string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  511. // string fieldName = fieldData[0];
  512. // sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
  513. // }
  514. // mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
  515. // foreach(Match m in mc)
  516. // {
  517. // string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
  518. // string str = fieldData[0];
  519. // string oldStr = fieldData[1];
  520. // string newStr = fieldData[2];
  521. // str = str.Replace(oldStr, newStr);
  522. // sql = sql.Replace(m.Value, str);
  523. // }
  524. // var FileName = item.Title + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  525. // string SendData = "{\"Operater\":\"" + sysAdmin.Id + "\",\"SqlString\":\"" + sql + "\",\"FileName\":\"执行\",\"MaxCount\":\"" + item.ExcuteFlag + "\",\"ConnectStr\":\"" + ConnectStr + "\"}";
  526. // RedisDbconn.Instance.AddList("ExportQueue", SendData);
  527. // return "success";
  528. // }
  529. #endregion
  530. #region 获取链接数据库字符串
  531. public string GetConnectStr(string key)
  532. {
  533. return Library.ConfigurationManager.AppSettings[key].ToString();
  534. }
  535. #endregion
  536. }
  537. }