/// <summary>
        /// 修改開始結束日期資訊
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="circleKey"></param>
        /// <returns></returns>
        public bool UpdateByCircleKey(DateTime startDate, DateTime endDate, string circleKey)
        {
            var learningcircleService = new LearningCircleService();
            var learningcircleInfo    = learningcircleService.GetDetailByOuterKey(circleKey);

            if (learningcircleInfo == null)
            {
                return(false);
            }
            var dateTimeTools = new Utility.DateTimeTools();
            var datas         = dateTimeTools.Getdata(learningcircleInfo.StartDate.Value.ToLocalTime(), learningcircleInfo.EndDate.Value.ToLocalTime(), circleKey);
            //  var removeDatas   = datas.Where(t => t.StartDate.Value < startDate || t.EndDate.Value > endDate).ToList();
            var db          = _uow.DbContext;
            var dbData      = db.TimeTable.Where(t => t.Course_No == circleKey).ToList(); //取得DB目前所有上課時間地點資訊
            var allData     = dbData.Union(datas);                                        //聯集需求的日期
            var deleteDatas = allData.Except(datas);                                      //找出不是需求的日期 -與DB資料的差集
            //找出不是需求的日期
            var removeDatas = db.TimeTable.Where(t => t.Course_No == circleKey && (t.StartDate.Value <startDate || t.EndDate.Value> endDate)).ToList();

            removeDatas = removeDatas.Union(deleteDatas).ToList(); //聯集需要刪除的資料
            //刪除更新設定區間外的日期
            db.TimeTable.RemoveRange(removeDatas);
            //取得最初的開始日期
            var firstTimeTable = datas.OrderBy(t => t.StartDate).FirstOrDefault();
            //取得最後的結束日期
            var lastTimeTable    = datas.OrderByDescending(t => t.EndDate).FirstOrDefault();
            var weekTableService = new WeekTableService();
            var weekTableInfo    = weekTableService.GetByCirclekey(circleKey);
            var weekOfDays       = weekTableInfo.WeekTable.Select(t => t.StartTime.ToLocalTime().DayOfWeek.ToString()).OrderBy(t => t);

            //新增DB所沒有的startDate日期區塊
            if (firstTimeTable.StartDate.HasValue && (firstTimeTable.StartDate.Value >= startDate))
            {
                var insertStartDates = new List <TimeTable>();

                var diffStartDay = (firstTimeTable.StartDate.Value - startDate).Days + 1;
                for (var firstDay = 1; firstDay < diffStartDay; firstDay++)
                {
                    var lastEntity = new TimeTable()
                    {
                        ClassRoom = firstTimeTable.ClassRoom,
                        ClassTime = firstTimeTable.ClassTime,
                        Course_No = firstTimeTable.Course_No,
                        Course_Id = firstTimeTable.Course_Id,
                        EndDate   = firstTimeTable.EndDate,
                        StartDate = firstTimeTable.StartDate
                    };
                    var lastStartDate = lastEntity.StartDate.Value.AddDays(-(firstDay));
                    var lastEndDate   = lastEntity.EndDate.Value.AddDays(-(firstDay));
                    if (weekOfDays.FirstOrDefault(t => t == lastStartDate.DayOfWeek.ToString()) != null)
                    {
                        lastEntity.StartDate = lastStartDate;
                        lastEntity.EndDate   = lastEndDate;
                        db.TimeTable.Add(lastEntity);
                    }
                }
            }

            //新增DB所沒有的endDate日期 區塊
            if (lastTimeTable.EndDate.HasValue && (lastTimeTable.EndDate.Value >= endDate))
            {
                var insertStartDates = new List <TimeTable>();
                var diffEndDay       = (endDate - lastTimeTable.EndDate.Value).Days + 1;
                for (var endDay = 1; endDay <= diffEndDay; endDay++)
                {
                    var nextEntity = new TimeTable()
                    {
                        ClassRoom = lastTimeTable.ClassRoom,
                        ClassTime = lastTimeTable.ClassTime,
                        Course_No = lastTimeTable.Course_No,
                        Course_Id = lastTimeTable.Course_Id,
                        EndDate   = lastTimeTable.EndDate,
                        StartDate = lastTimeTable.StartDate
                    };;
                    var nextStartDate = nextEntity.StartDate.Value.AddDays(endDay);
                    var nextEndDate   = nextEntity.EndDate.Value.AddDays(endDay);
                    if (weekOfDays.FirstOrDefault(t => t == nextStartDate.DayOfWeek.ToString()) != null)
                    {
                        nextEntity.StartDate = nextStartDate;
                        nextEntity.EndDate   = nextEndDate;
                        db.TimeTable.Add(nextEntity);
                    }
                }
            }

            //補上新加入的weeks資料
            foreach (var data in datas)
            {
                var checkData = dbData.FirstOrDefault(t => t.StartDate.Value.Date == data.StartDate.Value.Date);
                if (checkData != null)
                {
                    continue;
                }

                db.TimeTable.Add(data);
            }


            db.SaveChanges();
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// 建立邀請碼資料
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public bool Create(MemberInvitePostRequest requestData)
        {
            var inviteEmails = requestData.InviteEmail.ToList();

            if (inviteEmails.FirstOrDefault() == null)
            {
                return(false);
            }

            var db                    = _uow.DbContext;
            var captchaHelp           = new Utility.CaptchaHelper();
            var mails                 = new List <MemberInvite>();
            var learningCircleService = new LearningCircleService();
            var learningCircleInfo    = learningCircleService.GetDetailByOuterKey(requestData.CircleKey);

            if (learningCircleInfo == null)
            {
                return(false);
            }

            var organizationInfo = db.Organizations.FirstOrDefault(t => t.Id == learningCircleInfo.OrgId);
            var flipusOrgName    = ConfigurationManager.AppSettings["FlipusOrgName"].ToString();
            var inviteDomain     = ConfigurationManager.AppSettings["loginServer"].ToString();

            foreach (var inviteEmail in inviteEmails)
            {
                #region 將舊的code 設定失效
                var setOldInviteCode = db.MemberInvite.Where(t => t.InviteEmail == inviteEmail && t.CircleKey == requestData.CircleKey && t.Enable == true);
                if (setOldInviteCode != null && setOldInviteCode.Count() > 0)
                {
                    foreach (var oldInvite in setOldInviteCode)
                    {
                        oldInvite.Enable = false;
                    }
                }
                #endregion

                var inviteCode = captchaHelp.GenerateRandomText(10).ToLower();
                var inviteUrl  = HttpUtility.UrlEncode(string.Format("{3}/FlipusWeb/invite?r=3&o={0}&c={1}&t={2}&on={3}", organizationInfo.OrgCode, inviteCode, requestData.InviteType, inviteDomain, HttpUtility.UrlEncode(organizationInfo.Name)));

                var entity = new MemberInvite()
                {
                    CircleKey    = requestData.CircleKey.ToLower(),
                    Code         = inviteCode,
                    CreateDate   = DateTime.UtcNow,
                    Enable       = true,
                    InviteEmail  = inviteEmail,
                    IsCourseCode = false,
                    Type         = requestData.InviteType,
                    InviteUrl    = inviteUrl
                };
                db.MemberInvite.Add(entity);
                mails.Add(entity);
            } //結束新增
            try
            {
                //開始跑db
                db.SaveChanges();

                var emailDomain       = ConfigurationManager.AppSettings["MailDomain"].ToString();
                var emailSMTPPort     = Convert.ToInt32(ConfigurationManager.AppSettings["MailSMTPPort"].ToString());
                var emailAdminAddress = ConfigurationManager.AppSettings["MailAdminAddress"].ToString();
                var mailService       = new MailService();

                var tokenService  = new TokenService();
                var tokenInfo     = tokenService.GetTokenInfo(requestData.Token).Result;
                var teacherInfo   = db.Members.Find(tokenInfo.MemberId);
                var memberService = new MemberService();
                var teachers      = memberService.GetTeacherList(learningCircleInfo.LearningOuterKey);
                var teachersName  = string.Empty;
                foreach (var teacher in teachers)
                {
                    teachersName = string.Format("{0}、{1}", teachersName, teacher.Name);
                }
                teachersName = teachersName.Substring(1, teachersName.Length - 1);
                var weekTableService = new WeekTableService();
                var weekTableDatas   = weekTableService.GetByCirclekey(learningCircleInfo.LearningOuterKey);
                var weekTableTimes   = string.Empty;
                foreach (var weekTableData in weekTableDatas.WeekTable)
                {
                    weekTableTimes = string.Format("{0}、({1}){2:HH:mm} ~ {3:HH:mm}", weekTableTimes, weekTableData.Week, weekTableData.StartTime, weekTableData.EndTime);
                }
                weekTableTimes = weekTableTimes.Substring(1, weekTableTimes.Length - 1);

                //送邀請函信
                foreach (var code in mails)
                {
                    var mailContent = flipusOrgName == organizationInfo.OrgCode.ToString()
                                                                                ?
                                      //跑Flipus 非組織信字串
                                      string.Format("<tbody><tr><td align = \"left\" style = \"padding-top: 15px;\"></td></tr>" +
                                                    "<img src=\"http://scedev.eastus.cloudapp.azure.com/iThink/images/inviteimg/logo.png\"  alt = \"logo\" width = \"108\" height = \"48\"  />" +
                                                    "<tr><td align=\"left\" style=\"padding - top: 15px; \"><h2>您好!</h2>" +
                                                    "{0} 老師 邀請您一起加入,Flipus 翻轉校園學習圈的「{1}」,現在就與我們一同體驗最有溫度的學習互動!<br><br><br>" +
                                                    "請點擊以下連結,來完成加入課程的動作:<br>" +
                                                    "<a href=\"#\" style=\"color: #5bc289;\">{2}</a><br><br><br>" +
                                                    "課程名稱:{1}<br>" +
                                                    "適用對象:一般學員<br>" +
                                                    "授課老師:{7}<br>" +
                                                    "開課日期:{3:yyyy/MM/dd}~{4:yyyy/MM/dd}<br>" +
                                                    "上課時段:{5}<br>" +
                                                    "課程簡介:{6}" +
                                                    "</td></tr>" +
                                                    "<tr><td align = \"left\" style = \"padding-top: 30px; font-size: 13px; color: #868b8f;\" >" +
                                                    "<hr style = \"margin-bottom: 25px; border: 0; background-color: #ccc; height: 1px;\">" +
                                                    "※ 課程日期或上課時段等相關內容若有異動,請以實際課程資訊為主。<br>" +
                                                    "※ 此信件是由 Flipus 系統自動產生與寄出,請勿直接回覆。<br>" +
                                                    "若需要我們協助的問題,請透過此 <a href = \"mailto:[email protected]\" style = \"color: #5bc289;\" > [email protected]</a> 與我們聯繫,謝謝您!<br>" +
                                                    "<a href = \"http://scedev.eastus.cloudapp.azure.com/iThink/images/inviteimg/footer_banner.png\" title = \"flipus翻轉校園學習圈\" ><img src = \"http://scedev.eastus.cloudapp.azure.com/iThink/images/inviteimg/footer_banner.png\" alt = \"flipus翻轉校園學習圈\" width = \"600\" height = \"100\" style = \"margin-top: 15px;\"></a>" +
                                                    "<hr style = \"margin-top: 25px; border: 0; background-color: #ccc; height: 1px;\" ></td></tr><tr> " +
                                                    "<td align = \"left\" style = \"padding-top: 15px; font-size: 13px;\" >© 中國文化大學推廣教育部 All Rights Reserved.</td>" +
                                                    "</tr></tbody>",
                                                    teacherInfo.Name,
                                                    learningCircleInfo.Name,
                                                    code.InviteUrl,
                                                    learningCircleInfo.StartDate.Value.ToLocalTime(),
                                                    learningCircleInfo.EndDate.Value.ToLocalTime(),
                                                    weekTableTimes,
                                                    learningCircleInfo.Description,
                                                    teachersName)
                                                                                 :
                                      //跑組織信字串
                                      string.Format("<tbody><tr><td align = \"left\" style = \"padding-top: 15px;\"></td></tr>" +
                                                    "<img src=\"http://scedev.eastus.cloudapp.azure.com/iThink/images/inviteimg/logo.png\"  alt = \"logo\" width = \"108\" height = \"48\"  />" +
                                                    "<tr><td align=\"left\" style=\"padding - top: 15px; \"><h2>您好!</h2>" +
                                                    "{0} 老師 邀請您一起加入,{8}組織專屬課程 -「{1}」,現在就與我們一同體驗最有溫度的學習互動!<br><br><br>" +
                                                    "請點擊以下連結,來完成加入課程的動作:<br>" +
                                                    "<a href=\"#\" style=\"color: #5bc289;\">{2}</a><br><br><br>" +
                                                    "課程名稱:{1}<br>" +
                                                    "適用對象:{8}組織學員<br>" +
                                                    "授課老師:{7}<br>" +
                                                    "開課日期:{3:yyyy/MM/dd}~{4:yyyy/MM/dd}<br>" +
                                                    "上課時段:{5}<br>" +
                                                    "課程簡介:{6}" +
                                                    "</td></tr>" +
                                                    "<tr><td align = \"left\" style = \"padding-top: 30px; font-size: 13px; color: #868b8f;\" >" +
                                                    "<hr style = \"margin-bottom: 25px; border: 0; background-color: #ccc; height: 1px;\">" +
                                                    "※ 課程日期或上課時段等相關內容若有異動,請以實際課程資訊為主。<br>" +
                                                    "※ 此信件是由 Flipus 系統自動產生與寄出,請勿直接回覆。<br>" +
                                                    "若需要我們協助的問題,請透過此 <a href = \"mailto:[email protected]\" style = \"color: #5bc289;\" > [email protected]</a> 與我們聯繫,謝謝您!<br>" +
                                                    "<a href = \"http://scedev.eastus.cloudapp.azure.com/iThink/images/inviteimg/footer_banner.png\" title = \"flipus翻轉校園學習圈\" ><img src = \"http://scedev.eastus.cloudapp.azure.com/iThink/images/inviteimg/footer_banner.png\" alt = \"flipus翻轉校園學習圈\" width = \"600\" height = \"100\" style = \"margin-top: 15px;\"></a>" +
                                                    "<hr style = \"margin-top: 25px; border: 0; background-color: #ccc; height: 1px;\" ></td></tr><tr> " +
                                                    "<td align = \"left\" style = \"padding-top: 15px; font-size: 13px;\" >© 中國文化大學推廣教育部 All Rights Reserved.</td>" +
                                                    "</tr></tbody>",
                                                    teacherInfo.Name,
                                                    learningCircleInfo.Name,
                                                    code.InviteUrl,
                                                    learningCircleInfo.StartDate.Value.ToLocalTime(),
                                                    learningCircleInfo.EndDate.Value.ToLocalTime(),
                                                    weekTableTimes,
                                                    learningCircleInfo.Description,
                                                    teachersName,
                                                    organizationInfo.Name);

                    mailService.SendMail(emailDomain, emailSMTPPort, emailAdminAddress, new List <string>()
                    {
                        code.InviteEmail
                    }, mailContent, string.Format("Flipus 邀請您加入「{0}」", learningCircleInfo.Name), null);
                }
                if (requestData.CC)
                {
                    mailService.SendMail(emailDomain, emailSMTPPort, emailAdminAddress, new List <string>()
                    {
                        teacherInfo.Email
                    }, "已發送邀請碼完成", string.Format("[{0}] 課程邀請碼", learningCircleInfo.Name), null);
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);

                throw ex;
            }
        }