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.
48 lines
1.2 KiB
48 lines
1.2 KiB
class BatchExportShixunReportService
|
|
Error = Class.new(StandardError)
|
|
|
|
MAX_BATCH_LIMIT = 20
|
|
|
|
attr_reader :homework, :all_student_works
|
|
|
|
|
|
def initialize(homework, all_student_works)
|
|
@homework = homework
|
|
# @student_work_ids = student_work_ids
|
|
@all_student_works = all_student_works
|
|
|
|
end
|
|
|
|
def filename
|
|
@_filename ||= "#{homework.name}_#{Time.now.strftime('%Y%m%d_%H%M%S')}"
|
|
end
|
|
|
|
def zip
|
|
# validate!
|
|
student_works = all_student_works.includes(:myshixun, user: :user_extension)
|
|
|
|
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)
|
|
|
|
zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") }
|
|
next
|
|
end
|
|
end
|
|
zip_file
|
|
end
|
|
end
|
|
|
|
end
|