You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/batch_export_shixun_report_...

48 lines
1.2 KiB

6 years ago
class BatchExportShixunReportService
Error = Class.new(StandardError)
MAX_BATCH_LIMIT = 20
5 years ago
attr_reader :homework, :all_student_works
6 years ago
5 years ago
def initialize(homework, all_student_works)
6 years ago
@homework = homework
5 years ago
# @student_work_ids = student_work_ids
@all_student_works = all_student_works
6 years ago
end
def filename
5 years ago
@_filename ||= "#{homework.name}_#{Time.now.strftime('%Y%m%d_%H%M%S')}"
6 years ago
end
def zip
5 years ago
# validate!
student_works = all_student_works.includes(:myshixun, user: :user_extension)
6 years ago
if student_works.count.zero?
raise Error, '请选择要导出的学生实训报告'
end
pdfs = []
zip_file = Tempfile.new(filename)
Zip::File.open(zip_file.path, Zip::File::CREATE) do |zip|
student_works.find_each.map do |student_work|
export = ExportShixunReportService.new(homework, student_work)
pdf = export.to_pdf
pdfs << pdf
begin
zip.add(export.filename, pdf.path)
rescue => ex
Rails.logger.error(ex.message)
5 years ago
zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") }
6 years ago
next
end
end
zip_file
end
end
end