# encoding: utf-8
#
# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

module UserScoreHelper
  def calculate_collaboration_count(user)
    issue_c = 0
    issues = Issue.where('author_id = ?', user.id)
    issues.each do |issue|
      issue_c = issue_c + issue.journals.where("user_id <> ?", user.id).count
    end
    issue_c = issue_c + Journal.where("user_id = ?", user.id).count
    ############################
    memos = Memo.where('author_id = ? AND parent_id IS NOT NULL', user.id)

    memos.each do |m|
      if Memo.find(m.parent_id).author.id != user.id
        issue_c = issue_c + 1
      else
        issue_c = issue_c - 1
      end
    end


    pmemos = Memo.where('author_id = ? AND parent_id IS NULL', user.id)
    pmemos.each do |pm|
      issue_c = issue_c + pm.replies_count
    end
    ############################

    issue_c = issue_c + JournalsForMessage.where('user_id = ? AND reply_id IS NOT NULL AND reply_id <> ?', user.id, user.id).count + JournalsForMessage.where('reply_id = ? AND user_id <> ?', user.id, user.id).count



    return issue_c
  end

  def calculate_influence_count(user)
    watcher_count = Watcher.where("watchable_type = 'principal' AND watchable_id = ?", user.id).count
  end




  def calculate_skill_count(user)

    praise_count_l0 = 0
    praise_count_l1 = 0
    praise_count_l2 = 0
    tread_count_l0 = 0
    tread_count_l1 = 0
    tread_count_l2 = 0
    issues = Issue.where('author_id = ?', user.id)
    issues.each do |i|
      pts = PraiseTread.where('praise_tread_object_id = ?', i.id)
      pts.each do |p|
        templevel = calculate_level(User.find(p.user_id))

        if templevel.to_i == 0
          if p.praise_or_tread == 1
            praise_count_l0 = praise_count_l0 + 1
          else
            tread_count_l0 = tread_count_l0 + 1
          end
        end
        if templevel.to_i == 1
          if p.praise_or_tread == 1
            praise_count_l1 = praise_count_l1 + 1
          else
            tread_count_l1 = tread_count_l1 + 1
          end
        end
        if templevel.to_i == 2
          if p.praise_or_tread == 1
            praise_count_l2 = praise_count_l2 + 1
          else
            tread_count_l2 + tread_count_l2 + 1
          end
        end
      end
    end




    bids = Bid.where('author_id = ?', user.id)
    bids.each do |b|
      ptcs = PraiseTread.where('praise_tread_object_id = ?', b.id)
      ptcs.each do |p|
        templevel = calculate_level(User.find(p.user_id))

        if templevel.to_i == 0
          if p.praise_or_tread == 1
            praise_count_l0 = praise_count_l0 + 1
          else
            tread_count_l0 = tread_count_l0 + 1
          end
        end
        if templevel.to_i == 1
          if p.praise_or_tread == 1
            praise_count_l1 = praise_count_l1 + 1
          else
            tread_count_l1 = tread_count_l1 + 1
          end
        end
        if templevel.to_i == 2
          if p.praise_or_tread == 1
            praise_count_l2 = praise_count_l2 + 1
          else
            tread_count_l2 + tread_count_l2 + 1
          end
        end
      end
    end

    contests = Contest.where('author_id = ?', user.id)
    contests.each do |c|
      ptcs = PraiseTread.where('praise_tread_object_id = ?', c.id)
      ptcs.each do |p|
        templevel = calculate_level(User.find(p.user_id))

        if templevel.to_i == 0
          if p.praise_or_tread == 1
            praise_count_l0 = praise_count_l0 + 1
          else
            tread_count_l0 = tread_count_l0 + 1
          end
        end
        if templevel.to_i == 1
          if p.praise_or_tread == 1
            praise_count_l1 = praise_count_l1 + 1
          else
            tread_count_l1 = tread_count_l1 + 1
          end
        end
        if templevel.to_i == 2
          if p.praise_or_tread == 1
            praise_count_l2 = praise_count_l2 + 1
          else
            tread_count_l2 + tread_count_l2 + 1
          end
        end
      end
    end



#    case level
#      when 0 skill_score = praise_count - 0.5 * tread_count
#      when 1 skill_score = 2 * praise_count - 1.5 * tread_count
#      when 2 skill_socre = 3 * praise_count - 2.5 * tread_count
#      when 3 skill_socre = 4 * praise_count - 3.5 * tread_count
#      when 4 skill_socre = 5 * praise_count - 4.5 * tread_count
#    end

    skill_score = 2 * praise_count_l0.to_f + 3 * praise_count_l1.to_f + 4 * praise_count_l2.to_f
                  - 1 * tread_count_l0.to_f - 1.5 * tread_count_l1.to_f - 2 * tread_count_l2.to_f

    tread_user_count = PraiseTread.where('praise_or_tread = ? AND user_id = ?', 0, user.id).count

    skill_score = skill_score.to_f - tread_user_count.to_f







    return skill_score



  end

  def calculate_level(user)
    commit_count = user.changesets.count
    max_praise_num = 0


    issues = Issue.where('author_id = ?', user.id)
    issues.each do |i|
      ptcs = PraiseTreadCache.where('object_id = ?', i.id)
      ptcs.each do |p|
        if p.praise_num.to_i > max_praise_num.to_i
          max_praise_num = p.praise_num
        end
      end
    end

    bids = Bid.where('author_id = ?', user.id)
    bids.each do |b|
      ptcs = PraiseTreadCache.where('object_id = ?', b.id)
      ptcs.each do |p|
        if p.praise_num.to_i > max_praise_num.to_i
          max_praise_num = p.praise_num
        end
      end
    end

    contests = Contest.where('author_id = ?', user.id)
    contests.each do |c|
      ptcs = PraiseTreadCache.where('object_id = ?', c.id)
      ptcs.each do |p|
        if p.praise_num.to_i > max_praise_num.to_i
          max_praise_num = p.praise_num
        end
      end
    end


    best_answer_num = 0

    isManager = 0
    members = Member.where('user_id = ?', user.id)
    members.each do |m|
      @membership = m.memberships.all(:conditions => Project.visible_condition(User.current))
      @membership.each do |membership|
        #拥有编辑项目权限的可操作该项目
        if m.allowed_to?(:is_manager, membership.project, :global => false)
          isManager = 1
        end
      end


    end

    level = 0

    if max_praise_num > 4
      level = 1
    end
    if commit_count > 0 and commit_count < 101
      level = 1
    end
    if commit_count > 100 or isManager == 1
      level = 2
    end

    return level

  end

  def calculate_activity_count(user)
    # commit_count = user.changesets.count
    # issue_details_count = 0
    # issues = Issue.where('assigned_to_id = ?', user.id)
    # issues.each do |issue|
    # change_count = issue.journals.where("prop_key = ?", "done_ratio").count
    # issue_details_count = change_count + issue_details_count
    # end
    # file_count = user.file_commit.count
    # issue_count = Issue.where('author_id = ?', user.id).count
    #f = user.user_score.file
    #i = user.user_score.issue
    #f_max = UserScore.find_max_file
    #f_min = UserScore.find_min_file
    #i_max = UserScore.find_max_issue
    #i_min = UserScore.find_min_issue
    #score = 100 * ((f - f_min)/(f_max - f_min) + (i - i_min)/(i_max - i_min))
  end

  def calculate_file(user)
    file_count = user.file_commit.count
    issue_count = Issue.where('author_id = ?', user.id).count
    return (file_count + issue_count)
  end

  def calculate_issue(user)
    commit_count = user.changesets.count

    issue_details_count = 0
    issues = Issue.where('assigned_to_id = ?', user.id)

    change_count = 0
    issues.each do |issue|
      js = issue.journals
      js.each do |j|
        change_count = change_count + j.details.where("prop_key = ?", "done_ratio").count
      end


      issue_details_count = change_count + issue_details_count
    end

    return (commit_count + issue_details_count)

  end


  def calculate_attachments(user)
    attachments = Attachment.where("container_type IS NOT NULL AND container_type <> 'Issue' AND author_id = ?", user.id).count

    return attachments

  end

  def calculate_user_score(user)
    collaboration = calculate_collaboration_count(user)
    influence = calculate_influence_count(user)
    skill = calculate_skill_count(user)
    activity = calculate_activity_count(user)
    file = calculate_file(user)
    issue = calculate_issue(user)
    level = calculate_level(user)
    #user.user_score << UserScore.new(:collaboration => collaboration, :influence => influence, :skill => skill,
    #                                 :activity => activity, :file => file, :issue => issue, :level => level)
    UserScore.new(:collaboration => collaboration, :influence => influence, :skill => skill,
                                     :activity => activity, :file => file, :issue => issue, :level => level)
  end

  def update_user_score(user)
    collaboration = calculate_collaboration_count(user)
    influence = calculate_influence_count(user)
    skill = calculate_skill_count(user)
    file = calculate_file(user)
    issue = calculate_issue(user)

    ##activity = calculate_activity_count(user)

    level = calculate_level(user)
    user.user_score.update_attributes(:collaboration => collaboration, :influence => influence, :skill => skill,
                                      :activity => activity, :file => file, :issue => issue, :level => level)
  end

  #========================================================================================================
  #个人得分统计
  #========================================================================================================
  #type  1：个人得分、2：个人在项目project中的得分
  def get_option_number(user,type,project=nil)
    if project.nil?
      option_number = OptionNumber.where("user_id = '#{user.id}' and score_type = '#{type}'");
    else
      option_number = OptionNumber.where("user_id = '#{user.id}' and score_type = '#{type}' and project_id = '#{project.id}'");
    end

     result = nil
     if option_number.nil? || option_number.count == 0
       result = OptionNumber.new
       result.user_id = user.id
       result.memo = 0
       result.messages_for_issues = 0
       result.issues_status = 0
       result.replay_for_message = 0
       result.replay_for_memo = 0
       result.follow = 0
       result.tread = 0
       result.praise_by_one = 0
       result.praise_by_two = 0
       result.praise_by_three = 0
       result.tread_by_one = 0
       result.tread_by_two = 0
       result.tread_by_three = 0
       result.changeset = 0
       result.document = 0
       result.attachment = 0
       result.issue_done_ratio = 0
       result.post_issue = 0
       result.total_score = 0
       result.score_type  =type
       unless project.nil?
          result.project_id = project.id
       end
     else
       result = option_number.first
     end
     result
  end

  def get_option_num_by_id(user_id,type,project_id=nil)
    if project_id.nil?
      option_number = OptionNumber.where("user_id = '#{user_id}' and score_type = '#{type}'");
    else
      option_number = OptionNumber.where("user_id = '#{user_id}' and score_type = '#{type}' and project_id = '#{project_id}'");
    end

    result = nil
    if option_number.nil? || option_number.count == 0
      result = OptionNumber.new
      result.user_id = user_id
      result.memo = 0
      result.messages_for_issues = 0
      result.issues_status = 0
      result.replay_for_message = 0
      result.replay_for_memo = 0
      result.follow = 0
      result.tread = 0
      result.praise_by_one = 0
      result.praise_by_two = 0
      result.praise_by_three = 0
      result.tread_by_one = 0
      result.tread_by_two = 0
      result.tread_by_three = 0
      result.changeset = 0
      result.document = 0
      result.attachment = 0
      result.issue_done_ratio = 0
      result.post_issue = 0
      result.total_score = 0
      result.score_type  =type
      unless project_id.nil?
        result.project_id = project_id
      end
    else
      result = option_number.first
    end
    result
  end

  #更新分数
  def update_score(option_number)
    option_number.total_score = collaboration(option_number) + influence(option_number) + skill(option_number) + project_active(option_number)
    if option_number.total_score < 0
      option_number.total_score = 0
    end
    option_number.save
    option_number.total_score
  end

  #协同得分
  def collaboration(option_number)
    option_number.memo * 2 + option_number.messages_for_issues + option_number.issues_status + option_number.replay_for_message + option_number.replay_for_memo
  end
  #影响力得分
  def influence(option_number)
    option_number.follow * 2
  end
  #技术得分
  def skill(option_number)
     option_number.praise_by_one * 4  + option_number.praise_by_two * 6 + option_number.praise_by_three * 8 - option_number.tread * 2 - option_number.tread_by_one * 2 - option_number.tread_by_two * 4 - option_number.tread_by_three * 6
  end
  #项目贡献得分
  def project_active(option_number)
    option_number.changeset * 4 + option_number.document * 4 + option_number.attachment * 4 + option_number.issue_done_ratio * 2 + option_number.post_issue * 4
  end

  #更新发帖数
  def update_memo_number(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.memo = memo_num(user,project)#Message.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count +  Memo.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count
    update_score(option_number)
  end

  #发帖数
  def memo_num(user,project=nil)
    if project.nil?
      #Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count #+  Memo.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count
      users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM `messages` JOIN `boards` ON boards.project_id != -1 AND  messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = #{user.id}")
      result = 0
      users.each do |user|
        result = user.m_count
      end
      result
      #user.messages.where("parent_id IS NULL").count
    else
      #Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
      users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM `messages` JOIN `boards` ON boards.project_id = '#{project.id}' AND  messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = #{user.id}")
      result = 0
      users.each do |user|
         result = user.m_count
      end
      result
    end

  end

  def memo_score(user,project=nil)
     if project.nil?
       User.find_by_sql("SELECT `users`.id,(SELECT COUNT(*) * 2 FROM `messages` JOIN `boards` ON boards.project_id != -1 AND  messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = `users`.id) AS m_count
FROM `users` where id = #{user.id}")
     else
       User.find_by_sql("SELECT `users`.id,(SELECT COUNT(*) * 2 FROM `messages` JOIN `boards` ON boards.project_id = '#{project.id}' AND  messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = `users`.id) AS m_count
FROM `users` where id = #{user.id}")
     end
  end

  #更新对缺陷留言数
  def update_messges_for_issue(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.messages_for_issues = messges_for_issue_num(user,project)#Journal.includes(:user).where("user_id = '#{user.id}' and notes != '' and notes is not null").all.count
    update_score(option_number)
  end

  def messges_for_issue_num(user,project=nil)
    if project.nil?
      #Journal.includes(:user).where("user_id = '#{user.id}' and notes is not null and notes != ''").all.count
      users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals WHERE journals.user_id = #{user.id} AND journals.notes IS NOT NULL AND journals.notes != ''")
      result = 0
      if users.count > 0
         result = users.first.m_count
      end
      result
    else
      #Journal.includes(:user).joins(:issue).where("#{Journal.table_name}.user_id = '#{user.id}' and #{Issue.table_name}.project_id = '#{project.id}' and #{Journal.table_name}.notes != '' and #{Journal.table_name}.notes is not null").all.count
      users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals join issues on #{Journal.table_name}.journalized_type = 'Issue' and #{Journal.table_name}.journalized_id = #{Issue.table_name}.id WHERE journals.user_id = #{user.id} AND journals.notes IS NOT NULL AND journals.notes != ''and #{Issue.table_name}.project_id = '#{project.id}'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end
  end

  def messges_for_issue_score(user,project=nil)
    if project.nil?
     User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals WHERE journals.user_id = users.id AND journals.notes IS NOT NULL AND journals.notes != '') AS m_score FROM users where id = #{user.id}")
    else
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals join issues on #{Journal.table_name}.journalized_type = 'Issue' and #{Journal.table_name}.journalized_id = #{Issue.table_name}.id WHERE journals.user_id = users.id AND journals.notes IS NOT NULL AND journals.notes != ''and #{Issue.table_name}.project_id = '#{project.id}') AS m_score FROM users where id = #{user.id}")
    end
  end

  #更新更改缺陷状态状态次数
  def update_issues_status(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.issues_status = issues_status_num(user,project)#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
    update_score(option_number)
  end

  def issues_status_num(user,project=nil)
    if project.nil?
      #Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
      users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = #{user.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
      users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = #{user.id} and issues.project_id = '#{project.id}'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end

  end

  def  issues_status_score(user,project=nil)
    if project.nil?
      #Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id) AS m_count FROM users WHERE users.id = '#{user.id}'")
    else
      #Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id and issues.project_id = '#{project.id}') AS m_count FROM users WHERE users.id = '#{user.id}'")
    end
  end

  #更新对留言的回复数量
  def update_replay_for_message(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.replay_for_message = replay_for_message_num(user,project)#JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id}").count
    update_score(option_number)
  end

  def replay_for_message_num(user,project=nil)
    if project.nil?
      #JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project'").count
      users = JournalsForMessage.find_by_sql("SELECT COUNT(*) as m_count From #{JournalsForMessage.table_name} WHERE m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project' and jour_id = '#{project.id}'").count
      users = JournalsForMessage.find_by_sql("SELECT COUNT(*) as m_count From #{JournalsForMessage.table_name} WHERE m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project' and jour_id = '#{project.id}'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end

  end

  def replay_for_message_score(user,project=nil)
     if project.nil?
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project') as m_score FROM users WHERE users.id = '#{user.id}'")
     else
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project' and jour_id = '#{project.id}') as m_score FROM users WHERE users.id = '#{user.id}'")
     end
  end

  #更新对帖子的回复数量
  def update_replay_for_memo(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.replay_for_memo = replay_for_memo_num(user,project)#Message.includes(:author).where("parent_id IS NOT NULL and author_id = #{user.id}").all.count #+ Memo.includes(:author).where("parent_id IS NOT NULL and author_id = #{user.id}").all.count
    update_score(option_number)
  end

  def replay_for_memo_num(user,project=nil)
    if project.nil?
      #Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count
      users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = #{user.id} AND boards.project_id != -1")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
      users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = #{user.id} AND boards.project_id = #{project.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end
  end

  def replay_for_memo_score(user,project=nil)
    if project.nil?
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = users.id AND boards.project_id != -1) FROM users WHERE users.id = #{user.id}")
    else
      User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = users.id AND boards.project_id = #{project.id}) FROM users WHERE users.id = #{user.id}")
    end
  end

  #更新被关注的人数
  def update_follow(user,type)
    option_number = get_option_number(user,type)
    option_number.follow = follow_num(user)#Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
    update_score(option_number)
  end

  def follow_num(user)
    #Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
    users = Watcher.find_by_sql("SELECT COUNT(*) as m_count FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = #{user.id}")
    result = 0
    if users.count > 0
      result = users.first.m_count
    end
    result
  end

  def follow_score(user)
    User.find_by_sql("SELECT users.id, (SELECT COUNT(*) * 2 FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = users.id) FROM users WHERE users.id = '#{user.id}'")
  end

  #更新帖子踩各项数量
  def update_tread(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    tread_nums = tread_num(user,project)
    option_number.tread = tread_nums[:tread]
    option_number.tread_by_one = tread_nums[:tead_by_one]
    option_number.tread_by_two = tread_nums[:tread_by_two]
    option_number.tread_by_three = tread_nums[:tread_by_three]
    update_score(option_number)
  end

  def tread_num(user,project=nil)
    if project.nil?
      result0 = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0 and user_id = '#{user.id}'").all.count
      pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0").all
      result = []
      result1 = []
      result2 = []
      pts.each do |pt|
        obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
        if obj.nil?
          next
        end
        target_user = obj.author
        level = UserLevels.get_level(pt.user)#pt.user.get_level
        #project = pt.project
        if level == 1 && target_user.id == user.id
          result << pt
        elsif level == 2 &&  target_user.id == user.id
          result1 << pt
        elsif level == 3 && target_user.id == user.id
          result2 << pt
        end
      end
      {:tread=>result0,:tead_by_one => result.count,:tread_by_two => result1.count,:tread_by_three =>result2.count}
    else
      result0 = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0 and user_id = '#{user.id}'").all.count
      pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0").all
      result = []
      result1 = []
      result2 = []
      pts.each do |pt|
        obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
        if obj.nil? || (pt.praise_tread_object_type == "Issue" && obj.project.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.project.nil?)
          next
        end
        if !(pt.praise_tread_object_type == "Issue" && obj.project.id == project.id) && !(pt.praise_tread_object_type == "Message" && obj.board.project.id == project.id)
          next
        end
        target_user = obj.author
        level = UserLevels.get_level(pt.user)#pt.user.get_level
        #project = pt.project
        if level == 1 && target_user.id == user.id
          result << pt
        elsif level == 2 &&  target_user.id == user.id
          result1 << pt
        elsif level == 3 && target_user.id == user.id
          result2 << pt
        end
      end
      {:tread=>result0,:tead_by_one => result.count,:tread_by_two => result1.count,:tread_by_three =>result2.count}
    end

  end

  #更新帖子顶数量
  def update_praise(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    praise_nums = praise_num(user,project)
    option_number.praise_by_one = praise_nums[:praise_by_one]
    option_number.praise_by_two = praise_nums[:praise_by_two]
    option_number.praise_by_three = praise_nums[:praise_by_three]
    update_score(option_number)
  end

  def praise_num(user,project=nil)
    if !project.nil?
      pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 1").all
      result = []
      result1 = []
      result2 = []
      pts.each do |pt|
        obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
        if obj.nil? || (pt.praise_tread_object_type == "Issue" && obj.project.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.project.nil?)
          next
        end
        if !(pt.praise_tread_object_type == "Issue" && obj.project.id == project.id) && !(pt.praise_tread_object_type == "Message" && obj.board.project.id == project.id)
          next
        end
        target_user = obj.author
        level = UserLevels.get_level(pt.user)#pt.user.get_level
        #project = pt.project
        if level == 1 && target_user.id == user.id
          result << pt
        elsif level == 2 &&  target_user.id == user.id
          result1 << pt
        elsif level == 3 && target_user.id == user.id
          result2 << pt
        end
      end
      {:praise_by_one => result.count,:praise_by_two => result1.count,:praise_by_three => result2.count}
    else
      pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 1").all
      result = []
      result1 = []
      result2 = []
      pts.each do |pt|
        obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
        if obj.nil?
          next
        end
        #if obj.project.id == -1
        #  next
        #end
        target_user = obj.author
        level = UserLevels.get_level(pt.user)#pt.user.get_level
        project = pt.project
        if level == 1 && target_user.id == user.id
          result << pt
        elsif level == 2 &&  target_user.id == user.id
          result1 << pt
        elsif level == 3 && target_user.id == user.id
          result2 << pt
        end
      end
      {:praise_by_one => result.count,:praise_by_two => result1.count,:praise_by_three => result2.count}
    end

  end

  #更新提交代码次数
  def update_changeset(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.changeset = changeset_num(user,project)#Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
    update_score(option_number)
  end

  def changeset_num(user,project=nil)
    if project.nil?
      #Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
      users = Changeset.find_by_sql("SELECT COUNT(*) as m_count FROM #{Changeset.table_name} WHERE user_id = #{user.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Changeset.includes(:user).joins(:repository).where("#{Changeset.table_name}.user_id = '#{user.id}' and #{Repository.table_name}.project_id = #{project.id}").all.count
      users = Changeset.find_by_sql("SELECT COUNT(*) as m_count FROM #{Changeset.table_name} JOIN #{Repository.table_name} ON #{Changeset.table_name}.repository_id = #{Repository.table_name}.id WHERE #{Changeset.table_name}.user_id = #{user.id} and #{Repository.table_name}.project_id = #{project.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end

  end

  #更新文档提交次数
  def update_document(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.document = document_num(user,project)#Document.includes(:user).where("user_id = '#{user.id}'").all.count
    update_score(option_number)
  end

  def document_num(user,project=nil)
    if project.nil?
      #Document.includes(:user).where("user_id = '#{user.id}'").all.count
      users = Document.find_by_sql("SELECT COUNT(*) as m_count FROM #{Document.table_name} WHERE #{Document.table_name}.user_id = #{user.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Document.includes(:user,:project).where("user_id = '#{user.id}' and project_id = '#{project.id}'").all.count
      users = Document.find_by_sql("SELECT COUNT(*) as m_count FROM #{Document.table_name} WHERE #{Document.table_name}.user_id = #{user.id} and #{Document.table_name}.project_id = '#{project.id}'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end

  end

  #更新附件提交数量
  def update_attachment(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.attachment =  attachment_num(user,project)#Attachment.includes(:author).where("author_id = '#{user.id}'").all.count
    update_score(option_number)
  end

  def attachment_num(user,project=nil)
    if project.nil?
      #Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project'").all.count
      users = Attachment.find_by_sql("SELECT COUNT(*) as m_count FROM #{Attachment.table_name} WHERE author_id = '#{user.id}' and container_type = 'Project'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}").all.count
      users = Attachment.find_by_sql("SELECT COUNT(*) as m_count FROM #{Attachment.table_name} WHERE author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end
  end

  #更新缺陷完成度次数
  def update_issue_done_ratio(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.issue_done_ratio = issue_done_ratio_num(user,project) #Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
    update_score(option_number)
  end

  def issue_done_ratio_num(user,project=nil)
    if project.nil?
      #Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
      users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM #{Journal.table_name} JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id WHERE #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{Journal.table_name}.user_id = #{user.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
      users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM #{Journal.table_name} JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{Journal.table_name}.user_id = #{user.id} and #{Issue.table_name}.project_id = '#{project.id}'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end

  end

  #更新发布缺陷次数
  def update_post_issue(user,type,project=nil)
    option_number = get_option_number(user,type,project)
    option_number.post_issue = post_issue_num(user,project) #Issue.includes(:author).where("author_id = '#{user.id}'").all.count
    update_score(option_number)
  end

  def post_issue_num(user,project=nil)
    if project.nil?
      #Issue.includes(:author).where("author_id = '#{user.id}'").all.count
      users = Issue.find_by_sql("SELECT COUNT(*) as m_count FROM #{Issue.table_name} WHERE author_id = #{user.id}")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    else
      #Issue.includes(:author).where("author_id = '#{user.id}' and project_id = '#{project.id}'").all.count
      users = Issue.find_by_sql("SELECT COUNT(*) as m_count FROM #{Issue.table_name} WHERE author_id = #{user.id} and project_id = '#{project.id}'")
      result = 0
      if users.count > 0
        result = users.first.m_count
      end
      result
    end
  end

  #读取项目成员得分
  def read_user_project_scores(user,project)
    option_num = get_option_number(user,2,project)
    option_num.total_score
  end

  def user_scores(user,type,project=nil)
    ooption_num = get_option_number(user,type,project)
    update_memo_number(user,type,project)
    update_messges_for_issue(user,type,project)
    update_issues_status(user,type,project)
    update_replay_for_message(user,type,project)
    update_replay_for_memo(user,type,project)
    update_tread(user,type,project)
    update_praise(user,type,project)
    update_changeset(user,type,project)
    update_document(user,type,project)
    update_attachment(user,type,project)
    update_issue_done_ratio(user,type,project)
    update_post_issue(user,type,project)
    if project.nil?
      update_follow(user,type)
    end
    update_score(ooption_num)
    ooption_num
  end
end
