gitlink-forgeplus/app/controllers/api/pm/projects_controller.rb

126 lines
4.5 KiB
Ruby

class Api::Pm::ProjectsController < Api::Pm::BaseController
before_action :require_login, except: [:convert]
before_action :load_project, only: [:convert]
def convert
data = {
owner: @project.owner.try(:login),
identifier: @project.identifier,
name: @project.name
}
render_ok(data: data)
end
def issues_count
return tip_exception '参数错误' unless params[:pm_project_id].present?
@issues = Issue.where(pm_project_id: params[:pm_project_id])
case params[:participant_category].to_s
when 'aboutme' # 关于我的
@issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: %w[authored assigned atme], participant_id: current_user&.id})
when 'authoredme' # 我创建的
@issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: 'authored', participant_id: current_user&.id})
when 'assignedme' # 我负责的
@issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: 'assigned', participant_id: current_user&.id})
when 'atme' # @我的
@issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: 'atme', participant_id: current_user&.id})
end
data = {}
@issues_count = @issues.group(:pm_project_id).count
# requirement 1 task 2 bug 3
@issues_type_count = @issues.group(:pm_project_id, :pm_issue_type).count
params[:pm_project_id].map(&:to_i).map do |project_id|
data[project_id] = {
total: @issues_count[project_id] || 0,
requirement: @issues_type_count[[project_id, 1]] || 0,
task: @issues_type_count[[project_id, 2]] || 0,
bug: @issues_type_count[[project_id, 3]] || 0
}
end
render_ok(data: data)
end
def statistics
return tip_exception '参数错误' if params[:pm_project_id].blank?
@issues = Issue.where(pm_project_id: params[:pm_project_id])
type_count_data = @issues.group(:pm_issue_type).count
type_status = @issues.group(:pm_issue_type,:status_id).count
type_status_data = {}
IssueStatus.all.map do |e|
[1,2,3].map{ |type|
type_status_data[type] = {} if type_status_data[type].nil?
if type_status[[type,e.id]].nil?
type_status_data[type][e.id] = 0
else
type_status_data[type][e.id] = type_status[[type,e.id]]
end
}
end
open_data = {
"1": type_status_data[1][1].to_i + type_status_data[1][2].to_i,
"2": type_status_data[2][1].to_i + type_status_data[2][2].to_i,
"3": type_status_data[3][1].to_i + type_status_data[3][2].to_i,
}
data = {
pie_chart: type_count_data,
bar_chart: type_status_data,
open_data: open_data
}
render_ok(data: data)
end
def polyline
return tip_exception '参数错误' if params[:pm_project_id].blank?
time_line = (Time.current.beginning_of_day - 6.day) .. Time.current
@create_issues = Issue.where(pm_project_id: params[:pm_project_id],created_on: time_line)
@due_issues = Issue.where(pm_project_id: params[:pm_project_id],due_date: time_line)
@create_issues_count = @create_issues.group(:pm_issue_type,"DATE(created_on)").count
@due_issues_count = @due_issues.group(:pm_issue_type,"DATE(due_date)").count
data = {
create_issues: {},
due_issues: {}
}
7.times do |time|
current_time = Date.current - time.day
if @create_issues_count.present?
data[:create_issues][current_time] = {
"1": @create_issues_count[[1,current_time]] || 0,
"2": @create_issues_count[[2,current_time]] || 0,
"3": @create_issues_count[[3,current_time]] || 0
}
else
data[:create_issues][current_time] = {
"1": 0,
"2": 0,
"3": 0
}
end
if @due_issues_count.present?
data[:due_issues][current_time] = {
"1": @due_issues_count[[1,current_time]] || 0,
"2": @due_issues_count[[2,current_time]] || 0,
"3": @due_issues_count[[3,current_time]] || 0
}
else
data[:due_issues][current_time] = {
"1": 0,
"2": 0,
"3": 0
}
end
end
render_ok(data: data)
end
def bind_project
return render_forbidden('您没有操作权限!') unless @project.member?(current_user) || current_user.admin?
Issue.where(pm_project_id: params[:pm_project_id], user_id: current_user).update_all(project_id: params[:project_id])
end
private
def load_project
@project = Project.joins(:owner).find params[:project_id]
end
end