SU2-7 | Добавление модели Report с поддержкой загрузки файлов, обновление контроллера и представления для обработки отчетов, а также создание миграций для таблицы отчетов и активного хранилища.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
class ReportsController < ApplicationController
|
||||
def index
|
||||
@reports = Report.order(created_at: :desc).limit(10)
|
||||
end
|
||||
|
||||
def create
|
||||
inn = params[:inn]
|
||||
output_path = Rails.root.join('tmp', 'reports', inn)
|
||||
FileUtils.mkdir_p(output_path)
|
||||
report = Report.create!(inn: inn, status: 'processing')
|
||||
|
||||
FileProcessorJob.perform_async(inn, output_path.to_s)
|
||||
FileProcessorJob.perform_async(inn, report.id)
|
||||
|
||||
redirect_to reports_path, notice: 'Отчёт загружается в фоне'
|
||||
end
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Report < ApplicationRecord
|
||||
has_many_attached :files
|
||||
|
||||
validates :inn, presence: true
|
||||
end
|
||||
@@ -5,16 +5,35 @@ require 'stringio'
|
||||
class FileProcessorJob
|
||||
include Sidekiq::Job
|
||||
|
||||
def perform(inn, output_path)
|
||||
def perform(inn, report_id)
|
||||
report = Report.find(report_id)
|
||||
Rails.logger.info "=== Начало загрузки для ИНН: #{inn} ==="
|
||||
download_inn(inn, output_path)
|
||||
|
||||
# Временная папка
|
||||
temp_dir = Rails.root.join('tmp', 'reports', inn)
|
||||
FileUtils.mkdir_p(temp_dir)
|
||||
|
||||
download_inn(inn, temp_dir.to_s)
|
||||
|
||||
Dir.glob("#{temp_dir}/**/*").select { |f| File.file?(f) }.each do |file_path|
|
||||
report.files.attach(
|
||||
io: File.open(file_path),
|
||||
filename: File.basename(file_path)
|
||||
)
|
||||
end
|
||||
|
||||
# Удали временные файлы
|
||||
FileUtils.rm_rf(temp_dir)
|
||||
|
||||
report.update!(status: 'completed')
|
||||
Rails.logger.info "=== Завершено для ИНН: #{inn} ==="
|
||||
rescue => e
|
||||
report.update!(status: 'failed') if report
|
||||
Rails.logger.error "=== ОШИБКА: #{e.message} ==="
|
||||
Rails.logger.error e.backtrace.first(5).join("\n")
|
||||
raise
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def get_content(url, json_need = false)
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
<!-- app/views/reports/index.html.erb -->
|
||||
<div style="max-width: 500px; margin: 50px auto; padding: 20px;">
|
||||
<h1>Загрузка отчётов по ИНН</h1>
|
||||
<h1>Загрузка отчётов по ИНН</h1>
|
||||
|
||||
<% if notice %>
|
||||
<% if notice %>
|
||||
<p style="color: green;"><%= notice %></p>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= form_with url: reports_path, method: :post, local: true do |f| %>
|
||||
<div style="margin-bottom: 15px;">
|
||||
<%= form_with url: reports_path, method: :post, local: true do |f| %>
|
||||
<%= f.label :inn, "Введите ИНН:" %>
|
||||
<%= f.text_field :inn, placeholder: "1234567890", required: true, style: "width: 100%; padding: 8px;" %>
|
||||
</div>
|
||||
<%= f.text_field :inn, required: true %>
|
||||
<%= f.submit "Загрузить отчёт" %>
|
||||
<% end %>
|
||||
|
||||
<%= f.submit "Загрузить отчёт", style: "padding: 10px 20px; cursor: pointer;" %>
|
||||
<h2>История загрузок</h2>
|
||||
<% @reports.each do |report| %>
|
||||
<div style="border: 1px solid #ccc; padding: 10px; margin: 10px 0;">
|
||||
<p>ИНН: <%= report.inn %> | Статус: <%= report.status %></p>
|
||||
<% if report.files.attached? %>
|
||||
<p>Файлы:</p>
|
||||
<% report.files.each do |file| %>
|
||||
<%= link_to file.filename, rails_blob_path(file, disposition: "attachment") %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -0,0 +1,10 @@
|
||||
class CreateReports < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :reports do |t|
|
||||
t.string :inn
|
||||
t.string :status
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
Generated
+8
-1
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.1].define(version: 2025_08_05_090922) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2025_12_09_142642) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
@@ -42,6 +42,13 @@ ActiveRecord::Schema[7.1].define(version: 2025_08_05_090922) do
|
||||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "reports", force: :cascade do |t|
|
||||
t.string "inn"
|
||||
t.string "status"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "sessions", force: :cascade do |t|
|
||||
t.string "session_id", null: false
|
||||
t.string "cas_ticket"
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Report, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
Reference in New Issue
Block a user