EapAppServerMacDal.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.OFILM
  8. {
  9. public class EapAppServerMacDal
  10. {
  11. private IDatabase CurrDb = null;
  12. public EapAppServerMacDal(IDatabase db)
  13. {
  14. CurrDb = db;
  15. }
  16. public IEnumerable<EapAppserverMac> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  17. {
  18. var pros = CurrDb.FindListForCondition<EapAppserverMac>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  19. return pros;
  20. }
  21. public int GetCount(string filter)
  22. {
  23. string errorinfo = string.Empty;
  24. var entities = CurrDb.FindListForCondition<EapAppserverMac>(filter, ref errorinfo);
  25. if (entities != null)
  26. {
  27. return entities.Count();
  28. }
  29. return 0;
  30. }
  31. public EapAppserverMac Get(int id)
  32. {
  33. var pro = CurrDb.FindEntityFor<EapAppserverMac>(id);
  34. return pro;
  35. }
  36. public int Delete(int id, ref string msg)
  37. {
  38. if (CurrDb.DeleteFor<EapAppserverMac>(id) < 0)
  39. {
  40. msg = "删除失败";
  41. return -1;
  42. }
  43. msg = string.Empty;
  44. return 1;
  45. }
  46. /// <summary>
  47. /// 添加机台到EAP服务器
  48. /// </summary>
  49. /// <param name="eapServerId"></param>
  50. /// <param name="macIds"></param>
  51. /// <param name="userCode"></param>
  52. /// <param name="errorinfo"></param>
  53. /// <returns></returns>
  54. public int AddMacToEapServer(int eapServerId, IEnumerable<int> macIds, string userCode, ref string errorinfo)
  55. {
  56. //var sql = $"delete from eapappservermac where EapAppserverID={eapServerId}";
  57. //if (CurrDb.ExecuteBySql(sql) < 0)
  58. //{
  59. // errorinfo = "删除原数据失败";
  60. // return -1;
  61. //}
  62. if (macIds != null && macIds.Count() > 0)
  63. {
  64. foreach (var item in macIds)
  65. {
  66. var temp = new EapAppserverMac
  67. {
  68. EapAppserverID = eapServerId,
  69. MacID = item
  70. };
  71. if (CurrDb.InsertFor(temp, userCode) < 0)
  72. {
  73. errorinfo = "插入数据库失败";
  74. return -1;
  75. }
  76. }
  77. }
  78. return 1;
  79. }
  80. /// <summary>
  81. /// 更新机台所在的APPServer
  82. /// </summary>
  83. /// <param name="macId"></param>
  84. /// <param name="appServerId"></param>
  85. /// <param name="userCode"></param>
  86. /// <param name="errorinfo"></param>
  87. /// <returns></returns>
  88. public int Update(int macId, int appServerId, string userCode, ref string errorinfo)
  89. {
  90. var tmp = CurrDb.FindListForCondition<EapAppserverMac>($" and a.macid={macId}",
  91. ref errorinfo).FirstOrDefault();
  92. if (tmp == null)
  93. {
  94. return this.AddMacToEapServer(appServerId, new int[] { macId }, userCode, ref errorinfo);
  95. }
  96. tmp.EapAppserverID = appServerId;
  97. return CurrDb.UpdateFor(tmp, userCode);
  98. }
  99. public bool MoveMacToAppServer(int macId, string oldFactoryCode, string newFactoryCode, ref string errInfo)
  100. {
  101. bool result = false;
  102. List<FactoryAppServer> list = new List<FactoryAppServer>();
  103. list.Add(new FactoryAppServer { FactoryCode = "Factory02", AppServeCode = "AppServer001", AppServerId = 1 });
  104. list.Add(new FactoryAppServer { FactoryCode = "Factory02", AppServeCode = "AppServer002", AppServerId = 2 });
  105. list.Add(new FactoryAppServer { FactoryCode = "Factory03", AppServeCode = "AppServer003", AppServerId = 3 });
  106. list.Add(new FactoryAppServer { FactoryCode = "Factory03", AppServeCode = "AppServer004", AppServerId = 4 });
  107. list.Add(new FactoryAppServer { FactoryCode = "Factory04", AppServeCode = "AppServer005", AppServerId = 5 });
  108. list.Add(new FactoryAppServer { FactoryCode = "Factory04", AppServeCode = "AppServer006", AppServerId = 6 });
  109. var serverList = list.Where(s => s.FactoryCode == newFactoryCode);
  110. var mac = CurrDb.FindList<EapAppserverMac>($"select * from eapappservermac where MacID={macId}").FirstOrDefault();
  111. foreach (var server in serverList)
  112. {
  113. int count = Convert.ToInt32(CurrDb.FindList<string>($"select count(1) from eapappservermac where EapAppserverID={server.AppServerId}").FirstOrDefault());
  114. if (count < 200)
  115. {
  116. if (mac == null)
  117. {
  118. mac = new EapAppserverMac
  119. {
  120. EapAppserverID = server.AppServerId,
  121. MacID = macId
  122. };
  123. CurrDb.InsertFor(mac, "系统");
  124. }
  125. else
  126. {
  127. mac.EapAppserverID = server.AppServerId;
  128. CurrDb.UpdateFor(mac, "系统");
  129. }
  130. result = true;
  131. break;
  132. }
  133. }
  134. errInfo = result ? "" : $"园区{newFactoryCode}的服务器对应的机台都已达到200台,无法新增";
  135. return result;
  136. }
  137. }
  138. }