Accelerate recurring events calculating.
This commit is contained in:
parent
dfb7f23a3d
commit
7cccdd2006
|
@ -17,6 +17,7 @@ class Event
|
||||||
field :recurring, type: Boolean,default: false
|
field :recurring, type: Boolean,default: false
|
||||||
field :frequency
|
field :frequency
|
||||||
field :period
|
field :period
|
||||||
|
field :recurring_end_date, type: DateTime
|
||||||
field :create_user_id
|
field :create_user_id
|
||||||
field :update_user_id
|
field :update_user_id
|
||||||
field :module_key
|
field :module_key
|
||||||
|
@ -147,69 +148,65 @@ class Event
|
||||||
(date2.year-date1.year)*12+(date2.month-date1.month)
|
(date2.year-date1.year)*12+(date2.month-date1.month)
|
||||||
end
|
end
|
||||||
def self.recurring_event(start_date,end_date)
|
def self.recurring_event(start_date,end_date)
|
||||||
@recurring_events = self.where(:recurring => true)
|
@recurring_events = self.where(:recurring => true).any_of({:recurring_end_date=>nil}, {:recurring_end_date.gte=>start_date.utc})
|
||||||
@recurring = []
|
@recurring = []
|
||||||
@recurring_events.each do |re|
|
@recurring_events.each do |re|
|
||||||
edit_url = Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id)
|
has_recurring_end_date = re.recurring_end_date.present?
|
||||||
delete_url = Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)
|
data = re.as_json
|
||||||
data = {:id => re.id.to_s, :title=>re.title, :note=>re.note || "", :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => (re.calendar_type.color rescue nil), :edit_url => edit_url, :delete_url => delete_url, :url_linked => re.url_to_fronted}
|
period_str = nil
|
||||||
case re.period
|
is_month = false
|
||||||
when 'Daily'
|
is_year = false
|
||||||
days = (end_date.to_date-re.start.to_date).to_i
|
days = 1
|
||||||
freq = re.frequency.to_i
|
interval = nil
|
||||||
start_key = (start_date.to_date-re.start.to_date).to_i
|
if re.period == 'Daily'
|
||||||
start_key = 1 if start_key<0
|
period_str = 'day'
|
||||||
(start_key..days).each do |i|
|
interval = 1.day
|
||||||
if i%freq==0
|
elsif re.period == 'Weekly'
|
||||||
@start_date = re.start + i
|
period_str = 'week'
|
||||||
@end_date = re.end + i
|
days = 7
|
||||||
@recurring << data.merge({:start => @start_date, :end => @end_date})
|
interval = 1.week
|
||||||
|
elsif re.period == 'Monthly'
|
||||||
|
period_str = 'month'
|
||||||
|
is_month = true
|
||||||
|
days = 30
|
||||||
|
elsif re.period == 'Yearly'
|
||||||
|
period_str = 'year'
|
||||||
|
is_year = true
|
||||||
|
days = 365
|
||||||
end
|
end
|
||||||
end
|
if period_str
|
||||||
when "Weekly"
|
org_start = re.start
|
||||||
@start_date = re.start
|
@start_date = re.start
|
||||||
@end_date = re.end
|
@end_date = re.end
|
||||||
@i = TimeDifference.between(re.start.to_date,end_date.to_date).in_weeks.to_i
|
freq = re.frequency.to_i
|
||||||
(1..@i).each do |i|
|
interval = freq.send(period_str)
|
||||||
@start_date += (7*re.frequency.to_i)
|
if is_month
|
||||||
@end_date += (7*re.frequency.to_i)
|
add_interval = ((start_date.year * 12 + start_date.month) - (@start_date.year * 12 + @start_date.month) - 1)
|
||||||
@recurring << data.merge({:start => @start_date, :end => @end_date})
|
elsif is_year
|
||||||
end
|
add_interval = (start_date.year - @start_date.year - 1)
|
||||||
when "Monthly"
|
else
|
||||||
if !(start_date..end_date).cover?(re.start) && start_date.to_datetime>re.start
|
add_interval = (((start_date - @start_date - 1.day).to_i / 1.day) / days)
|
||||||
sd = re.start
|
|
||||||
ed = re.end
|
|
||||||
end_datetime = end_date.to_datetime
|
|
||||||
start_datetime = start_date.to_datetime
|
|
||||||
months = self.get_diff_month(sd,start_datetime)..self.get_diff_month(sd,end_datetime)
|
|
||||||
months.each do |diff_month|
|
|
||||||
if (diff_month%re.frequency.to_i)==0
|
|
||||||
sd_tp = sd + diff_month.month
|
|
||||||
ed_tp = ed + diff_month.month
|
|
||||||
if sd_tp>start_date
|
|
||||||
@recurring << data.merge({:start => sd_tp, :end => ed_tp})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
when "Yearly"
|
|
||||||
if !(start_date..end_date).cover?(re.start)
|
|
||||||
sd = re.start
|
|
||||||
ed = re.end
|
|
||||||
start_datetime = start_date.to_datetime
|
|
||||||
end_datetime = end_date.to_datetime
|
|
||||||
if start_datetime>sd && start_datetime.year != sd.year
|
|
||||||
((start_datetime.year-sd.year)..(end_datetime.year-sd.year)).each do |year_diff|
|
|
||||||
if (year_diff%re.frequency.to_i)==0
|
|
||||||
sd_tp = sd + year_diff.year
|
|
||||||
ed_tp = ed + year_diff.year
|
|
||||||
if (start_date..end_date)&(sd_tp..ed_tp)
|
|
||||||
@recurring << data.merge({:start => sd_tp, :end => ed_tp})
|
|
||||||
end
|
end
|
||||||
|
if add_interval < 0
|
||||||
|
add_interval = add_interval % freq
|
||||||
end
|
end
|
||||||
|
rest = add_interval % freq
|
||||||
|
if rest != 0
|
||||||
|
add_interval += (freq - rest)
|
||||||
end
|
end
|
||||||
|
add_interval = add_interval.send(period_str)
|
||||||
|
@start_date += add_interval
|
||||||
|
@end_date += add_interval
|
||||||
|
new_end_date = has_recurring_end_date ? [re.recurring_end_date,end_date].min : end_date
|
||||||
|
while @start_date <= new_end_date do
|
||||||
|
if @start_date >= start_date && @start_date != org_start
|
||||||
|
@recurring << data.merge({:start => @start_date.to_json.gsub('"',''), :end => @end_date.to_json.gsub('"','')})
|
||||||
end
|
end
|
||||||
|
@start_date += interval
|
||||||
|
@end_date += interval
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
next
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue