EapAppServerDal.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using SharpCifs.Util.Sharpen;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace DllEapDal.OFILM
  9. {
  10. public class EapAppServerDal
  11. {
  12. private IDatabase CurrDb = null;
  13. public EapAppServerDal(IDatabase db)
  14. {
  15. CurrDb = db;
  16. }
  17. public IEnumerable<EapAppserver> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  18. {
  19. var pros = CurrDb.FindListForCondition<EapAppserver>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  20. return pros;
  21. }
  22. public int GetCount(string filter)
  23. {
  24. string errorinfo = string.Empty;
  25. var entities = CurrDb.FindListForCondition<EapAppserver>(filter, ref errorinfo);
  26. if (entities != null)
  27. {
  28. return entities.Count();
  29. }
  30. return 0;
  31. }
  32. public EapAppserver Get(int id)
  33. {
  34. var pro = CurrDb.FindEntityFor<EapAppserver>(id);
  35. return pro;
  36. }
  37. /// <summary>
  38. /// 添加角色并返回角色Id
  39. /// </summary>
  40. /// <param name="role"></param>
  41. /// <param name="userCode"></param>
  42. /// <returns></returns>
  43. public int Add(EapAppserver pro, string userCode, ref string errorinfo)
  44. {
  45. var entities = CurrDb.FindListForCondition<EapAppserver>($" and a.FCode='{pro.FCode}' ", ref errorinfo);
  46. if (entities != null && entities.Count() > 0)
  47. {
  48. errorinfo = "已存在相同编号的APP服务器,请确认";
  49. return -1;
  50. }
  51. CurrDb.InsertFor(pro, userCode);
  52. var sql = "select @@identity;";
  53. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  54. return id;
  55. }
  56. /// <summary>
  57. /// 待优化 存在删除后不能
  58. /// </summary>
  59. /// <param name="role"></param>
  60. /// <param name="userCode"></param>
  61. /// <param name="errorinfo"></param>
  62. /// <returns></returns>
  63. public int Update(EapAppserver role, string userCode, ref string errorinfo)
  64. {
  65. var entities = CurrDb.FindListForCondition<EapAppserver>($" and a.FCode='{role.FCode}' " +
  66. $"and a.ID<>{role.ID}", ref errorinfo);
  67. if (entities != null && entities.Count() > 0)
  68. {
  69. errorinfo = "已存在相同编号的APP服务器,请确认";
  70. return -1;
  71. }
  72. if (CurrDb.UpdateFor(role, userCode) < 0)
  73. {
  74. return -1;
  75. }
  76. return role.ID;
  77. }
  78. public IEnumerable<EapAppserverMac> geteapappservermac(int id)
  79. {
  80. var sql = $"select * from eapappservermac where EapAppserverID={id}";
  81. return CurrDb.FindList<EapAppserverMac>(sql);
  82. }
  83. public int Delete(int id, ref string msg)
  84. {
  85. var sql = $"delete from eapappservermac where EapAppserverID={id}";
  86. if (CurrDb.ExecuteBySql(sql) < 0)
  87. {
  88. msg = "删除该APP服务器下的机台失败";
  89. return -1;
  90. }
  91. if (CurrDb.DeleteFor<EapAppserver>(id) < 0)
  92. {
  93. msg = "删除失败";
  94. return -1;
  95. }
  96. msg = string.Empty;
  97. return 1;
  98. }
  99. /// <summary>
  100. /// 获取机台下挂数量最少的AP服务器
  101. /// </summary>
  102. /// <param name="factoryId"></param>
  103. /// <returns></returns>
  104. public EapAppserver GetMacLeastServer(Machine mac)
  105. {
  106. if (mac.FCode.StartsWith("AA") && mac.FactoryId == 8)
  107. {
  108. return this.Get(8);
  109. }
  110. var sql = $@"select EapAppserverID from eapappservermac a
  111. inner join EapAppserver b on a.EapAppserverID=b.id
  112. where factoryid={mac.FactoryId} and b.id<>8
  113. group by EapAppserverID order by count(1) asc limit 0,1";
  114. var serverId = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  115. if (serverId > 0)
  116. {
  117. return this.Get(serverId);
  118. }
  119. return null;
  120. }
  121. }
  122. }