123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- 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<MacStatus>(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<MacStatus>(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<MacStatus>(id);
- //mst.StatusID = 6;
- //关闭广播状态功能
- //Start(mst);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- /// <summary>
- /// 将状态发送到queue
- /// </summary>
- /// <param name="mst"></param>
- 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();
- }
- }
- /// <summary>
- /// 创建消息队列连接
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<Machine>(condition, ref errorinfo).ToList()[0];
- condition = $" and a.MacID={mst.ID}";
- EapAppserverMac eapmac = CurrDb.FindListForCondition<EapAppserverMac>(condition, ref errorinfo).ToList()[0];
- condition = $" and a.APServerID={eapmac.EapAppserverID}";
- MQServerDetail mqdetail = CurrDb.FindListForCondition<MQServerDetail>(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();
- }
- }
- /// <summary>
- /// 广播状态
- /// </summary>
- /// <param name="status"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private int BroadcastStatus(IModel channel, MacStatus status, ref string errorinfo)
- {
- try
- {
- byte[] queuedata = EntityHelper.SerializeBytes<MacStatus>(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<MacStatus>(id);
- // return mst;
- // //}
- // }
- // catch (Exception e)
- // {
- // errorinfo = e.Message;
- // return null;
- // }
- //}
- /// <summary>
- /// 更新上个状态结束时间
- /// </summary>
- /// <param name="maccode">机台编号</param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<MacStatus>(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<MacStatus>(entity, usercode);
- if (result < 0)
- return -1;
- return 1;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return -1;
- }
- }
- /// <summary>
- /// 更新上个状态结束时间
- /// </summary>
- /// <param name="maccode">机台编号</param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int AddMacStatus01(MacStatus mst, string usercode, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.MacCode='{mst.MacCode}'";
- List<MacStatus01> templist = CurrDb.FindListForCondition<MacStatus01>(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<MacStatus01>(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<MacStatus01>(templist[0], usercode);
- }
- else
- {
- MacStatus01 entity = new MacStatus01();
- Type type = typeof(MacStatus01);
- PropertyInfo[] props = type.GetProperties();
- foreach (var item in props)
- {
- EntityHelper.SetPropertyValue<MacStatus01>(entity, item.Name, EntityHelper.GetPropertyValue<MacStatus>(mst, item.Name));
- }
- CurrDb.InsertFor<MacStatus01>(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<MacStatus>(maxid);
- return entity;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- /// <summary>
- /// 更新上个状态结束时间
- /// </summary>
- /// <param name="maccode">机台编号</param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<MacStatus>(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<MacStatus>(entity, usercode);
- if (result < 0)
- return -1;
- return 1;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return -1;
- }
- }
- /// <summary>
- /// 修改当前结束时间
- /// </summary>
- /// <param name="mst"></param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int UpdateStatus(MacStatus mst, ref string errorinfo)
- {
- try
- {
- MacStatus entity = CurrDb.FindEntityFor<MacStatus>(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<MacStatus>(entity);
- if (result < 0)
- return -1;
- return 1;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return -1;
- }
- }
- public LayoutDetail BatUpdatePreStatus(List<LayoutDetail> 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<MacStatus01> GetMacStatus01ByStatusID(int statusID)
- {
- return CurrDb.FindList<MacStatus01>($@"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<MacStatus01>($@"select * from MacStatus01 where MacCode='{macCode}' order by id desc LIMIT 1").FirstOrDefault();
- }
- #region MQ
- /// <summary>
- /// 根据MACID注册对应的通道
- /// </summary>
- /// <param name="macId"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 根据MACID获取对应的队列服务器信息
- /// </summary>
- /// <param name="macId"></param>
- /// <returns></returns>
- 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<MQServer>(sql).FirstOrDefault();
- return mqServer;
- }
- }
- /// <summary>
- /// 将状态推送到队列上
- /// </summary>
- /// <param name="macStatus"></param>
- /// <returns></returns>
- 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>(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
- }
- }
|