MacOrderDal.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using DllHsms;
  4. using DllHsmsWeb;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. namespace DllEapDal
  11. {
  12. public class MacOrderDal
  13. {
  14. private IDatabase CurrDb = null;
  15. public MacOrderDal(IDatabase db)
  16. {
  17. CurrDb = db;
  18. }
  19. public Machine ReadMachine(string maccode, ref string errorinfo)
  20. {
  21. //读取机台信息
  22. string condition = $" and a.fcode='{maccode}'";
  23. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  24. if (macs == null)
  25. return null;
  26. if (macs.Count <= 0)
  27. {
  28. errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
  29. return null;
  30. }
  31. return macs[0];
  32. }
  33. public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, ref string errorinfo)
  34. {
  35. //读取机台信息
  36. string condition = $" and a.macid={macid}";
  37. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  38. if (macs == null)
  39. return null;
  40. if (macs.Count <= 0)
  41. {
  42. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  43. return null;
  44. }
  45. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
  46. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  47. if (details == null)
  48. return null;
  49. if (details.Count <= 0)
  50. {
  51. errorinfo = "未找到您要的指令。";
  52. return null;
  53. }
  54. return details[0];
  55. }
  56. public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, string fname, ref string errorinfo)
  57. {
  58. //读取机台信息
  59. string condition = $" and a.macid={macid}";
  60. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  61. if (macs == null)
  62. return null;
  63. if (macs.Count <= 0)
  64. {
  65. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  66. return null;
  67. }
  68. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
  69. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  70. if (details == null)
  71. return null;
  72. if (details.Count <= 0)
  73. {
  74. errorinfo = "未找到您要的指令。";
  75. return null;
  76. }
  77. return details[0];
  78. }
  79. public int SendStopMac(string maccode, ref string errorinfo)
  80. {
  81. try
  82. {
  83. Machine mac = ReadMachine(maccode, ref errorinfo);
  84. if (mac == null)
  85. return -1;
  86. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo);
  87. if (order == null)
  88. return -1;
  89. //从机型参数中读取程序参数信息
  90. string condition = $" and a.preid={order.ID}";
  91. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  92. if (datas == null)
  93. return -1;
  94. HsmsWeb accessmac = new HsmsWeb();
  95. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  96. if (rec == null)
  97. return -1;
  98. //休眠5秒,等待机台状态恢复
  99. //Thread.Sleep(5000);
  100. //order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
  101. //if (order == null)
  102. // return -1;
  103. ////从机型参数中读取程序参数信息
  104. //condition = $" and a.preid={order.ID}";
  105. //datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  106. //if (datas == null)
  107. // return -1;
  108. //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  109. //if (rec == null)
  110. // return -1;
  111. return 1;
  112. }
  113. catch (Exception ex)
  114. {
  115. errorinfo = ex.Message.ToString();
  116. return -1;
  117. }
  118. }
  119. public int SendS10F3(string maccode, ref string errorinfo)
  120. {
  121. try
  122. {
  123. Machine mac = ReadMachine(maccode, ref errorinfo);
  124. if (mac == null)
  125. return -1;
  126. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo);
  127. if (order == null)
  128. return -1;
  129. //从机型参数中读取程序参数信息
  130. string condition = $" and a.preid={order.ID}";
  131. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  132. if (datas == null)
  133. return -1;
  134. HsmsWeb accessmac = new HsmsWeb();
  135. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  136. if (rec == null)
  137. return -1;
  138. //休眠5秒,等待机台状态恢复
  139. Thread.Sleep(5000);
  140. order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
  141. if (order == null)
  142. return -1;
  143. //从机型参数中读取程序参数信息
  144. condition = $" and a.preid={order.ID}";
  145. datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  146. if (datas == null)
  147. return -1;
  148. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  149. if (rec == null)
  150. return -1;
  151. return 1;
  152. }
  153. catch (Exception ex)
  154. {
  155. errorinfo = ex.Message.ToString();
  156. return -1;
  157. }
  158. }
  159. #region 关联指令
  160. /// <summary>
  161. /// 关联机台指令
  162. /// </summary>
  163. /// <param name="macId"></param>
  164. /// <param name="userCode"></param>
  165. /// <param name="errorinfo"></param>
  166. /// <returns></returns>
  167. public int BindOrders(Machine mac, string userCode, ref string errorinfo)
  168. {
  169. // var mac = CurrDb.FindEntityFor<Machine>(macId);
  170. int mstId = this.GetOrderMstId(mac.MModeID);
  171. if (mstId < 0)
  172. {
  173. errorinfo = "未找到当前设备对应的机型指令";
  174. return -1;
  175. }
  176. var macorder = CurrDb.FindListForCondition<MacOrder>($" and a.MacId={mac.ID}", ref errorinfo).FirstOrDefault();
  177. if (macorder == null)
  178. {
  179. var tmp = new MacOrder { PreID = mstId, MacID = mac.ID };
  180. return this.InsertMacOrder(tmp, userCode);
  181. }
  182. else
  183. {
  184. if (macorder.PreID != mstId)
  185. {
  186. macorder.PreID = mstId;
  187. if (CurrDb.UpdateFor(macorder, userCode) < 0)
  188. {
  189. errorinfo = "更新数据库失败";
  190. return -1;
  191. }
  192. }
  193. }
  194. return 1;
  195. }
  196. /// <summary>
  197. /// 根据机型Id获取该机型的指令主档ID
  198. /// </summary>
  199. /// <param name="modelId"></param>
  200. /// <returns></returns>
  201. public int GetOrderMstId(int modelId)
  202. {
  203. string sql = $@"select id from ordermst where MModeID={modelId}";
  204. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  205. }
  206. /// <summary>
  207. /// 新增机台指令表
  208. /// </summary>
  209. /// <param name="order"></param>
  210. /// <param name="userCode"></param>
  211. /// <returns></returns>
  212. public int InsertMacOrder(MacOrder order, string userCode)
  213. {
  214. return CurrDb.InsertFor(order, userCode);
  215. }
  216. #endregion
  217. }
  218. }