新增:导入导出工作项接口

This commit is contained in:
yystopf 2024-08-07 11:01:24 +08:00
parent cc5a671458
commit 6f0afffb3b
2 changed files with 64 additions and 0 deletions

View File

@ -124,6 +124,68 @@ class Api::Pm::IssuesController < Api::Pm::BaseController
end
end
def export
return render_error('请输入正确的项目ID.') if params[:pm_project_id].blank?
Axlsx::Package.new do |p|
[['requirement', 1], ['task', 2], ['bug', 3]].each do |type|
p.workbook.add_worksheet(:name => type[0]) do |sheet|
@issues = Issue.where(pm_project_id: params[:pm_project_id], pm_issue_type: type[1])
sheet.add_row ["标题", "正文", "创建者", "创建时间", "修改者", "更新时间", "状态", "负责人", "优先级", "标记", "开始时间","结束时间", "预估工时"]
@issues.each do |issue|
sheet.add_row [issue.subject, issue.description, issue.user.try(:login), issue.created_on.strftime("%Y-%m-%d %H:%M:%S"), issue.changer.try(:login), issue.updated_on.strftime("%Y-%m-%d %H:%M:%S"), issue.status_id, issue.assigners.pluck(:login).join(","), issue.priority_id, issue.issue_tags.pluck(:name, :color).join(","), issue.start_date.present? ? issue.start_date.strftime("%Y-%m-%d") : "", issue.due_date.present? ? issue.due_date.strftime("%Y-%m-%d") : "", issue.time_scale]
end
end
end
p.serialize('public/导出工作项.xlsx')
end
send_file('public/导出工作项.xlsx')
end
def import
return render_error('请上传正确的文件') if params[:file].blank? || !params[:file].is_a?(ActionDispatch::Http::UploadedFile)
return render_error('请输入正确的项目ID.') if params[:pm_project_id].blank?
return render_error('请输入正确的组织ID.') if params[:organization_id].blank?
types = {requirement: 1, task: 2, bug: 3}
doc = SimpleXlsxReader.parse(params[:file].to_io)
doc.sheets.each do |sheet|
type = types["#{sheet.name}".to_sym]
sheet.rows.each.with_index do |row, index|
next if index == 0
issue = Issue.new(issue_classify: "issue", project_id: 0, pm_project_id: params[:pm_project_id], pm_issue_type: type, tracker_id: Tracker.first.id)
issue.subject = row[0]
issue.description = row[1]
author = User.find_by(login: row[2]) || User.first
issue.user = author
issue.created_on = row[3]
changer = User.find_by(login: row[4]) || User.first
issue.changer = changer
issue.updated_on = row[5]
issue.status_id = row[6].to_i
row[7].split(',').each do |a|
u = User.find_by(login: a)
next unless u.present?
issue.assigners << u
end
issue.priority_id = row[8]
row[9].split(',').each_slice(2).to_a.each do |t|
tag = IssueTag.find_by(project_id: 0, organization_id: params[:organization_id], name: t[0])
if tag.present?
issue.issue_tags << tag
else
tag = IssueTag.create(project_id: 0,organization_id: params[:organization_id], name: t[0], color: t[1])
issue.issue_tags << tag
end
end
issue.start_date = row[10]
issue.due_date = row[11]
issue.time_scale = row[12]
issue.save
end
end
end
private
def check_issue_operate_permission
return if params[:project_id].to_i.zero?

View File

@ -8,6 +8,8 @@ defaults format: :json do
get :priorities
get :tags
get :statues
get :export
get :import
end
member do
get :link_index