using Apache.NMS;
using Newtonsoft.Json.Linq;
using PushPlatform.Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushInfo.Act.BLL
{
    public class MessageQueueBLL
    {
        /// <summary>
        /// 消息队列监听程序启动
        /// </summary>
        public void Start()
        {
            try
            {
                //开始监听消息对象
                LogHelper.Debug("====1.开始监听随机系统推送数据到药物系统数据=====");
                MG.Core.Mipf.MgMethod md = new MG.Core.Mipf.MgMethod();
                md.Listener += new MG.Core.Mipf.MgListenerCollection.MgListener(MipfRandomChange);
                md.StartListener("MipfQueue_EdcRandomChange");
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }

        #region 随机系统推送数据到药物系统
        void MipfRandomChange(Apache.NMS.IMessage msgInfo)
        {
            try
            {
                string text = ((ITextMessage)msgInfo).Text;
                LogHelper.Info(text);
                JObject param = JObject.Parse(text);

                using (PushPlatform.Common.MIPFService.EdcMipfServiceSoapClient mipfBll = new PushPlatform.Common.MIPFService.EdcMipfServiceSoapClient())
                {
                    string projectId = param.GetValue<string>("projectId") != null ? param.GetValue<string>("projectId").ToString() : "";
                    string hosId = param.GetValue<string>("hosId") != null ? param.GetValue<string>("hosId").ToString() : "";
                    string groupId = param.GetValue<string>("groupId") != null ? param.GetValue<string>("groupId").ToString() : "";
                    string patientName = param.GetValue<string>("patientName") != null ? param.GetValue<string>("patientName").ToString() : "";
                    string patientNumber = param.GetValue<string>("patientNumber") != null ? param.GetValue<string>("patientNumber").ToString() : "";
                    string gender = param.GetValue<string>("gender") != null ? param.GetValue<string>("gender").ToString() : "";
                    string createUser = param.GetValue<string>("createUser") != null ? param.GetValue<string>("createUser").ToString() : "";
                    DateTime changeDate = DateTime.Now;
                    DateTime? birthday = null;
                    DateTime inDate = DateTime.Now;
                    if (DateTime.TryParse(param.GetValue<string>("birthday") != null ? param.GetValue<string>("birthday").ToString() : "", out changeDate))
                    {
                        birthday = changeDate;
                    }
                    if (DateTime.TryParse(param.GetValue<string>("inDate") != null ? param.GetValue<string>("inDate").ToString() : "", out changeDate))
                    {
                        inDate = changeDate;
                    }
                    var result = mipfBll.InsertPatient("4e6985e0-6993-47d9-877a-e266236fb4f6", "5fa5432e-0856-48ac-a06a-0d3a15000740", groupId, patientName, patientNumber, gender, createUser, birthday, inDate);
                    if (result.Result == 1)
                    {
                        LogHelper.Debug("数据处理成功");
                    }
                    else
                    {
                        LogHelper.Debug(text + "数据处理成功" + DateTime.Now.ToString());
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }
        #endregion
    }
}
