// GET: F8YLMessage
        public ActionResult MessageIndex(string pageType, string projectId, string hospitalId, string patientId)
        {
            //patientId = "93";
            //ViewBag.patientId = patientId;
            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();  // Session["role"]
            ViewBag.Token = token;
            var currentUserId = Session["CurrentUserID"] == null ? "-1" : Session["CurrentUserID"].ToString();
            var currentUserHospitalID = Session["CurrentUserHospitalID"] == null ? "-1" : Session["CurrentUserHospitalID"].ToString();
            var currentUserRole = Session["role"] == null ? "XXXXXXX" : Session["role"].ToString();
            string dateParam = Convert.ToString(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-01"));

            ViewBag.currentUserId = currentUserId;
            ViewBag.pageType = pageType;
            ViewBag.projectId = projectId == null ? "-1" : projectId;
            ViewBag.hospitalId = hospitalId == null ? "-1" : hospitalId;
            ViewBag.patientId = string.IsNullOrEmpty(patientId) ? currentUserId : patientId;
            ViewBag.currentUserRole = currentUserRole;

            List<JoinedProjectEntity> projectList = new List<JoinedProjectEntity>();
            //if (currentUserRole != "90")
            //{
            //    // 生成项目列表,传递到页面;
            //    ProjectJoined projectJoined = projectRequest.Joined(token);
            //    if (projectJoined.data != null)
            //    {
            //        projectList = projectJoined.data;
            //    }
            //}
            // 生成项目列表,传递到页面;
            ProjectJoined projectJoined = projectRequest.Joined(token, "0");
            if (projectJoined.data != null)
            {
                projectList = projectJoined.data;
            }

            ViewBag.ProjectList = projectList;

            // 生成用户列表,传递到页面;
            //HospitalUserProfileResponse hospitalUsers = new HospitalUserProfileResponse();
            //hospitalUsers = userRequest.SearchUsers(token, currentUserHospitalID, "");

            //ProjectRelativeUserResponse projectRelativeUserResponse = new ProjectRelativeUserResponse();
            //projectRelativeUserResponse = projectRequest.ProjectRelativeUsers(token, "", "");
            List<UserDataEntity> userList = new List<UserDataEntity>();
            string userHospitalID = currentUserHospitalID;

            if (currentUserRole == "90" || currentUserRole == "80")
            {
                if (currentUserRole == "90")
                {
                    userHospitalID = "";
                }
                HospitalUserProfileResponse hospitalUsers = new HospitalUserProfileResponse();
                hospitalUsers = userRequest.SearchUsers(token, userHospitalID, "");
                if (hospitalUsers.data != null && hospitalUsers.data.data != null)
                {
                    foreach (HospitalUserProfileInfo u in hospitalUsers.data.data)
                    {
                        UserDataEntity user = new UserDataEntity();
                        user.dataId = u.id;
                        user.role = u.role;
                        user.username = u.username;
                        userList.Add(user);
                    }
                }
            }
            else
            {
                Dictionary<string, UserDataEntity> messageUserList = projectRequest.GenerageProjectRelativeUsers(token, "", "");
                userList = FilterBusersByCurrentUserRole(messageUserList, currentUserRole, currentUserId);

            }
            ViewBag.ProjectUsers = userList;

            // 调用接口,获取发送给自己的消息列表和公告列表
            //RecvResponse recvResponseMessage = messageRequest.Recv(token, "0", dateParam, "");
            ChatResponse recvResponseMessage = messageRequest.Chat(token, "0");

            // 生成消息列表,传递到页面;
            List<PageMessageEntity> pageMessageList = new List<PageMessageEntity>();

            if (recvResponseMessage.data != null)
            {
                Dictionary<string, string> usersInfo = new Dictionary<string, string>();
                foreach (ChatResponseDataChatEntity item in recvResponseMessage.data.data)
                {
                    PageMessageEntity pageMessageEntity = new PageMessageEntity();

                    pageMessageEntity.id = item.id;
                    pageMessageEntity.userid = item.userid;
                    pageMessageEntity.ctime = item.ctime;
                    pageMessageEntity.msg = item.msg;
                    pageMessageEntity.label = "";
                    pageMessageEntity.username = item.user.username;
                    //if (usersInfo.ContainsKey(item.userid) == true)
                    //{
                    //    pageMessageEntity.username = usersInfo[item.userid];
                    //}
                    //else
                    //{
                    //    UserProfileResponse user = userRequest.profile(token, item.userid);
                    //    if (user.data != null)
                    //    {
                    //        pageMessageEntity.username = user.data.username;
                    //    }
                    //    else
                    //    {
                    //        pageMessageEntity.username = "******";
                    //    }

                    //    usersInfo.Add(item.userid, pageMessageEntity.username);
                    //}

                    //bool isExists = false;
                    // foreach (PageMessageEntity msg in pageMessageList)
                    //{
                    //    if (msg.userid == pageMessageEntity.userid)
                    //    {
                    //        if (Convert.ToDateTime(pageMessageEntity.ctime) > Convert.ToDateTime(msg.ctime))
                    //        {
                    //            pageMessageList.Remove(msg);
                    //            pageMessageList.Add(pageMessageEntity);
                    //        }
                    //        isExists = true;
                    //    }
                    //}
                    //if (isExists == false)
                    //{
                    pageMessageList.Add(pageMessageEntity);
                    //}
                }
            }

            //foreach (PageMessageEntity msg in pageMessageList)
            //{
            //    ChatResponse chatResponse = messageRequest.Chat(token, msg.userid);
            //    if (chatResponse.data != null)
            //    {
            //        msg.msg = chatResponse.data.data.First<ChatResponseDataChatEntity>().msg;
            //        msg.ctime = chatResponse.data.data.First<ChatResponseDataChatEntity>().ctime;
            //    }
            //}

            ViewBag.MessageList = pageMessageList;

            // 生成公告列表,传递到页面;
            List<PageMessageEntity> pageBroadcastList = new List<PageMessageEntity>();
            if (currentUserRole == "90"|| currentUserRole == "80")
            {
                RecvResponse recvResponseBroadcast = messageRequest.Recv(token, "10", dateParam, "");
                if (recvResponseBroadcast.data != null)
                {
                    foreach (RecvResponseDataMessageEntity item in recvResponseBroadcast.data.data)
                    {
                        PageMessageEntity pageBroadcastEntity = new PageMessageEntity();
                        pageBroadcastEntity.id = item.id;
                        pageBroadcastEntity.userid = item.userid;
                        pageBroadcastEntity.ctime = item.ctime;
                        pageBroadcastEntity.msg = item.content;
                        pageBroadcastEntity.url = item.url;  //add by jack
                        if (item.label == string.Empty)
                        {
                            pageBroadcastEntity.label = "公告标题";
                        }
                        else
                        {
                            pageBroadcastEntity.label = item.label;
                        }
                        UserProfileResponse user = userRequest.profile(token, item.userid);
                        pageBroadcastEntity.username = user.data == null ? "OOO" : user.data.username;
                        pageBroadcastList.Add(pageBroadcastEntity);
                    }
                }
            }
            else
            {
                foreach (JoinedProjectEntity projectItem in projectList)
                {
                    RecvResponse recvResponseBroadcast = messageRequest.Recv(token, "10", dateParam, projectItem.id);
                    if (recvResponseBroadcast.data != null)
                    {
                        foreach (RecvResponseDataMessageEntity item in recvResponseBroadcast.data.data)
                        {
                            PageMessageEntity pageBroadcastEntity = new PageMessageEntity();
                            pageBroadcastEntity.id = item.id;
                            pageBroadcastEntity.userid = item.userid;
                            pageBroadcastEntity.ctime = item.ctime;
                            pageBroadcastEntity.msg = item.content;
                            pageBroadcastEntity.url = item.url;  //add by jack
                            if (item.label == string.Empty)
                            {
                                pageBroadcastEntity.label = "公告标题";
                            }
                            else
                            {
                                pageBroadcastEntity.label = item.label;
                            }
                            UserProfileResponse user = userRequest.profile(token, item.userid);
                            pageBroadcastEntity.username = user.data == null ? "OOO" : user.data.username;
                            pageBroadcastList.Add(pageBroadcastEntity);
                        }
                    }
                }
            }

            ViewBag.BroadcastList = pageBroadcastList;

            //取模板类型列表
            TplKindListResponse tplkindList = tplRequest.TplKindList(token, "1");
            ViewBag.tplkindList = tplkindList;

            //取第一个分类下的所有模板
            TplListResponse tpllistDefault = tplRequest.TplList(token, tplkindList.data[0].id, "");
            ViewBag.tpllistDefault = tpllistDefault;



            return View();
        }
        /// <summary>
        /// 3、	用户-PI/医生/病人/列表  之 获取医院病人列表
        /// </summary>
        /// <param name="token"></param>
        /// <param name="hospitalid"></param>
        /// <param name="page"></param>
        /// <param name="q"></param>
        public string getSearchHospitalPatient(string token, string hospitalid, string q)
        {
            try
            {
                string ApiResponse = string.Empty;
                HospitalUserProfileResponse hospitalUserProfileResponse = new HospitalUserProfileResponse();
                Dictionary<string, string> sPara = new Dictionary<string, string>();

                sPara.Add("hospitalid", hospitalid);
                sPara.Add("q", q);

                ApiResponse = F8YLSubmit.BuildGetRequest(sPara, "ucenter/user/search/patient?token=" + token);
                return ApiResponse;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public string GetUserProfileByHospitalId(string hospitalId)
        {
            string jsonUser = string.Empty;

            try
            {
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
                HospitalUserProfileResponse hupr = new HospitalUserProfileResponse();

                UserRequest ur = new UserRequest();
                hupr = ur.SearchUsers(token, hospitalId, "");
                if (hupr.data != null)
                {
                    hupr.data.data.Sort((left, right) => Convert.ToInt32(right.role) - Convert.ToInt32(left.role));//按角色由大到小排序

                    jsonUser = JsonHelper.SerializeObject(hupr);
                }
                else if (hupr.code == 0 && hupr.data == null)
                {
                    APIResponseBaseExtend res = new APIResponseBaseExtend
                    {
                        code = 0,
                        message = "无用户。",
                        data = null

                    };
                    jsonUser = JsonHelper.SerializeObject(res);
                }

            }
            catch (Exception ex)
            {

                AppLog.Instance.Write("GetUserProfileByHospitalId", AppLog.LogMessageType.Error, ex);
            }
            return jsonUser;
        }
        /// <summary>
        /// 3、	用户-PI/医生/病人/列表  之 获取医院病人列表
        /// </summary>
        /// <param name="token"></param>
        /// <param name="hospitalid"></param>
        /// <param name="page"></param>
        /// <param name="q"></param>
        public HospitalUserProfileResponse SearchHospitalPatient(string token, string hospitalid, string q)
        {
            try
            {
                string ApiResponse = string.Empty;
                HospitalUserProfileResponse hospitalUserProfileResponse = new HospitalUserProfileResponse();
                Dictionary<string, string> sPara = new Dictionary<string, string>();

                sPara.Add("hospitalid", hospitalid);
                sPara.Add("q", q);

                ApiResponse = F8YLSubmit.BuildGetRequest(sPara, "ucenter/user/search/patient?token=" + token);
                if (!ApiResponse.Contains("[]"))
                {
                    hospitalUserProfileResponse = JsonHelper.DeserializeJsonToObject<HospitalUserProfileResponse>(ApiResponse);
                }
                return hospitalUserProfileResponse;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //ProjectDetail
        public ActionResult ProjectDetail(string id)
        {
            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

            ViewBag.role = Session["role"] == null ? "XXXXXXX" : Session["role"].ToString();
            ViewBag.username = Session["username"] == null ? "XXXXXXX" : Session["username"].ToString();

            ViewBag.sessionHospitalid= Session["hospitalid"] == null ? "XXXXXXX" : Session["hospitalid"].ToString();


            //实体
            ProjectDetailResponse projectDetailResponse = new ProjectDetailResponse();

            HospitalRequest hospitalrequest = new HospitalRequest();

            HospitalSearchResponse response = new HospitalSearchResponse();//医院列表

            HospitalUserProfileResponse hospitalprofile = new HospitalUserProfileResponse();//医院pi

            ProjectTplListResponse tplListResponse = new ProjectTplListResponse();//模板列表

            TplDetailResponse tpldetailresponse = new TplDetailResponse();//模板详情

            //方法
            UserRequest userrequest = new UserRequest();
            ViewBag.UserFile = userrequest.profile(token, "").data;

            ProjectRequest projecttplrequest = new ProjectRequest();

            //TplRequest tplrequest = new TplRequest();

            List<HospitalUserProfileResponse> listhospitalfrofile = new List<HospitalUserProfileResponse>();

            string apiResponse = string.Empty;
            try
            {
                projectDetailResponse = projectRequest.detail(token, id);

                if (projectDetailResponse.data != null)
                {
                    if (projectDetailResponse.data.tpl == null)
                    {
                        projectDetailResponse.data.tpl = new List<ProjectDetailTplInfo>();
                    }

                    //模板列表
                    tplListResponse = projecttplrequest.ProjectTplList(token, projectDetailResponse.data.id, "", 1);

                    if (tplListResponse.data != null)
                    {
                        //列表detail
                        tpldetailresponse = tplrequest.TplDetail(token, tplListResponse.data.data[0].tplid);
                    }

                    //医院列表
                    response = hospitalrequest.Search(token, 1, "");

                    //医院pi
                    if (response.data != null)
                    {
                        foreach (HospitalEntity hs in response.data.data)
                        {
                            hospitalprofile = userrequest.SearchHospitalPi(token, hs.id, "");
                            if (hospitalprofile.data != null)
                            {
                                listhospitalfrofile.Add(hospitalprofile);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            ViewBag.TplList = tplListResponse;//tpllist
            ViewBag.hospitalSearch = response;//hospitallist
            ViewBag.hopital = listhospitalfrofile;//pi
            ViewBag.TplDetail = tpldetailresponse;//tpldetail
            ViewBag.Token = token;
            return View(projectDetailResponse);
        }