JsonResult GetFirstInterviewData(Guid id)
        {
            StudentInfoEntity  studentInfo = repository.StudentInfo.FirstOrDefault(s => s.StudentID == id);
            AppRelationsEntity appRelation = repository.AppRelation.FirstOrDefault(a => a.StudentID == id);
            SaleTrackEntity    SaleTrack;
            IEnumerable <SaleTrackParticipantsEntity> SaleTrackParticipants = null;

            if (repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == 1) != null)
            {
                SaleTrack             = repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == 1);
                SaleTrackParticipants = repository.SaleTrackParticipants.Where(s => s.SaleTrackID == SaleTrack.TrackItemID).Select(s => s);
            }
            else
            {
                SaleTrack = new SaleTrackEntity
                {
                    TrackItemID   = Guid.NewGuid(),
                    StudentID     = id,
                    Inputor       = HttpContext.User.Identity.Name,
                    StateName     = "初访",
                    TrackPattern  = TrackPattern.面谈,
                    TrackDate     = studentInfo.CreateTime.AddDays(1),
                    ToDo          = "了解客户需求,完成初访登记表!",
                    IsComplete    = TrackIsComplete.否,
                    SignIntention = appRelation.IsSign,
                    Remark        = ""
                };
                SaleTrackParticipants = Enumerable.Empty <SaleTrackParticipantsEntity>();
            }
            return(Json(new SaleTrackAjaxViewModel {
                SaleTrackItem = SaleTrack,
                SaleTrackParticipant = SaleTrackParticipants
            }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult FirstInterview(Guid id)
        {
            StudentInfoEntity studentInfo = repository.StudentInfo.FirstOrDefault(s => s.StudentID == id);
            SaleTrackEntity   SaleTrack;
            IEnumerable <SaleTrackParticipantsEntity> SaleTrackParticipants = null;

            if (repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == 1) != null)
            {
                SaleTrack             = repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == 1);
                SaleTrackParticipants = repository.SaleTrackParticipants.Where(s => s.SaleTrackID == SaleTrack.TrackItemID).Select(s => s);
            }
            else
            {
                SaleTrack = new SaleTrackEntity
                {
                    TrackItemID  = Guid.NewGuid(),
                    StudentID    = id,
                    Inputor      = "Admin",
                    StateName    = "初访",
                    TrackPattern = TrackPattern.面谈,
                    TrackDate    = studentInfo.CreateTime.AddDays(1),
                    ToDo         = "了解客户需求,完成初访登记表!",
                    IsComplete   = TrackIsComplete.否,
                    Remark       = "请输入备注信息"
                };
                SaleTrackParticipants = Enumerable.Empty <SaleTrackParticipantsEntity>();
            }
            return(View(new SaleTrackViewModel
            {
                StudentInfo = studentInfo,
                SaleTrack = SaleTrack,
                SaleTrackParticipants = SaleTrackParticipants,
                AdditionalIdentity = SaleParticipantIdentity.咨询顾问
            }));
        }
        public JsonResult SetGetFromDone(string id, string trackID, int trackNo, bool isSign, DateTime signDate, string getFrom)
        {
            Guid studentID = new Guid(id);
            Guid trackId;

            if (trackID == string.Empty)
            {
                trackId = Guid.NewGuid();
            }
            else
            {
                trackId = new Guid(trackID);
            }
            SaleTrackEntity trackItem = repository.SaleTrack.SingleOrDefault(s => s.StudentID == studentID && (s.TrackItemID == trackId || s.TrackNo == trackNo));

            trackItem.IsGetFromDone = true;
            trackItem.GetFromTrack  = getFrom;
            repository.SaveSaleTrack(trackItem);

            if (isSign == true)
            {
                AppRelationsEntity appRelation = repository.AppRelation.SingleOrDefault(s => s.StudentID == studentID);
                appRelation.IsSign        = IsSign.已签约;
                appRelation.SignDate      = signDate;
                appRelation.SignTrackItem = trackId;
                repository.SaveAppRelation(appRelation);
            }

            return(Json(new { StudentID = id, SetResult = true, SignResult = isSign }));
        }
        /// <summary>
        /// 检测学生的某个销售进度是否已完成
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="trackNo">销售序号</param>
        /// <returns></returns>
        public bool CheckGerFromDone(Guid studentId, int trackNo)
        {
            bool            isGetFromDone = false;
            SaleTrackEntity trackItem     = repository.SaleTrack.SingleOrDefault(s => s.StudentID == studentId && s.TrackNo == trackNo);

            isGetFromDone = trackItem.IsGetFromDone;
            return(isGetFromDone);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="id">学生ID</param>
        /// <param name="trackID">SaleTrackID</param>
        /// <returns></returns>
        public ViewResult GetFromInterview(Guid id, Guid trackID)
        {
            SaleTrackEntity   saleTrack   = repository.SaleTrack.SingleOrDefault(s => s.TrackItemID == trackID && s.StudentID == id);
            StudentInfoEntity studentInfo = repository.StudentInfo.SingleOrDefault(s => s.StudentID == id);

            ViewBag.StudentInfo = studentInfo;
            return(View(saleTrack));
        }
        public void SaveSaleTrack(SaleTrackEntity saleTrackItem)
        {
            SaleTrackEntity originSaleTrackItem = context.SaleTrack.SingleOrDefault(s => s.TrackItemID == saleTrackItem.TrackItemID);

            if (originSaleTrackItem == null)
            {
                int nextTrackNo = context.SaleTrack.Count(s => s.StudentID == saleTrackItem.StudentID) == 0 ? 1 : context.SaleTrack.Where(s => s.StudentID == saleTrackItem.StudentID).Max(s => s.TrackNo);
                saleTrackItem.TrackNo = (byte)nextTrackNo;
                context.SaleTrack.Add(saleTrackItem);
            }
            else
            {
                context.Entry(originSaleTrackItem).CurrentValues.SetValues(saleTrackItem);
            }

            context.SaveChanges();
        }
        /// <summary>
        /// 获取客户的访谈历史
        /// </summary>
        /// <param name="studentID"></param>
        /// <returns></returns>
        public JsonResult GetSaleTrackHistory(string studentID)
        {
            if (studentID == null || studentID == string.Empty)
            {
                return(Json(false, JsonRequestBehavior.AllowGet));
            }

            Guid id = new Guid(studentID);

            List <SaleTrackAjaxViewModel> historyList = new List <SaleTrackAjaxViewModel>();

            IEnumerable <SaleTrackParticipantsEntity>[] participantsArray = null;
            IEnumerable <SaleTrackEntity> saleTrackEntityHistory          = repository.SaleTrack.Where(s => s.StudentID == id).OrderBy(s => s.TrackNo);

            if (saleTrackEntityHistory.Count() > 0)
            {
                participantsArray = new IEnumerable <SaleTrackParticipantsEntity> [saleTrackEntityHistory.Count()];
                for (int i = 0; i < saleTrackEntityHistory.Count(); i++)
                {
                    SaleTrackEntity item = saleTrackEntityHistory.ElementAt(i);
                    participantsArray[i] = repository.SaleTrackParticipants.Where(s => s.SaleTrackID == item.TrackItemID);
                    historyList.Add(new SaleTrackAjaxViewModel {
                        SaleTrackItem = item, SaleTrackParticipant = participantsArray[i]
                    });
                }
            }

            //以下写法会造成SaleTrackHistory中出现一样的ParticipantList
            //foreach (SaleTrackEntity item in repository.SaleTrack.Where(s=>s.StudentID == id).OrderBy(s=>s.TrackNo))
            //{
            //    IEnumerable<SaleTrackParticipantsEntity> participants = repository.SaleTrackParticipants.Where(s => s.SaleTrackID == item.TrackItemID);
            //    historyList.Add(new SaleTrackAjaxViewModel { SaleTrackItem = item, SaleTrackParticipant = participants });
            //    participants = null;
            //}

            StudentInfoEntity studentInfo = repository.StudentInfo.SingleOrDefault(s => s.StudentID == id);

            return(Json(new { StudentInfo = studentInfo, SaleTrackHistory = historyList }, JsonRequestBehavior.AllowGet));
        }
        public void SaveSaleTrack(SaleTrackEntity saleTrackItem, IEnumerable <SaleTrackParticipantsEntity> saleTrackParticipants)
        {
            SaleTrackEntity originSaleTrackItem = context.SaleTrack.SingleOrDefault(s => s.TrackItemID == saleTrackItem.TrackItemID);

            if (originSaleTrackItem == null)
            {
                int nextTrackNo = context.SaleTrack.Count(s => s.StudentID == saleTrackItem.StudentID) == 0 ? 1 : context.SaleTrack.Where(s => s.StudentID == saleTrackItem.StudentID).Max(s => s.TrackNo) + 1;
                saleTrackItem.TrackNo = (byte)nextTrackNo;
                context.SaleTrack.Add(saleTrackItem);
            }
            else
            {
                context.SaleTrackParticipants.RemoveRange(context.SaleTrackParticipants.Where(s => s.SaleTrackID == originSaleTrackItem.TrackItemID).Select(s => s));
                context.Entry(originSaleTrackItem).CurrentValues.SetValues(saleTrackItem);
            }
            if (saleTrackParticipants != null && saleTrackParticipants.Count() > 0)
            {
                context.SaleTrackParticipants.AddRange(saleTrackParticipants);  //添加参与人
            }

            context.SaveChanges();
        }
        public JsonResult SetGetFromDone(GetFromInterviewModel ajaxData)
        {
            Guid trackId;
            Guid studentID;

            if (ajaxData.trackID == string.Empty || ajaxData.trackID == null)
            {
                return(Json(false));
            }

            trackId = new Guid(ajaxData.trackID);
            SaleTrackEntity trackItem = repository.SaleTrack.SingleOrDefault(s => s.TrackItemID == trackId);

            studentID = trackItem.StudentID;
            trackItem.IsGetFromDone  = true;
            trackItem.GetFromTrack   = ajaxData.getFrom;
            trackItem.ClientMostCare = ajaxData.clientMostCare;
            trackItem.SignIntention  = ajaxData.isSign;
            repository.SaveSaleTrack(trackItem);


            AppRelationsEntity appRelation = repository.AppRelation.SingleOrDefault(s => s.StudentID == studentID);

            if (ajaxData.isSign == IsSign.已签约)
            {
                appRelation.IsSign        = IsSign.已签约;
                appRelation.SignDate      = ajaxData.signDate;
                appRelation.SignTrackItem = trackId;
            }
            else
            {
                appRelation.IsSign = trackItem.SignIntention;
            }
            repository.SaveAppRelation(appRelation);

            return(Json(true));
        }
        public ViewResult CommonInterview(Guid id, int TrackNo)
        {
            StudentInfoEntity studentInfo = repository.StudentInfo.FirstOrDefault(s => s.StudentID == id);
            SaleTrackEntity   SaleTrack;
            IEnumerable <SaleTrackParticipantsEntity> SaleTrackParticipants = null;

            if (repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == TrackNo) != null)
            {
                SaleTrack             = repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == TrackNo);
                SaleTrackParticipants = repository.SaleTrackParticipants.Where(s => s.SaleTrackID == SaleTrack.TrackItemID).Select(s => s);
            }
            else
            {
                DateTime nextDate = repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == TrackNo - 1).TrackDate;
                SaleTrack = new SaleTrackEntity
                {
                    TrackItemID  = Guid.NewGuid(),
                    StudentID    = id,
                    Inputor      = "Admin",
                    StateName    = "第" + TrackNo.ToString() + "次回访",
                    TrackNo      = (byte)TrackNo,
                    TrackPattern = TrackPattern.面谈,
                    TrackDate    = nextDate.AddDays(1),
                    ToDo         = "第" + TrackNo.ToString() + "次回访",
                    IsComplete   = TrackIsComplete.否,
                    Remark       = "请输入备注信息 "
                };
                SaleTrackParticipants = Enumerable.Empty <SaleTrackParticipantsEntity>();
            }
            return(View(new SaleTrackViewModel
            {
                StudentInfo = studentInfo,
                SaleTrack = SaleTrack,
                SaleTrackParticipants = SaleTrackParticipants,
                AdditionalIdentity = SaleParticipantIdentity.咨询顾问
            }));
        }
        private bool SetEmail(Guid trackItemId)
        {
            bool setResult = true;

            //获取EmailConfig
            XDocument emailConfig = XDocument.Load(Server.MapPath(Url.Content("~/Content/xml/EmailConfig.xml")));

            SaleTrackEntity currentTrackItem =
                repository.SaleTrack
                .SingleOrDefault(s => s.TrackItemID == trackItemId);
            Guid studentId = currentTrackItem.StudentID;
            IEnumerable <SaleTrackParticipantsEntity> currentParticipants =
                repository.SaleTrackParticipants
                .Where(s => s.SaleTrackID == trackItemId)
                .Select(s => s);

            //问候语句,从xml处读取,方便随时更改,stringFormat参数化的;{0}:参与人名字,{1}:访谈时间
            //(看来也可以做到管理员管理的内容中,note by sam, 2014.03.16)
            string greetStr = emailConfig.Descendants("EmailGreet").SingleOrDefault().Value;

            #region 邮件正文

            StringBuilder curInterviewContent = new StringBuilder();
            curInterviewContent.Append("<p>&nbsp;</p><p>");
            curInterviewContent.AppendFormat("回访阶段名:{0},回访日期:{1}",
                                             currentTrackItem.StateName, currentTrackItem.TrackDate.ToString("yyyy/MM/dd HH:mm"));
            curInterviewContent.Append("<br/>");
            curInterviewContent.Append("回访参与人:");

            int participantCount = currentParticipants.Count();
            foreach (SaleTrackParticipantsEntity participant in currentParticipants)
            {
                curInterviewContent.AppendFormat("{0}({1});", participant.ParticipantName, participant.ParticipantIdentity);
            }
            curInterviewContent.Append("<br/>");
            curInterviewContent.AppendFormat("回访内容:{0}</p>", currentTrackItem.ToDo);

            if (currentTrackItem.TrackNo != 1)
            {
                curInterviewContent.Append("<p>历史回访记录:<br/>");
                IEnumerable <SaleTrackEntity> historyTrackItem =
                    repository.SaleTrack
                    .Where(s => s.TrackNo < currentTrackItem.TrackNo && s.StudentID == studentId)
                    .Select(s => s);
                foreach (SaleTrackEntity trackItem in historyTrackItem)
                {
                    curInterviewContent.AppendFormat("回访阶段名:{0},回访日期:{1},获得信息:{2}<br/>",
                                                     trackItem.StateName, trackItem.TrackDate.ToString("yyyy/MM/dd HH:mm"), trackItem.GetFromTrack);
                }
                curInterviewContent.Append("</p>");
            }

            curInterviewContent.AppendFormat("<p>&nbsp;</p><p>APP邮件提醒助手<br/>{0}</p>", DateTime.Now.ToLongDateString());

            #endregion

            string title      = emailConfig.Descendants("EmailTitle").SingleOrDefault().Value;   //邮件标题
            string strHost    = emailConfig.Descendants("EmailHost").SingleOrDefault().Value;    //邮件smtp服务器
            string strAccount = emailConfig.Descendants("EmailAccount").SingleOrDefault().Value; //发送方邮箱账号
            string strPwd     = emailConfig.Descendants("EmailPwd").SingleOrDefault().Value;     //发送方邮箱密码
            string strFrom    = emailConfig.Descendants("EmailFrom").SingleOrDefault().Value;    //发件人邮箱地址

            //bool emailResult = sendMail(strTo1, strTo2, title, content, strHost, strAccount, strPwd, strFrom);
            //return emailResult;

            bool sendResult = true;
            foreach (SaleTrackParticipantsEntity participant in currentParticipants
                     .Where(p => p.ParticipantIdentity == SaleParticipantIdentity.顾问助理 || p.ParticipantIdentity == SaleParticipantIdentity.咨询顾问)
                     .Select(p => p))
            {
                sendResult = sendMail(participant.ParticipantEmail, title,
                                      string.Format(greetStr, participant.ParticipantName, DateTime.Now.ToLongDateString()) + curInterviewContent,
                                      strHost, strAccount, strPwd, strFrom);
                if (!sendResult)
                {
                    setResult = false;
                }
                break;
            }
            return(setResult);
        }
        /// <summary>
        /// 获取所有销售数据
        /// </summary>
        /// <returns></returns>
        public JsonResult List(string sort, string keyword)
        {
            string userName = HttpContext.User.Identity.Name;
            PermissionPValueEntity userPermission = userInfoRepository.GetUserPermission(userName);

            IEnumerable <StudentInfoViewModel> StudentsInfo      = null;
            List <SaleTrackListItemModel>      SaleTrackList     = new List <SaleTrackListItemModel>();
            SaleTrackListItemModel             saleTrackListItem = null;

            if (sort == "学生" && keyword != "")
            {
                StudentsInfo = repository.StudentInfo
                               .Where(s => s.NameCn == keyword)
                               .Join(repository.AppRelation, s => s.StudentID, a => a.StudentID, (s, a) => new StudentInfoViewModel {
                    StudentInfo = s, AppRelation = a
                });
            }
            else if (sort == "销售负责人" && keyword != "")
            {
                Guid saleConsultantID = repository.UserInfo.FirstOrDefault(u => u.UserNameCn.Contains(keyword)).UserID;
                StudentsInfo = repository.AppRelation
                               .Where(a => a.SaleConsultant == saleConsultantID)
                               .Join(repository.StudentInfo, a => a.StudentID, s => s.StudentID, (a, s) => new StudentInfoViewModel {
                    StudentInfo = s, AppRelation = a
                });
            }
            else if (sort == "已签约")
            {
                StudentsInfo = repository.AppRelation
                               .Where(a => a.IsSign == IsSign.已签约)
                               .Join(repository.StudentInfo, a => a.StudentID, s => s.StudentID, (a, s) => new StudentInfoViewModel {
                    AppRelation = a, StudentInfo = s
                })                                                                                                                                               //调用Join函数,连结两个集合,返回一个包对象
                               .OrderBy(r => r.StudentInfo.NameCn);
            }
            else if (sort == "未签约")
            {
                StudentsInfo = repository.AppRelation
                               .Where(a => a.IsSign != IsSign.已签约)
                               .Join(repository.StudentInfo, a => a.StudentID, s => s.StudentID, (a, s) => new StudentInfoViewModel {
                    AppRelation = a, StudentInfo = s
                })                                                                                                                                              //调用Join函数,连结两个集合,返回一个包对象
                               .OrderBy(r => r.StudentInfo.NameCn);
            }
            else
            {
                StudentsInfo = repository.StudentInfo
                               .Join(repository.AppRelation, s => s.StudentID, a => a.StudentID, (s, a) => new StudentInfoViewModel {
                    AppRelation = a, StudentInfo = s
                })                                                                                                                                                   //调用Join函数,连结两个集合,返回一个包对象
                               .OrderByDescending(r => r.AppRelation.IsSign);
            }

            //如果不允许展示全部销售,就只展示自己负责销售的学生
            if (!userPermission.IsSaleListAll)
            {
                StudentsInfo = StudentsInfo.Where(s => s.AppRelation.SaleConsultantName == userName);
            }

            foreach (StudentInfoViewModel studentInfoViewModel in StudentsInfo)
            {
                saleTrackListItem = new SaleTrackListItemModel
                {
                    StudentInfo = studentInfoViewModel.StudentInfo,
                    AppRelation = studentInfoViewModel.AppRelation
                };
                Guid            id = studentInfoViewModel.StudentInfo.StudentID;
                int             maxTrackNo;
                SaleTrackEntity saleTrackEntity = null;

                if (repository.SaleTrack.Where(s => s.StudentID == id).Count() <= 0)
                {
                    maxTrackNo      = 0;
                    saleTrackEntity = new SaleTrackEntity
                    {
                        TrackItemID   = Guid.Empty,
                        StudentID     = id,
                        TrackNo       = 0,
                        Inputor       = HttpContext.User.Identity.Name,
                        StateName     = "未开始",
                        TrackPattern  = TrackPattern.电话,
                        TrackDate     = studentInfoViewModel.StudentInfo.CreateTime,
                        ToDo          = "客户主动咨询",
                        IsComplete    = TrackIsComplete.否,
                        SignIntention = studentInfoViewModel.AppRelation.IsSign,
                        Remark        = ""
                    };
                }
                else
                {
                    maxTrackNo      = repository.SaleTrack.Where(s => s.StudentID == id).Max(s => s.TrackNo);
                    saleTrackEntity = repository.SaleTrack.SingleOrDefault(s => s.StudentID == id && s.TrackNo == maxTrackNo);
                }
                saleTrackListItem.CurrentSaleTrack = saleTrackEntity;
                saleTrackListItem.FromCollection   = repository.StudentFrom.Where(s => s.StudentID == id);

                SaleTrackList.Add(saleTrackListItem);
            }

            return(Json(SaleTrackList, JsonRequestBehavior.AllowGet));
        }