MachineController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using Cksoft.Unity.Log4NetConfig;
  5. using DllEapDal;
  6. using DllEapEntity;
  7. using DllEapEntity.Dtos;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Configuration;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. namespace WebApplet.Controllers
  15. {
  16. [Route("eap/api/[controller]/[action]")]
  17. public class MachineController : ControllerBase
  18. {
  19. //public IActionResult Index()
  20. //{
  21. // return View();
  22. //}
  23. public IConfiguration Configuration { get; set; }
  24. public MachineController(IConfiguration configuration)
  25. {
  26. Configuration = configuration;
  27. }
  28. [HttpGet]
  29. public async Task<LayuiModel<Machine>> Get(string filter, int pageIndex = 1, int pageSize = 10, string sortField = "a.FCode", string sortOrder = "ascend")
  30. {
  31. string userCode = Request.Headers["usercode"];
  32. if (sortOrder == "descend")
  33. {
  34. sortOrder = "desc";
  35. }
  36. else
  37. {
  38. sortOrder = "asc";
  39. }
  40. if (sortField == "null")
  41. {
  42. sortField = "a.FCode";
  43. }
  44. int start, end;
  45. start = (pageIndex - 1) * pageSize + 1;
  46. end = start + pageSize;
  47. using (IDatabase db = DbFactory.Base("eapslave"))
  48. {
  49. db.BeginTrans();
  50. var dal = new MachineDal(db, userCode);
  51. var total = dal.GetCount(filter);
  52. string errorinfo = string.Empty;
  53. var roles = dal.Get(start, pageSize, sortOrder, sortField, filter,ref errorinfo);
  54. return await Task.FromResult(new LayuiModel<Machine>
  55. {
  56. code = 1,
  57. count = total,
  58. data = roles,
  59. msg = errorinfo
  60. });
  61. }
  62. }
  63. [HttpGet]
  64. public IEnumerable<Machine> GetMachines(string filter)
  65. {
  66. using (IDatabase db = DbFactory.Base("eapslave"))
  67. {
  68. var dal = new MachineDal(db);
  69. return dal.Get(filter);
  70. }
  71. }
  72. [HttpGet]
  73. public async Task<EapResponse> GetByCode(string code)
  74. {
  75. var res = new EapResponse();
  76. using (IDatabase db = DbFactory.Base("eapslave"))
  77. {
  78. var dal = new MachineDal(db);
  79. var mac = dal.Get($" and a.FCode='{code}'").FirstOrDefault();
  80. if (mac == null)
  81. {
  82. res.Code = -1;
  83. res.Msg = $"未找到编号为【{code.ToUpper()}】的机台";
  84. return await Task.FromResult(res);
  85. }
  86. res.Data = mac;
  87. return await Task.FromResult(res);
  88. }
  89. }
  90. /// <summary>
  91. /// 修改真实ip
  92. /// </summary>
  93. /// <param name="programMst"></param>
  94. /// <returns></returns>
  95. [HttpPost]
  96. public EapResponse UpdateRealIP([FromBody] Machine programMst)
  97. {
  98. using (IDatabase db = DbFactory.Base("eap"))
  99. {
  100. db.BeginTrans();
  101. var dal = new MachineDal(db);
  102. string errorinfo = string.Empty;
  103. var response = new EapResponse() { Code = 1, Msg = string.Empty };
  104. int id = -1;
  105. id = dal.UpdateRealIP(programMst, ref errorinfo);
  106. if (id < 0)
  107. {
  108. db.Rollback();
  109. response.Code = -1;
  110. response.Msg = errorinfo;
  111. }
  112. else
  113. {
  114. db.Commit();
  115. // LogHelper<Machine>.LogFatal("修改Machine-->修改值:" + Json.ToJson(programMst), "用户操作", "system-winform");
  116. }
  117. response.Id = id;
  118. return response;
  119. }
  120. }
  121. [HttpGet]
  122. public Machine GetSingleMac(string mac)
  123. {
  124. using (IDatabase db = DbFactory.Base("eapslave"))
  125. {
  126. var dal = new MachineDal(db);
  127. return dal.GetMac(mac);
  128. }
  129. }
  130. [HttpGet]
  131. public string GetMacType(string macCode)
  132. {
  133. using (IDatabase db = DbFactory.Base("eapslave"))
  134. {
  135. var dal = new MachineDal(db);
  136. return dal.GetMacType(macCode);
  137. }
  138. }
  139. [HttpPost]
  140. //[ButtonFilter]
  141. public async Task<EapResponse> Add([FromBody] Machine programMst)
  142. {
  143. string usercode = Request.Headers["usercode"];
  144. using (IDatabase db = DbFactory.Base("eap"))
  145. {
  146. db.BeginTrans();
  147. var dal = new MachineDal(db);
  148. Machine preMac = null;
  149. string errorinfo = string.Empty;
  150. var response = new EapResponse() { Code = 1, Msg = string.Empty };
  151. int id = -1;
  152. if (programMst.ID == 0)
  153. {
  154. preMac = programMst;
  155. id = dal.Add(programMst, usercode, ref errorinfo);
  156. }
  157. else
  158. {
  159. preMac = dal.Get(programMst.ID);
  160. id = dal.Update(programMst, usercode, ref errorinfo);
  161. }
  162. if (id < 0)
  163. {
  164. db.Rollback();
  165. response.Code = -1;
  166. response.Msg = errorinfo;
  167. }
  168. else
  169. {
  170. db.Commit();
  171. if (programMst.ID == 0)
  172. LogHelper<Machine>.LogFatal("新增Machine-->" + Json.ToJson(programMst), "用户操作", usercode);
  173. else
  174. LogHelper<Machine>.LogFatal("修改Machine-->修改值:" + Json.ToJson(programMst), "用户操作", usercode);
  175. if (this.ReConnect(preMac, id, ref errorinfo) < 0)
  176. {
  177. response.Msg = $"机台信息修改成功,自动重连操作失败,失败原因:{errorinfo}";
  178. response.Code = -2;
  179. }
  180. }
  181. response.Id = id;
  182. return await Task.FromResult(response);
  183. }
  184. }
  185. public int ReConnect(Machine preMac, int macId, ref string errorinfo)
  186. {
  187. if (!string.IsNullOrEmpty(Configuration["Client"]) && Configuration["Client"] == "Ofilm")
  188. {
  189. using (IDatabase serverDb = DbFactory.Base("eap"))
  190. {
  191. var machineDal = new MachineDal(serverDb);
  192. serverDb.BeginTrans();
  193. var flag = machineDal.DoAutoReconnect(preMac, macId, ref errorinfo);
  194. if (flag < 0 && flag != -2)
  195. {
  196. serverDb.Rollback();
  197. return -1;
  198. }
  199. serverDb.Commit();
  200. return flag;
  201. }
  202. }
  203. return 1;
  204. }
  205. }
  206. }