CustomQueryDoController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. Dictionary<string, string> vals = new Dictionary<string, string>();
  141. foreach(DataRow dr in dtsub.Rows)
  142. {
  143. foreach(string key in TextFields)
  144. {
  145. vals.Add(key, dr[key].ToString());
  146. }
  147. }
  148. dicValsJson.Add(sub.Alias + "." + sub.DataKey, vals);
  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(dicVals.ContainsKey(fieldName))
  201. {
  202. sql = sql.Replace(m.Value, dicVals[fieldName]);
  203. }
  204. else
  205. {
  206. sql = sql.Replace(m.Value, "");
  207. }
  208. }
  209. mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
  210. foreach(Match m in mc)
  211. {
  212. string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
  213. string str = fieldData[0];
  214. string oldStr = fieldData[1];
  215. string newStr = fieldData[2];
  216. str = str.Replace(oldStr, newStr);
  217. sql = sql.Replace(m.Value, str);
  218. }
  219. List<Dictionary<string, string>> diclist = new List<Dictionary<string, string>>();
  220. if(item.ExcuteFlag == 0)
  221. {
  222. if(page > 1)
  223. {
  224. int skip = (page - 1) * limit;
  225. sql += " limit " + skip + "," + limit;
  226. }
  227. else
  228. {
  229. sql += " limit " + limit;
  230. }
  231. DataTable dt = CustomerSqlConn.dtable(sql, ConnectStr);
  232. foreach(DataRow dr in dt.Rows)
  233. {
  234. Dictionary<string, string> row = new Dictionary<string, string>();
  235. foreach(DataColumn dc in dt.Columns)
  236. {
  237. Match m = Regex.Match(dr[dc.ColumnName].ToString(), @"\#\{.*?\}\#");
  238. if(m.Success)
  239. {
  240. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  241. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  242. foreach(string field in fieldVals.Keys)
  243. {
  244. row.Add(dc.ColumnName + field, fieldVals[field]);
  245. }
  246. }
  247. else
  248. {
  249. if (dc.DataType == typeof(DateTime))
  250. {
  251. row.Add(dc.ColumnName, string.IsNullOrEmpty(dr[dc.ColumnName].ToString()) ? "" : DateTime.Parse(dr[dc.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
  252. }
  253. else
  254. {
  255. row.Add(dc.ColumnName, dr[dc.ColumnName].ToString());
  256. }
  257. }
  258. }
  259. diclist.Add(row);
  260. }
  261. }
  262. else if(item.ExcuteFlag == 2)
  263. {
  264. string redisCommand = sql;
  265. string[] redisCommandList = redisCommand.Split(' ');
  266. string kind = redisCommandList[0];
  267. string key = redisCommandList[1];
  268. if(kind == "lrange_json")
  269. {
  270. List<Dictionary<string, string>> list = new CustomRedisDbconn(ConnectStr).GetList<Dictionary<string, string>>(key, page, limit);
  271. foreach(Dictionary<string, string> dr in list)
  272. {
  273. Dictionary<string, string> row = new Dictionary<string, string>();
  274. foreach(string ColumnName in dr.Keys)
  275. {
  276. Match m = Regex.Match(dr[ColumnName], @"\#\{.*?\}\#");
  277. if(m.Success)
  278. {
  279. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  280. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  281. foreach(string field in fieldVals.Keys)
  282. {
  283. row.Add(ColumnName + field, fieldVals[field]);
  284. }
  285. }
  286. else
  287. {
  288. row.Add(ColumnName, dr[ColumnName]);
  289. }
  290. }
  291. diclist.Add(row);
  292. }
  293. }
  294. else if(kind == "lrange_str")
  295. {
  296. List<string> list = new CustomRedisDbconn(ConnectStr).GetList<string>(key, page, limit);
  297. foreach(string val in list)
  298. {
  299. Dictionary<string, string> row = new Dictionary<string, string>();
  300. Match m = Regex.Match(val, @"\#\{.*?\}\#");
  301. if(m.Success)
  302. {
  303. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  304. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  305. foreach(string field in fieldVals.Keys)
  306. {
  307. row.Add(field, fieldVals[field]);
  308. }
  309. }
  310. else
  311. {
  312. row.Add("值", val);
  313. }
  314. diclist.Add(row);
  315. }
  316. }
  317. else if (kind == "get")
  318. {
  319. Dictionary<string, string> row = new Dictionary<string, string>();
  320. row.Add("值", new CustomRedisDbconn(ConnectStr).Get<string>(key));
  321. diclist.Add(row);
  322. }
  323. }
  324. Dictionary<string, object> obj = new Dictionary<string, object>();
  325. obj.Add("code", 0);
  326. obj.Add("msg", "");
  327. obj.Add("count", 300);
  328. obj.Add("data", diclist);
  329. return Json(obj);
  330. }
  331. #endregion
  332. #region 导出Excel
  333. /// <summary>
  334. /// 导出Excel
  335. /// </summary>
  336. /// <returns></returns>
  337. public string ExportExcel(int Id)
  338. {
  339. var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.AdminName == SysUserName && m.Status > -1);
  340. Dictionary<string, string> dicVals = new Dictionary<string, string>();
  341. List<Dictionary<string, string>> subJson = new List<Dictionary<string, string>>();
  342. List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
  343. foreach(CustomQuerySub sub in subItems)
  344. {
  345. Dictionary<string, string> subJsonItem = new Dictionary<string, string>();
  346. string ConnectStr = sub.DatabaseConnect;
  347. if(!string.IsNullOrEmpty(ConnectStr))
  348. {
  349. ConnectStr = GetConnectStr(sub.DatabaseConnect);
  350. }
  351. else
  352. {
  353. ConnectStr = MysqlConn.ReadSqlConnStr;
  354. }
  355. string sqlstr = sub.SqlContent;
  356. MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
  357. foreach(Match m in mcsub)
  358. {
  359. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  360. string fieldName = fieldData[0];
  361. sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
  362. }
  363. mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
  364. foreach(Match m in mcsub)
  365. {
  366. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  367. if(dicVals.ContainsKey(fieldName))
  368. {
  369. sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
  370. }
  371. }
  372. DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
  373. if(dtsub.Rows.Count > 0)
  374. {
  375. if(dtsub.Rows.Count > 1)
  376. {
  377. string val = "";
  378. foreach(DataRow dr in dtsub.Rows)
  379. {
  380. val += dr[0].ToString() + ",";
  381. }
  382. val = val.TrimEnd(',');
  383. dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
  384. }
  385. else
  386. {
  387. DataRow dr = dtsub.Rows[0];
  388. foreach(DataColumn dc in dtsub.Columns)
  389. {
  390. dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
  391. }
  392. }
  393. }
  394. subJsonItem.Add("DatabaseConnect", sub.DatabaseConnect);
  395. subJsonItem.Add("SqlContent", sqlstr);
  396. subJsonItem.Add("DataKey", sub.DataKey);
  397. subJsonItem.Add("DataText", sub.DataText);
  398. subJsonItem.Add("Alias", sub.Alias);
  399. subJson.Add(subJsonItem);
  400. }
  401. CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
  402. string sql = item.SqlContent;
  403. MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  404. foreach(Match m in mc)
  405. {
  406. string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  407. string fieldName = fieldData[0];
  408. sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
  409. }
  410. mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
  411. foreach(Match m in mc)
  412. {
  413. string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
  414. string str = fieldData[0];
  415. string oldStr = fieldData[1];
  416. string newStr = fieldData[2];
  417. str = str.Replace(oldStr, newStr);
  418. sql = sql.Replace(m.Value, str);
  419. }
  420. var FileName = item.Title + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  421. string SubSqlString = "";
  422. if(subJson.Count > 0)
  423. {
  424. SubSqlString = ",\"SubSqlString\":" + Newtonsoft.Json.JsonConvert.SerializeObject(subJson);
  425. }
  426. string SendData = "{\"Operater\":\"" + sysAdmin.Id + "\",\"SqlString\":\"" + sql + "\",\"FileName\":\"" + FileName + "\",\"MaxCount\":\"0\"" + SubSqlString + "}";
  427. RedisDbconn.Instance.AddList("ExportQueue", SendData);
  428. return "success";
  429. }
  430. #endregion
  431. #region 执行
  432. /// <summary>
  433. /// 执行
  434. /// </summary>
  435. /// <returns></returns>
  436. // public string Excute(int Id)
  437. // {
  438. // var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.AdminName == SysUserName && m.Status > -1);
  439. // string ConnectStr = "";
  440. // Dictionary<string, string> dicVals = new Dictionary<string, string>();
  441. // List<CustomQuerySub> subItems = db.CustomQuerySub.Where(m => m.ParentId == Id).ToList();
  442. // foreach(CustomQuerySub sub in subItems)
  443. // {
  444. // ConnectStr = sub.DatabaseConnect;
  445. // if(!string.IsNullOrEmpty(ConnectStr))
  446. // {
  447. // ConnectStr = GetConnectStr(sub.DatabaseConnect);
  448. // }
  449. // else
  450. // {
  451. // ConnectStr = MysqlConn.ReadSqlConnStr;
  452. // }
  453. // string sqlstr = sub.SqlContent;
  454. // MatchCollection mcsub = Regex.Matches(sqlstr, @"\$\{.*?\}\$");
  455. // foreach(Match m in mcsub)
  456. // {
  457. // string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  458. // string fieldName = fieldData[0];
  459. // sqlstr = sqlstr.Replace(m.Value, Request.Query[fieldName].ToString());
  460. // }
  461. // mcsub = Regex.Matches(sqlstr, @"\#\{.*?\}\#");
  462. // foreach(Match m in mcsub)
  463. // {
  464. // string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  465. // if(dicVals.ContainsKey(fieldName))
  466. // {
  467. // sqlstr = sqlstr.Replace(m.Value, dicVals[fieldName]);
  468. // }
  469. // }
  470. // DataTable dtsub = CustomerSqlConn.dtable(sqlstr, ConnectStr);
  471. // if(dtsub.Rows.Count > 0)
  472. // {
  473. // if(dtsub.Rows.Count > 1)
  474. // {
  475. // string val = "";
  476. // foreach(DataRow dr in dtsub.Rows)
  477. // {
  478. // val += dr[0].ToString() + ",";
  479. // }
  480. // val = val.TrimEnd(',');
  481. // dicVals.Add(sub.Alias + "." + dtsub.Columns[0].ColumnName, val);
  482. // }
  483. // else
  484. // {
  485. // DataRow dr = dtsub.Rows[0];
  486. // foreach(DataColumn dc in dtsub.Columns)
  487. // {
  488. // dicVals.Add(sub.Alias + "." + dc.ColumnName, dr[dc.ColumnName].ToString());
  489. // }
  490. // }
  491. // }
  492. // }
  493. // CustomQuery item = db.CustomQuery.FirstOrDefault(m => m.Id == Id) ?? new CustomQuery();
  494. // ConnectStr = item.DatabaseConnect;
  495. // if(!string.IsNullOrEmpty(ConnectStr))
  496. // {
  497. // ConnectStr = GetConnectStr(item.DatabaseConnect);
  498. // }
  499. // else
  500. // {
  501. // ConnectStr = MysqlConn.ReadSqlConnStr;
  502. // }
  503. // string sql = item.SqlContent;
  504. // MatchCollection mc = Regex.Matches(sql, @"\$\{.*?\}\$");
  505. // foreach(Match m in mc)
  506. // {
  507. // string[] fieldData = m.Value.Replace("${", "").Replace("}$", "").Split('|');
  508. // string fieldName = fieldData[0];
  509. // sql = sql.Replace(m.Value, Request.Query[fieldName].ToString());
  510. // }
  511. // mc = Regex.Matches(sql, @"\$\replace{.*?\}\$");
  512. // foreach(Match m in mc)
  513. // {
  514. // string[] fieldData = m.Value.Replace("$replace{", "").Replace("}$", "").Split('|');
  515. // string str = fieldData[0];
  516. // string oldStr = fieldData[1];
  517. // string newStr = fieldData[2];
  518. // str = str.Replace(oldStr, newStr);
  519. // sql = sql.Replace(m.Value, str);
  520. // }
  521. // var FileName = item.Title + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  522. // string SendData = "{\"Operater\":\"" + sysAdmin.Id + "\",\"SqlString\":\"" + sql + "\",\"FileName\":\"执行\",\"MaxCount\":\"" + item.ExcuteFlag + "\",\"ConnectStr\":\"" + ConnectStr + "\"}";
  523. // RedisDbconn.Instance.AddList("ExportQueue", SendData);
  524. // return "success";
  525. // }
  526. #endregion
  527. #region 获取链接数据库字符串
  528. public string GetConnectStr(string key)
  529. {
  530. return Library.ConfigurationManager.AppSettings[key].ToString();
  531. }
  532. #endregion
  533. }
  534. }