# == Schema Information
#
# Table name: journals
#
#  id               :integer          not null, primary key
#  journalized_id   :integer          default("0"), not null
#  journalized_type :string(30)       default(""), not null
#  user_id          :integer          default("0"), not null
#  notes            :text(4294967295)
#  created_on       :datetime         not null
#  private_notes    :boolean          default("0"), not null
#  parent_id        :integer
#  comments_count   :integer          default("0")
#  reply_id         :integer
#  review_id        :integer
#  commit_id        :string(255)
#  diff             :text(4294967295)
#  line_code        :string(255)
#  path             :string(255)
#  state            :integer          default("0")
#  resolve_at       :datetime
#  resolveer_id     :integer
#  need_respond     :boolean          default("0")
#  updated_on       :datetime
#  operate_by       :string(255)      default("Issue")
#
# Indexes
#
#  index_journals_on_created_on      (created_on)
#  index_journals_on_journalized_id  (journalized_id)
#  index_journals_on_parent_id       (parent_id)
#  index_journals_on_review_id       (review_id)
#  index_journals_on_user_id         (user_id)
#  journals_journalized_id           (journalized_id,journalized_type)
#

class Journal < ApplicationRecord
  include DunCheckAble
  serialize :diff, JSON
  alias_attribute :note, :notes
  belongs_to :user
  belongs_to :issue, foreign_key: :journalized_id, :touch => true, optional: true
  belongs_to :journalized, polymorphic: true
  belongs_to :review, optional: true
  belongs_to :resolveer, class_name: 'User', foreign_key: :resolveer_id, optional: true
  belongs_to :parent_journal, class_name: 'Journal', foreign_key: :parent_id, optional: true, counter_cache: :comments_count
  belongs_to :reply_journal, class_name: 'Journal', foreign_key: :reply_id, optional: true
  has_many :journal_details, :dependent => :delete_all
  has_many :attachments, as: :container, dependent: :destroy
  has_many :first_ten_children_journals, -> { order(created_on: :asc).limit(20)}, class_name: 'Journal', foreign_key: :parent_id
  has_many :children_journals, class_name: 'Journal', foreign_key: :parent_id, dependent: :destroy

  scope :journal_includes, ->{includes(:user, :journal_details, :attachments)}
  scope :parent_journals, ->{where(parent_id: nil)}
  scope :children_journals, lambda{|journal_id| where(parent_id: journal_id)}
  scope :operate_journals, ->{where(notes: nil)}

  enum state: {opened: 0, resolved: 1, disabled: 2}

  after_save :associate_attachment_container

  def is_journal_detail?
    self.notes.blank? && self.journal_details.present?
  end

  # 关附件到功能
  def associate_attachment_container
    return if self.issue&.project_id.to_i == 0
    att_ids = []
    # 附件的格式为（/api/attachments/ + 附件id）的形式，提取出id进行附件属性关联，做附件访问权限控制
    att_ids += self.notes.to_s.scan(/\(\/api\/attachments\/.+\)/).map{|s|s.match(/\d+/)[0]}
    att_ids += self.notes.to_s.scan(/\/api\/attachments\/.+\"/).map{|s|s.match(/\d+/)[0]}
    att_ids += self.notes.to_s.scan(/\/api\/attachments\/\d+/).map{|s|s.match(/\d+/)[0]}
    if att_ids.present?
      Attachment.where(id: att_ids).where("container_type IS NULL OR container_type = 'Journal'").update_all(container_id: self.issue.project_id, container_type: "Project")
    end

    att_ids2 = []
    # uuid_regex= /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/
    # 附件的格式为（/api/attachments/ + uuid）的形式，提取出id进行附件属性关联，做附件访问权限控制
    att_ids2 += self.notes.to_s.scan(/\(\/api\/attachments\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)/).map{|s|s.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/)[0]}
    att_ids2 += self.notes.to_s.scan(/\/api\/attachments\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/).map{|s|s.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/)[0]}
    if att_ids2.present?
      Attachment.where(uuid: att_ids).where("container_type IS NULL OR container_type = 'Journal'").update_all(container_id: self.issue.project_id, container_type: "Project")
    end
  end

  def operate_by_content
    return '' if self.operate_by == "Issue"
    if self.operate_by.starts_with?("Project#")
      project_id, commit_sha = self.operate_by.scan(/#(\d+).*?@(\w+)/)[0]
      project =Project.find_by_id(project_id)
      return "通过 #{project&.owner&.real_name}/#{project&.name} 提交 <a href=\"#{Rails.application.config_for(:configuration)['platform_url']}/#{project&.owner&.login}/#{project&.identifier}/commits/#{commit_sha}\">#{commit_sha[0...10]}</a>"
    end
  rescue 
    return ''
  end

  def pm_operate_category
    detail = self.journal_details.take 
    if %w(requirement task bug).include?(detail.property) && detail.prop_key.to_s == "1"
      return "issue"
    else
      return %w(requirement task bug attr).include?(detail.property) ? detail.prop_key : detail.property
    end
  end

  def pm_dashboard_operate_content 
    content = self.pm_operate_content
    if content.start_with?('创建了')
      content += "<b>#{self.issue.subject}</b>"
    else 
      prefix = '将'
      prefix += "计划" if self.issue.pm_issue_type == 1
      prefix += "任务" if self.issue.pm_issue_type == 2 
      prefix += "缺陷" if self.issue.pm_issue_type == 3 
      prefix += "<b>#{self.issue.subject}</b>"
      content = prefix + content.sub('将', '的')
    end
    content
  end

  def pm_operate_content
    content = "#{operate_by_content}"
    detail = self.journal_details.take 
    case detail.property
    when 'requirement'
        case detail.prop_key
        when 'status_id'
          old_value = IssueStatus.find_by_id(detail.old_value)&.name 
          new_value = IssueStatus.find_by_id(detail.value)&.name 
          content += "将状态"
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "设置为<b>#{new_value}</b>"
          else 
            new_value = "未设置" if detail.value.blank?
            content += "由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
          end
          content.gsub!('新增', '待评审')
          content.gsub!('正在解决', '进行中')
          content.gsub!('已解决', '已完成')
          content.gsub!('关闭', '已关闭')
          content.gsub!('拒绝', '已拒绝')
          return content
        when 'root_id'
          old_value = "<b><#{Issue.find_by_id(detail.old_value)&.subject}></b>"
          new_value = "<b><#{Issue.find_by_id(detail.value)&.subject}></b>"
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "关联了父需求#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "取消了关联的父需求#{old_value}"
            else
              content += "将关联的父需求由#{old_value}更改为#{new_value}"
            end
          end
          return content
        when 'leaf_issue'
          old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "新建了子需求#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "删除了关联的子需求#{old_value}"
            else
              content += "新建了子需求#{new_value}"
            end
          end
          return content
        when 'tag_leaf_issue'
          old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "关联了子需求#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "取消了关联的子需求#{old_value}"
            else
              content += "关联了子需求#{new_value}"
            end
          end
          return content
        else
          content += "创建了需求"
        end
    when 'task'
        case detail.prop_key
        when 'status_id'
          old_value = IssueStatus.find_by_id(detail.old_value)&.name 
          new_value = IssueStatus.find_by_id(detail.value)&.name 
          content += "将状态"
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "设置为<b>#{new_value}</b>"
          else 
            new_value = "未设置" if detail.value.blank?
            content += "由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
          end
          content.gsub!('新增', '待处理')
          content.gsub!('正在解决', '进行中')
          content.gsub!('已解决', '已完成')
          content.gsub!('关闭', '已关闭')
          content.gsub!('拒绝', '已拒绝')
          return content
        when 'root_id'
          old_value = "<b><#{Issue.find_by_id(detail.old_value)&.subject}></b>" 
          new_value = "<b><#{Issue.find_by_id(detail.value)&.subject}></b>" 
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "关联了父任务#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "取消了关联的父任务#{old_value}"
            else
              content += "将关联的父任务由#{old_value}>更改为#{new_value}"
            end
          end
          return content
        when 'leaf_issue'
          old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "新建了子任务#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "删除了关联的子任务#{old_value}"
            else
              content += "新建了子任务#{new_value}"
            end
          end
          return content
        when 'tag_leaf_issue'
          old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "关联了子任务#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "取消了关联的子任务#{old_value}"
            else
              content += "关联了子任务#{new_value}"
            end
          end
          return content
        else
          content += "创建了任务"
        end
    when 'bug'
        case detail.prop_key
        when 'status_id'
          old_value = IssueStatus.find_by_id(detail.old_value)&.name 
          new_value = IssueStatus.find_by_id(detail.value)&.name 
          content += "将状态"
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "设置为<b>#{new_value}</b>"
          else 
            new_value = "未设置" if detail.value.blank?
            content += "由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
          end
          content.gsub!('新增', '待修复')
          content.gsub!('正在解决', '修复中')
          content.gsub!('已解决', '已修复')
          content.gsub!('关闭', '已关闭')
          content.gsub!('拒绝', '已拒绝')
          return content
        when 'root_id'
          old_value = "<b><#{Issue.find_by_id(detail.old_value)&.subject}></b>" 
          new_value = "<b><#{Issue.find_by_id(detail.value)&.subject}></b>" 
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "关联了父缺陷#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "取消了关联的父缺陷#{old_value}"
            else
              content += "将关联的父缺陷由#{old_value}更改为#{new_value}"
            end
          end
          return content
        when 'leaf_issue'
          old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "新建了子缺陷#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "删除了关联的子缺陷#{old_value}"
            else
              content += "新建了子缺陷#{new_value}"
            end
          end
          return content
        when 'tag_leaf_issue'
          old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("、")
          if detail.old_value.nil? || detail.old_value.blank? 
            content += "关联了子缺陷#{new_value}"
          else
            if detail.value.nil? || detail.value.blank? 
              content += "取消了关联的子缺陷#{old_value}"
            else
              content += "关联了子缺陷#{new_value}"
            end
          end
          return content
        else
          content += "创建了缺陷"
        end
    when 'attr'
      case detail.prop_key
      when 'subject'
        content += "修改了<b>标题</b>"
      when 'description'
        content += "修改了<b>正文</b>"
      when 'priority_id'
        old_value = IssuePriority.find_by_id(detail.old_value)&.name 
        new_value = IssuePriority.find_by_id(detail.value)&.name
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "将优先级设置为<b>#{new_value}</b>"
        else 
          new_value = "未设置" if detail.value.blank?
          content += "将优先级由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
        end
        return content
      when 'status_id'
        old_value = IssueStatus.find_by_id(detail.old_value)&.name 
        new_value = IssueStatus.find_by_id(detail.value)&.name 
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "将状态设置为<b>#{new_value}</b>"
        else 
          new_value = "未设置" if detail.value.blank?
          content += "将状态由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
        end
        case self.issue.pm_issue_type.to_i
        when 1
          content.gsub!('新增', '待评审')
          content.gsub!('正在解决', '进行中')
          content.gsub!('已解决', '已完成')
          content.gsub!('关闭', '已关闭')
          content.gsub!('拒绝', '已拒绝')
        when 2
          content.gsub!('新增', '待处理')
          content.gsub!('正在解决', '进行中')
          content.gsub!('已解决', '已完成')
          content.gsub!('关闭', '已关闭')
          content.gsub!('拒绝', '已拒绝')
        when 3
          content.gsub!('新增', '待修复')
          content.gsub!('正在解决', '修复中')
          content.gsub!('已解决', '已修复')
          content.gsub!('关闭', '已关闭')
          content.gsub!('拒绝', '已拒绝')
        end
        return content
      when 'pm_issue_type'
        old_value = detail.old_value
        new_value = detail.value
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "将工作项类型设置为<b>#{new_value}</b>"
        else 
          new_value = "未设置" if detail.value.blank?
          content += "将工作项类型由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
        end
        content.gsub!('1', '需求')
        content.gsub!('2', '任务')
        content.gsub!('3', '缺陷')
        return content
      when 'pm_sprint_id'
        old_value = detail.old_value
        new_value = detail.value
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "添加了关联迭代"
        else
          if detail.value.nil? || detail.value.blank? 
            content += "将关联迭代更改为<b>未设置</b>"
          else
            content += "变更了关联迭代"
          end
        end
        return content
      when 'project_id'
        old_value = Project.find_by_id(detail.old_value)&.name
        new_value = Project.find_by_id(detail.value)&.name
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "添加关联代码库<b>#{new_value}</b>"
        else
          if detail.value.nil? || detail.value.blank? 
            content += "将关联代码库更改为<b>未设置</b>"
          else
            content += "将关联代码库由<b>#{old_value}</b>改为<b>#{new_value}</b>"
          end
        end
        return content
      when 'branch_name'
        old_value = detail.old_value 
        new_value = detail.value
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "添加关联分支<b>#{new_value}</b>"
        else
          if detail.value.nil? || detail.value.blank? 
            content += "将关联分支更改为<b>未设置</b>"
          else
            content += "将关联分支由<b>#{old_value}</b>改为<b>#{new_value}</b>"
          end
        end
        return content

      when 'start_date'
        old_value = detail.old_value 
        new_value = detail.value
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "添加开始时间<b>#{new_value}</b>"
        else
          if detail.value.nil? || detail.value.blank? 
            content += "将开始时间更改为<b>未设置</b>"
          else
            content += "将开始时间由<b>#{old_value}</b>改为<b>#{new_value}</b>"
          end
        end
        return content

      when 'due_date'
        old_value = detail.old_value 
        new_value = detail.value
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "添加结束时间<b>#{new_value}</b>"
        else
          if detail.value.nil? || detail.value.blank? 
            content += "将结束时间更改为<b>未设置</b>"
          else
            content += "将结束时间由<b>#{old_value}</b>改为<b>#{new_value}</b>"
          end
        end
        return content
      when 'time_scale'
        old_value = detail.old_value 
        new_value = detail.value
        if detail.old_value.nil? || detail.old_value.blank? 
          content += "添加预估工时<b>#{new_value}</b>"
        else
          if detail.value.nil? || detail.value.blank? 
            content += "将预估工时更改为<b>未设置</b>"
          else
            content += "将预估工时由<b>#{old_value}</b>改为<b>#{new_value}</b>"
          end
        end
        return content        
      end
    when 'attachment'
      old_value = detail.old_value.to_s
      new_value = detail.value.to_s
      if detail.old_value.nil? || detail.old_value.blank?
        content += "上传了附件"
      else
        if detail.value.nil? || detail.value.blank? 
          content += "删除了附件"
        else
          content += "上传了附件"
        end
      end
      return content 
    when 'assigner'
      old_value = User.where(id: detail.old_value.split(",")).map{|u| "<b>#{u.real_name}</b>"}.join("、")
      new_value = User.where(id: detail.value.split(",")).map{|u| "<b>#{u.real_name}</b>"}.join("、")
      if detail.old_value.nil? || detail.old_value.blank? 
        content += "添加负责人#{new_value}"
      else
        if detail.value.nil? || detail.value.blank? 
          content += "将负责人更改为<b>未设置</b>"
        else
          content += "将负责人由#{old_value}更改为#{new_value}"
        end
      end
      return content
    when 'issue_tag'
      old_value = IssueTag.where(id: detail.old_value.split(",")).map{|t| "<b>#{t.name}</b>"}.join("、")
      new_value = IssueTag.where(id: detail.value.split(",")).map{|t| "<b>#{t.name}</b>"}.join("、")
      if detail.old_value.nil? || detail.old_value.blank? 
        content += "添加标记#{new_value}"
      else
        if detail.value.nil? || detail.value.blank? 
          content += "将标记更改为<b>未设置</b>"
        else
          content += "将标记由#{old_value}更改为#{new_value}"
        end
      end
      return content
    when 'link_issue'
      old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| i.pm_issue_type.nil? && i.pm_project_id.nil? ? "疑修 <b>【#{i.subject}】</b>" : "工作项 <b>[#{i.pm_issue_type_chinese_string}]-<#{i.subject}></b>"}.join("、")
      new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i|  i.pm_issue_type.nil? && i.pm_project_id.nil? ? "疑修 <b>【#{i.subject}】</b>" : "工作项 <b>[#{i.pm_issue_type_chinese_string}]-<#{i.subject}></b>"}.join("、")
      if detail.old_value.nil? || detail.old_value.blank? 
        content += "新建了关联的#{new_value}"
      else 
        if detail.value.nil? || detail.value.blank? 
          content += "删除了关联的#{old_value}"
        else 
          content += "新建了关联的#{new_value}"
        end
      end
      return content
    when 'tag_link_issue'
      old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| i.pm_issue_type.nil? && i.pm_project_id.nil? ? "疑修 <b>【#{i.subject}】</b>" : "工作项 <b>[#{i.pm_issue_type_chinese_string}]-<#{i.subject}></b>"}.join("、")
      new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i|  i.pm_issue_type.nil? && i.pm_project_id.nil? ? "疑修 <b>【#{i.subject}】</b>" : "工作项 <b>[#{i.pm_issue_type_chinese_string}]-<#{i.subject}></b>"}.join("、")
      if detail.old_value.nil? || detail.old_value.blank? 
        content += "关联了#{new_value}"
      else 
        if detail.value.nil? || detail.value.blank? 
          content += "取消了关联的#{old_value}"
        else 
          content += "关联了#{new_value}"
        end
      end
      return content
    when 'issue'
      issue = self.issue
      case issue.pm_issue_type
      when 1
        content += "创建了需求"
      when 2 
        content += "创建了任务"
      when 3
        content += "创建了缺陷"
      end
    end

    return content
  end

  def operate_content 
    content = "#{operate_by_content}"
    detail = self.journal_details.take
    case detail.property
    when 'issue'
      content += "创建了<b>疑修</b>"
      return content
    when 'attachment'
      old_value = Attachment.where("BINARY id in (?) or uuid in (?)", detail.old_value.to_s.split(","), detail.old_value.to_s.split(",")).pluck(:filename).join("、")
      new_value = Attachment.where("BINARY id in (?) or uuid in (?)", detail.value.to_s.split(","), detail.value.to_s.split(",")).pluck(:filename).join("、")
      if old_value.nil? || old_value.blank?
        content += "添加了<b>#{new_value}</b>附件"
      else
        new_value = "无" if new_value.blank?
        content += "将附件由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
      end
      return content
    when 'issue_tag'
      old_value = IssueTag.where(id: detail.old_value.split(",")).pluck(:name).join("、")
      new_value = IssueTag.where(id: detail.value.split(",")).pluck(:name).join("、")
      if old_value.nil? || old_value.blank?
        content += "添加了<b>#{new_value}</b>标记"
      else
        new_value = "无" if new_value.blank?
        content += "将标记由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
      end
      return content
    when 'assigner'
      old_value = User.where(id: detail.old_value.split(",")).map{|u| u.real_name}.join("、")
      new_value = User.where(id: detail.value.split(",")).map{|u| u.real_name}.join("、")
      if old_value.nil? || old_value.blank?
        content += "添加负责人<b>#{new_value}</b>"
      else
        new_value = "无" if new_value.blank?
        content += "将负责人由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
      end
      return content
    when 'attr'
      content += "将"
      case detail.prop_key
      when 'subject'
        content += "修改了<b>标题</b>"
      when 'description'
        content += "修改了<b>描述</b>"
      when 'status_id'
        old_value = IssueStatus.find_by_id(detail.old_value)&.name
        new_value = IssueStatus.find_by_id(detail.value)&.name
        content += "状态"
      when 'priority_id'
        old_value = IssuePriority.find_by_id(detail.old_value)&.name
        new_value = IssuePriority.find_by_id(detail.value)&.name
        content += "优先级"
      when 'fixed_version_id'
        old_value = Version.find_by_id(detail.old_value)&.name
        new_value = Version.find_by_id(detail.value)&.name
        content += "里程碑"
      when 'branch_name'
        old_value = detail.old_value
        new_value = detail.value
        content += "关联分支"
      when 'start_date'
        old_value = detail.old_value
        new_value = detail.value
        content += "开始日期"
      when 'due_date'
        old_value = detail.old_value
        new_value = detail.value
        content += "结束日期"
      when 'assigned_to_id'
        old_value = User.find_by_id(detail.old_value)&.real_name
        new_value = User.find_by_id(detail.value)&.real_name
        content += "负责人"
      end
      if old_value.nil? || old_value.blank?
        content += "设置为<b>#{new_value}</b>"
      else
        new_value = "无" if new_value.blank?
        content += "由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
      end
      return content
    end
    content = self.pm_operate_content if content == ""
  end

  def journal_content
    send_details = []
    if self.is_journal_detail?
      details = self.journal_details.select(:property, :prop_key, :old_value, :value).pluck(:property, :prop_key, :old_value, :value)
      if details.size > 0
        details.each do |de|
          if de[0] == "attr"
            content = ""
          else
            content = "附件"
          end
          old_value = de[2]
          value = de[3]
          if de[1].to_i > 0
            prop_name = ""
          else
            prop_name = I18n.t("journal_detail.#{de[1]}")
            case de[1]
            when "description"
              old_value = "描述已更新"
              value = "修改了描述"
            when "is_private"
              old_value = I18n.t("journal_detail.#{de[2]}")
              value = I18n.t("journal_detail.#{de[3]}")
            when "assigned_to_id"
              u = User.select(:id, :login, :lastname, :firstname, :nickname)
              old_value = de[2].to_i > 0 ? u.find_by_id(de[2]).try(:show_real_name) : ""
              assign_user = de[3].to_i > 0 ? u.find_by_id(de[3]) : ""
              if assign_user.present?
                value = assign_user.try(:show_real_name)
              else
                value = ""
              end

            when "tracker_id"
              t = Tracker.select(:id, :name)
              old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name) : ""
              tracker_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
              if tracker_name
                value = tracker_name.try(:name)
              else
                value = ""
              end

            when "status_id"
              t = IssueStatus.select(:id, :name)
              old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name) : ""
              type_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
              if type_name
                value = type_name.try(:name)
              else
                value = ""
              end
            when "priority_id"
              t = IssuePriority.select(:id, :name)
              old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name): ""
              type_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
              if type_name
                value = type_name.try(:name)
              else
                value = ""
              end
            when "issue_tags_value"
              t = IssueTag.select(:id, :name)
              old_value = de[2].to_i > 0 ? t.where(id: de[2].split(",")).select(:id,:name,:color).as_json : ""
              if de[3].present?
                value = t.where(id: de[3].split(",")).select(:id,:name,:color).as_json
              else
                value = ""
              end
            when "fixed_version_id"
              t = Version.select(:id, :name)
              old_value = de[2].to_i > 0 ? t.find_by_id(de[2]).try(:name) : ""
              type_name = de[3].to_i > 0 ? t.find_by_id(de[3]) : ""
              if type_name
                value = type_name.try(:name)
              else
                value = ""
              end
            when "end_time"
              t = IssueTime.select(:id, :start_time, :end_time)
              type_name = de[2].to_i > 0 ? t.find_by_id(de[2]) : ""
              if type_name.present?
                old_value = "停止工作"
                d_value = type_name.end_time.to_i - type_name.start_time.to_i
                value = "#{Time.at(d_value).utc.strftime('%H h %M min %S s')}"
              else
                old_value = "停止工作"
                value = ""
              end
            when "issue_depend"
              t = Issue.select(:id,:subject )
              type_name = de[3].present? ? t&.find_by_id(de[3]) : ""
              if type_name.present?
                old_value = "增加依赖"
                value = {
                  id: de[3],
                  name: type_name.try(:subject)
                }
              else
                old_value = "增加依赖"
                value = ""
              end
            when "destroy_issue_depend"
              t = Issue.select(:id,:subject )
              type_name = de[3].present? ? t&.find_by_id(de[3]) : ""
              if type_name.present?
                old_value = "删除依赖"
                value = {
                  id: de[3],
                  name: type_name.try(:subject)
                }
              else
                old_value = "删除依赖"
                value = ""
              end
            when "done_ratio"
              old_value = "#{de[2]}%"
              value = "#{de[3]}%"
            else
              old_value = de[2]
              value = de[3]
            end
          end
          prop_hash = {
            detail: (content + prop_name),
            old_value: old_value,
            value: value
          }
          send_details.push(prop_hash)
        end
      end
    end
    send_details
  end

  def to_builder
    Jbuilder.new do |journal|
      journal.(self, :id, :notes, :comments_count)
      if self.parent_journal.present?
        journal.parent_journal self.parent_journal.to_builder
      end
      if self.reply_journal.present?
        journal.reply_journal self.reply_journal.to_builder
      end
    end
  end
end
