forked from Trustie/forgeplus
报名资料导出
This commit is contained in:
parent
a5021df2ac
commit
20a69528fa
|
|
@ -1,6 +1,6 @@
|
|||
class CompetitionInfosController < ApplicationController
|
||||
include ApplicationHelper
|
||||
before_action :find_competition, only: [:show, :edit, :update, :enroll, :upload, :enroll_status, :statistics, :enroll_list, :enroll_update, :enroll_status_update, :upload_enroll_template]
|
||||
before_action :find_competition, only: [:show, :edit, :update, :enroll, :upload, :enroll_status, :statistics, :enroll_list, :enroll_update, :enroll_status_update, :upload_enroll_template, :enroll_template]
|
||||
before_action :require_admin, only: [:index, :new, :edit, :create]
|
||||
before_action :check_competition_manager, only: [:update, :enroll_status_update]
|
||||
|
||||
|
|
@ -105,6 +105,31 @@ class CompetitionInfosController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def enroll_template
|
||||
competition_users = CompetitionUser.where(competition_info: @competition_info.id).order("updated_at desc")
|
||||
competition_users = competition_users.where(status: [1,2]).where("competition_users.enroll_template_id is not null")
|
||||
zones = @competition_info.manager_zones(current_user)
|
||||
if params[:zone].present?
|
||||
competition_users = zones.include?(params[:zone].to_s) ? competition_users.where(zone: params[:zone].to_s) : competition_users.where(zone: [])
|
||||
else
|
||||
competition_users = current_user.admin? ? competition_users : competition_users.where(zone: zones)
|
||||
end
|
||||
if params[:sub_competition].present?
|
||||
competition_users = competition_users.where(sub_competition: params[:sub_competition].to_s)
|
||||
end
|
||||
if params[:keyword].present?
|
||||
competition_users = competition_users.where("competition_users.org_name LIKE :keyword OR competition_users.leader LIKE :keyword OR competition_users.phone LIKE :keyword", keyword: "%#{params[:keyword].to_s}%")
|
||||
end
|
||||
respond_to do |format|
|
||||
format.json
|
||||
format.zip {
|
||||
Rails.logger.info("######_______________########competition_users.size: #{competition_users.size}")
|
||||
service = Competition::ZipEnrollTemplateService.new(competition_users,"")
|
||||
send_file service.zip, filename: service.filename, type: 'application/zip'
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
require 'rubygems'
|
||||
require 'zip'
|
||||
|
||||
class Competition::ZipEnrollTemplateService
|
||||
|
||||
attr_reader :competition_users, :path
|
||||
|
||||
def initialize(competition_users, path)
|
||||
@competition_users = competition_users
|
||||
@path = path
|
||||
end
|
||||
|
||||
def filename
|
||||
export_name = "报名资料-#{Time.now.strftime('%Y%m%d%H%M%S')}"
|
||||
"#{export_name.strip}.zip"
|
||||
end
|
||||
|
||||
def zip
|
||||
zip_file = Rails.root.join(@path, filename)
|
||||
Zip::File.open(zip_file, Zip::File::CREATE) do |attach_zip|
|
||||
if @competition_users.present?
|
||||
@competition_users.each do |c_user|
|
||||
attach = Attachment.find_by(id: c_user.enroll_template_id)
|
||||
next if attach.blank?
|
||||
attach_local_path = "#{Rails.root}/files/#{attach.disk_directory}/#{attach.disk_filename}"
|
||||
attach_filename = "#{c_user.id}_#{c_user.leader}_#{attach.filename}"
|
||||
begin
|
||||
attach_zip.add(attach_filename, attach_local_path)
|
||||
rescue => ex
|
||||
Rails.logger.error("########—————————————2222222#######" + ex.message)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
zip_file
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -398,6 +398,7 @@ Rails.application.routes.draw do
|
|||
post :enroll
|
||||
post :upload
|
||||
post :upload_enroll_template
|
||||
get :enroll_template
|
||||
end
|
||||
resources :competition_notices, only: [:show, :index, :update, :destroy, :create]
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue