diff --git a/app/controllers/admin/event_news_controller.rb b/app/controllers/admin/event_news_controller.rb index 19b87c1..14f260f 100644 --- a/app/controllers/admin/event_news_controller.rb +++ b/app/controllers/admin/event_news_controller.rb @@ -112,11 +112,52 @@ class Admin::EventNewsController < OrbitAdminController end def export_excel - @event_news = EventNews.where(:category_id.ne=>nil).desc(:created_at) - respond_to do |format| - format.xlsx { - response.headers['Content-Disposition'] = 'attachment; filename="event_news_export.xlsx"' - } + @thread = Multithread.where(:key=>'export_event_news').first + update_flag = true + if @thread.nil? + @thread = Multithread.create(:key=>'export_event_news',:status=>{:status=>'Processing'}) + else + update_flag = false if @thread.status[:status] == 'Processing' && @thread.respond_to?(:updated_at) && (@thread.updated_at > DateTime.now - 1.minute rescue false) + if update_flag + @thread.update(:status=>{:status=>'Processing'}) + end + end + if update_flag + Thread.new do + begin + @event_news = EventNews.where(:category_id.ne=>nil).desc(:created_at) + last_updated = EventNews.max(:updated_at).to_i + filename = "public/event_news_export_#{last_updated}.xlsx" + if File.exist?(filename) + @thread.update(:status=>{:status=>'finish','finish_percent'=>100,'info'=>I18n.t('event_news.read_from_cache')}) + else + excel_contents = render_to_string( handlers: [:axlsx], formats: [:xlsx] , layout: false, template: 'admin/event_news/export_excel.xlsx', locals: {:@event_news=>@event_news,:@thread=>@thread} ) + File.open(filename, 'w') do |f| + f.write excel_contents + end + end + @thread.status[:file] = filename + @thread.status[:filename] = "event_news_export_#{DateTime.now.in_time_zone(Time.zone.utc_offset / 3600).strftime('%Y_%m_%d_%H%M')}.xlsx" + @thread.save + rescue => e + @thread.status[:status] = 'error' + # @thread.status[:info] = [e.to_s, e.backtrace] + puts [e.to_s, e.backtrace] + @thread.save + end + end + end + redirect_to admin_event_news_import_path(:thread_id=>@thread.id.to_s) + end + def render_404 + render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => 404, :formats => [:html] + end + def download_file_from_thread + @thread = Multithread.where(:id=>params[:id]).first if params[:id].present? + if @thread && @thread.status[:file] + send_file(@thread.status[:file],:filename=>@thread.status[:filename]) + else + render_404 end end @@ -127,6 +168,7 @@ class Admin::EventNewsController < OrbitAdminController end def import + @thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present? end diff --git a/app/views/admin/event_news/export_excel.xlsx.axlsx b/app/views/admin/event_news/export_excel.xlsx.axlsx index b564b9f..e0bc7eb 100644 --- a/app/views/admin/event_news/export_excel.xlsx.axlsx +++ b/app/views/admin/event_news/export_excel.xlsx.axlsx @@ -165,7 +165,13 @@ wb.add_worksheet(name: "EventNewsModule") do |sheet| sheet.add_row row, :style => heading sheet.add_row row1 sheet.add_row row2, :style => example - + if @thread + all_count = @event_news.count + puts_every_count = [all_count * 3 / 100, 1].max + current_count = 0 + finish_percent = 0 + @thread.update(:status=>{:status=>'Processing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + end @event_news.each do |anns| row = [] row << categories.to_a.index(anns.category) @@ -230,6 +236,17 @@ wb.add_worksheet(name: "EventNewsModule") do |sheet| t = carousel_images.collect{|l|l.description_translations["zh_tw"]} row << t.join(";") sheet.add_row row + if @thread + current_count += 1 + if current_count % puts_every_count == 0 + finish_percent = (current_count * 100.0 / all_count).round(1) + @thread.update(:status=>{:status=>'Processing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + end + end + end + if @thread + finish_percent = 100 + @thread.update(:status=>{:status=>'finish','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) end diff --git a/app/views/admin/event_news/import.html.erb b/app/views/admin/event_news/import.html.erb index 9b1524a..6464d48 100644 --- a/app/views/admin/event_news/import.html.erb +++ b/app/views/admin/event_news/import.html.erb @@ -1,11 +1,40 @@ <% content_for :page_specific_javascript do %> <% end %> +<% if @thread %> +
+<% end %>