MacrecipeDal.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapEntity.Dtos;
  5. using DllEapEntity.RA;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace DllEapDal.RA
  13. {
  14. public class MacrecipeDal
  15. {
  16. IDatabase Db;
  17. private ILogger logger;
  18. public MacrecipeDal(IDatabase db)
  19. {
  20. Db = db;
  21. logger = AppConfigurtaionServices.MyLog;
  22. }
  23. public IEnumerable<MachineInfo> get(MachineInfo m, int pageIndex, int pageSize, out int total)
  24. {
  25. string sql = " ";
  26. if (m.ID > 0)
  27. {
  28. sql += $@" and a.id='{m.ID}'";
  29. }
  30. if (!string.IsNullOrEmpty(m.Factory))
  31. sql += $@" and a.factory='{m.Factory}'";
  32. if (!string.IsNullOrEmpty(m.FCode))
  33. sql += $@" and a.FCode like '%{m.FCode}%'";
  34. if (!string.IsNullOrEmpty(m.MCode))
  35. sql += $@" and a.MCode='{m.MCode}'";
  36. if (!string.IsNullOrEmpty(m.IPAdress))
  37. sql += $@" and a.IPAdress='{m.IPAdress}'";
  38. if (!string.IsNullOrEmpty(m.MacNum))
  39. sql += $@" and a.MacNum like '%{m.MacNum}%'";
  40. if (!string.IsNullOrEmpty(m.SName))
  41. sql += $@" and b.SName='{m.SName}'";
  42. if (!string.IsNullOrEmpty(m.FName))
  43. sql += $@" and b.FName='{m.FName}'";
  44. if (!string.IsNullOrEmpty(m.Model))
  45. sql += $@" and b.Model='{m.Model}'";
  46. if (!string.IsNullOrEmpty(m.Status))
  47. sql += $@" and c.status='{m.Status}'";
  48. string s = $@"limit {(pageIndex - 1) * pageSize},{pageSize}";
  49. string ss = m.GetQueryTabSql();
  50. string a = "select count(1) from" + ss + " where 1=1" + sql;
  51. total = Convert.ToInt32(Db.FindObject(a));
  52. string error = "";
  53. var data = Db.FindListForCondition<MachineInfo>(sql+ " ORDER by a.FCode " + s , ref error);
  54. var modeCodes = data.Where(c => !string.IsNullOrEmpty(c.ModeCode)).Select(c => c.ModeCode).Distinct();
  55. List<Staff> staff = new List<Staff>();
  56. if (modeCodes != null)
  57. {
  58. var sss = string.Join(",", modeCodes.Select(c => $"'{c}'"));
  59. if (sss.Any())
  60. {
  61. IDatabase db= DbFactory.Base("eapslave");
  62. staff = db.FindList<Staff>($@"SELECT FCode,FName from staff where FCode in ({sss})").ToList();
  63. }
  64. }
  65. var dic = new Dictionary<string, string>();
  66. dic.Add("-1", "离线"); dic.Add("0", "停机"); dic.Add("1", "运转"); dic.Add("2", "预约");
  67. foreach (var item in data)
  68. {
  69. if (staff != null)
  70. {
  71. item.ModeCode = staff.FirstOrDefault(c => c.FCode == item.ModeCode)?.FName;
  72. }
  73. switch ((item.Status))
  74. {
  75. case "-1":
  76. item.Statusname = "离线";
  77. break;
  78. case "0":
  79. item.Statusname = "停机";
  80. break;
  81. case "1":
  82. item.Statusname = "运转";
  83. break;
  84. case "2":
  85. item.Statusname = "预约";
  86. break;
  87. default:
  88. item.Statusname = "暂无";
  89. break;
  90. }
  91. }
  92. return data;
  93. }
  94. public string Add(MachineInfo m)
  95. {
  96. try
  97. {
  98. Db.BeginTrans();
  99. int id = Supplier(m.SName, m.FName, m.Model, m.ModeCode);
  100. if (id > 0)
  101. {
  102. var ent = Db.FindList<Machine>($"select * from machine where ipadress='{m.IPAdress}'").FirstOrDefault();
  103. if (ent != null)
  104. {
  105. return "该IP地址与" + ent.FCode + "重复";
  106. }
  107. var e = Db.FindList<Machine>($"select * from machine where fcode='{m.FCode}'").FirstOrDefault();
  108. if (e != null)
  109. {
  110. return "该设备ID已存在";
  111. }
  112. var en = Db.FindList<Machine>($"select * from machine where mcode='{m.MCode}'").FirstOrDefault();
  113. if (e != null)
  114. {
  115. return "该机身码与" + en.FCode + "重复";
  116. }
  117. Machine mac = new Machine()
  118. {
  119. FCode = m.FCode,
  120. Factory = m.Factory,
  121. IPAdress = m.IPAdress,
  122. SupplierID = id,
  123. MCode = m.MCode,
  124. Port = m.Port,
  125. Remark = m.Remark,
  126. ModeCode = m.ModeCode,
  127. ModeTime = DateTime.Now,
  128. RecCode = m.ModeCode,
  129. RecTime = DateTime.Now,
  130. MacNum = m.MacNum
  131. };
  132. if (Db.InsertFor<Machine>(mac, m.ModeCode) > 0)
  133. {
  134. Db.Commit();
  135. Task.Factory.StartNew(() =>
  136. {
  137. GetService(mac.FCode,mac.ModeCode);
  138. });
  139. return " 数据新增成功";
  140. }
  141. }
  142. return "厂商数据处理失败";
  143. }
  144. catch (Exception ex)
  145. {
  146. return ex.ToString();
  147. }
  148. }
  149. public IEnumerable<SelectDto<string>> GetModel()
  150. {
  151. string sql = $@" SELECT DISTINCT(b.Model) as model
  152. FROM machine a left JOIN supplier b on a.SupplierID = b.id where 1=1";
  153. var data = Db.FindList<Supplier>(sql);
  154. var dto =new List<SelectDto<string>>();
  155. dto.Add(new SelectDto<string>() { Label = "全部", Value = null });
  156. foreach (var item in data)
  157. {
  158. dto.Add(new SelectDto<string>() { Label = item.Model, Value = item.Model });
  159. }
  160. return dto;
  161. }
  162. public bool ReConnect(int ID, string fcode)
  163. {
  164. var data = Db.FindList<Machine>($@"select * from machine where id={ID}").FirstOrDefault();
  165. if (data != null)
  166. {
  167. return GetService(data.FCode, fcode);
  168. }
  169. else
  170. {
  171. return false;
  172. }
  173. }
  174. public string delete(int id, string fcode)
  175. {
  176. string er = "";
  177. var x = Db.FindListForCondition<Machine>($@" and id={id}", ref er).FirstOrDefault();
  178. StopService(x.FCode,fcode);
  179. if (Db.DeleteForCondition<Machine>($@" and id={id}") > 0)
  180. {
  181. logger.LogError(fcode + "删除了" + x.FCode + DateTime.Now);
  182. return "删除成功";
  183. }
  184. else
  185. {
  186. Task.Factory.StartNew(() =>
  187. {
  188. GetService(x.FCode,fcode);
  189. });
  190. }
  191. return "删除失败";
  192. }
  193. private int Supplier(string SName, string FName, string Model, string ModeCode)
  194. {
  195. try
  196. {
  197. string sql = " ";
  198. if (!string.IsNullOrEmpty(SName))
  199. sql += $@" and SName='{SName}'";
  200. if (!string.IsNullOrEmpty(FName))
  201. sql += $@" and FName='{FName}'";
  202. if (!string.IsNullOrEmpty(Model))
  203. sql += $@" and Model='{Model}'";
  204. int id = 0;
  205. string error = "";
  206. Supplier s = Db.FindListForCondition<Supplier>(sql, ref error).FirstOrDefault();
  207. if (s == null)
  208. {
  209. Supplier su = new Supplier()
  210. {
  211. SName = SName,
  212. Model = Model,
  213. FName = FName
  214. };
  215. if (Db.InsertFor<Supplier>(su, ModeCode) > 0)
  216. {
  217. var a = Db.FindObject($@"Select @@identity");
  218. id = Convert.ToInt32(a);
  219. }
  220. }
  221. else
  222. {
  223. id = s.ID;
  224. }
  225. return id;
  226. }
  227. catch (Exception ex)
  228. {
  229. Console.WriteLine(ex.ToString());
  230. return 0;
  231. }
  232. }
  233. public string Update(MachineInfo m)
  234. {
  235. try
  236. {
  237. Db.BeginTrans();
  238. string sql = $@" and id='{m.ID}'";
  239. string error = "";
  240. Machine mac = Db.FindListForCondition<Machine>(sql, ref error).FirstOrDefault();
  241. if (mac == null)
  242. {
  243. Add(m);
  244. }
  245. else
  246. {
  247. int id = Supplier(m.SName, m.FName, m.Model, m.ModeCode);
  248. if (id > 0)
  249. {
  250. var ent = Db.FindList<Machine>($"select * from machine where ipadress='{m.IPAdress}'").FirstOrDefault();
  251. if (ent != null && ent.ID != m.ID)
  252. {
  253. return "该IP地址与" + ent.FCode + "重复";
  254. }
  255. var e = Db.FindList<Machine>($"select * from machine where fcode='{m.FCode}'").FirstOrDefault();
  256. if (e != null && e.ID != m.ID)
  257. {
  258. return "该设备ID已存在";
  259. }
  260. var en = Db.FindList<Machine>($"select * from machine where mcode='{m.MCode}'").FirstOrDefault();
  261. if (en != null && en.ID != m.ID)
  262. {
  263. return "该机身码与" + en.FCode + "重复";
  264. }
  265. mac.FCode = m.FCode ?? mac.FCode;
  266. mac.Factory = m.Factory ?? mac.Factory;
  267. mac.IPAdress = m.IPAdress ?? mac.IPAdress;
  268. mac.SupplierID = id;
  269. mac.MCode = m.MCode ?? mac.MCode;
  270. mac.Port = m.Port ?? mac.Port;
  271. mac.Remark = m.Remark ?? mac.Remark;
  272. mac.ModeCode = m.ModeCode;
  273. mac.ModeTime = DateTime.Now;
  274. mac.MacNum = m.MacNum;
  275. if (Db.UpdateFor<Machine>(mac, error) > 0)
  276. {
  277. Task.Factory.StartNew(() =>
  278. {
  279. GetService(mac.FCode,mac.ModeCode);
  280. });
  281. Db.Commit();
  282. }
  283. }
  284. else
  285. {
  286. error = "厂商数据处理失败";
  287. }
  288. }
  289. return error ?? "修改成功";
  290. }
  291. catch (Exception ex)
  292. {
  293. return ex.ToString();
  294. }
  295. }
  296. public bool GetService(string fcode, string usercode)
  297. {
  298. var dic = new Dictionary<string, string>();
  299. dic.Add("fcode", fcode);
  300. dic.Add("usercode", usercode);
  301. string error = string.Empty;
  302. string url = "http://192.168.56.31:5555/eap/api/AddMachine/Get";
  303. return HttpRequestHelper<bool>.Get(url, dic, ref error);
  304. }
  305. public bool StopService(string fcode, string usercode)
  306. {
  307. var dic = new Dictionary<string, string>();
  308. dic.Add("fcode", fcode);
  309. dic.Add("usercode", usercode);
  310. string error = string.Empty;
  311. string url = "http://192.168.56.31:5555/eap/api/AddMachine/Stop";
  312. return HttpRequestHelper<bool>.Get(url, dic, ref error);
  313. }
  314. }
  315. }