using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using Apache.NMS; using Apache.NMS.ActiveMQ; using System.Threading.Tasks; using System.Threading; using System.Data.SqlClient; namespace SOH.FenZhen.Server { public partial class FenZhenService : ServiceBase { IConnection connection; ISession session; IMessageProducer prod; IConnection connection_recive_ks; ISession session_recive_ks; IConnection connection_recive_qt; ISession session_recive_qt; Timer timer; public FenZhenService() { InitializeComponent(); } protected override void OnStart(string[] args) { start(); } object _lock = new object(); bool processing = false; string[] xs = new string[] { "欧阳", "太史", "端木", "上官", "司马", "东方", "独孤", "南宫", "万俟", "闻人", "夏侯", "诸葛", "尉迟", "公羊", "赫连", "澹台", "皇甫", "宗政", "濮阳", "公冶", "太叔", "申屠", "公孙", "慕容", "仲孙", "钟离", "长孙", "宇文", "司徒", "鲜于", "司空", "闾丘", "子车", "亓官", "司寇", "巫马", "公西", "颛孙", "壤驷", "公良", "漆雕", "乐正", "宰父", "谷梁", "拓跋", "夹谷", "轩辕", "令狐", "段干", "百里", "呼延", "东郭", "南门", "羊舌", "微生", "公户", "公玉", "公仪", "梁丘", "公仲", "公上", "公门", "公山", "公坚", "左丘", "公伯", "西门", "公祖", "第五", "公乘", "贯丘", "公皙", "南荣", "东里", "东宫", "仲长", "子书", "子桑", "即墨", "达奚", "褚师", "吴铭", }; public void start() { timer = new Timer(process, null, 6000, 60000); IConnectionFactory factory = new ConnectionFactory("tcp://192.168.20.2:61616/"); connection = factory.CreateConnection(); //Create the Session session = connection.CreateSession(); //Create the Producer for the topic/queue prod = session.CreateProducer( new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("kstj")); Thread t1 = new Thread(recive_ks); Thread t2 = new Thread(recive_qt); t1.Start(); t2.Start(); //Thread t = new Thread(Processing); //t.Start(); while (true) { Thread.Sleep(10000); Console.WriteLine("sleep1000000"); } } private void process(object state) { //throw new NotImplementedException(); lock (_lock) { if (!processing) { Processing(); } } } private void recive_qt() { IConnectionFactory factory = new ConnectionFactory("tcp://192.168.20.2:61616/"); connection_recive_qt = factory.CreateConnection(); //dconnection_recive_ks.ClientId = System.Net.Dns.GetHostName() + "_reciveqt"; connection_recive_qt.ClientId = "server recive qt"; connection_recive_qt.Start(); //Create the Session session_recive_qt = connection_recive_qt.CreateSession(); IMessageConsumer consumer = session_recive_qt.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("qtdj"), "qtdj listener", null, false); consumer.Listener += new MessageListener(consumer_Listener_qt); } private void consumer_Listener_qt(IMessage message) { ITextMessage itm = message as ITextMessage; //Console.WriteLine(itm.Text); Console.WriteLine(System.Web.HttpUtility.UrlDecode(itm.Text)); // throw new NotImplementedException(); var qts = Newtonsoft.Json.JsonConvert.DeserializeObject(System.Web.HttpUtility.UrlDecode(itm.Text)); if (Array.IndexOf(xs, qts.xm.Substring(0, 2)) == -1) { if (qts.xb == 1) { qts.xm = qts.xm.Substring(0, 1) + "女士(" + qts.tm.ToString().Substring(qts.tm.ToString().Length - 3, 3) + ")"; } else { qts.xm = qts.xm.Substring(0, 1) + "先生(" + qts.tm.ToString().Substring(qts.tm.ToString().Length - 3, 3) + ")"; } } else { for (int i = 0; i < xs.Length; i++) { if (qts.xm.Substring(0, 2).Equals(xs[i])) { if (qts.xb == 1) { qts.xm = xs[i] + "女士(" + qts.tm.ToString().Substring(qts.tm.ToString().Length - 3, 3) + ")"; } else { qts.xm = xs[i] + "先生(" + qts.tm.ToString().Substring(qts.tm.ToString().Length - 3, 3) + ")"; } } } } string sqlyssj = @"INSERT INTO [pd_yssj] ([xm] ,[xb] ,[tjsj] ,[nl] ,[yssj] ,[qtxtid]) VALUES (@xm ,@xb ,@tjsj ,@nl ,@yssj ,@qtxtid) select @@IDENTITY "; SqlParameter[] paras = { new SqlParameter("xm",SqlDbType.VarChar), new SqlParameter("xb",SqlDbType.SmallInt), new SqlParameter("tjsj",SqlDbType.DateTime), new SqlParameter("nl",SqlDbType.Int), new SqlParameter("yssj",SqlDbType.VarChar), new SqlParameter("qtxtid",SqlDbType.VarChar), }; paras[0].Value = qts.xm; paras[1].Value = qts.xb; paras[2].Value = qts.tjsj; paras[3].Value = qts.nl; paras[4].Value = System.Web.HttpUtility.UrlDecode(itm.Text); paras[5].Value = qts.tm.ToString(); int ysid = int.Parse(DbHelperSQL.GetSingle(sqlyssj, paras).ToString()); string sql_pdry = "select ysid from pd_rylb where qtxtid='" + qts.tm + "'"; var dr1 = DbHelperSQL.ExecuteReader(sql_pdry); int pdryid = 0; if (dr1.Read()) { pdryid = (int)dr1[0]; dr1.Close(); } else { dr1.Close(); string sql_pdrylb = @"INSERT INTO [pd_rylb] ([xm] ,[xb] ,[pdsj] ,[nl] ,[ysid] ,[qtxtid]) VALUES (@xm ,@xb ,@pdsj ,@nl ,@ysid ,@qtxtid) select @@IDENTITY "; SqlParameter[] paras_pdry = { new SqlParameter("xm",SqlDbType.VarChar), new SqlParameter("xb",SqlDbType.TinyInt), new SqlParameter("pdsj",SqlDbType.DateTime), new SqlParameter("nl",SqlDbType.Int), new SqlParameter("ysid",SqlDbType.Int), new SqlParameter("qtxtid",SqlDbType.VarChar) }; paras_pdry[0].Value = qts.xm; paras_pdry[1].Value = qts.xb; paras_pdry[2].Value = DateTime.Now; paras_pdry[3].Value = qts.nl; paras_pdry[4].Value = ysid; paras_pdry[5].Value = qts.tm.ToString(); pdryid = int.Parse(DbHelperSQL.GetSingle(sql_pdrylb, paras_pdry).ToString()); } qts.xms.ForEach(t => { string sql_xm = @"select a.xmid from pd_ksxm a join pd_wlks b on a.ksid=b.id join pd_qtxtxmdy c on a.xmid=c.xmid where b.isOpen=1 and c.qtxtxmid=" + t; SqlDataReader dr = DbHelperSQL.ExecuteReader(sql_xm); int xmid = 0; if (dr.Read()) { xmid = (int)dr[0]; } dr.Close(); if (xmid != 0) { string sql_cz = "select count(1) from pd_pdxms where pdid=" + pdryid + " and xmid=" + xmid; int count = int.Parse(DbHelperSQL.GetSingle(sql_cz).ToString()); if (count == 0) { string sql_pdxm = @"INSERT INTO [pd_pdxms] ([pdid] ,[xmid] ,[zxbz] ) VALUES (" + pdryid + @" ," + xmid + @" ,0 ) "; DbHelperSQL.ExecuteSql(sql_pdxm); } } }); lock (_lock) { if (!processing) Processing(); } } #region 接收科室发送的信息 private void recive_ks() { IConnectionFactory factory = new ConnectionFactory("tcp://192.168.20.2:61616/"); connection_recive_ks = factory.CreateConnection(); connection_recive_ks.ClientId = System.Net.Dns.GetHostName() + "receive_ks"; connection_recive_ks.Start(); //Create the Session session_recive_ks = connection_recive_ks.CreateSession(); IMessageConsumer consumer = session_recive_ks.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("wctj"), "ks_wc listener", null, false); consumer.Listener += new MessageListener(consumer_Listener_ks); IMessageConsumer kstj = session_recive_ks.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("kaishitijian"), "ks_tj listener", null, false); kstj.Listener += new MessageListener(kstj_Listener_ks); } private void kstj_Listener_ks(IMessage message) { //throw new NotImplementedException(); ITextMessage itm = message as ITextMessage; Console.WriteLine("开始体检:" + itm.Text); string[] cs = itm.Text.Split(','); if (cs.Length != 2) { return; } string tm = cs[0]; string ks = cs[1]; string sql = "select id from pd_rylb where qtxtid='" + tm + "'"; var dr = DbHelperSQL.ExecuteReader(sql); if (dr.Read()) { string pdid = dr[0].ToString(); dr.Close(); //2016年3月21日08:09:10 李延璋,解决客人没有按照屏幕排队,会造成排队混乱的问题 string sql_selKspdlb = "select ksid from pd_kspdlb where pd_kspdlb.zxbz in (0,1) and pd_kspdlb.pdid=" + pdid + " and ksid=" + ks; var drKspdlb = DbHelperSQL.ExecuteReader(sql_selKspdlb); if (drKspdlb.Read()) { //string pdksid = drKspdlb[0].ToString(); drKspdlb.Close(); // /* string sql_upKspdlb1 = "update pd_kspdlb set ksid=" + ks + " where pd_kspdlb.zxbz=1 and pd_kspdlb.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_upKspdlb1); string sql_upKspdlb = "update pd_kspdlb set ksid=" + ks + " where pd_kspdlb.zxbz=0 and pd_kspdlb.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_upKspdlb); string sql_upPdxms2 = "update pd_pdxms set zxbz=0,zxsj='',pdsj='' where pd_pdxms.zxbz=2 and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_upPdxms2); string sql_upPdxms = "update pd_pdxms set zxbz=0,zxsj='',pdsj='' where pd_pdxms.zxbz=1 and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_upPdxms); string sql_upKspdlb = "update pd_kspdlb set ksid=" + ks + " where pd_kspdlb.zxbz=0 and pd_kspdlb.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_upKspdlb); string sql_upPdxms1 = "update pd_pdxms set zxbz=1,zxsj=GETDATE(),pdsj=GETDATE() from pd_ksxm where pd_ksxm.xmid=pd_pdxms.xmid"+ " and pd_ksxm.ksid="+ks+" and pd_pdxms.pdid="+pdid; DbHelperSQL.ExecuteSql(sql_upPdxms1);*/ // } else { sql_selKspdlb = "select ksid from pd_kspdlb where pd_kspdlb.zxbz in (0,1) and pd_kspdlb.pdid=" + pdid;//查询当前正在排队的科室 drKspdlb = DbHelperSQL.ExecuteReader(sql_selKspdlb); if (drKspdlb.Read()) { string pdksid = drKspdlb[0].ToString(); drKspdlb.Close(); sql = "select count(1) from pd_pdxms,pd_ksxm where pd_pdxms.pdid=" + pdid + " and pd_ksxm.xmid=pd_pdxms.xmid and pd_ksxm.ksid=" + ks; if(DbHelperSQL.GetSingle(sql).ToString()!="0") { string delsql = "delete from pd_kspdlb where pdid=" + pdid + " and ksid=" + pdksid; DbHelperSQL.ExecuteSql(delsql); ITextMessage im1 = prod.CreateTextMessage(); im1.Properties.SetString("ks", ks); var ksm = new kstjMessage(); string sql_xm1 = "select xm from pd_rylb where id=" + pdid; string xm1 = "错误"; var xmdr1 = DbHelperSQL.ExecuteReader(sql_xm1); if (xmdr1.Read()) { xm1 = xmdr1[0].ToString(); } xmdr1.Close(); ksm.xm = xm1; ksm.pdid = int.Parse(pdid); ksm.pdsj = DateTime.Now; ksm.zxzt = 2; im1.Text = System.Web.HttpUtility.UrlEncode(Newtonsoft.Json.JsonConvert.SerializeObject(ksm)); prod.Send(im1);//删除原来科室的排队信息 sql = "insert into pd_kspdlb(pdid,ksid,pdsj,zxbz,yjsysj) values(" + pdid + "," + ks + ",getdate(),0,0)"; DbHelperSQL.ExecuteSql(sql); sql = "update pd_pdxms set zxbz=1,zxsj=GETDATE() from pd_xm,pd_ksxm where pd_pdxms.zxbz=0 and pd_xm.id=pd_ksxm.xmid and pd_ksxm.ksid=" + ks + " and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql); } else { return; } } else { drKspdlb.Close(); return; } } string sql_up = "update pd_pdxms set zxbz=2,zxsj=GETDATE() from pd_xm,pd_ksxm where pd_pdxms.zxbz=1 and pd_xm.id=pd_ksxm.xmid and pd_ksxm.ksid=" + ks + " and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_up); sql_up = "update pd_kspdlb set zxbz=1,zxsj=getdate() where zxbz=0 and ksid=" + ks + " and pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_up); ITextMessage itm1 = prod.CreateTextMessage(); itm1.Properties.SetString("ks", ks); var pdmsg = new kstjMessage(); string sql_xm = "select xm from pd_rylb where id=" + pdid; string xm = "错误"; var xmdr = DbHelperSQL.ExecuteReader(sql_xm); if (xmdr.Read()) { xm = xmdr[0].ToString(); } xmdr.Close(); pdmsg.xm = xm; pdmsg.pdid = int.Parse(pdid); pdmsg.pdsj = DateTime.Now; pdmsg.zxzt = 1; itm1.Text = System.Web.HttpUtility.UrlEncode(Newtonsoft.Json.JsonConvert.SerializeObject(pdmsg)); prod.Send(itm1); } else { dr.Close(); } } private void consumer_Listener_ks(IMessage message) { ITextMessage itm = message as ITextMessage; Console.WriteLine("结束体检:" + itm.Text); string[] cs = itm.Text.Split(','); if (cs.Length != 2) { return; } string tm = cs[0]; string ks = cs[1]; string sql = "select id from pd_rylb where qtxtid='" + tm + "'"; var dr = DbHelperSQL.ExecuteReader(sql); if (dr.Read()) { string pdid = dr[0].ToString(); dr.Close(); sql = "select zxbz from pd_kspdlb where pdid=" + pdid + " and ksid=" + ks; DataTable dt = DbHelperSQL.Query(sql).Tables[0]; if(dt.Rows.Count>0) { string zxbz = dt.Rows[0][0].ToString(); if(zxbz=="0") { sql = "update pd_pdxms set zxbz=2,zxsj=GETDATE() from pd_xm,pd_ksxm where pd_pdxms.zxbz=1 and pd_xm.id=pd_ksxm.xmid and pd_ksxm.ksid=" + ks + " and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql); sql = "update pd_kspdlb set zxbz=1,zxsj=getdate() where zxbz=0 and ksid=" + ks + " and pdid=" + pdid; DbHelperSQL.ExecuteSql(sql); } } else { sql = "select count(1) from pd_pdxms,pd_ksxm where pd_pdxms.pdid=" + pdid + " and pd_ksxm.xmid=pd_pdxms.xmid and pd_ksxm.ksid=" + ks; if (DbHelperSQL.GetSingle(sql).ToString() != "0") { sql = "insert into pd_kspdlb(pdid,ksid,pdsj,zxbz,yjsysj) values(" + pdid + "," + ks + ",getdate(),0,0)"; DbHelperSQL.ExecuteSql(sql); sql = "update pd_pdxms set zxbz=1,zxsj=GETDATE() from pd_xm,pd_ksxm where pd_pdxms.zxbz=0 and pd_xm.id=pd_ksxm.xmid and pd_ksxm.ksid=" + ks + " and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql); sql = "update pd_pdxms set zxbz=2,zxsj=GETDATE() from pd_xm,pd_ksxm where pd_pdxms.zxbz=1 and pd_xm.id=pd_ksxm.xmid and pd_ksxm.ksid=" + ks + " and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql); sql = "update pd_kspdlb set zxbz=1,zxsj=getdate() where zxbz=0 and ksid=" + ks + " and pdid=" + pdid; DbHelperSQL.ExecuteSql(sql); } else { return; } } string sql_up = "update pd_pdxms set zxbz=3,wcsj=GETDATE() from pd_xm,pd_ksxm where pd_pdxms.zxbz=2 and pd_xm.id=pd_ksxm.xmid and pd_ksxm.ksid=" + ks + " and pd_pdxms.pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_up); sql_up = "update pd_kspdlb set zxbz=2,wcsj=getdate() where zxbz=1 and ksid=" + ks + " and pdid=" + pdid; DbHelperSQL.ExecuteSql(sql_up); ITextMessage itm1 = prod.CreateTextMessage(); itm1.Properties.SetString("ks", ks); var pdmsg = new kstjMessage(); string sql_xm = "select xm from pd_rylb where id=" + pdid; string xm = "错误"; var xmdr = DbHelperSQL.ExecuteReader(sql_xm); if (xmdr.Read()) { xm = xmdr[0].ToString(); } xmdr.Close(); pdmsg.xm = xm; pdmsg.pdid = int.Parse(pdid); pdmsg.pdsj = DateTime.Now; pdmsg.zxzt = 2; itm1.Text = System.Web.HttpUtility.UrlEncode(Newtonsoft.Json.JsonConvert.SerializeObject(pdmsg)); prod.Send(itm1); } else { dr.Close(); } } #endregion private void Processing() { lock (_lock) { processing = true; //Send Messages } lock (_lock) { processing = false; } } /// /// 安排排队 /// /// /// private void sendpd(string id, string ksid) { //throw new NotImplementedException(); prod = session.CreateProducer( new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("kstj")); // prod.Send(itm); } class kstjMessage { public string xm { get; set; } public int pdid { get; set; } public DateTime pdsj { get; set; } public byte zxzt { get; set; } } protected override void OnStop() { connection.Stop(); connection.Close(); connection_recive_qt.Stop(); connection_recive_qt.Close(); connection_recive_ks.Stop(); connection_recive_ks.Close(); timer.Dispose(); } class qtdj { public int tm { get; set; } public string xm { get; set; } public short xb { get; set; } public short nl { get; set; } public DateTime tjsj { get; set; } public List xms { get; set; } } } } /* string sql = "select id from pd_rylb where exists( select 1 from pd_pdxms where pd_rylb.id=pdid and zxbz=0) order by pdsj "; var dt = DbHelperSQL.Query(sql).Tables[0]; foreach (DataRow dr in dt.Rows) { string id = dr[0].ToString(); sql = "select top 1 pd_xm.id,pd_xm.qzxm from pd_xm,pd_pdxms where pd_xm.id=pd_pdxms.xmid and pd_pdxms.pdid=" + id + " and pd_pdxms.zxbz=0 order by pd_xm.xmqz desc "; var dr_pdxm = DbHelperSQL.ExecuteReader(sql); int xmid = 0; string qzid = null; if (dr_pdxm.Read()) { xmid = (int)dr_pdxm[0]; qzid = dr_pdxm[1] == System.DBNull.Value ? null : dr_pdxm[1].ToString(); } dr_pdxm.Close(); while (true) { if (!string.IsNullOrEmpty(qzid)) { sql = "select id,qzxm from pd_xm where id=" + qzid; dr_pdxm = DbHelperSQL.ExecuteReader(sql); if (dr_pdxm.Read()) { xmid = (int)dr_pdxm[0]; qzid = dr_pdxm[1] == System.DBNull.Value ? null : dr_pdxm[1].ToString(); } dr_pdxm.Close(); } else { break; } } string kspdsj = @"select SUM(isnull(yjsysj,0)) pdsj,pd_wlks.id ksid from pd_wlks left join pd_kspdlb on pd_kspdlb.ksid=pd_wlks.id and (zxbz=0 or zxbz=1) group by pd_wlks.id,pd_wlks.zcpdsj,pd_wlks.zdpdrs having pd_wlks.zcpdsj>SUM(isnull(yjsysj,0)) and pd_wlks.zdpdrs>=COUNT(1) order by SUM(yjsysj)"; var pdsjdt = DbHelperSQL.Query(kspdsj).Tables[0]; string sql_xmks = "select ksid from pd_ksxm where xmid=" + xmid; var qzxmksdt = DbHelperSQL.Query(sql_xmks).Tables[0]; int ksid = 0; int minsj = 0; bool ap = false; foreach (DataRow qzxmks in qzxmksdt.Rows) { DataRow[] drs = pdsjdt.Select("ksid=" + qzxmks[0].ToString()); if (drs.Count() == 0) { // sendpd(id, qzxmks[0].ToString()); //ap = true; continue; } else { if (ksid == 0) { ksid = int.Parse(drs[0]["ksid"].ToString()); minsj = int.Parse(drs[0]["pdsj"].ToString()); } else { if (minsj > int.Parse(drs[0]["pdsj"].ToString())) { ksid = int.Parse(drs[0]["ksid"].ToString()); minsj = int.Parse(drs[0]["pdsj"].ToString()); } } } } if ((!ap) && ksid != 0) { sendpd(id, ksid.ToString()); ap = true; } if (!ap) { if (pdsjdt.Rows.Count > 0) { sendpd(id, pdsjdt.Rows[0]["ksid"].ToString()); } } } */