using Cksoft.Data; using DllEapEntity; using DllHsms; using DllHsmsWeb; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace DllEapDal { public class MacOrderDal { private IDatabase CurrDb = null; public MacOrderDal(IDatabase db) { CurrDb = db; } public Machine ReadMachine(string maccode, ref string errorinfo) { //读取机台信息 string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs == null) return null; if (macs.Count <= 0) { errorinfo = $"未找到机台编号【{maccode}】的机台信息。"; return null; } return macs[0]; } public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, ref string errorinfo) { //读取机台信息 string condition = $" and a.macid={macid}"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs == null) return null; if (macs.Count <= 0) { errorinfo = $"未找到机台ID【{macid}】的机台指令信息。"; return null; } condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}"; List details = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (details == null) return null; if (details.Count <= 0) { errorinfo = "未找到您要的指令。"; return null; } return details[0]; } public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, string fname, ref string errorinfo) { //读取机台信息 string condition = $" and a.macid={macid}"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs == null) return null; if (macs.Count <= 0) { errorinfo = $"未找到机台ID【{macid}】的机台指令信息。"; return null; } condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'"; List details = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (details == null) return null; if (details.Count <= 0) { errorinfo = "未找到您要的指令。"; return null; } return details[0]; } public int SendStopMac(string maccode, ref string errorinfo) { try { Machine mac = ReadMachine(maccode, ref errorinfo); if (mac == null) return -1; OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo); if (order == null) return -1; //从机型参数中读取程序参数信息 string condition = $" and a.preid={order.ID}"; List datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); if (rec == null) return -1; //休眠5秒,等待机台状态恢复 //Thread.Sleep(5000); //order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo); //if (order == null) // return -1; ////从机型参数中读取程序参数信息 //condition = $" and a.preid={order.ID}"; //datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //if (datas == null) // return -1; //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); //if (rec == null) // return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int SendS10F3(string maccode, ref string errorinfo) { try { Machine mac = ReadMachine(maccode, ref errorinfo); if (mac == null) return -1; OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo); if (order == null) return -1; //从机型参数中读取程序参数信息 string condition = $" and a.preid={order.ID}"; List datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); if (rec == null) return -1; //休眠5秒,等待机台状态恢复 Thread.Sleep(5000); order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo); if (order == null) return -1; //从机型参数中读取程序参数信息 condition = $" and a.preid={order.ID}"; datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); if (rec == null) return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } #region 关联指令 /// /// 关联机台指令 /// /// /// /// /// public int BindOrders(Machine mac, string userCode, ref string errorinfo) { // var mac = CurrDb.FindEntityFor(macId); int mstId = this.GetOrderMstId(mac.MModeID); if (mstId < 0) { errorinfo = "未找到当前设备对应的机型指令"; return -1; } var macorder = CurrDb.FindListForCondition($" and a.MacId={mac.ID}", ref errorinfo).FirstOrDefault(); if (macorder == null) { var tmp = new MacOrder { PreID = mstId, MacID = mac.ID }; return this.InsertMacOrder(tmp, userCode); } else { if (macorder.PreID != mstId) { macorder.PreID = mstId; if (CurrDb.UpdateFor(macorder, userCode) < 0) { errorinfo = "更新数据库失败"; return -1; } } } return 1; } /// /// 根据机型Id获取该机型的指令主档ID /// /// /// public int GetOrderMstId(int modelId) { string sql = $@"select id from ordermst where MModeID={modelId}"; return Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); } /// /// 新增机台指令表 /// /// /// /// public int InsertMacOrder(MacOrder order, string userCode) { return CurrDb.InsertFor(order, userCode); } #endregion } }