MacOrderSendDal.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllHsms;
  5. using DllHsmsWeb;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. namespace DllEapDal
  12. {
  13. public class MacOrderSendDal
  14. {
  15. private IDatabase CurrDb = null;
  16. public MacOrderSendDal(IDatabase db)
  17. {
  18. CurrDb = db;
  19. }
  20. public Machine ReadMachine(string maccode, ref string errorinfo)
  21. {
  22. //读取机台信息
  23. string condition = $" and a.fcode='{maccode}'";
  24. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  25. if (macs == null)
  26. return null;
  27. if (macs.Count <= 0)
  28. {
  29. errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
  30. return null;
  31. }
  32. return macs[0];
  33. }
  34. public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, ref string errorinfo)
  35. {
  36. //读取机台信息
  37. string condition = $" and a.macid={macid}";
  38. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  39. if (macs == null)
  40. return null;
  41. if (macs.Count <= 0)
  42. {
  43. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  44. return null;
  45. }
  46. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
  47. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  48. if (details == null)
  49. return null;
  50. if (details.Count <= 0)
  51. {
  52. errorinfo = "未找到您要的指令。";
  53. return null;
  54. }
  55. return details[0];
  56. }
  57. public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, string fname, ref string errorinfo)
  58. {
  59. //读取机台信息
  60. string condition = $" and a.macid={macid}";
  61. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  62. if (macs == null)
  63. return null;
  64. if (macs.Count <= 0)
  65. {
  66. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  67. return null;
  68. }
  69. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
  70. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  71. if (details == null)
  72. return null;
  73. if (details.Count <= 0)
  74. {
  75. errorinfo = "未找到您要的指令。";
  76. return null;
  77. }
  78. return details[0];
  79. }
  80. public int SendStopMac(string maccode, ref string errorinfo)
  81. {
  82. try
  83. {
  84. Machine mac = ReadMachine(maccode, ref errorinfo);
  85. if (mac == null)
  86. return -1;
  87. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo);
  88. if (order == null)
  89. return -1;
  90. //从机型参数中读取程序参数信息
  91. string condition = $" and a.preid={order.ID}";
  92. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  93. if (datas == null)
  94. return -1;
  95. HsmsWeb accessmac = new HsmsWeb();
  96. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  97. if (rec == null)
  98. return -1;
  99. return 1;
  100. }
  101. catch (Exception ex)
  102. {
  103. errorinfo = ex.Message.ToString();
  104. return -1;
  105. }
  106. }
  107. public int SendResumeMac(string maccode, ref string errorinfo)
  108. {
  109. try
  110. {
  111. Machine mac = ReadMachine(maccode, ref errorinfo);
  112. if (mac == null)
  113. return -1;
  114. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
  115. if (order == null)
  116. return -1;
  117. //从机型参数中读取程序参数信息
  118. string condition = $" and a.preid={order.ID}";
  119. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  120. if (datas == null)
  121. return -1;
  122. HsmsWeb accessmac = new HsmsWeb();
  123. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  124. if (rec == null)
  125. return -1;
  126. return 1;
  127. }
  128. catch (Exception ex)
  129. {
  130. errorinfo = ex.Message.ToString();
  131. return -1;
  132. }
  133. }
  134. public int SendS10F3(string maccode,string info, ref string errorinfo)
  135. {
  136. try
  137. {
  138. Machine mac = ReadMachine(maccode, ref errorinfo);
  139. if (mac == null)
  140. return -1;
  141. OrderDetail order = ReadMachineOrderDetail(mac.ID, 10, 3, "s10f3", ref errorinfo);
  142. if (order == null)
  143. return -1;
  144. //从机型参数中读取程序参数信息
  145. string condition = $" and a.preid={order.ID}";
  146. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).OrderBy(t => t.FNum).ToList();
  147. if (datas == null)
  148. return -1;
  149. List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();
  150. tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t => t.FNum).ToList();
  151. tempdatas[1].FContent = info;
  152. tempdatas[1].FLen = info.Length;
  153. HsmsWeb accessmac = new HsmsWeb();
  154. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  155. if (rec == null)
  156. return -1;
  157. if(rec.Datalists[0].FContent!="00")
  158. {
  159. errorinfo = rec.Datalists[0].FContent;
  160. return -1;
  161. }
  162. return 1;
  163. }
  164. catch (Exception ex)
  165. {
  166. errorinfo = ex.Message.ToString();
  167. return -1;
  168. }
  169. }
  170. public List<OrderData> SendOrder(Machine mac, int sval,int fval,string fname, ref string errorinfo)
  171. {
  172. try
  173. {
  174. OrderDetail order = ReadMachineOrderDetail(mac.ID, sval, fval, fname, ref errorinfo);
  175. if (order == null)
  176. return null;
  177. //从机型参数中读取程序参数信息
  178. string condition = $" and a.preid={order.ID}";
  179. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).OrderBy(t => t.FNum).ToList();
  180. if (datas == null)
  181. return null;
  182. HsmsWeb accessmac = new HsmsWeb();
  183. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  184. if (rec == null)
  185. return null;
  186. return rec.Datalists;
  187. }
  188. catch (Exception ex)
  189. {
  190. errorinfo = ex.Message.ToString();
  191. return null;
  192. }
  193. }
  194. public int SendS7F5(string maccode, string programname, ref string errorinfo)
  195. {
  196. try
  197. {
  198. Machine mac = ReadMachine(maccode, ref errorinfo);
  199. if (mac == null)
  200. return -1;
  201. OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 5, "s7f5", ref errorinfo);
  202. if (order == null)
  203. return -1;
  204. //从机型参数中读取程序参数信息
  205. string condition = $" and a.preid={order.ID}";
  206. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).OrderBy(t => t.FNum).ToList();
  207. if (datas == null)
  208. return -1;
  209. datas[0].FContent = programname;
  210. datas[0].FLen = programname.Length;
  211. //List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();
  212. //tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t => t.FNum).ToList();
  213. //tempdatas[0].FContent = programname;
  214. //tempdatas[0].FLen = programname.Length;
  215. HsmsWeb accessmac = new HsmsWeb();
  216. int result = accessmac.SendOrderNoAnswer(mac.FCode, order, datas, ref errorinfo);
  217. if (result <=0)
  218. return -1;
  219. return 1;
  220. }
  221. catch (Exception ex)
  222. {
  223. errorinfo = ex.Message.ToString();
  224. return -1;
  225. }
  226. }
  227. public List<MacParam> GetParamValue(string macCode, List<MacParam> paramlists,ref string errorinfo)
  228. {
  229. try
  230. {
  231. string condition = $" and a.fcode='{macCode}'";
  232. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  233. if (macs == null || macs.Count <= 0)
  234. {
  235. errorinfo = $"在EAP系统中,未找到机台编号为【{macCode}】的机台信息。";
  236. return null;
  237. }
  238. string fcode = "";
  239. foreach (var item in paramlists)
  240. {
  241. if (fcode == "")
  242. fcode = $"'{item.FCode}'";
  243. else
  244. fcode = $"{fcode},'{item.FCode}'";
  245. }
  246. condition = $" and b.FCode in({fcode}) and a.PreID={macs[0].MModeID}";
  247. List<MMSecDetail> secdetails = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
  248. if (secdetails == null || secdetails.Count <= 0)
  249. {
  250. errorinfo = $"在EAP系统中,未找您给的参数代码【{fcode}】。";
  251. return null;
  252. }
  253. List<MMSecDetail> svids = secdetails.Where(t => t.SecFType == SecType.SVID).ToList();
  254. if (svids.Count > 0)
  255. {
  256. //从机台中取值
  257. svids = ReadDataFor(macs[0], svids, SecType.SVID, ref errorinfo);
  258. if (svids == null)
  259. return null;
  260. }
  261. List<MMSecDetail> ecids = secdetails.Where(t => t.SecFType == SecType.ECID).ToList();
  262. if (ecids.Count > 0)
  263. {
  264. //从机台中取值
  265. ecids = ReadDataFor(macs[0], ecids, SecType.ECID, ref errorinfo);
  266. if (ecids == null)
  267. return null;
  268. }
  269. foreach(var item in paramlists)
  270. {
  271. var templist = secdetails.Where(t => t.SecFCode == item.FCode).ToList();
  272. if (templist.Count > 0)
  273. item.FValue = templist[0].Remark;
  274. }
  275. return paramlists;
  276. }
  277. catch (Exception ex)
  278. {
  279. errorinfo = ex.Message.ToString();
  280. return null;
  281. }
  282. }
  283. private List<OrderData> GetData(List<MMSecDetail> mmsecs, int preid, ref string errorinfo)
  284. {
  285. List<OrderData> ldata = new List<OrderData>();
  286. OrderData lentity = new OrderData();
  287. lentity.ID = 1;
  288. lentity.ParentID = 0;
  289. lentity.PreID = preid;
  290. lentity.FCode = "L";
  291. lentity.FLen = 1;
  292. ldata.Add(lentity);
  293. int id = 2;
  294. int fnum = 0;
  295. foreach (var item in mmsecs)
  296. {
  297. id++;
  298. fnum += 10;
  299. OrderData entity = new OrderData();
  300. entity.ID = id;
  301. entity.ParentID = 1;
  302. entity.FNum = fnum;
  303. entity.PreID = preid;
  304. entity.FCode = item.DCode;
  305. entity.FContent = item.FVal;
  306. entity.FLen = 1;
  307. ldata.Add(entity);
  308. }
  309. return ldata;
  310. }
  311. //db机台主动取数据
  312. private List<MMSecDetail> ReadDataFor(Machine mac, List<MMSecDetail> mmsecs, int ftype, ref string errorinfo)
  313. {
  314. try
  315. {
  316. int sval = 1;
  317. int fval = 3;
  318. if (ftype == SecType.ECID)
  319. {
  320. sval = 2;
  321. fval = 13;
  322. }
  323. string condition = $" and a.preid=(SELECT preid FROM macorder where macid={mac.ID}) and a.sval={sval} and a.fval={fval}";
  324. List<OrderDetail> orders = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  325. if (orders.Count <= 0)
  326. {
  327. errorinfo = $"未找到机台{mac.FCode}对应的S{sval}F{fval}指令。";
  328. return null;
  329. }
  330. List<OrderData> datas = GetData(mmsecs, orders[0].ID, ref errorinfo);
  331. if (datas == null)
  332. return null;
  333. HsmsWeb accessmac = new HsmsWeb();
  334. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, orders[0], datas, ref errorinfo);
  335. if (rec == null)
  336. return null;
  337. for (int i = 1; i < rec.Datalists.Count; i++)
  338. {
  339. mmsecs[i - 1].Remark = rec.Datalists[i].FContent;
  340. }
  341. return mmsecs;
  342. }
  343. catch (Exception ex)
  344. {
  345. errorinfo = ex.Message.ToString();
  346. return null;
  347. }
  348. }
  349. public int SendS10F3NoAnswer(string maccode, string info, ref string errorinfo)
  350. {
  351. try
  352. {
  353. Machine mac = ReadMachine(maccode, ref errorinfo);
  354. if (mac == null)
  355. return -1;
  356. OrderDetail order = ReadMachineOrderDetail(mac.ID, 10, 3, "s10f3", ref errorinfo);
  357. if (order == null)
  358. return -1;
  359. //从机型参数中读取程序参数信息
  360. string condition = $" and a.preid={order.ID}";
  361. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).OrderBy(t => t.FNum).ToList();
  362. if (datas == null)
  363. return -1;
  364. List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();
  365. tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t => t.FNum).ToList();
  366. tempdatas[1].FContent = info;
  367. tempdatas[1].FLen = info.Length;
  368. HsmsWeb accessmac = new HsmsWeb();
  369. return accessmac.SendOrderNoAnswer(mac.FCode, order, datas, ref errorinfo);
  370. }
  371. catch (Exception ex)
  372. {
  373. errorinfo = ex.Message.ToString();
  374. return -1;
  375. }
  376. }
  377. }
  378. }