HsmsQueueDal.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace DllEapDal
  8. {
  9. public class HsmsQueueDal
  10. {
  11. private IDatabase CurrDb;
  12. public HsmsQueueDal(IDatabase db)
  13. {
  14. CurrDb = db;
  15. }
  16. public IEnumerable<HsmsQueue> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  17. {
  18. var pros = CurrDb.FindListForCondition<HsmsQueue>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  19. if (pros != null && pros.Count() > 0)
  20. {
  21. foreach (var item in pros)
  22. {
  23. var events = CurrDb.FindListForCondition<HsmsQueueEvent>($" and a.HsmsQueueID='{item.ID}' order by a.id", ref errorinfo);
  24. item.Events = events;
  25. }
  26. }
  27. return pros;
  28. }
  29. public int GetCount(string filter)
  30. {
  31. string errorinfo = string.Empty;
  32. var entities = CurrDb.FindListForCondition<HsmsQueue>(filter, ref errorinfo);
  33. if (entities != null)
  34. {
  35. return entities.Count();
  36. }
  37. return 0;
  38. }
  39. public HsmsQueue Get(int id)
  40. {
  41. string errorinfo = string.Empty;
  42. var pro = CurrDb.FindEntityFor<HsmsQueue>(id);
  43. if (pro != null)
  44. {
  45. var events = CurrDb.FindListForCondition<HsmsQueueEvent>($" and a.HsmsQueueID='{pro.ID}' order by a.id", ref errorinfo);
  46. pro.Events = events;
  47. }
  48. return pro;
  49. }
  50. /// <summary>
  51. /// 添加角色并返回角色Id
  52. /// </summary>
  53. /// <param name="role"></param>
  54. /// <param name="userCode"></param>
  55. /// <returns></returns>
  56. public int Add(HsmsQueue pro, string userCode, ref string errorinfo)
  57. {
  58. var entities = CurrDb.FindListForCondition<HsmsQueue>($" and a.FName='{pro.FName}' ", ref errorinfo);
  59. if (entities != null && entities.Count() > 0)
  60. {
  61. errorinfo = "队列已存在,请确认";
  62. return -1;
  63. }
  64. if (CurrDb.InsertFor<HsmsQueue>(pro, userCode) < 0)
  65. {
  66. errorinfo = "插入队列主表失败";
  67. return -1;
  68. }
  69. var sql = "select @@identity;";
  70. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  71. if (pro.Events != null && pro.Events.Count() > 0)
  72. {
  73. foreach (var item in pro.Events)
  74. {
  75. var temp = new HsmsQueueEvent();
  76. temp.HsmsQueueID = id;
  77. temp.SVal = item.SVal;
  78. temp.FVal = item.FVal;
  79. temp.remark = item.remark;
  80. temp.rectime = temp.modtime = DateTime.Now;
  81. if (CurrDb.InsertFor(temp, userCode) < 0)
  82. {
  83. errorinfo = "插入队列子表失败";
  84. return -1;
  85. }
  86. }
  87. }
  88. return id;
  89. }
  90. /// <summary>
  91. /// 待优化 存在删除后不能
  92. /// </summary>
  93. /// <param name="role"></param>
  94. /// <param name="userCode"></param>
  95. /// <param name="errorinfo"></param>
  96. /// <returns></returns>
  97. public int Update(HsmsQueue role, string userCode, ref string errorinfo)
  98. {
  99. var entities = CurrDb.FindListForCondition<HsmsQueue>($" and a.FName='{role.FName}' " +
  100. $"and a.ID<>{role.ID}", ref errorinfo);
  101. if (entities != null && entities.Count() > 0)
  102. {
  103. errorinfo = "已存在相同的队列,请确认";
  104. return -1;
  105. }
  106. if (CurrDb.UpdateFor(role, userCode) < 0)
  107. {
  108. return -1;
  109. }
  110. var sql = $"delete from HsmsQueueEvent where HsmsQueueID={role.ID}";
  111. if (CurrDb.ExecuteBySql(sql) < 0)
  112. {
  113. return -1;
  114. }
  115. if (role.Events != null && role.Events.Count() > 0)
  116. {
  117. foreach (var item in role.Events)
  118. {
  119. var temp = new HsmsQueueEvent();
  120. temp.HsmsQueueID = role.ID;
  121. temp.SVal = item.SVal;
  122. temp.FVal = item.FVal;
  123. temp.remark = item.remark;
  124. temp.rectime = temp.modtime = DateTime.Now;
  125. if (CurrDb.InsertFor(temp, userCode) < 0)
  126. {
  127. errorinfo = "插入队列子表失败";
  128. return -1;
  129. }
  130. }
  131. }
  132. return role.ID;
  133. }
  134. public int Delete(int id, ref string msg)
  135. {
  136. if (CurrDb.DeleteFor<HsmsQueue>(id) < 0)
  137. {
  138. msg = "删除失败";
  139. return -1;
  140. }
  141. msg = string.Empty;
  142. return 1;
  143. }
  144. public IEnumerable<HsmsQueueEvent> GetQueueEvents(string filter, ref string errorinfo)
  145. {
  146. return CurrDb.FindListForCondition<HsmsQueueEvent>(filter + " order by a.id", ref errorinfo);
  147. }
  148. }
  149. }