|
|
class IssuesController < ApplicationController
|
|
|
before_action :require_login
|
|
|
before_action :set_project
|
|
|
before_action :check_project_public, only: [:index ,:show, :copy, :index_chosen]
|
|
|
before_action :check_issue_permission, except: [:index, :show, :copy, :index_chosen]
|
|
|
before_action :set_issue, only: [:edit, :update, :destroy, :show, :copy, :index_chosen]
|
|
|
|
|
|
def index
|
|
|
issues = @project.issues.includes(:user,:tracker, :issue_tag, :version, :issue_status, :journals)
|
|
|
|
|
|
issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user))
|
|
|
@all_issues_size = issues.size
|
|
|
@open_issues_size = issues.where.not(status_id: 5).size
|
|
|
@close_issues_size = issues.where(status_id: 5).size
|
|
|
@assign_to_me_size = issues.where(assigned_to_id: current_user&.id).size
|
|
|
@my_published_size = issues.where(author_id: current_user&.id).size
|
|
|
|
|
|
status_type = params[:status_type].to_s #issue状态的选择
|
|
|
search_name = params[:search].to_s
|
|
|
start_time = params[:start_date]
|
|
|
end_time = params[:due_date]
|
|
|
|
|
|
if status_type.to_s == "1" #表示开启中的
|
|
|
issues = issues.where.not(status_id: 5)
|
|
|
elsif status_type.to_s == "2" #表示关闭中的
|
|
|
issues = issues.where(status_id: 5)
|
|
|
end
|
|
|
|
|
|
if search_name.present?
|
|
|
issues = issues.where("subject like ?", "%#{search_name}%")
|
|
|
end
|
|
|
|
|
|
if start_time&.present? || end_time&.present?
|
|
|
issues = issues.where("start_date between ? and ?",start_time&.present? ? start_time.to_date : Time.now.to_date, end_time&.present? ? end_time.to_date : Time.now.to_date)
|
|
|
end
|
|
|
|
|
|
issues = issues.where(author_id: params[:author_id]) if params[:author_id].present?
|
|
|
issues = issues.where(assigned_to_id: params[:assigned_to_id]) if params[:assigned_to_id].present?
|
|
|
issues = issues.where(tracker_id: params[:tracker_id]) if params[:tracker_id].present?
|
|
|
issues = issues.where(status_id: params[:status_id]) if params[:status_id].present?
|
|
|
issues = issues.where(priority_id: params[:priority_id]) if params[:priority_id].present?
|
|
|
issues = issues.where(fixed_version_id: params[:fixed_version_id]) if params[:fixed_version_id].present?
|
|
|
issues = issues.where(done_ratio: params[:done_ratio].to_i) if params[:done_ratio].present?
|
|
|
|
|
|
order_type = params[:order_type] || "desc" #或者"asc"
|
|
|
order_name = params[:order_name] || "created_on" #或者"updated_on"
|
|
|
|
|
|
@page = params[:page]
|
|
|
@limit = params[:limit] || "15"
|
|
|
@issues = issues.order("#{order_name} #{order_type}")
|
|
|
@issues_size = issues.size
|
|
|
@issues = issues.order("#{order_name} #{order_type}").page(@page).per(@limit)
|
|
|
|
|
|
respond_to do |format|
|
|
|
format.json
|
|
|
format.xlsx{
|
|
|
set_export_cookies
|
|
|
export_issues(@issues)
|
|
|
export_name = "#{@project.name}_issues列表_#{Time.now.strftime('%Y%m%d_%H%M%S')}"
|
|
|
render xlsx: "#{export_name.strip}",template: "issues/index.xlsx.axlsx",locals: {table_columns:@table_columns,issues:@export_issues}
|
|
|
}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def index_chosen
|
|
|
@issue_chosen = issue_left_chosen(@project, nil)
|
|
|
end
|
|
|
|
|
|
def commit_issues
|
|
|
issues = @project.issues.includes(:user,:tracker)
|
|
|
issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user))
|
|
|
@all_issues_size = issues.size
|
|
|
@open_issues_size = issues.where.not(status_id: 5).size
|
|
|
@close_issues_size = issues.where(status_id: 5).size
|
|
|
|
|
|
status_type = params[:status_type].to_s
|
|
|
|
|
|
if status_type.to_s == "1" #表示开启中的
|
|
|
issues = issues.where.not(status_id: 5)
|
|
|
elsif status_type.to_s == "2" #表示关闭中的
|
|
|
issues = issues.where(status_id: 5)
|
|
|
end
|
|
|
|
|
|
@commit_issues = []
|
|
|
total_commit_issues = {
|
|
|
name: "合计",
|
|
|
user_login: nil,
|
|
|
all_count: issues.size,
|
|
|
trackers: trackers_size(issues)
|
|
|
}
|
|
|
@commit_issues.push(total_commit_issues)
|
|
|
|
|
|
members = issues.pluck(:assigned_to_id).uniq
|
|
|
members.each do |m|
|
|
|
user = User.select(:id, :login, :firstname, :lastname).find(m)
|
|
|
user_issues = issues.where(assigned_to_id: m) #指派给
|
|
|
member_params = {
|
|
|
name: user.try(:show_real_name),
|
|
|
user_login: user.try(:login),
|
|
|
all_count: issues.size,
|
|
|
trackers: trackers_size(user_issues)
|
|
|
}
|
|
|
@commit_issues.push(member_params)
|
|
|
end
|
|
|
|
|
|
un_assign = issues.where(assigned_to_id: nil)
|
|
|
total_commit_issues = {
|
|
|
name: "未指派",
|
|
|
user_login: nil,
|
|
|
all_count: un_assign.size,
|
|
|
trackers: trackers_size(un_assign)
|
|
|
}
|
|
|
@commit_issues.push(total_commit_issues)
|
|
|
|
|
|
end
|
|
|
|
|
|
def new
|
|
|
@issue_chosen = issue_left_chosen(@project, nil)
|
|
|
end
|
|
|
|
|
|
def create
|
|
|
if params[:subject].blank?
|
|
|
normal_status(-1, "标题不能为空")
|
|
|
else
|
|
|
issue_params = {
|
|
|
subject: params[:subject],
|
|
|
description: params[:description],
|
|
|
is_private: params[:is_private],
|
|
|
assigned_to_id: params[:assigned_to_id],
|
|
|
tracker_id: params[:tracker_id],
|
|
|
status_id: params[:status_id],
|
|
|
priority_id: params[:priority_id],
|
|
|
fixed_version_id: params[:fixed_version_id],
|
|
|
start_date: params[:start_date].to_s.to_date,
|
|
|
due_date: params[:due_date].to_s.to_date,
|
|
|
estimated_hours: params[:estimated_hours],
|
|
|
done_ratio: params[:done_ratio]
|
|
|
}
|
|
|
@issue = Issue.new(issue_params.merge(author_id: current_user.id, project_id: @project.id))
|
|
|
if @issue.save!
|
|
|
if params[:attachment_ids].present?
|
|
|
params[:attachment_ids].each do |id|
|
|
|
attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id)
|
|
|
unless attachment.blank?
|
|
|
attachment.container = @issue
|
|
|
attachment.save
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
if params[:assigned_to_id].present?
|
|
|
Tiding.create!(user_id: params[:assigned_to_id], trigger_user_id: current_user.id,
|
|
|
container_id: @issue.id, container_type: 'Issue',
|
|
|
parent_container_id: @project.id, parent_container_type: "Project",
|
|
|
tiding_type: 'issue', status: 0)
|
|
|
end
|
|
|
normal_status(0, "创建成功")
|
|
|
else
|
|
|
normal_status(-1, "创建失败")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
def edit
|
|
|
@issue_chosen = issue_left_chosen(@project, @issue.id)
|
|
|
@issue_attachments = @issue.attachments
|
|
|
end
|
|
|
|
|
|
def update
|
|
|
issue_params = {
|
|
|
subject: params[:subject],
|
|
|
description: params[:description],
|
|
|
is_private: params[:is_private],
|
|
|
assigned_to_id: params[:assigned_to_id],
|
|
|
tracker_id: params[:tracker_id],
|
|
|
status_id: params[:status_id],
|
|
|
priority_id: params[:priority_id],
|
|
|
fixed_version_id: params[:fixed_version_id],
|
|
|
start_date: params[:start_date].to_s.to_date,
|
|
|
due_date: params[:due_date].to_s.to_date,
|
|
|
estimated_hours: params[:estimated_hours],
|
|
|
done_ratio: params[:done_ratio],
|
|
|
closed_on: (params[:status_id].to_i == 5) ? Time.now : nil
|
|
|
}
|
|
|
|
|
|
if @issue.update_attributes(issue_params)
|
|
|
issue_files = params[:attachment_ids]
|
|
|
change_files = false
|
|
|
issue_file_ids = []
|
|
|
if issue_files.present?
|
|
|
issue_attachments = @issue&.attachments
|
|
|
issue_file_ids = issue_attachments&.pluck(:id)
|
|
|
left_file_ids = issue_file_ids - issue_files
|
|
|
new_file_ids = issue_files - issue_file_ids
|
|
|
if left_file_ids.size > 0
|
|
|
change_files = true
|
|
|
issue_attachments.where(id: left_file_ids).delete_all
|
|
|
end
|
|
|
if new_file_ids.size > 0
|
|
|
change_files = true
|
|
|
new_file_ids.each do |id|
|
|
|
attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id)
|
|
|
unless attachment.blank?
|
|
|
attachment.container = @issue
|
|
|
attachment.save
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
@issue.create_journal_detail(change_files, issue_files, issue_file_ids)
|
|
|
normal_status(0, "更新成功")
|
|
|
else
|
|
|
normal_status(-1, "更新失败")
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
def show
|
|
|
@issue_attachments = @issue.attachments
|
|
|
@issue_user = @issue.user
|
|
|
@issue_assign_to = @issue.get_assign_user
|
|
|
end
|
|
|
|
|
|
def destroy
|
|
|
if @issue.destroy
|
|
|
normal_status(0, "删除成功")
|
|
|
else
|
|
|
normal_status(-1, "删除失败")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def copy
|
|
|
@new_issue = @issue.dup
|
|
|
if @new_issue.save
|
|
|
@status = 1
|
|
|
else
|
|
|
@status = -1
|
|
|
end
|
|
|
end
|
|
|
|
|
|
private
|
|
|
def set_project
|
|
|
@project = Project.find_by_id(params[:project_id])
|
|
|
unless @project.present?
|
|
|
normal_status(-1, "项目不存在")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def check_project_public
|
|
|
unless @project.is_public || @project.member?(current_user) || current_user.admin?
|
|
|
normal_status(-1, "您没有权限")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def set_issue
|
|
|
@issue = Issue.find_by_id(params[:id])
|
|
|
unless @issue.present?
|
|
|
normal_status(-1, "标签不存在")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def check_issue_permission
|
|
|
unless @project.member?(current_user) || current_user.admin?
|
|
|
normal_status(-1, "您没有权限")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def issue_left_chosen(project,issue_id)
|
|
|
issue_info = Array.new(9)
|
|
|
if issue_id.present?
|
|
|
select_arrays = [:assigned_to_id, :tracker_id, :status_id, :priority_id, :fixed_version_id, :start_date, :due_date, :estimated_hours, :done_ratio]
|
|
|
issue_info = Issue.select(select_arrays).where(id: issue_id).pluck(select_arrays)
|
|
|
issue_info = issue_info[0]
|
|
|
end
|
|
|
project_members = project.members_user_infos
|
|
|
project_members_info = [] #指派给
|
|
|
project_members.each do |member|
|
|
|
real_name = member[2] + member[3]
|
|
|
unless real_name.present?
|
|
|
real_name = member[1]
|
|
|
end
|
|
|
user_id = member[0]
|
|
|
is_chosen = ((member[0].to_s == issue_info[0].to_s) ? "1" : "0")
|
|
|
member_info = {id: user_id, name: real_name, is_chosen: is_chosen}
|
|
|
project_members_info.push(member_info)
|
|
|
end
|
|
|
|
|
|
tracker_info = project.trackers&.pluck(:id, :name, :position)
|
|
|
new_tracker_info = [] #类型
|
|
|
if tracker_info.size > 0
|
|
|
tracker_info.each do |t|
|
|
|
is_chosen = (t[0] == issue_info[1]) ? "1" : "0"
|
|
|
new_tracker = {id: t[0], name: t[1], position: t[2], is_chosen: is_chosen}
|
|
|
new_tracker_info.push(new_tracker)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
issue_status = IssueStatus&.pluck(:id,:name,:position)
|
|
|
new_status_info = [] #缺陷类型
|
|
|
if issue_status.size > 0
|
|
|
issue_status.each do |t|
|
|
|
is_chosen = (t[0] == issue_info[2]) ? "1" : "0"
|
|
|
new_issue = {id: t[0], name: t[1], position: t[2], is_chosen: is_chosen}
|
|
|
new_status_info.push(new_issue)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
issue_priority = project.issue_tags&.pluck(:id,:title, :color)
|
|
|
new_priority_info = [] #issue标签,(优先程度)
|
|
|
if issue_priority.size > 0
|
|
|
issue_priority.each do |t|
|
|
|
is_chosen = (t[0] == issue_info[3]) ? "1" : "0"
|
|
|
new_issue = {id: t[0], name: t[1], color: t[2], is_chosen: is_chosen}
|
|
|
new_priority_info.push(new_issue)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
issue_versions = project.versions&.pluck(:id,:name, :status)
|
|
|
new_version_info = [] #issue里程碑
|
|
|
if issue_versions.size > 0
|
|
|
issue_versions.each do |t|
|
|
|
is_chosen = (t[0] == issue_info[4]) ? "1" : "0"
|
|
|
new_issue = {id: t[0], name: t[1], status: t[2], is_chosen: is_chosen}
|
|
|
new_version_info.push(new_issue)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
issue_done_ratio = %w(0 10 20 30 40 50 60 70 80 90 100)
|
|
|
new_done_info = [] #完成度
|
|
|
if issue_done_ratio.size > 0
|
|
|
issue_done_ratio.each do |t|
|
|
|
is_chosen = (t == issue_info[8].to_s) ? "1" : "0"
|
|
|
new_issue = {id:t.to_i, ratio: (t.to_s + "%"), is_chosen: is_chosen}
|
|
|
new_done_info.push(new_issue)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
{
|
|
|
"assign_user": project_members_info,
|
|
|
"tracker": new_tracker_info,
|
|
|
"issue_status": new_status_info,
|
|
|
"issue_tag": new_priority_info,
|
|
|
"issue_version": new_version_info,
|
|
|
"start_date": issue_info[5],
|
|
|
"due_date": issue_info[6],
|
|
|
"estimated_hours": issue_info[7],
|
|
|
"done_ratio": new_done_info
|
|
|
}
|
|
|
|
|
|
end
|
|
|
|
|
|
def export_issues(issues)
|
|
|
@table_columns = %w(ID 类型 标题 描述 状态 指派给 优先级 发布人 创建时间 里程碑 开始时间 截止时间 完成度)
|
|
|
@export_issues = []
|
|
|
issues.each do |i|
|
|
|
issue_array = [i.id, i.tracker.try(:name), i.subject, i.description, i.issue_status.try(:name),i.get_assign_user.try(:show_real_name),
|
|
|
i.issue_tag.try(:title), i.user.try(:show_real_name), format_time(i.created_on), i.version.try(:title), i.start_date.to_s, i.due_date.to_s, i.done_ratio.to_s + "%" ]
|
|
|
|
|
|
@export_issues.push(issue_array)
|
|
|
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def trackers_size(issues)
|
|
|
trackers_id = Tracker.pluck(:id,:name)
|
|
|
tracker_array = []
|
|
|
trackers_id.each do |t|
|
|
|
tracker_info = {
|
|
|
id: t[0],
|
|
|
name: t[1],
|
|
|
issues_count: issues.issues_count(t[0])
|
|
|
}
|
|
|
tracker_array.push(tracker_info)
|
|
|
end
|
|
|
tracker_array
|
|
|
end
|
|
|
end |