﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ctrip.SOA.Comm;
using Arch.SOA.BLL.Enums;
using Arch.SOA.ESB.Entity;
using Arch.SOA.BLL.Msmq;
using Arch.SOA.BLL.Log;

namespace Arch.SOA.BLL.XmlRequest.Internal
{
    public sealed class MsmqHeartBeatRequest : RequestProcess
    {
        protected override void ParticularHandling(string requestXml, Response response)
        {
            MsmqRegister.Create().HeartBeat(XMLSerializer.DeSerialize(requestXml, typeof(Request)) as Request);
            response.Header.ResultCode = ResultCode.Success;
        }

        ///// <summary>
        ///// MSMQ服务心跳处理 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <param name="response"></param>
        //internal static bool MSMQHeartBeat(Request request, ref Response response)
        //{
        //    MSMQHeartBeatRequest rq = null;
        //    try
        //    {
        //        rq = (MSMQHeartBeatRequest)request.RequestBody;
        //        lock (MSMQRout.msmqlist)
        //        {
        //            #region 移除5分钟都没有发送心跳请求的MSMQ
        //            for (int i = 0; i < MSMQRout.msmqlist.Count; i++)
        //            {
        //                TimeSpan ts = DateTime.Now - MSMQRout.msmqlist[i].LastOnlineTime;
        //                if (ts.TotalMinutes > 5)
        //                {
        //                    Arch.Gov.SysLog.Entity.LogRequest log =
        //                        new Arch.Gov.SysLog.Entity.LogRequest("ESB",
        //                        MSMQRout.msmqlist[i].SubSystemID + "-" + MSMQRout.msmqlist[i].ServerIP + "-"
        //                        + MSMQRout.msmqlist[i].MQName, "MSMQRout", "MSMQListRemove");
        //                    Loger.WriteSOALog(log);
        //                    MSMQRout.msmqlist.RemoveAt(i--);
        //                    //Checker.AddMsmqItem(MSMQRout.msmqlist);
        //                    break;
        //                }
        //            }
        //            #endregion

        //            #region 查找有没有对应的MSMQ
        //            foreach (MSMQEntity mq in MSMQRout.msmqlist)
        //            {
        //                if (mq.SubSystemID == rq.SubSystemID &&
        //                    mq.ServerIP == rq.MSMQServer &&
        //                    mq.MQName == rq.MSMQName)
        //                {
        //                    mq.LastOnlineTime = DateTime.Now;
        //                    response.Header.ResultCode = ResultCode.Success;

        //                    //Arch.Gov.SysLog.Entity.LogRequest log = new Arch.Gov.SysLog.Entity.LogRequest("ESB", mq.SubSystemID + "-" + mq.ServerIP + "-" + mq.MQName, "MSMQRout", "MSMQ Online");
        //                    //Loger.WriteSOALog(log);

        //                    return true;
        //                }
        //            }
        //            #endregion

        //            #region 注册新的MSMQ
        //            MSMQEntity msmq = new MSMQEntity();
        //            msmq.SubSystemID = rq.SubSystemID;
        //            msmq.ServerIP = rq.MSMQServer;
        //            msmq.MQName = rq.MSMQName;
        //            msmq.LastOnlineTime = DateTime.Now;
        //            //huangwei add 2010-11-25
        //            msmq.IsSubSystemDefault = rq.IsSubSystemDefault;
        //            msmq.RequestTypes = rq.RequestTypes;

        //            MSMQRout.msmqlist.Add(msmq);
        //            response.Header.ResultCode = ResultCode.Success;
        //            Arch.Gov.SysLog.Entity.LogRequest log1 = new Arch.Gov.SysLog.Entity.LogRequest("ESB", msmq.SubSystemID + "-" + msmq.ServerIP + "-" + msmq.MQName, "MSMQRout", "MSMQ Registe");
        //            Loger.WriteSOALog(log1);

        //            #endregion
        //        }
        //        //Checker.AddMsmqItem(MSMQRout.msmqlist);
        //        return true;
        //    }
        //    catch (Exception e)
        //    {
        //        response.Header.ResultCode = ResultCode.Fail;
        //        response.Header.ResultMsg = e.ToString();

        //        string msmqinfo = rq != null ? rq.SubSystemID + "-" + rq.MSMQServer + "-" + rq.MSMQName : "";

        //        //Modified by ZZX,2010-11-29
        //        LogStrategy.WriteLog("ESB", string.Empty, e, RequestError.GetWebServiceModuleID(), 0, "Err:MSMQHeartBeat-" + msmqinfo, "MSMQRout", "Err:MSMQHeartBeat-" + msmqinfo, Client.GetClientIPAddress());

        //        //LogManager.WriteException("", e, RequestError.GetWebServiceModuleID(), 0, "Err:MSMQHeartBeat-" + msmqinfo, Client.GetClientIPAddress());
        //        //Arch.Gov.SysLog.Entity.LogRequest log = new Arch.Gov.SysLog.Entity.LogRequest("ESB", e.ToString(), "MSMQRout", "Err:MSMQHeartBeat-" + msmqinfo);
        //        //Loger.WriteSOALog(log);
        //        return false;
        //    }
        //}


        public MsmqHeartBeatRequest(RequestHead reqHead)
            : base(reqHead)
        {
        }
    }
}
