# == Schema Information
#
# Table name: message_templates
#
#  id               :integer          not null, primary key
#  type             :string(255)
#  sys_notice       :text(65535)
#  email            :text(65535)
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  notification_url :string(255)
#  email_title      :string(255)
#

# 统一模板（

class MessageTemplate::CustomTip < MessageTemplate 
  
  # MessageTemplate::CustomTip.get_message_content(User.where(login: 'yystopf'), "hahah")
  def self.get_message_content(receivers, template, props={})
   return '', '', '' if receivers.blank? || template.blank?
   content = template.sys_notice
   notification_url = template.notification_url
   props.each do |k, v|
    content.gsub!("{#{k}}", v)
    notification_url.gsub!("{#{k}}", v)
   end
   notification_url.gsub!('{baseurl}', base_url)
   return receivers_string(receivers), content, notification_url
 rescue => e
   Rails.logger.info("MessageTemplate::CustomTip.get_message_content [ERROR] #{e}")
   return '', '', ''
 end

 def self.get_email_message_content(receiver, template, props={})
    return '', '', '' if receiver.blank? || template.blank?
    title = template.email_title
    content = template.email
    props.each do |k, v|
      title.gsub!("{#{k}}", v)
      content.gsub!("{#{k}}", v)
    end
    content.gsub!('{receiver}', receiver&.real_name)
    title.gsub!('{platform}', PLATFORM)
    content.gsub!('{platform}', PLATFORM)
    content.gsub!('{baseurl}', base_url)

    return receiver&.mail, title, content
 rescue => e
   Rails.logger.info("MessageTemplate::CustomTip.get_email_message_content [ERROR] #{e}")
   return '', '', ''
 end

  def self.get_email_content(template, props = {})
    return '', '', '' if template.blank?
    title = template.email_title
    content = template.email
    props.each do |k, v|
      title.gsub!("{#{k}}", v)
      content.gsub!("{#{k}}", v)
    end
    title.gsub!('{platform}', PLATFORM)
    content.gsub!('{platform}', PLATFORM)
    content.gsub!('{baseurl}', base_url)

    return title, content
  rescue => e
    Rails.logger.info("MessageTemplate::CustomTip.get_email_content [ERROR] #{e}")
    return '', ''
  end
end