class Api::Pm::WeeklyIssuesController < Api::Pm::BaseController

  def personal 
    @enterprise_identifier = params[:enterprise_identifier] || ''
    return render_error('请输入正确的用户ID.') if params[:user_id].blank?
    @all_issues = Issue.joins(:issue_participants).where(issue_participants: {participant_id: params[:user_id], participant_type: ['assigned']})
    @all_issues = @all_issues.where(enterprise_identifier: @enterprise_identifier, pm_issue_type: [1,2,3]) 
    @this_week_all_issues = @all_issues.where("due_date >= ? and start_date <= ?", Date.today.beginning_of_week.to_s, Date.today.end_of_week.to_s).distinct
    @this_week_all_issues = @this_week_all_issues.order('created_on desc')
    @next_week_all_issues = @all_issues.where("due_date >= ? and start_date <=?", (Date.today.beginning_of_week+1.week).to_s, (Date.today.end_of_week+1.week).to_s).distinct
    @next_week_all_issues = @next_week_all_issues.order('created_on desc')
    @this_week_requirement_issues = @this_week_all_issues.where(pm_issue_type: 1)
    @this_week_task_issues = @this_week_all_issues.where(pm_issue_type: 2)
    @this_week_bug_issues = @this_week_all_issues.where(pm_issue_type: 3)
    @close_requirement_issues = @this_week_requirement_issues.where(status_id: [3,5])
    @close_task_issues =  @this_week_task_issues.where(status_id: [3,5])
    @close_bug_issues =  @this_week_bug_issues.where(status_id: [3,5])
    this_week_page = params[:this_week_page] || 1
    this_week_limit = params[:this_week_limit] || 15
    @this_week_all_issues = @this_week_all_issues.page(this_week_page).per(this_week_limit)
    next_week_page = params[:next_week_page] || 1 
    next_week_limit = params[:next_week_limit] || 15
    @next_week_all_issues = @next_week_all_issues.page(next_week_page).per(next_week_limit)
  end

  def group 
    @enterprise_identifier = params[:enterprise_identifier] || ''
    @all_issues = Issue.where(enterprise_identifier: @enterprise_identifier, pm_issue_type: [1,2,3])
    @all_issues = @all_issues.where(pm_project_id: params[:pm_project_ids].split(",")) if params[:pm_project_ids].present?
    @this_week_all_issues = @all_issues.where("due_date >= ? and start_date <= ?", Date.today.beginning_of_week.to_s, Date.today.end_of_week.to_s)
    @this_week_all_issues_group_count = @this_week_all_issues.group(:pm_project_id).count 
    data = {}
    @this_week_all_issues_group_count.keys.each do |pm_project_id| 
      this_project_id_requirements = @this_week_all_issues.where(pm_project_id: pm_project_id, pm_issue_type: 1)
      this_project_id_tasks = @this_week_all_issues.where(pm_project_id: pm_project_id, pm_issue_type: 2)
      this_project_id_bugs = @this_week_all_issues.where(pm_project_id: pm_project_id, pm_issue_type: 3)
      this_project_id_close_requirements = this_project_id_requirements.where(status_id: [3,5])
      this_project_id_close_tasks = this_project_id_tasks.where(status_id: [3,5])
      this_project_id_close_bugs = this_project_id_bugs.where(status_id: [3,5])
      data[pm_project_id] = {
        this_week_requirement_issues_count: this_project_id_requirements.count,
        this_week_task_issues_count: this_project_id_tasks.count,
        this_week_bug_issues_count: this_project_id_bugs.count,
        close_requirement_issues_count: this_project_id_close_requirements.count, 
        close_task_issues_count: this_project_id_close_tasks.count,
        close_bug_issues_count: this_project_id_close_bugs.count,
        this_week_task_issues: this_project_id_tasks
      }

      
    end

    render :json => data
  end
  
  def group_issues
    @enterprise_identifier = params[:enterprise_identifier] || ''
    @all_issues = Issue.where(enterprise_identifier: @enterprise_identifier, pm_issue_type: [1,2,3])
    @all_issues = @all_issues.where(pm_project_id: params[:pm_project_ids].split(",")) if params[:pm_project_ids].present?
    @all_issues = @all_issues.where(pm_issue_type: params[:pm_issue_type].to_i) if params[:pm_issue_type].present?
    @this_week_all_issues = @all_issues.where("due_date >= ? and start_date <= ?", Date.today.beginning_of_week.to_s, Date.today.end_of_week.to_s)
    @this_week_all_issues = @this_week_all_issues.order('created_on desc')
    @this_week_all_issues = kaminari_paginate(@this_week_all_issues)
  end
  def personal_issues
    @enterprise_identifier = params[:enterprise_identifier] || ''
    return render_error('请输入正确的用户ID.') if params[:user_id].blank?
    @all_issues = Issue.joins(:issue_participants).where(issue_participants: {participant_id: params[:user_id], participant_type: ['assigned']})
    @all_issues = @all_issues.where(enterprise_identifier: @enterprise_identifier, pm_issue_type: [1,2,3]) 
    @this_week_all_issues = @all_issues.where("due_date >= ? and start_date <= ?", Date.today.beginning_of_week.to_s, Date.today.end_of_week.to_s).distinct
    @this_week_all_issues = @this_week_all_issues.order('created_on desc')
    @next_week_all_issues = @all_issues.where("due_date >= ? and start_date <=?", (Date.today.beginning_of_week+1.week).to_s, (Date.today.end_of_week+1.week).to_s).distinct
    @next_week_all_issues = @next_week_all_issues.order('created_on desc')
    this_week_page = params[:this_week_page] || 1
    this_week_limit = params[:this_week_limit] || 15
    @this_week_all_issues = @this_week_all_issues.page(this_week_page).per(this_week_limit)
    next_week_page = params[:next_week_page] || 1 
    next_week_limit = params[:next_week_limit] || 15
    @next_week_all_issues = @next_week_all_issues.page(next_week_page).per(next_week_limit)
  end
end

