/// <summary>
        /// 给某个员工发消息,到某个app上
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="lillyId"></param>
        /// <param name="email"></param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public JsonResult SendMessageToEmployee(string encriptedPackage)
        {
            // 先解密成json
            log.Debug("encriptedPackage is {0}", encriptedPackage);
            var rDel = new RijndaelManaged();

            rDel.Key     = keyArray;
            rDel.Mode    = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();

            var toEncryptArray = Convert.FromBase64String(encriptedPackage);

            byte[] resultArray   = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            var    decriptedJson = UTF8Encoding.UTF8.GetString(resultArray);

            log.Debug("decriptedJson is {0}", decriptedJson);

            var responseResult = new ResponseResult();

            // 反序列化内容
            var o = JsonHelper.FromJson <SendMessageToEmployeeData>(decriptedJson);

            // 如果没有lillyid的话必须要邮箱,根据邮箱查人的lillyid
            string lillyId = "";

            if (string.IsNullOrEmpty(o.LillyId))
            {
                if (string.IsNullOrEmpty(o.Email))
                {
                    responseResult.responseCode = "1201";
                    responseResult.remark       = "At lease one of Lilly ID and Email must exist!";
                    return(Json(responseResult));
                }
                var emp = WeChatCommonService.lstUser.FirstOrDefault(a => a.email.ToUpper() == o.Email.ToUpper());
                if (emp == null)
                {
                    responseResult.responseCode = "1202";
                    responseResult.remark       = "Can't find employee by given email!";
                    return(Json(responseResult));
                }
                lillyId = emp.userid;
            }
            else
            {
                lillyId = o.LillyId;
            }


            // Send Message
            var sendMsgResult = WechatCommon.SendMsg(int.Parse(o.AppId), "text", lillyId, "", "", o.Message, null);

            if (sendMsgResult.errcode != Weixin.ReturnCode_QY.请求成功)
            {
                responseResult.responseCode = sendMsgResult.errcode.ToString();
                responseResult.remark       = sendMsgResult.errmsg;
            }

            return(Json(responseResult));
        }
        public JsonResult PostFeedback(QuestionManageView objModal, string Id)
        {
            //验证错误
            if (!BeforeAddOrUpdate(objModal, Id) || !ModelState.IsValid)
            {
                return(Json(GetErrorJson(), JsonRequestBehavior.AllowGet));
            }

            objModal.Status    = "已回答";
            objModal.AUsername = User.Identity.GetUserName();
            _objService.UpdateView(objModal);
            string reply    = "";
            var    BaseUrl  = CommonService.GetSysConfig("WeChatUrl", "");
            var    question = objModal.Question;

            if (question.Length > 20)
            {
                question = question.Substring(0, 20) + "。。。";
            }
            var fBConfig = QAFBConfig(objModal.Category);

            if (fBConfig == null)
            {
                return(ErrorNotification("QAFeedback字段值为空"));
            }
            reply = string.Format("{0}\r\n{1}:{2}\r\n{3}:{4}", fBConfig.AContent1, fBConfig.AContent2, question, fBConfig.AContent3, objModal.Answer);

            WechatCommon.SendMsg(objModal.AppId.GetValueOrDefault(), "text", objModal.QUserName, "", "", reply, null);

            return(Json(doJson(null), JsonRequestBehavior.AllowGet));
        }
        public JsonResult Commit(QuestionManageView objModal, string Id)
        {
            objModal.Status    = "待确认";
            objModal.AUsername = User.Identity.Name;

            if (objModal.Answer == null || objModal.Answer == "")
            {
                objModal.Answer = "待确认" + "<br/>" + "您好,您的问题已收到,我们正在为您跟进。请耐心等待,谢谢!";
            }
            else
            {
                objModal.Answer = "待确认" + "<br/>" + objModal.Answer;
            }
            _objService.UpdateView(objModal);

            string reply     = "";
            var    BaseUrl   = CommonService.GetSysConfig("WeChatUrl", "");
            int    compareId = int.Parse(Id);
            var    question  = _objService.Repository.Entities.Where(x => x.Id == compareId).AsNoTracking().FirstOrDefault();

            reply = string.Format("您好,您的问题已收到,我们正在为您跟进。请耐心等待,谢谢!");
            if (question != null)
            {
                WechatCommon.SendMsg(objModal.AppId.GetValueOrDefault(), "text", question.QUserName, "", "", reply, null);
            }
            return(Json(doJson(null), JsonRequestBehavior.AllowGet));
        }
        public JsonResult CommonPost(QuestionManageView objModal, string Id)
        {
            //验证错误
            if (!BeforeAddOrUpdate(objModal, Id) || !ModelState.IsValid)
            {
                return(Json(GetErrorJson(), JsonRequestBehavior.AllowGet));
            }
            objModal.Status    = "已回答";
            objModal.AUsername = User.Identity.Name;
            _objService.UpdateView(objModal);

            string reply     = "";
            var    BaseUrl   = CommonService.GetSysConfig("WeChatUrl", "");
            int    compareId = int.Parse(Id);
            var    question  = _objService.Repository.Entities.Where(x => x.Id == compareId).AsNoTracking().FirstOrDefault();
            var    qConfig   = QuesitonConfig(question.Category);

            if (qConfig == null)
            {
                return(ErrorNotification("QuesitonConfig字段值为空"));
            }
            objModal.questionConfig = qConfig;
            reply = string.Format(objModal.questionConfig.questionContent, string.Format("<a href=\"{0}/hreservice/QuestionManage/Index?appId={1}&category={2}\">" + objModal.questionConfig.questionPara + "</a>", BaseUrl, objModal.AppId, question.Category));
            if (question != null)
            {
                WechatCommon.SendMsg(objModal.AppId.GetValueOrDefault(), "text", question.QUserName, "", "", reply, null);
            }
            return(Json(doJson(null), JsonRequestBehavior.AllowGet));
        }
        private void Sendmessage(int id, PollingEntity poll, decimal score)
        {
            var lillyid = ViewBag.LillyId;

            if (!string.IsNullOrEmpty(id.ToString()) && id != 0)
            {
                //var poll = _objService.Repository.Entities.FirstOrDefault(a => a.Id == id&& a.IsDeleted!=true);
                var reply = string.Empty;
                if (!string.IsNullOrEmpty(poll.ReplyMessage))
                {
                    if (poll.ReplyMessage.IndexOf("{0}") > 0)
                    {
                        reply = poll.ReplyMessage.Replace("{0}", score.ToString());
                    }
                    else
                    {
                        LogManager.GetLogger("wechat").Debug <string>("有奖问答回复文字应包含{0}");
                    }
                }


                //如果分数符合要求则发消息
                if (!string.IsNullOrEmpty(poll.AppId) && !string.IsNullOrEmpty(poll.ReplyMessage))
                {
                    LogManager.GetLogger("wechat").Debug <string>("reply=" + reply);
                    LogManager.GetLogger("wechat").Debug <string>("lillyid=" + lillyid);
                    WechatCommon.SendMsg(int.Parse(poll.AppId), "text", lillyid, "", "", reply, null);
                }
            }
        }
        private void GetThreeData(string lillyId, string threeDate)
        {
            var resultViewModel = new OnboardChiefView.EmployeesView();
            //第3天入职
            var threeList = _onboardService.GetEmployeesView(lillyId, threeDate);

            resultViewModel.List = threeList ?? new List <OnboardChiefView.EmployeesView>();
            if (resultViewModel.List.Count > 0)
            {
                foreach (var item in GetManagerLillyIDs(resultViewModel.List))
                {
                    string url = string.Format("<a href=\"{0}/sad/onboard/OnboardIndex?appId={1}&OnboardDate={2}\">戳这里</a>", BaseUrl, appId, threeDate);
                    reply = threeMsg.Replace("{#URL}", url);
                    WechatCommon.SendMsg(int.Parse(appId), "text", item.SupervisorLillyID, "", "", reply, null);
                    _Logger.Debug <string>(string.Format("week managerID:{0},address:{1}", item.SupervisorLillyID, reply));
                }
            }
        }
        private void SendWeChatMessage(MeetingRoomRepairView objModel, int id)
        {
            //string reply = string.Empty;
            //string msg = string.Empty;
            var    appId             = CommonService.GetSysConfig("RoomRepairAppID", "");
            string RoomRepairLillyID = CommonService.GetSysConfig("RoomRepairLillyID", "");
            var    jsonString        = CommonService.GetSysConfig("RoomRepairEmail", "");
            string repairType        = CommonService.GetSysConfig("RepairType", "");
            var    js                         = new JavaScriptSerializer();
            object jsonResult                 = null;
            var    WechatMessageTemplate      = "";
            var    WechatMessageTemplateOther = "";

            jsonResult = js.Deserialize <object>(jsonString);
            var fullResult = jsonResult as Dictionary <string, object>;

            if (fullResult != null)
            {
                WechatMessageTemplate      = fullResult["WechatMessageTemplate"] as string;
                WechatMessageTemplateOther = fullResult["WechatMessageTemplate-other"] as string;
            }

            if (objModel != null)
            {
                if (string.IsNullOrEmpty(objModel.Comment))
                {
                    WechatMessageTemplate = WechatMessageTemplate.Replace("#Location#", objModel.Location).Replace("#RoomName#", objModel.RoomName).Replace("#Type#", RepairType(objModel.Type))
                                            .Replace("#RepairID#", objModel.CreatedUserID).Replace("#RepairName#", objModel.RepairName).Replace("#RepairEmail#", objModel.RepairEmail);

                    _Logger.Error <string>(string.Format("SendWeChatMessage_MeetingRoomRepair:{0}", WechatMessageTemplate));
                    WechatCommon.SendMsg(int.Parse(appId), "text", RoomRepairLillyID, "", "", WechatMessageTemplate, null);
                }
                else
                {
                    WechatMessageTemplateOther = WechatMessageTemplateOther.Replace("#Location#", objModel.Location).Replace("#RoomName#", objModel.RoomName).Replace("#Type#", RepairType(objModel.Type))
                                                 .Replace("#RepairID#", objModel.CreatedUserID).Replace("#RepairName#", objModel.RepairName).Replace("#RepairEmail#", objModel.RepairEmail).Replace("#RepairComment#", objModel.Comment);

                    _Logger.Error <string>(string.Format("SendWeChatMessage_MeetingRoomRepair:{0}", WechatMessageTemplateOther));
                    WechatCommon.SendMsg(int.Parse(appId), "text", RoomRepairLillyID, "", "", WechatMessageTemplateOther, null);
                }
            }
        }
        public JsonResult PostTicket(QuestionManageView objModal, string Id)
        {
            //验证错误
            if (!BeforeTicketAddOrUpdate(objModal, Id) || !ModelState.IsValid)
            {
                return(Json(GetErrorJson(), JsonRequestBehavior.AllowGet));
            }

            objModal.Status    = "已开单";
            objModal.AUsername = User.Identity.Name;
            _objService.UpdateView(objModal);
            string reply   = "";
            var    BaseUrl = CommonService.GetSysConfig("WeChatUrl", "");

            reply = string.Format("您好,您的问题已回复,快点开{0}查看单号吧!", string.Format("<a href=\"{0}/hreservice/QuestionManage/raiseticket?appid={1}&category={2}\">提交问题</a>", BaseUrl, objModal.AppId, objModal.Category));

            WechatCommon.SendMsg(objModal.AppId.GetValueOrDefault(), "text", objModal.QUserName, "", "", reply, null);

            return(Json(doJson(null), JsonRequestBehavior.AllowGet));
        }
        private void GetCurrentData(string lillyId, string currentDate)
        {
            var resultViewModel = new OnboardChiefView.EmployeesView();
            //当天入职
            var currentList = _onboardService.GetEmployeesView(lillyId, currentDate);

            resultViewModel.List = currentList ?? new List <OnboardChiefView.EmployeesView>();

            if (resultViewModel.List.Count > 0)
            {
                foreach (var item in GetManagerLillyIDs(resultViewModel.List))
                {
                    string url      = string.Format("<a href=\"{0}/sad/onboard/OnboardIndex?appId={1}&OnboardDate={2}\">戳这里</a>", BaseUrl, appId, currentDate);
                    string file_url = string.Format("<a href=\"{0}/content/img/" + fileName.Trim() + "\">此处</a>", BaseUrl);
                    reply = currentMsg.Replace("{#URL}", url).Replace("{#File_URL}", file_url);

                    WechatCommon.SendMsg(int.Parse(appId), "text", item.SupervisorLillyID, "", "", reply, null);
                    _Logger.Debug <string>(string.Format("current managerID:{0},address:{1}", item.SupervisorLillyID, reply));
                }
            }
        }
        public override JsonResult Post(QuestionManageView objModal, string Id)
        {
            //验证错误
            if (!BeforeAddOrUpdate(objModal, Id) || !ModelState.IsValid)
            {
                return(Json(GetErrorJson(), JsonRequestBehavior.AllowGet));
            }
            objModal.Status    = "已回答";
            objModal.AUsername = User.Identity.Name;
            _objService.UpdateView(objModal);

            string reply     = "";
            var    BaseUrl   = CommonService.GetSysConfig("WeChatUrl", "");
            int    compareId = int.Parse(Id);
            var    question  = _objService.Repository.Entities.Where(x => x.Id == compareId).AsNoTracking().FirstOrDefault();

            reply = string.Format("您好,您的问题小礼已回复,快点开{0}查看答案吧!", string.Format("<a href=\"{0}/hreservice/QuestionManage/RaiseQuestion?appId={1}\">在线服务</a>", BaseUrl, objModal.AppId));
            if (question != null)
            {
                WechatCommon.SendMsg(objModal.AppId.GetValueOrDefault(), "text", question.QUserName, "", "", reply, null);
            }
            return(Json(doJson(null), JsonRequestBehavior.AllowGet));
        }
        private void SendWeChatMessage(int id, string type)
        {
            string reply          = string.Empty;
            string msg            = string.Empty;
            var    BaseUrl        = CommonService.GetSysConfig("WeChatUrl", "");
            var    appId          = CommonService.GetSysConfig(HseAppIdKey, "");
            var    managerLillyId = CommonService.GetSysConfig("HSELillyID", "");

            if (type == "SafetyTrouble")
            {
                msg   = "您好,微信中有员工已提交安全隐患事件,请尽快查看。谢谢!点击";
                reply = string.Format(msg + "<a href=\"{0}/HSE/HseAccident/SafetyTroubleDetail?appid={1}&id={2}\">这里</a>打开报告",
                                      BaseUrl, appId, id);
            }
            else
            {
                msg   = "您好,微信中有员工已提交安全汇报事件,请尽快查看。谢谢!点击";
                reply = string.Format(msg + "<a href=\"{0}/HSE/HseAccident/SafetyIncidentDetail?appid={1}&id={2}\">这里</a>打开报告",
                                      BaseUrl, appId, id);
            }

            WechatCommon.SendMsg(int.Parse(appId), "text", managerLillyId, "", "", reply, null);
            _Logger.Error <string>(string.Format("SendWeChatMessage_safetyReport:{0}", reply));
        }