RoadFlow2.1 临时演示

Dictionary.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. namespace RoadFlow.Platform
  6. {
  7. public class Dictionary
  8. {
  9. private RoadFlow.Data.Interface.IDictionary dataDictionary;
  10. private static string cacheKey = RoadFlow.Utility.Keys.CacheKeys.Dictionary.ToString();
  11. public Dictionary()
  12. {
  13. this.dataDictionary = Data.Factory.Factory.GetDictionary();
  14. }
  15. /// <summary>
  16. /// 新增
  17. /// </summary>
  18. public int Add(RoadFlow.Data.Model.Dictionary model)
  19. {
  20. return dataDictionary.Add(model);
  21. }
  22. /// <summary>
  23. /// 更新
  24. /// </summary>
  25. public int Update(RoadFlow.Data.Model.Dictionary model)
  26. {
  27. return dataDictionary.Update(model);
  28. }
  29. /// <summary>
  30. /// 查询所有记录
  31. /// </summary>
  32. public List<RoadFlow.Data.Model.Dictionary> GetAll(bool fromCache=false)
  33. {
  34. if (!fromCache)
  35. {
  36. return dataDictionary.GetAll();
  37. }
  38. else
  39. {
  40. object obj = RoadFlow.Cache.IO.Opation.Get(cacheKey);
  41. if (obj != null && obj is List<RoadFlow.Data.Model.Dictionary>)
  42. {
  43. return obj as List<RoadFlow.Data.Model.Dictionary>;
  44. }
  45. else
  46. {
  47. var list = dataDictionary.GetAll();
  48. RoadFlow.Cache.IO.Opation.Set(cacheKey, list);
  49. return list;
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// 查询单条记录
  55. /// </summary>
  56. public RoadFlow.Data.Model.Dictionary Get(Guid id, bool fromCache = false)
  57. {
  58. return fromCache ? GetAll(true).Find(p => p.ID == id) : dataDictionary.Get(id);
  59. }
  60. /// <summary>
  61. /// 删除
  62. /// </summary>
  63. public int Delete(Guid id)
  64. {
  65. return dataDictionary.Delete(id);
  66. }
  67. /// <summary>
  68. /// 查询记录条数
  69. /// </summary>
  70. public long GetCount()
  71. {
  72. return dataDictionary.GetCount();
  73. }
  74. /// <summary>
  75. /// 查询根记录
  76. /// </summary>
  77. public RoadFlow.Data.Model.Dictionary GetRoot()
  78. {
  79. return dataDictionary.GetRoot();
  80. }
  81. /// <summary>
  82. /// 查询下级记录
  83. /// </summary>
  84. public List<RoadFlow.Data.Model.Dictionary> GetChilds(Guid id, bool fromCache=false)
  85. {
  86. return fromCache ? getChildsByIDFromCache(id) : dataDictionary.GetChilds(id);
  87. }
  88. /// <summary>
  89. /// 查询下级记录
  90. /// </summary>
  91. public List<RoadFlow.Data.Model.Dictionary> GetChilds(string code, bool fromCache=false)
  92. {
  93. return code.IsNullOrEmpty() ? new List<RoadFlow.Data.Model.Dictionary>() :
  94. fromCache ? getChildsByCodeFromCache(code) :
  95. dataDictionary.GetChilds(code.Trim());
  96. }
  97. private List<RoadFlow.Data.Model.Dictionary> getChildsByCodeFromCache(string code)
  98. {
  99. var list = GetAll(true);
  100. var dict = list.Find(p => string.Compare(p.Code, code, true) == 0);
  101. return dict == null ? new List<RoadFlow.Data.Model.Dictionary>() : list.FindAll(p => p.ParentID == dict.ID).OrderBy(p=>p.Sort).ToList();
  102. }
  103. private List<RoadFlow.Data.Model.Dictionary> getChildsByIDFromCache(Guid id)
  104. {
  105. var list = GetAll(true);
  106. return list.FindAll(p => p.ParentID == id).OrderBy(p=>p.Sort).ToList();
  107. }
  108. /// <summary>
  109. /// 得到所有下级
  110. /// </summary>
  111. /// <param name="code"></param>
  112. /// <param name="fromCache">是否使用缓存</param>
  113. /// <returns></returns>
  114. public List<RoadFlow.Data.Model.Dictionary> GetAllChilds(string code, bool fromCache)
  115. {
  116. if (code.IsNullOrEmpty()) return new List<RoadFlow.Data.Model.Dictionary>();
  117. var dict = GetByCode(code, fromCache);
  118. if (dict == null) return new List<RoadFlow.Data.Model.Dictionary>();
  119. return GetAllChilds(dict.ID, fromCache);
  120. }
  121. /// <summary>
  122. /// 得到所有下级
  123. /// </summary>
  124. /// <param name="id"></param>
  125. /// <param name="fromCache">是否使用缓存</param>
  126. /// <returns></returns>
  127. public List<RoadFlow.Data.Model.Dictionary> GetAllChilds(Guid id, bool fromCache=false)
  128. {
  129. List<RoadFlow.Data.Model.Dictionary> list = new List<RoadFlow.Data.Model.Dictionary>();
  130. addChilds(list, id, fromCache);
  131. return list;
  132. }
  133. private void addChilds(List<RoadFlow.Data.Model.Dictionary> list, Guid id, bool fromCache=false)
  134. {
  135. var childs = fromCache ? getChildsByIDFromCache(id) : GetChilds(id);
  136. foreach (var child in childs)
  137. {
  138. list.Add(child);
  139. addChilds(list, child.ID, fromCache);
  140. }
  141. }
  142. /// <summary>
  143. /// 得到一个项的所有下级项ID字符串
  144. /// </summary>
  145. /// <param name="id"></param>
  146. /// <param name="isSelf">是否包含自己</param>
  147. /// <returns></returns>
  148. public string GetAllChildsIDString(Guid id, bool isSelf = true)
  149. {
  150. StringBuilder sb = new StringBuilder();
  151. if (isSelf)
  152. {
  153. sb.Append(id);
  154. sb.Append(",");
  155. }
  156. var childs = GetAllChilds(id, true);
  157. foreach (var child in childs)
  158. {
  159. sb.Append(child.ID);
  160. sb.Append(",");
  161. }
  162. return sb.ToString().TrimEnd(',');
  163. }
  164. /// <summary>
  165. /// 查询上级记录
  166. /// </summary>
  167. public RoadFlow.Data.Model.Dictionary GetParent(Guid id)
  168. {
  169. return dataDictionary.GetParent(id);
  170. }
  171. /// <summary>
  172. /// 是否包含下级记录
  173. /// </summary>
  174. public bool HasChilds(Guid id)
  175. {
  176. return dataDictionary.HasChilds(id);
  177. }
  178. /// <summary>
  179. /// 得到最大排序
  180. /// </summary>
  181. public int GetMaxSort(Guid id)
  182. {
  183. return dataDictionary.GetMaxSort(id);
  184. }
  185. /// <summary>
  186. /// 更新排序
  187. /// </summary>
  188. public int UpdateSort(Guid id, int sort)
  189. {
  190. return dataDictionary.UpdateSort(id, sort);
  191. }
  192. /// <summary>
  193. /// 根据代码查询一条记录
  194. /// </summary>
  195. /// <param name="code"></param>
  196. /// <param name="fromCache">是否使用缓存</param>
  197. /// <returns></returns>
  198. public RoadFlow.Data.Model.Dictionary GetByCode(string code, bool fromCache=false)
  199. {
  200. return code.IsNullOrEmpty() ? null :
  201. fromCache ? GetAll(true).Find(p => string.Compare(p.Code, code, true) == 0) : dataDictionary.GetByCode(code.Trim());
  202. }
  203. /// <summary>
  204. /// 下拉选项时以哪个字段作为值字段
  205. /// </summary>
  206. public enum OptionValueField
  207. {
  208. ID,
  209. Title,
  210. Code,
  211. Value,
  212. Other,
  213. Note
  214. }
  215. /// <summary>
  216. /// 根据ID得到选项
  217. /// </summary>
  218. /// <param name="id"></param>
  219. /// <param name="value"></param>
  220. /// <returns></returns>
  221. public string GetOptionsByID(Guid id, OptionValueField valueField = OptionValueField.Value, string value = "")
  222. {
  223. var childs = GetAllChilds(id, true);
  224. StringBuilder options = new StringBuilder(childs.Count * 100);
  225. StringBuilder space = new StringBuilder();
  226. foreach (var child in childs)
  227. {
  228. space.Clear();
  229. int parentCount = getParentCount(childs, child);
  230. for (int i = 0; i < parentCount-1; i++)
  231. {
  232. space.Append("&nbsp;&nbsp;");
  233. }
  234. if (parentCount > 0)
  235. {
  236. space.Append("┝");
  237. }
  238. string value1 = getOptionsValue(valueField, child);
  239. options.AppendFormat("<option value=\"{0}\"{1}>{2}{3}</option>" + parentCount, value1, value1 == value ? " selected=\"selected\"" : "", space.ToString(), child.Title);
  240. }
  241. return options.ToString();
  242. }
  243. /// <summary>
  244. /// 得到一个字典项的上级节点数
  245. /// </summary>
  246. /// <param name="dictList"></param>
  247. /// <param name="dict"></param>
  248. /// <returns></returns>
  249. private int getParentCount(List<RoadFlow.Data.Model.Dictionary> dictList, RoadFlow.Data.Model.Dictionary dict)
  250. {
  251. int parent = 0;
  252. RoadFlow.Data.Model.Dictionary parentDict = dictList.Find(p => p.ID == dict.ParentID);
  253. while (parentDict != null)
  254. {
  255. parentDict = dictList.Find(p => p.ID == parentDict.ParentID);
  256. parent++;
  257. }
  258. return parent;
  259. }
  260. /// <summary>
  261. /// 根据代码得到选项
  262. /// </summary>
  263. /// <param name="id"></param>
  264. /// <param name="value"></param>
  265. /// <returns></returns>
  266. public string GetOptionsByCode(string code, OptionValueField valueField = OptionValueField.Value, string value="")
  267. {
  268. return GetOptionsByID(GetIDByCode(code), valueField, value);
  269. }
  270. /// <summary>
  271. /// 根据ID得到单选项
  272. /// </summary>
  273. /// <param name="id"></param>
  274. /// <param name="name">名称</param>
  275. /// <param name="valueField"></param>
  276. /// <param name="value"></param>
  277. /// <param name="attr">其它属性</param>
  278. /// <returns></returns>
  279. public string GetRadiosByID(Guid id, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "")
  280. {
  281. var childs = GetChilds(id, true);
  282. return getRadios(childs, name, valueField, value, attr);
  283. }
  284. /// <summary>
  285. /// 根据代码得到单选项
  286. /// </summary>
  287. /// <param name="code"></param>
  288. /// <param name="name">名称</param>
  289. /// <param name="valueField"></param>
  290. /// <param name="value"></param>
  291. /// <param name="attr">其它属性</param>
  292. /// <returns></returns>
  293. public string GetRadiosByCode(string code, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr="")
  294. {
  295. if (code.IsNullOrEmpty()) return "";
  296. var childs = GetChilds(code.Trim(), true);
  297. return getRadios(childs, name, valueField, value, attr);
  298. }
  299. private string getRadios(List<RoadFlow.Data.Model.Dictionary> childs, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "")
  300. {
  301. StringBuilder options = new StringBuilder(childs.Count * 100);
  302. foreach (var child in childs)
  303. {
  304. string value1 = getOptionsValue(valueField, child);
  305. options.Append("<input type=\"radio\" style=\"vertical-align:middle;\" ");
  306. options.AppendFormat("id=\"{0}_{1}\" ", name, child.ID.ToString("N"));
  307. options.AppendFormat("name=\"{0}\" ", name);
  308. options.AppendFormat("value=\"{0}\" ", value1);
  309. options.Append(string.Compare(value, value1, true) == 0 ? "checked=\"checked\" " : "");
  310. options.Append(attr);
  311. options.Append("/>");
  312. options.AppendFormat("<label style=\"vertical-align:middle;margin-right:3px;\" for=\"{0}_{1}\">{2}</label>", name, child.ID.ToString("N"), child.Title);
  313. }
  314. return options.ToString();
  315. }
  316. /// <summary>
  317. /// 根据ID得到多选项
  318. /// </summary>
  319. /// <param name="code"></param>
  320. /// <param name="name">名称</param>
  321. /// <param name="valueField"></param>
  322. /// <param name="value"></param>
  323. /// <param name="attr">其它属性</param>
  324. /// <returns></returns>
  325. public string GetCheckboxsByID(Guid id, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "")
  326. {
  327. var childs = GetChilds(id, true);
  328. return getCheckboxs(childs, name, valueField, value, attr);
  329. }
  330. /// <summary>
  331. /// 根据代码得到多选项
  332. /// </summary>
  333. /// <param name="code"></param>
  334. /// <param name="name">名称</param>
  335. /// <param name="valueField"></param>
  336. /// <param name="value"></param>
  337. /// <param name="attr">其它属性</param>
  338. /// <returns></returns>
  339. public string GetCheckboxsByCode(string code, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "")
  340. {
  341. if (code.IsNullOrEmpty()) return "";
  342. var childs = GetChilds(code.Trim(), true);
  343. return getCheckboxs(childs, name, valueField, value, attr);
  344. }
  345. private string getCheckboxs(List<RoadFlow.Data.Model.Dictionary> childs, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "")
  346. {
  347. StringBuilder options = new StringBuilder(childs.Count * 100);
  348. foreach (var child in childs)
  349. {
  350. string value1 = getOptionsValue(valueField, child);
  351. options.Append("<input type=\"checkbox\" style=\"vertical-align:middle;\" ");
  352. options.AppendFormat("id=\"{0}_{1}\" ", name, child.ID.ToString("N"));
  353. options.AppendFormat("name=\"{0}\" ", name);
  354. options.AppendFormat("value=\"{0}\" ", value1);
  355. options.Append(value.Contains(value1) ? "checked=\"checked\"" : "");
  356. options.Append(attr);
  357. options.Append("/>");
  358. options.AppendFormat("<label style=\"vertical-align:middle;margin-right:3px;\" for=\"{0}_{1}\">{2}</label>", name, child.ID.ToString("N"), child.Title);
  359. }
  360. return options.ToString();
  361. }
  362. private string getOptionsValue(OptionValueField valueField, RoadFlow.Data.Model.Dictionary dict)
  363. {
  364. string value = string.Empty;
  365. switch (valueField)
  366. {
  367. case OptionValueField.Code:
  368. value = dict.Code;
  369. break;
  370. case OptionValueField.ID:
  371. value = dict.ID.ToString();
  372. break;
  373. case OptionValueField.Note:
  374. value = dict.Note;
  375. break;
  376. case OptionValueField.Other:
  377. value = dict.Other;
  378. break;
  379. case OptionValueField.Title:
  380. value = dict.Title;
  381. break;
  382. case OptionValueField.Value:
  383. value = dict.Value;
  384. break;
  385. }
  386. return value;
  387. }
  388. /// <summary>
  389. /// 刷新字典缓存
  390. /// </summary>
  391. public void RefreshCache()
  392. {
  393. RoadFlow.Cache.IO.Opation.Set(cacheKey, GetAll());
  394. }
  395. /// <summary>
  396. /// 检查代码是否存在
  397. /// </summary>
  398. /// <param name="code"></param>
  399. /// <param name="id"></param>
  400. /// <returns></returns>
  401. public bool HasCode(string code, string id="")
  402. {
  403. if (code.IsNullOrEmpty())
  404. {
  405. return false;
  406. }
  407. var dict = GetByCode(code.Trim());
  408. Guid gid;
  409. if (dict == null)
  410. {
  411. return false;
  412. }
  413. else
  414. {
  415. if (id.IsGuid(out gid) && dict.ID == gid)
  416. {
  417. return false;
  418. }
  419. else
  420. {
  421. return true;
  422. }
  423. }
  424. }
  425. /// <summary>
  426. /// 删除一个字典及其所有下级
  427. /// </summary>
  428. /// <param name="id"></param>
  429. /// <returns></returns>
  430. public int DeleteAndAllChilds(Guid id)
  431. {
  432. int i = 0;
  433. var childs = GetAllChilds(id);
  434. foreach (var child in childs)
  435. {
  436. Delete(child.ID);
  437. i++;
  438. }
  439. Delete(id);
  440. i++;
  441. return i;
  442. }
  443. /// <summary>
  444. /// 得到标题
  445. /// </summary>
  446. /// <param name="id"></param>
  447. /// <returns></returns>
  448. public string GetTitle(Guid id)
  449. {
  450. var dict = Get(id, true);
  451. return dict == null ? "" : dict.Title;
  452. }
  453. /// <summary>
  454. /// 得到标题
  455. /// </summary>
  456. /// <param name="id"></param>
  457. /// <returns></returns>
  458. public string GetTitle(string code)
  459. {
  460. if (code.IsNullOrEmpty()) return "";
  461. var dict = GetByCode(code.Trim(), true);
  462. return dict == null ? "" : dict.Title;
  463. }
  464. /// <summary>
  465. /// 得到标题
  466. /// </summary>
  467. /// <param name="code"></param>
  468. /// <param name="value"></param>
  469. /// <returns></returns>
  470. public string GetTitle(string code, string value)
  471. {
  472. if (code.IsNullOrEmpty()) return "";
  473. var childs = getChildsByCodeFromCache(code.Trim());
  474. var child = childs.Find(p => p.Value == value);
  475. return child == null ? "" : child.Title;
  476. }
  477. /// <summary>
  478. /// 根据代码得到ID
  479. /// </summary>
  480. /// <param name="code"></param>
  481. /// <returns></returns>
  482. public Guid GetIDByCode(string code)
  483. {
  484. var dict = GetByCode(code, true);
  485. return dict == null ? Guid.Empty : dict.ID;
  486. }
  487. /// <summary>
  488. /// 得到combox表格html
  489. /// </summary>
  490. /// <param name="id"></param>
  491. /// <param name="valueField"></param>
  492. /// <param name="defaultValue"></param>
  493. /// <returns></returns>
  494. public string GetComboxTableHtmlByID(string id, RoadFlow.Platform.Dictionary.OptionValueField valueField, string defaultValue)
  495. {
  496. if (!id.IsGuid())
  497. {
  498. return "";
  499. }
  500. var dicts = GetChilds(id.ToGuid());
  501. StringBuilder html = new StringBuilder(2000);
  502. html.Append("<table><thead><tr><th>标题</th><th>备注</th><th>其它</th></tr></thead><tbody>");
  503. foreach (var dict in dicts)
  504. {
  505. html.Append("<tr>");
  506. html.AppendFormat("<td value=\"{0}\"{1}>", dict.ID, dict.ID.ToString().Equals(defaultValue, StringComparison.CurrentCultureIgnoreCase) ? " selected=\"selected\"" : "");
  507. html.Append(dict.Title);
  508. html.Append("</td>");
  509. html.Append("<td>");
  510. html.Append(dict.Note);
  511. html.Append("</td>");
  512. html.Append("<td>");
  513. html.Append(dict.Other);
  514. html.Append("</td>");
  515. html.Append("</tr>");
  516. }
  517. html.Append("</tbody>");
  518. html.Append("</table>");
  519. return html.ToString();
  520. }
  521. }
  522. }