123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- (function()
- {
- angular.module('appSdk')
- .factory('WeeksBusiness', ['WeeksDataAccess', '$q', function (WeeksDataAccess, $q) {
-
- function isDateInCourse(course, date)
- {
- return course.BeginDate < date && course.EndDate > date;
- }
-
- var Business = {};
-
- Business.cachedWeeks = [];
-
- Business.currentWeekId = null;
-
- Business.cacheWeek = function(week, data)
- {
- data = {
- week: week,
- data: data,
- time: new Date()
- };
- Business.cachedWeeks.push(data);
- };
-
- Business.getCachedWeek = function(data)
- {
- var limitDate = new Date();
- limitDate = new Date(limitDate.getTime() - 10 * 60000);
-
- var week = Business.cachedWeeks.find(function(week)
- {
- return data.group_id == week.data.group_id &&
- data.type_id == week.data.type_id &&
- data.week_id == week.data.week_id &&
- limitDate <= week.time;
- });
- if (week != null) {
- return week.week;
- }
- return null;
- };
-
- Business.formatWeek = function(data)
- {
- data.DayList.forEach(function(day) {
- day.DateTime = new Date(day.DateTime);
- day.CourseList.forEach(function (course) {
-
- course.BeginDate = new Date(course.BeginDate);
- var dt = new Date(course.BeginDate);
- var secs = dt.getSeconds() + (60 * dt.getMinutes()) + (60 * 60 * dt.getHours());
- course.BeginPercent = secs / (60 * 60 * 24) * 100;
- course.BeginSeconds = secs;
-
- course.EndDate = new Date(course.EndDate);
- dt = new Date(course.EndDate);
- secs = dt.getSeconds() + (60 * dt.getMinutes()) + (60 * 60 * dt.getHours());
- course.EndPercent = secs / (60 * 60 * 24) * 100;
- course.EndSeconds = secs;
- });
- });
- data.DayList.forEach(function(day) {
- day.CourseList.forEach(function (course) {
- course.ParallelCoursesCount = 1;
- course.ParallelCoursesPosition = 0;
- day.CourseList.forEach(function (course2) {
- if (course.Id == course2.Id) {
- return;
- }
- var beginBeforeEndIn = (course2.BeginDate <= course.BeginDate && isDateInCourse(course, course2.EndDate));
- var beginInEndAfter = (course2.EndDate >= course.EndDate && isDateInCourse(course, course2.BeginDate));
- var beginBeforeEndAfter = (course2.BeginDate <= course.BeginDate && course2.EndDate >= course.EndDate);
- var beginInEndIn = (isDateInCourse(course, course2.EndDate) && isDateInCourse(course, course2.BeginDate));
- if (beginBeforeEndIn || beginInEndAfter || beginBeforeEndAfter || beginInEndIn) {
- ++course.ParallelCoursesCount;
- course2.ParallelCoursesPosition = Math.max(course.ParallelCoursesPosition, course2.ParallelCoursesPosition) + 1
- }
- });
- });
- });
- };
-
- Business.getWeek = function(data, promise)
- {
- var defer = $q.defer();
- var week = Business.getCachedWeek(data);
- if (week == null) {
- WeeksDataAccess.getWeek(data, promise)
- .then(function (week) {
- Business.formatWeek(week);
- Business.cacheWeek(week, data);
- defer.resolve(week);
- }, defer.reject);
- }
- else {
- defer.resolve(week);
- }
- return defer.promise;
- };
-
- Business.getCurrentWeek = function(data, promise)
- {
- data.week_id = Business.currentWeekId;
- var defer = $q.defer();
- var week = Business.getCachedWeek(data);
- if (week == null) {
- WeeksDataAccess.getCurrentWeek(data, promise)
- .then(function (week) {
- Business.formatWeek(week);
- Business.currentWeekId = week.Id;
- data.week_id = week.Id;
- Business.cacheWeek(week, data);
- defer.resolve(week);
- }, defer.reject);
- }
- else {
- defer.resolve(week);
- }
- return defer.promise;
- };
-
- return Business;
- }]);
- })();
|