123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- 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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(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<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(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<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(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<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(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<OrderData> datas = CurrDb.FindListForCondition<OrderData>(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<OrderData>(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<OrderData> datas = CurrDb.FindListForCondition<OrderData>(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<OrderData>(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 关联指令
- /// <summary>
- /// 关联机台指令
- /// </summary>
- /// <param name="macId"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int BindOrders(Machine mac, string userCode, ref string errorinfo)
- {
- // var mac = CurrDb.FindEntityFor<Machine>(macId);
- int mstId = this.GetOrderMstId(mac.MModeID);
- if (mstId < 0)
- {
- errorinfo = "未找到当前设备对应的机型指令";
- return -1;
- }
- var macorder = CurrDb.FindListForCondition<MacOrder>($" 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;
- }
- /// <summary>
- /// 根据机型Id获取该机型的指令主档ID
- /// </summary>
- /// <param name="modelId"></param>
- /// <returns></returns>
- public int GetOrderMstId(int modelId)
- {
- string sql = $@"select id from ordermst where MModeID={modelId}";
- return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
- }
- /// <summary>
- /// 新增机台指令表
- /// </summary>
- /// <param name="order"></param>
- /// <param name="userCode"></param>
- /// <returns></returns>
- public int InsertMacOrder(MacOrder order, string userCode)
- {
- return CurrDb.InsertFor(order, userCode);
- }
- #endregion
- }
- }
|