CommonDynamicSelectExcutor.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. using DirectService.Tools;
  2. using JLHHJSvr.BLL;
  3. using JLHHJSvr.Com;
  4. using JLHHJSvr.LJException;
  5. using LJLib.DAL.SQL;
  6. using LJLib.Net.SPI.Server;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Data.SqlClient;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Xml;
  19. namespace JLHHJSvr.Excutor
  20. {
  21. internal class CommonDynamicSelectExcutor : ExcutorBase<CommonDynamicSelectRequest, CommonDynamicSelectResponse>
  22. {
  23. protected override void ExcuteInternal(CommonDynamicSelectRequest request, object state, CommonDynamicSelectResponse rslt)
  24. {
  25. var tokendata = BllHelper.GetToken(request.token);
  26. if (tokendata == null)
  27. {
  28. rslt.ErrMsg = "会话已经中断";
  29. return;
  30. }
  31. if (request.queryparams == null)
  32. {
  33. throw new ArgumentNullException("queryparams");
  34. }
  35. if (string.IsNullOrEmpty(request.dsname))
  36. {
  37. throw new ArgumentNullException("dsname");
  38. }
  39. using (var con = new SqlConnection(GlobalVar.ConnectionString))
  40. using (var cmd = con.CreateCommand())
  41. {
  42. con.Open();
  43. var rsltgroup = GetXmlResult(cmd, request.dsname, request, tokendata, false);
  44. rslt.datatable = rsltgroup.datatable;
  45. rslt.tableinfo = rsltgroup.tableinfo;
  46. rslt.totalcnt = rsltgroup.totalcnt;
  47. rslt.pageindex = rsltgroup.pageindex;
  48. rslt.pagesize = rsltgroup.pagesize;
  49. }
  50. }
  51. private const string _mapperAppend = "_Mapper_";
  52. private Regex _staticParmReg = new Regex("[\\$]+[\\w]+[\\$]*");
  53. private Regex _listParmReg = new Regex("@@[\\w]+@@");
  54. private Dictionary<string, string> _xmlEscapeMap = new Dictionary<string, string>
  55. {
  56. {"&", "&amp;"},
  57. {"'", "&apos;"},
  58. {"\"", "&quot;"},
  59. {">", "&gt;"},
  60. {"<", "&lt;"},
  61. };
  62. private Regex _computeRefReg = new Regex("([A-Za-z_][\\w]+)([\\s]*)([(]*)");
  63. private QueryResult GetXmlResult(SqlCommand cmd, string dsname, CommonDynamicSelectRequest request, TokenData tokendata, bool ifmapper)
  64. {
  65. var rslt = new QueryResult();
  66. var rootPath = GlobalVar.App_Data + "\\DataStore\\";
  67. #if DEBUG
  68. rootPath = rootPath.Substring(0, rootPath.IndexOf("\\bin\\")) + "\\DataStore\\";
  69. #endif
  70. var filePath = rootPath + dsname + ".xml";
  71. if (!File.Exists(filePath))
  72. {
  73. throw new LJCommonException("缺失接口文件:" + dsname + ".xml");
  74. }
  75. var queryParams = request.queryparams;
  76. var wholexml = File.ReadAllText(filePath);
  77. var treatedsb = new StringBuilder();
  78. // 逐行扫描,检查全局变量与转义符
  79. var staticParmDic = new Dictionary<string, string>();
  80. using (StringReader reader = new StringReader(wholexml))
  81. {
  82. var incomment = false;
  83. string line;
  84. while ((line = reader.ReadLine()) != null)
  85. {
  86. var lineTrim = line.Trim();
  87. if (line.Contains("<!--"))
  88. {
  89. incomment = true;
  90. var commentStartIndex = line.IndexOf("<!--");
  91. if (commentStartIndex > 0)
  92. {
  93. treatedsb.AppendLine(line.Substring(0, commentStartIndex));
  94. }
  95. }
  96. if (incomment)
  97. {
  98. if (line.Contains("-->"))
  99. {
  100. incomment = false;
  101. var commenttail = line.Substring(line.IndexOf("-->") + 3);
  102. if (!string.IsNullOrEmpty(commenttail))
  103. {
  104. treatedsb.AppendLine(commenttail);
  105. }
  106. }
  107. continue;
  108. }
  109. // 处理$标记的全局变量
  110. var staticMatches = _staticParmReg.Matches(line);
  111. var staticMatchesStr = new HashSet<string>();
  112. foreach (var match in staticMatches)
  113. {
  114. var matchstr = match.ToString();
  115. staticMatchesStr.Add(matchstr);
  116. if (matchstr.StartsWith("$$"))
  117. {
  118. }
  119. else if (matchstr.StartsWith("$"))
  120. {
  121. var newname = "@" + matchstr.Substring(1);
  122. }
  123. }
  124. //$$开头$$结束为全局数组变量,直接作字符串替换
  125. foreach (var item in staticMatchesStr.Where(x => x.StartsWith("$$")))
  126. {
  127. if (!staticParmDic.ContainsKey(item))
  128. {
  129. var staticParm = item.Trim('$');
  130. var staticVal = string.Empty;
  131. staticVal = staticVal ?? string.Empty;
  132. staticParmDic[item] = staticVal.Trim(new[] { '(', ')' });
  133. }
  134. if (lineTrim.StartsWith("<") && lineTrim.EndsWith(">"))
  135. {
  136. //xml标签
  137. }
  138. else
  139. {
  140. var staticVal = staticParmDic[item];
  141. line = line.Replace(item, staticVal);
  142. }
  143. }
  144. // $开头的全局变量转换为@开头的变量
  145. foreach (var item in staticMatchesStr.Where(x => x.StartsWith("$") && !x.StartsWith("$$")))
  146. {
  147. var staticParm = item.Trim('$');
  148. if (!queryParams.ContainsKey(staticParm))
  149. {
  150. if (staticParm.StartsWith("user_"))
  151. {
  152. // 查询用户权限值
  153. var keyStr = staticParm.Substring("user_".Length);
  154. var userKeyStr = "0";
  155. cmd.CommandText = @"select " + keyStr + " from u_user_jlhprice where empid = @empid";
  156. cmd.Parameters.Clear();
  157. cmd.Parameters.AddWithValue("@empid", tokendata.userid);
  158. using (var UserReader = cmd.ExecuteReader())
  159. {
  160. if (UserReader.Read())
  161. {
  162. userKeyStr = Convert.ToString(UserReader[keyStr]).Trim();
  163. }
  164. }
  165. queryParams[staticParm] = userKeyStr;
  166. }
  167. else
  168. {
  169. throw new NotImplementedException(staticParm);
  170. }
  171. }
  172. line = line.Replace(item, "@" + item.Substring(1));
  173. }
  174. if (lineTrim.StartsWith("<") && lineTrim.EndsWith(">"))
  175. {
  176. //xml标签
  177. line = line.Replace(" $", " ");
  178. line = line.Replace(" @", " ");
  179. line = line.Replace("!=\"", "_notequals=\"");
  180. }
  181. else
  182. {
  183. // 处理@@标记的数组变量
  184. var listParmMatches = _listParmReg.Matches(line);
  185. foreach (var match in listParmMatches)
  186. {
  187. var matchstr = match.ToString();
  188. var pname = matchstr.Trim('@');
  189. if (!queryParams.ContainsKey(pname))
  190. {
  191. continue;
  192. //throw new LJCommonException($"未提供参数" + pname);
  193. }
  194. if (queryParams.GetValue(pname).Type != JTokenType.Array)
  195. {
  196. continue;
  197. }
  198. var listval = queryParams.Value<JArray>(pname).Select(x => "'" + x + "'");
  199. var pval = string.Join(",", listval);
  200. line = line.Replace(matchstr, pval);
  201. }
  202. // 处理转义字符
  203. foreach (var escapeItem in _xmlEscapeMap)
  204. {
  205. line = line.Replace(escapeItem.Key, escapeItem.Value);
  206. }
  207. }
  208. treatedsb.AppendLine(line);
  209. }
  210. }
  211. var treatedxml = treatedsb.ToString();
  212. var xmlDoc = new XmlDocument();
  213. xmlDoc.LoadXml(treatedxml);
  214. XmlNode selectNode = null;
  215. XmlNode rootNode = null;
  216. foreach (XmlNode rchild in xmlDoc.ChildNodes)
  217. {
  218. if (rchild.Name != "xml")
  219. {
  220. rootNode = rchild;
  221. break;
  222. }
  223. }
  224. if (rootNode == null)
  225. {
  226. throw new LJCommonException("格式错误,不存在根节点");
  227. }
  228. if (rootNode.Name == "data")
  229. {
  230. var dataChild = rootNode.FirstChild;
  231. if (dataChild == null)
  232. {
  233. throw new LJCommonException("格式错误,data不存在根节点");
  234. }
  235. if (dataChild.Name == "json")
  236. {
  237. var dataJson = dataChild.InnerText;
  238. rslt.datatable = JsonConvert.DeserializeObject<JArray>(dataJson);
  239. return rslt;
  240. }
  241. else
  242. {
  243. throw new LJCommonException("未支持的data节点:" + dataChild.Name);
  244. }
  245. }
  246. else if (rootNode.Name == "select")
  247. {
  248. selectNode = rootNode;
  249. }
  250. else
  251. {
  252. throw new LJCommonException("未支持的根节点:" + rootNode.Name);
  253. }
  254. var selectbase = string.Empty;
  255. var selectstr = selectNode.SelectSingleNode("selectstr")?.InnerText;
  256. var orderstr = selectNode.SelectSingleNode("orderstr")?.InnerText;
  257. var whereList = new List<string>();
  258. var whereNode = selectNode.SelectSingleNode("where");
  259. if (whereNode != null)
  260. {
  261. foreach (XmlNode whereChild in whereNode.ChildNodes)
  262. {
  263. if (whereChild.Name == "when")
  264. {
  265. var match = true;
  266. foreach (XmlAttribute attr in whereChild.Attributes)
  267. {
  268. var attrname = attr.Name;
  269. var attrval = attr.Value;
  270. JToken valjtoken = null;
  271. if (attrval.StartsWith("@"))
  272. {
  273. var pname = attrval.Replace("@", "");
  274. attrval = null;
  275. if (queryParams.ContainsKey(pname))
  276. {
  277. valjtoken = queryParams[pname];
  278. switch (valjtoken.Type)
  279. {
  280. case JTokenType.Null:
  281. case JTokenType.None:
  282. case JTokenType.Undefined:
  283. attrval = null;
  284. break;
  285. case JTokenType.Object:
  286. case JTokenType.Array:
  287. attrval = valjtoken.ToString();
  288. break;
  289. default:
  290. attrval = valjtoken.ToString();
  291. break;
  292. }
  293. }
  294. }
  295. if (attrname == "notnull")
  296. {
  297. if (attrval == null)
  298. {
  299. match = false;
  300. break;
  301. }
  302. }
  303. else if (attrname == "notempty")
  304. {
  305. if (string.IsNullOrEmpty(attrval))
  306. {
  307. match = false;
  308. break;
  309. }
  310. if (valjtoken.Type == JTokenType.Array)
  311. {
  312. if ((valjtoken as JArray).Count == 0)
  313. {
  314. match = false;
  315. break;
  316. }
  317. }
  318. }
  319. else
  320. {
  321. var ifnot = false;
  322. var attrnameVal = attrname.TrimStart('@');
  323. if (attrnameVal.EndsWith("_notequals"))
  324. {
  325. ifnot = true;
  326. attrnameVal = attrnameVal.Substring(0, attrnameVal.IndexOf("_notequals"));
  327. }
  328. if (!queryParams.ContainsKey(attrnameVal))
  329. {
  330. throw new NotImplementedException(attr.ToString());
  331. }
  332. attrnameVal = queryParams[attrnameVal].ToString();
  333. if (string.Equals(attrnameVal, attrval) != !ifnot)
  334. {
  335. match = false;
  336. break;
  337. }
  338. }
  339. }
  340. if (match)
  341. {
  342. whereList.Add(whereChild.InnerText.Trim());
  343. }
  344. }
  345. else
  346. {
  347. throw new NotImplementedException();
  348. }
  349. }
  350. }
  351. var wherestr = ListEx.GetWhereStr(whereList);
  352. var parmDic = new Dictionary<string, object>();
  353. foreach (var item in queryParams)
  354. {
  355. if (item.Value is JArray)
  356. {
  357. }
  358. else
  359. {
  360. parmDic[item.Key] = item.Value.ToString();
  361. }
  362. }
  363. var displayfields = selectNode.SelectSingleNode("displayfields");
  364. var uncomputefields = new Dictionary<string, string>();
  365. string rownumfield = null;
  366. foreach (XmlNode fieldNode in displayfields)
  367. {
  368. string field = null;
  369. XmlAttribute computeAttr = null;
  370. foreach (XmlAttribute attr in fieldNode.Attributes)
  371. {
  372. if (string.Equals(attr.Name, "field", StringComparison.OrdinalIgnoreCase))
  373. {
  374. field = attr.Value;
  375. }
  376. else if (string.Equals(attr.Name, "compute", StringComparison.OrdinalIgnoreCase))
  377. {
  378. computeAttr = attr;
  379. }
  380. }
  381. if (computeAttr != null)
  382. {
  383. var computeexpr = computeAttr.Value;
  384. if (!string.IsNullOrEmpty(computeexpr))
  385. {
  386. if (computeexpr.IndexOf("getrow(", StringComparison.OrdinalIgnoreCase) >= 0)
  387. {
  388. rownumfield = field;
  389. }
  390. else
  391. {
  392. uncomputefields[field] = computeexpr;
  393. }
  394. fieldNode.Attributes.Remove(computeAttr);
  395. }
  396. }
  397. }
  398. var parseResult = SqlStrHelper.ParseSelectStr(selectstr);
  399. if (parseResult.selectStr.ToUpper().Contains("DISTINCT"))
  400. {
  401. throw new NotImplementedException("未支持DISTINCT");
  402. }
  403. if (uncomputefields.Count > 0)
  404. {
  405. // 多次循环,防因嵌套解析失败
  406. var checkcnt = uncomputefields.Count;
  407. for (var i = 0; i < checkcnt; i++)
  408. {
  409. if (uncomputefields.Count == 0)
  410. {
  411. break;
  412. }
  413. var flist = uncomputefields.Keys.ToList();
  414. foreach (var compf in flist)
  415. {
  416. if (!uncomputefields.ContainsKey(compf))
  417. {
  418. continue;
  419. }
  420. var waitnext = false;
  421. var computeexpr = uncomputefields[compf];
  422. var refMatches = _computeRefReg.Matches(computeexpr);
  423. var refFields = new HashSet<string>();
  424. foreach (Match refmatch in refMatches)
  425. {
  426. if (string.IsNullOrEmpty(refmatch.Groups[3].ToString()))
  427. {
  428. var refField = refmatch.Groups[1].ToString();
  429. if (!parseResult.selectFieldsDic.ContainsKey(refField))
  430. {
  431. waitnext = true;
  432. break;
  433. }
  434. refFields.Add(refField);
  435. }
  436. }
  437. if (waitnext)
  438. {
  439. continue;
  440. }
  441. var refSorts = refFields.OrderByDescending(x => x.Length).ToList();
  442. var holderid = -1;
  443. foreach (var reff in refSorts)
  444. {
  445. holderid++;
  446. computeexpr = computeexpr.Replace(reff, "{" + holderid.ToString("000") + "}");
  447. }
  448. for (var hindex = 0; hindex <= holderid; hindex++)
  449. {
  450. var reff = refSorts[hindex];
  451. var refselect = parseResult.selectFieldsDic[reff];
  452. var asIndex = refselect.LastIndexOf(" as ", StringComparison.OrdinalIgnoreCase);
  453. if (asIndex > 0)
  454. {
  455. var asname = refselect.Substring(asIndex + " as ".Length).Trim();
  456. if (string.Equals(asname, reff, StringComparison.OrdinalIgnoreCase))
  457. {
  458. refselect = refselect.Substring(0, asIndex).Trim();
  459. }
  460. }
  461. computeexpr = computeexpr.Replace("{" + hindex.ToString("000") + "}", "(" + refselect + ")");
  462. }
  463. parseResult.selectFieldsDic[compf] = computeexpr + " AS " + compf;
  464. uncomputefields.Remove(compf);
  465. }
  466. }
  467. if (uncomputefields.Count > 0)
  468. {
  469. throw new LJCommonException($"解析计算列失败:[{string.Join(",", uncomputefields.Keys)}]");
  470. }
  471. }
  472. if (!string.IsNullOrEmpty(request.orderstr?.Trim()))
  473. {
  474. var orderArr = request.orderstr.Split(',');
  475. var orderlist = new List<string>();
  476. foreach (var orderitem in orderArr)
  477. {
  478. var orderfield = orderitem.Trim();
  479. var descIndex = orderfield.IndexOf(" desc");
  480. if (descIndex > 0)
  481. {
  482. orderfield = orderfield.Substring(0, descIndex).Trim();
  483. }
  484. var ascIndex = orderfield.IndexOf(" asc");
  485. if (ascIndex > 0)
  486. {
  487. orderfield = orderfield.Substring(0, ascIndex).Trim();
  488. }
  489. if (!parseResult.selectFieldsDic.ContainsKey(orderfield))
  490. {
  491. throw new LJCommonException($"排序失败,数据列中不包含[{orderfield}]");
  492. }
  493. var dbfield = parseResult.selectFieldsDic[orderfield];
  494. dbfield = RemoveAS(dbfield);
  495. if (descIndex > 0)
  496. {
  497. dbfield += " desc";
  498. }
  499. orderlist.Add(dbfield);
  500. }
  501. orderstr = string.Join(",", orderlist);
  502. }
  503. var mergesql = SqlStrHelper.BuildSelectStrL1(parseResult, string.Join(",", parseResult.selectFieldsDic.Keys), wherestr, orderstr, request.pageindex, request.pagesize);
  504. cmd.CommandText = mergesql;
  505. cmd.CommandType = CommandType.Text;
  506. cmd.Parameters.Clear();
  507. if (queryParams != null)
  508. {
  509. foreach (var item in queryParams)
  510. {
  511. if (item.Value == null)
  512. {
  513. cmd.Parameters.AddWithValue("@" + item.Key, DBNull.Value);
  514. }
  515. else
  516. {
  517. cmd.Parameters.AddWithValue("@" + item.Key, item.Value.ToString());
  518. }
  519. }
  520. }
  521. rslt.datatable = new JArray();
  522. var dbcolnames = parseResult.selectFieldsDic.Keys.ToList();
  523. var rownum = (request.pageindex - 1) * request.pagesize;
  524. if (rownum < 0)
  525. {
  526. rownum = 0;
  527. }
  528. using (var reader = cmd.ExecuteReader())
  529. {
  530. while (reader.Read())
  531. {
  532. rownum++;
  533. var row = new JObject();
  534. if (rownumfield != null)
  535. {
  536. row.Add(rownumfield, rownum);
  537. }
  538. for (var i = 0; i < dbcolnames.Count; i++)
  539. {
  540. var p = dbcolnames[i];
  541. var dbval = reader[p];
  542. if (ifmapper)
  543. {
  544. if (i == 0)
  545. {
  546. p = "value";
  547. }
  548. else if (i == 1)
  549. {
  550. p = "label";
  551. }
  552. }
  553. if (dbval == DBNull.Value)
  554. {
  555. row.Add(p, null);
  556. }
  557. else if (dbval is string)
  558. {
  559. row.Add(p, (dbval as string).Trim());
  560. }
  561. else if (dbval is DateTime)
  562. {
  563. row.Add(p, (DateTime)dbval);
  564. }
  565. else
  566. {
  567. row.Add(p, dbval.ToString());
  568. }
  569. }
  570. rslt.datatable.Add(row);
  571. }
  572. }
  573. // 处理总行数
  574. if (!ifmapper)
  575. {
  576. string countstr = parseResult.selectStr + " COUNT(0) " + parseResult.fromStr;
  577. if (!string.IsNullOrEmpty(wherestr))
  578. {
  579. countstr += " WHERE " + wherestr;
  580. }
  581. cmd.CommandText = countstr;
  582. try
  583. {
  584. var totalcnt = Convert.ToInt32(cmd.ExecuteScalar());
  585. rslt.totalcnt = totalcnt;
  586. rslt.pageindex = request.pageindex;
  587. rslt.pagesize = request.pagesize;
  588. }
  589. catch (Exception ex)
  590. {
  591. throw new Exception(string.Format("ex:{0}\r\ncmd:{1}", ex, cmd.CommandText));
  592. }
  593. }
  594. //处理列信息,集成用户习惯
  595. if (request.pageindex <= 1 && !ifmapper)
  596. {
  597. var preferenceobj = new JObject();
  598. var preferencejson = BllHelper.GetValue(cmd,tokendata.userid, request.dwname, request.itemname, string.Empty, request.ifcompress == 1 ? true : false);
  599. if (string.IsNullOrEmpty(preferencejson))//如果没有自己的布局方案,尝试获取系统的布局方案
  600. {
  601. preferencejson = BllHelper.GetValue(cmd,-1, request.dwname, request.itemname, string.Empty, request.ifcompress == 1 ? true : false);
  602. }
  603. //preferencejson = "{tableprop:{enabled:false,oSize:0,mode:\"default\"},columns:[{field:\"printid\",search:{order:1,labelposition:\"left\"}}]}";
  604. if (!string.IsNullOrEmpty(preferencejson))
  605. {
  606. try
  607. {
  608. preferenceobj = JsonConvert.DeserializeObject<JObject>(preferencejson);
  609. }
  610. catch (Exception e)
  611. {
  612. Trace.Write("解析json失败" + preferenceobj);
  613. }
  614. }
  615. var oldcolDic = new Dictionary<string, JObject>(StringComparer.OrdinalIgnoreCase);
  616. var colSortDic = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
  617. var sortid = 0;
  618. if (preferenceobj.ContainsKey("columns"))
  619. {
  620. var oldcols = preferenceobj.GetValue("columns") as JArray;
  621. foreach (JObject col in oldcols)
  622. {
  623. var field = col["field"].ToString();
  624. oldcolDic[field] = col;
  625. sortid++;
  626. colSortDic[field] = sortid;
  627. }
  628. }
  629. var cols = new List<JObject>();
  630. var colSortDicDefault = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
  631. sortid = 0;
  632. foreach (XmlNode fieldNode in displayfields.ChildNodes)
  633. {
  634. var col = new JObject();
  635. string field = null;
  636. string mapper = null;
  637. foreach (XmlAttribute attr in fieldNode.Attributes)
  638. {
  639. if (string.Equals(attr.Name, "mapper", StringComparison.OrdinalIgnoreCase))
  640. {
  641. mapper = attr.Value;
  642. continue;
  643. }
  644. col.Add(attr.Name, attr.Value);
  645. if (string.Equals(attr.Name, "field", StringComparison.OrdinalIgnoreCase))
  646. {
  647. field = attr.Value;
  648. }
  649. }
  650. string dbField = null;
  651. var title = fieldNode.InnerText.Trim();
  652. if (field == rownumfield)
  653. {
  654. dbField = string.Empty;
  655. }
  656. else if (parseResult.selectFieldsDic.ContainsKey(field))
  657. {
  658. dbField = parseResult.selectFieldsDic[field];
  659. }
  660. else
  661. {
  662. throw new LJCommonException("语法错误:" + field + ":" + title + ",输出项无法匹配对应sql数据");
  663. }
  664. col.Add("title", title);
  665. handleCustomTitle(cmd, tokendata.userid, col, dbField);
  666. if (!string.IsNullOrEmpty(field) && oldcolDic.ContainsKey(field))
  667. {
  668. var oldInfo = oldcolDic[field];
  669. foreach (var prop in oldInfo.Properties())
  670. {
  671. if (string.Equals(prop.Name, "field", StringComparison.OrdinalIgnoreCase))
  672. {
  673. continue;
  674. }
  675. if (string.Equals(prop.Name, "title", StringComparison.OrdinalIgnoreCase))
  676. {
  677. continue;
  678. }
  679. //col.Add(prop.Name, prop.Value);
  680. col[prop.Name] = prop.Value;
  681. }
  682. }
  683. if (mapper != null)
  684. {
  685. if (string.IsNullOrEmpty(mapper))
  686. {
  687. mapper = RemoveAS(dbField);
  688. }
  689. var mappername = mapper;
  690. if (mappername.Contains("."))
  691. {
  692. mappername = mappername.Substring(mappername.IndexOf(".") + 1);
  693. }
  694. if (!File.Exists(rootPath + _mapperAppend + mappername + ".xml"))
  695. {
  696. mappername = mapper.Replace(".", "_");
  697. if (!File.Exists(rootPath + _mapperAppend + mappername + ".xml"))
  698. {
  699. throw new LJCommonException("未匹配到mapper对应的xml:" + mapper);
  700. }
  701. }
  702. var mapperreq = new CommonDynamicSelectRequest
  703. {
  704. queryparams = queryParams
  705. };
  706. var mapperrslt = GetXmlResult(cmd, _mapperAppend + mappername, mapperreq, tokendata, true);
  707. col.Add("enum", mapperrslt.datatable);
  708. }
  709. cols.Add(col);
  710. sortid++;
  711. colSortDicDefault[field] = sortid;
  712. }
  713. var colSort = cols.OrderBy(x =>
  714. {
  715. var field = x.GetValue("field")?.ToString();
  716. if (colSortDic.ContainsKey(field))
  717. {
  718. return colSortDic[field];
  719. }
  720. else
  721. {
  722. return int.MaxValue;
  723. }
  724. }).ThenBy(x =>
  725. {
  726. var field = x.GetValue("field")?.ToString();
  727. if (colSortDicDefault.ContainsKey(field))
  728. {
  729. return colSortDicDefault[field];
  730. }
  731. else
  732. {
  733. return int.MaxValue;
  734. }
  735. });
  736. var colSortJArr = new JArray();
  737. foreach (var col in colSort)
  738. {
  739. colSortJArr.Add(col);
  740. }
  741. preferenceobj["columns"] = colSortJArr;
  742. rslt.tableinfo = preferenceobj;
  743. }
  744. return rslt;
  745. }
  746. private void handleCustomTitle(SqlCommand cmd, int empid, JObject col, string dbfield)
  747. {
  748. var field = col.GetValue("field")?.ToString();
  749. var title = col.GetValue("title")?.ToString();
  750. var custitle = title;
  751. var visibleLimited = false;
  752. // 处理标题文本
  753. /*if (field.Contains("status") && title.Contains("配置"))
  754. {
  755. var opTitle = OptionHelper.GetOpString(cmd, "029");
  756. if (!string.IsNullOrEmpty(opTitle))
  757. {
  758. custitle = custitle.Replace("配置", opTitle);
  759. }
  760. }
  761. else if (field.Contains("woodcode") && title.Contains("配置1"))
  762. {
  763. var opTitle = OptionHelper.GetOpString(cmd, "027");
  764. if (!string.IsNullOrEmpty(opTitle))
  765. {
  766. custitle = custitle.Replace("配置1", opTitle);
  767. }
  768. }
  769. else if (field.Contains("pcode") && title.Contains("配置2"))
  770. {
  771. var opTitle = OptionHelper.GetOpString(cmd, "028");
  772. if (!string.IsNullOrEmpty(opTitle))
  773. {
  774. custitle = custitle.Replace("配置2", opTitle);
  775. }
  776. }
  777. else if (field.Contains("mtrlsectype"))
  778. {
  779. var opTitle = OptionHelper.GetOpString(cmd, "041");
  780. if (!string.IsNullOrEmpty(opTitle))
  781. {
  782. custitle = opTitle;
  783. }
  784. }
  785. else if (field.Contains("zxmtrlmode"))
  786. {
  787. var opTitle = OptionHelper.GetOpString(cmd, "042");
  788. if (!string.IsNullOrEmpty(opTitle))
  789. {
  790. custitle = opTitle;
  791. }
  792. }
  793. else if (field.Contains("usermtrlmode"))
  794. {
  795. var opTitle = OptionHelper.GetOpString(cmd, "128");
  796. if (!string.IsNullOrEmpty(opTitle))
  797. {
  798. custitle = opTitle;
  799. }
  800. }
  801. else if (field.Contains("otheramt"))
  802. {
  803. string opTitle = null;
  804. if (dbfield.Contains("u_outware.otheramt") || dbfield.Contains("u_saletask.otheramt"))
  805. {
  806. opTitle = OptionHelper.GetOpString(cmd, "050");
  807. }
  808. else if (dbfield.Contains("u_inware.otheramt"))
  809. {
  810. opTitle = OptionHelper.GetOpString(cmd, "136");
  811. }
  812. if (!string.IsNullOrEmpty(opTitle))
  813. {
  814. custitle = opTitle;
  815. }
  816. }
  817. else if (field.Contains("excolumn"))
  818. {
  819. var dbcolname = field;
  820. var inputType = 0;
  821. dbcolname = RemoveAS(dbcolname);
  822. if (dbcolname.Contains("."))
  823. {
  824. dbcolname = dbcolname.Substring(dbcolname.LastIndexOf(".") + 1);
  825. }
  826. cmd.CommandText = @"SELECT replacename,inputtype,ifuse FROM u_ext_billcolumn where ifuse = 1 AND columnname = @columnname";
  827. cmd.Parameters.Clear();
  828. cmd.Parameters.AddWithValue("@columnname", dbcolname);
  829. using (var reader = cmd.ExecuteReader())
  830. {
  831. if (reader.Read() && Convert.ToInt32(reader["ifuse"]) == 1)
  832. {
  833. custitle = reader["replacename"].ToString().Trim();
  834. inputType = Convert.ToInt32(reader["inputtype"]);
  835. }
  836. else
  837. {
  838. visibleLimited = true;
  839. }
  840. }
  841. switch (inputType)
  842. {
  843. case 0: // 未指定
  844. break;
  845. case 1: // 数值
  846. col["datatype"] = "number";
  847. break;
  848. case 2: // 文本
  849. break;
  850. case 3: // 日期
  851. col["datatype"] = "date";
  852. break;
  853. case 4: // 复合
  854. break;
  855. default:
  856. break;
  857. }
  858. }*/
  859. if (!string.Equals(title, custitle))
  860. {
  861. col["title"] = custitle;
  862. }
  863. //处理显示权限
  864. /*if (!col.ContainsKey("funcid"))
  865. {
  866. if (dbfield.Contains("u_spt.name") || dbfield.Contains("sptname"))
  867. {
  868. if (!title.Contains("甲方名称"))
  869. {
  870. col.Add("funcid", 6535);
  871. }
  872. }
  873. }
  874. if (col.ContainsKey("funcid"))
  875. {
  876. var funcid = Convert.ToInt32(col.GetValue("funcid").ToString());
  877. if (!FuncPowerHelper.CheckFuncPower(cmd, empid, funcid))
  878. {
  879. visibleLimited = true;
  880. }
  881. }
  882. if (col.ContainsKey("funcid_notequals"))
  883. {
  884. var funcid = Convert.ToInt32(col.GetValue("funcid_notequals").ToString());
  885. if (!FuncPowerHelper.CheckFuncPower(cmd, empid, funcid))
  886. {
  887. visibleLimited = true;
  888. }
  889. }*/
  890. if (visibleLimited)
  891. {
  892. col.Add("limited", true);
  893. }
  894. }
  895. private string RemoveAS(string oristr)
  896. {
  897. var asindex = oristr.IndexOf(" as ", StringComparison.OrdinalIgnoreCase);
  898. if (asindex > 0)
  899. {
  900. return oristr.Substring(0, asindex);
  901. }
  902. return oristr;
  903. }
  904. private class QueryResult
  905. {
  906. public JArray datatable { get; set; }
  907. public JObject tableinfo { get; set; }
  908. public int totalcnt { get; set; }
  909. public int pageindex { get; set; }
  910. public int pagesize { get; set; }
  911. }
  912. }
  913. }