From ddf20eea84ac08b5e0650038ad36a6f88e9450f0 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Sat, 2 Nov 2019 15:48:34 +0800 Subject: [PATCH] competition certificate generate --- .../competition_modules_controller.rb | 2 +- app/helpers/pdfkit_helper.rb | 5 ++++ ...te_competition_personal_certificate_job.rb | 19 +++++++++++++++ ...nerate_competition_team_certificate_job.rb | 24 +++++++++++++++++++ app/models/competition_prize.rb | 24 +++++++++++++++++++ app/models/team_member.rb | 4 ++++ .../approve_competition_prize_user_service.rb | 21 +++++++++++----- .../personal.html.erb | 19 +++++++++++++++ .../competition_certificates/teacher.html.erb | 19 +++++++++++++++ .../competition_certificates/team.html.erb | 18 ++++++++++++++ .../competition_settings/index.html.erb | 4 +++- 11 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 app/helpers/pdfkit_helper.rb create mode 100644 app/jobs/generate_competition_personal_certificate_job.rb create mode 100644 app/jobs/generate_competition_team_certificate_job.rb create mode 100644 app/templates/competition_certificates/personal.html.erb create mode 100644 app/templates/competition_certificates/teacher.html.erb create mode 100644 app/templates/competition_certificates/team.html.erb diff --git a/app/controllers/competitions/competition_modules_controller.rb b/app/controllers/competitions/competition_modules_controller.rb index 8c66368fb..8eec4acc5 100644 --- a/app/controllers/competitions/competition_modules_controller.rb +++ b/app/controllers/competitions/competition_modules_controller.rb @@ -7,7 +7,7 @@ class Competitions::CompetitionModulesController < Competitions::BaseController @modules = current_competition.unhidden_competition_modules.order(position: :asc) # 未登录、未获奖用户,不展示获奖证书栏目 - if !current_user.logged? || !current_competition.competition_prize_users.exists?(user: current_user) + if !current_user.logged? || !current_competition.finished? || !current_competition.competition_prize_users.exists?(user: current_user) @modules = @modules.select { |mod| mod.name != '获奖证书' } end end diff --git a/app/helpers/pdfkit_helper.rb b/app/helpers/pdfkit_helper.rb new file mode 100644 index 000000000..950ccc61f --- /dev/null +++ b/app/helpers/pdfkit_helper.rb @@ -0,0 +1,5 @@ +module PdfkitHelper + def download_image(url) + 'data:image/png;base64,' + Base64.encode64(open(url) { |io| io.read }) + end +end \ No newline at end of file diff --git a/app/jobs/generate_competition_personal_certificate_job.rb b/app/jobs/generate_competition_personal_certificate_job.rb new file mode 100644 index 000000000..efe0c8ff6 --- /dev/null +++ b/app/jobs/generate_competition_personal_certificate_job.rb @@ -0,0 +1,19 @@ +# 生成竞赛个人证书Job +class GenerateCompetitionPersonalCertificateJob < ApplicationJob + queue_as :default + + def perform(prize_user_id) + @prize_user = CompetitionPrizeUser.find_by(id: prize_user_id) + return if @prize_user.blank? || @prize_user.certificate_exist? + + template = @prize_user.user.is_teacher? ? 'teacher' : 'personal' + file = File.open(Rails.root.join("app/templates/competition_certificates/#{template}.html.erb")) + html = ERB.new(file.read).result(binding) + kit = PDFKit.new(html, page_width: 842, page_height: 595) + + path = @prize_user.certificate_path + dir = File.dirname(path) + FileUtils.mkdir_p(dir) unless File.directory?(dir) + kit.to_pdf(path) + end +end \ No newline at end of file diff --git a/app/jobs/generate_competition_team_certificate_job.rb b/app/jobs/generate_competition_team_certificate_job.rb new file mode 100644 index 000000000..4e119c5dc --- /dev/null +++ b/app/jobs/generate_competition_team_certificate_job.rb @@ -0,0 +1,24 @@ +# 生成竞赛团体证书Job +class GenerateCompetitionTeamCertificateJob < ApplicationJob + queue_as :default + + def perform(competition_team_id) + @team = CompetitionTeam.find_by(id: competition_team_id) + @prize = @team&.competition_prize_users&.first&.competition_prize + return if @team.blank? || !@prize.team_certificate_exists? || @team.certificate_exists? + + members = @team.team_members.includes(user: :user_extension).to_a + + @member_names = members.select { |m| !m.user.is_teacher? }.map(&:user_name).join('、') + @teacher_names = members.select { |m| m.user.is_teacher? }.map(&:user_name).join('、') + + file = File.open(Rails.root.join("app/templates/competition_certificates/team.html.erb")) + html = ERB.new(file.read).result(binding) + kit = PDFKit.new(html, page_width: 842, page_height: 595) + + path = @team.certificate_path + dir = File.dirname(path) + FileUtils.mkdir_p(dir) unless File.directory?(dir) + kit.to_pdf(path) + end +end \ No newline at end of file diff --git a/app/models/competition_prize.rb b/app/models/competition_prize.rb index 8acbc6713..dd551fabe 100644 --- a/app/models/competition_prize.rb +++ b/app/models/competition_prize.rb @@ -7,6 +7,30 @@ class CompetitionPrize < ApplicationRecord enumerize :category, in: %i[bonus unset] + def member_certificate_exists? + Util::FileManage.exists?(self, self.class.member_suffix) + end + + def teacher_certificate_exists? + Util::FileManage.exists?(self, self.class.teacher_suffix) + end + + def team_certificate_exists? + Util::FileManage.exists?(self, self.class.team_suffix) + end + + def member_certificate_path + Util::FileManage.source_disk_filename(self, self.class.member_suffix) + end + + def teacher_certificate_path + Util::FileManage.source_disk_filename(self, self.class.teacher_suffix) + end + + def team_certificate_path + Util::FileManage.source_disk_filename(self, self.class.team_suffix) + end + def self.member_suffix '_member' end diff --git a/app/models/team_member.rb b/app/models/team_member.rb index 31890ea2e..90bbe1613 100644 --- a/app/models/team_member.rb +++ b/app/models/team_member.rb @@ -13,4 +13,8 @@ class TeamMember < ApplicationRecord def en_role is_teacher? ? 'teacher' : 'member' end + + def user_name + user&.real_name + end end \ No newline at end of file diff --git a/app/services/admins/approve_competition_prize_user_service.rb b/app/services/admins/approve_competition_prize_user_service.rb index cd1c2101b..efdd72253 100644 --- a/app/services/admins/approve_competition_prize_user_service.rb +++ b/app/services/admins/approve_competition_prize_user_service.rb @@ -1,26 +1,35 @@ class Admins::ApproveCompetitionPrizeUserService < ApplicationService - attr_reader :prize_user, :approver + attr_reader :competition, :prize_user, :approver def initialize(prize_user, approver) - @prize_user = prize_user - @approver = approver + @competition = prize_user.competition + @prize_user = prize_user + @approver = approver end def call raise Error, '请勿重复审批' if prize_user.approved? raise Error, '该用户未认证完成' unless prize_user.user_certified? + prize = prize_user.competition_prize + ActiveRecord::Base.transaction do prize_user.approve prize_user.approver = approver prize_user.approved_at = Time.now prize_user.save! - if prize_user.competition_team.all_prize_approved? - # TODO: 生成团队证书 + # 生成个人证书 + # 是老师并且教师证书模板存在 或者是学生而且个人证书模板存在 + if (prize_user.user.is_teacher? && prize.teacher_certificate_exists?) || + (!prize_user.user.is_teacher? && prize.member_certificate_exists?) + GenerateCompetitionPersonalCertificateJob.perform_later(prize_user.id) end - # TODO: 生成个人证书 + # 生成团队证书 + if !competition.personal? && prize_user.competition_team.all_prize_approved? && prize.team_certificate_exists? + GenerateCompetitionTeamCertificateJob.perform_later(prize_user.competition_team.id) + end end end end \ No newline at end of file diff --git a/app/templates/competition_certificates/personal.html.erb b/app/templates/competition_certificates/personal.html.erb new file mode 100644 index 000000000..cec70e5d5 --- /dev/null +++ b/app/templates/competition_certificates/personal.html.erb @@ -0,0 +1,19 @@ + + + + + + + +
+ +
+

<%= @prize_user.user.school_name %> <%= @prize_user.user.real_name %>(学号 <%= @prize_user.student_id %>):

+

+ 荣获第二届“全国高校绿色计算大赛”( <%= @prize_user.competition.sub_title %> )“全国<%= @prize_user.competition_prize.name %>”。 +
特发此证,以资鼓励。 +

+
+
+ + \ No newline at end of file diff --git a/app/templates/competition_certificates/teacher.html.erb b/app/templates/competition_certificates/teacher.html.erb new file mode 100644 index 000000000..d57e620a7 --- /dev/null +++ b/app/templates/competition_certificates/teacher.html.erb @@ -0,0 +1,19 @@ + + + + + + + +
+ +
+

<%= @prize_user.user.school_name %> <%= @prize_user.user.real_name %> 老师:

+

+ 在第二届“全国高校绿色计算大赛”(<%= @prize_user.competition.sub_title %>)中,带领学生团队 表现突出,成绩优异,荣获“优秀指导教师”称号。 +

+

特发此证,以资鼓励。

+
+
+ + \ No newline at end of file diff --git a/app/templates/competition_certificates/team.html.erb b/app/templates/competition_certificates/team.html.erb new file mode 100644 index 000000000..e889d3c62 --- /dev/null +++ b/app/templates/competition_certificates/team.html.erb @@ -0,0 +1,18 @@ + + + + + + + +
+ +
+

+ <%= @member_names %>(指导老师:<%= @teacher_names %>) 在第二届“全国高校绿色计算大赛”(任务挑战组)中成绩突出,荣获“团体 <%= @prize.name %>”。 +

+

特发此证,以资鼓励。

+
+
+ + \ No newline at end of file diff --git a/app/views/admins/competition_settings/index.html.erb b/app/views/admins/competition_settings/index.html.erb index 8547c5a8b..8f483b219 100644 --- a/app/views/admins/competition_settings/index.html.erb +++ b/app/views/admins/competition_settings/index.html.erb @@ -513,7 +513,9 @@
奖项配置 - <%= link_to '新增奖项', new_admins_competition_competition_prize_path(@competition), remote: true, class: 'btn btn-primary btn-sm add-competition-prize-btn' %> + <% unless @competition.competition_prize_users.exists? %> + <%= link_to '新增奖项', new_admins_competition_competition_prize_path(@competition), remote: true, class: 'btn btn-primary btn-sm add-competition-prize-btn' %> + <% end %> <% if @competition.finished? && !@competition.competition_prize_users.exists? %> <%= javascript_void_link '生成获奖记录', class: 'btn btn-primary btn-sm ml-2 generate-prize-user-action' %>