using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using DllEapEntity; using DllHsms; using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace DllStatusShowDal { public class MacStatusDal { private IDatabase CurrDb = null; private string EapDbCode = "eap"; public long CurrMaxBlock = 0; public string CurrShareFileDir = ""; private string MqIpAddress = ""; private string MqUser = ""; private string MqPasswd = ""; private string MacQueue = "";//机台对应的队列名称 public MacStatusDal(IDatabase db) { CurrDb = db; } public MacStatus IMacStatus(MacStatus mst, string usercode, ref string errorinfo, bool update01 = true) { try { int result = UpdatePreStatus(mst, usercode, ref errorinfo, update01); if (result <= 0) return null; result = CurrDb.InsertFor(mst, usercode); if (result < 0) { return null; } object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } int id = int.Parse(objid.ToString()); mst = CurrDb.FindEntityFor(id); //mst.StatusID = 6; //关闭广播状态功能 //Start(mst); return mst; } catch (Exception e) { errorinfo = e.Message; return null; } } public MacStatus UpdateLastSvidAlarm(MacStatus lastStatus, MacStatus mst, string usercode, ref string errorinfo) { try { var result = 0; if (lastStatus.AlarmCode == "0000") { result = CurrDb.DeleteFor(lastStatus.ID); if (result <= 0) return null; mst.STime = lastStatus.STime; } result = AddMacStatus01(mst, string.Empty, ref errorinfo); if (result < 0) return null; result = CurrDb.InsertFor(mst, usercode); if (result < 0) { return null; } object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } int id = int.Parse(objid.ToString()); mst = CurrDb.FindEntityFor(id); //mst.StatusID = 6; //关闭广播状态功能 //Start(mst); return mst; } catch (Exception e) { errorinfo = e.Message; return null; } } /// /// 将状态发送到queue /// /// private void Start(MacStatus mst) { IModel channel = null; IConnection connection = null; try { string error = string.Empty; connection = CreateQueueConn(mst.MacCode, ref error); channel = connection.CreateModel(); BroadcastStatus(channel, mst, ref error); } catch (Exception ex) { } finally { if (channel != null) channel.Close(); if (connection != null) connection.Close(); } } /// /// 创建消息队列连接 /// /// /// /// private IConnection CreateQueueConn(string maccode, ref string errorinfo) { MacQueue = GetMacQueueName(maccode, ref errorinfo); if (MacQueue == "") return null; var factory = new ConnectionFactory(); factory.HostName = MqIpAddress; factory.UserName = MqUser; factory.Password = MqPasswd; return factory.CreateConnection(); } //获取机台对应的消息队列名称 private string GetMacQueueName(string maccode, ref string errorinfo) { IDatabase CurrDb = null; try { CurrDb = DbFactory.Base(EapDbCode); string condition = $" and a.FCode='{maccode}'"; Machine mst = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; condition = $" and a.MacID={mst.ID}"; EapAppserverMac eapmac = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; condition = $" and a.APServerID={eapmac.EapAppserverID}"; MQServerDetail mqdetail = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; MqIpAddress = mqdetail.IpAddress; MqUser = mqdetail.FUser; MqPasswd = mqdetail.FPasswd; CurrShareFileDir = mqdetail.ShareDir; return eapmac.EapAppserverFCode; } catch (Exception ex) { errorinfo = "获取机台对应的队列名称发生错误:" + ex.Message.ToString(); return ""; } finally { if (CurrDb != null) CurrDb.Close(); } } /// /// 广播状态 /// /// /// /// private int BroadcastStatus(IModel channel, MacStatus status, ref string errorinfo) { try { byte[] queuedata = EntityHelper.SerializeBytes(status); if (queuedata == null) return -1; string StatusExchange = AppConfigurtaionServices.Configuration["rabbitmq:StatusExchange"]; channel.ExchangeDeclare(exchange: StatusExchange, type: "fanout", durable: false, autoDelete: true); channel.BasicPublish(StatusExchange, "", null, queuedata); //开始传递 return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //public MacStatus IMacStatus(MacStatus mst, ref string errorinfo) //{ // try // { // int result = 0; // result = CurrDb.Insert(mst); // if (result < 0) // { // return null; // } // //using (CurrDb = DbFactory.Base("eap")) // //{ // string sqlstr = $"select max(id) from MacStatus where MacCode='{mst.MacCode}'"; // object objid = CurrDb.FindObject(sqlstr); // // object objid = CurrDb.FindObject("select @@IDENTITY"); // if (objid.ToString() == "") // { // return null; // } // int id = int.Parse(objid.ToString()); // mst = CurrDb.FindEntityFor(id); // return mst; // //} // } // catch (Exception e) // { // errorinfo = e.Message; // return null; // } //} /// /// 更新上个状态结束时间 /// /// 机台编号 /// /// /// public int UpdatePreStatus(MacStatus mst, string usercode, ref string errorinfo, bool update01 = true) { try { int result = 1; //插入最后状态 if (update01) { result = AddMacStatus01(mst, usercode, ref errorinfo); if (result <= 0) { errorinfo = $"添加MacStatus01发生错误:{errorinfo}"; return -1; } } string sqlstr = $"select max(id) from MacStatus where MacCode='{mst.MacCode}'"; object maxobj = CurrDb.FindObject(sqlstr); if (maxobj.ToString() == "") { return 1; } int maxid = int.Parse(maxobj.ToString()); MacStatus entity = CurrDb.FindEntityFor(maxid); if (entity.ETime > entity.STime)//说明已经更新,无需处理 return 1; //if (entity.ETime != null)//说明已经更新 // return 1; entity.ETime = mst.STime; TimeSpan ts1 = new TimeSpan(entity.STime.Ticks); TimeSpan ts2 = new TimeSpan(entity.ETime.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); entity.FLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位 result = CurrDb.UpdateFor(entity, usercode); if (result < 0) return -1; return 1; } catch (Exception e) { errorinfo = e.Message; return -1; } } /// /// 更新上个状态结束时间 /// /// 机台编号 /// /// /// public int AddMacStatus01(MacStatus mst, string usercode, ref string errorinfo) { try { string condition = $" and a.MacCode='{mst.MacCode}'"; List templist = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templist.Count > 0) { if (templist[0].StatusID == mst.StatusID) { templist[0].Remark = mst.Remark; templist[0].AlarmCode = mst.AlarmCode; templist[0].AlarmDescribe = mst.AlarmDescribe; CurrDb.UpdateFor(templist[0], usercode); return 1; } templist[0].StatusID = mst.StatusID; templist[0].STime = mst.STime; templist[0].ETime = mst.ETime; templist[0].Remark = mst.Remark; templist[0].AlarmCode = mst.AlarmCode; templist[0].AlarmDescribe = mst.AlarmDescribe; CurrDb.UpdateFor(templist[0], usercode); } else { MacStatus01 entity = new MacStatus01(); Type type = typeof(MacStatus01); PropertyInfo[] props = type.GetProperties(); foreach (var item in props) { EntityHelper.SetPropertyValue(entity, item.Name, EntityHelper.GetPropertyValue(mst, item.Name)); } CurrDb.InsertFor(entity, usercode); } return 1; } catch (Exception e) { errorinfo = e.Message; return -1; } } public MacStatus GetLastStatus(string maccode, ref string errorinfo) { try { string sqlstr = $"select max(id) from MacStatus where MacCode='{maccode}'"; object maxobj = CurrDb.FindObject(sqlstr); if (maxobj.ToString() == "") { return null; } int maxid = int.Parse(maxobj.ToString()); MacStatus entity = CurrDb.FindEntityFor(maxid); return entity; } catch (Exception e) { errorinfo = e.Message; return null; } } /// /// 更新上个状态结束时间 /// /// 机台编号 /// /// /// private int UpdatePreStatus(string code, string usercode, ref string errorinfo) { try { string sqlstr = $"select max(id) from MacStatus where MacCode='{code}'"; object maxobj = CurrDb.FindObject(sqlstr); if (maxobj.ToString() == "") { return 1; } int maxid = int.Parse(maxobj.ToString()); MacStatus entity = CurrDb.FindEntityFor(maxid); if (entity.ETime > entity.STime)//说明已经更新,无需处理 return 1; //if (entity.ETime != null)//说明已经更新 // return 1; entity.ETime = DateTime.Now; TimeSpan ts1 = new TimeSpan(entity.STime.Ticks); TimeSpan ts2 = new TimeSpan(entity.ETime.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); entity.FLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位 int result = CurrDb.UpdateFor(entity, usercode); if (result < 0) return -1; return 1; } catch (Exception e) { errorinfo = e.Message; return -1; } } /// /// 修改当前结束时间 /// /// /// /// /// public int UpdateStatus(MacStatus mst, ref string errorinfo) { try { MacStatus entity = CurrDb.FindEntityFor(mst.ID); if (entity.ETime > entity.STime)//说明已经更新,无需处理 return 1; //if (entity.ETime != null)//说明已经更新 // return 1; entity.ETime = DateTime.Now; TimeSpan ts1 = new TimeSpan(entity.STime.Ticks); TimeSpan ts2 = new TimeSpan(entity.ETime.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); entity.FLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位 int result = CurrDb.Update(entity); if (result < 0) return -1; return 1; } catch (Exception e) { errorinfo = e.Message; return -1; } } public LayoutDetail BatUpdatePreStatus(List details, string usercode, ref string errorinfo) { try { foreach (LayoutDetail item in details) { if (item.EntityTypeID != EntityType.Machine) continue; int result = UpdatePreStatus(item.FCode, usercode, ref errorinfo); if (result <= 0) return null; } return details[0]; } catch (Exception e) { errorinfo = e.Message; return null; } } public IEnumerable GetMacStatus01ByStatusID(int statusID) { return CurrDb.FindList($@"select * from MacStatus01 where StatusID={statusID} and STime>='{DateTime.Today.AddDays(-1)}' and STime<'{DateTime.Today}' order by STime"); } public MacStatus01 GetMacStatus01ByMacode(string macCode) { return CurrDb.FindList($@"select * from MacStatus01 where MacCode='{macCode}' order by id desc LIMIT 1").FirstOrDefault(); } #region MQ /// /// 根据MACID注册对应的通道 /// /// /// /// private IModel RegeditChannel(int macId, ref string errorinfo) { try { var server = this.GetCurrentMqServer(macId); if (server == null) { errorinfo = "当前机台没有绑定对应的MQ服务器"; return null; } var factory = new ConnectionFactory(); factory.HostName = server.IpAddress; factory.UserName = server.FUser; factory.Password = server.FPasswd; factory.VirtualHost = "/test"; var connection = factory.CreateConnection(); var channel = connection.CreateModel(); return channel; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } /// /// 根据MACID获取对应的队列服务器信息 /// /// /// private MQServer GetCurrentMqServer(int macId) { using (IDatabase db = DbFactory.Base("eapslave")) { var sql = $@"select * from mqserver where id = ( select mstid from mqserverdetail where APServerID=( select eapappserverid from eapappservermac where macid={macId}))"; var mqServer = db.FindList(sql).FirstOrDefault(); return mqServer; } } /// /// 将状态推送到队列上 /// /// /// public int PushStatusToMQ(int macId, MacStatus macStatus, ref string errorinfo) { var mqName = AppConfigurtaionServices.Configuration["mqname:statusmqname"]; if (string.IsNullOrEmpty(mqName)) { errorinfo = "从接口配置文件中获取MQ名称失败"; return -1; } IModel chanel = null; try { chanel = this.RegeditChannel(macId, ref errorinfo); if (chanel == null) return -1; chanel.QueueDeclare(mqName, false, false, false, null); byte[] queuedata = EntityHelper.SerializeBytes(macStatus); chanel.BasicPublish("", mqName, null, queuedata); return 1; } catch (Exception ex) { errorinfo = ex.Message; return -1; } finally { if (chanel != null) { chanel.Close(); chanel.Dispose(); } } } #endregion } }