SU2-7 | Добавление модели Report с поддержкой загрузки файлов, обновление контроллера и представления для обработки отчетов, а также создание миграций для таблицы отчетов и активного хранилища.
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
class ReportsController < ApplicationController
|
class ReportsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
|
@reports = Report.order(created_at: :desc).limit(10)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
inn = params[:inn]
|
inn = params[:inn]
|
||||||
output_path = Rails.root.join('tmp', 'reports', inn)
|
report = Report.create!(inn: inn, status: 'processing')
|
||||||
FileUtils.mkdir_p(output_path)
|
|
||||||
|
|
||||||
FileProcessorJob.perform_async(inn, output_path.to_s)
|
FileProcessorJob.perform_async(inn, report.id)
|
||||||
|
|
||||||
redirect_to reports_path, notice: 'Отчёт загружается в фоне'
|
redirect_to reports_path, notice: 'Отчёт загружается в фоне'
|
||||||
end
|
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
|
class FileProcessorJob
|
||||||
include Sidekiq::Job
|
include Sidekiq::Job
|
||||||
|
|
||||||
def perform(inn, output_path)
|
def perform(inn, report_id)
|
||||||
|
report = Report.find(report_id)
|
||||||
Rails.logger.info "=== Начало загрузки для ИНН: #{inn} ==="
|
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} ==="
|
Rails.logger.info "=== Завершено для ИНН: #{inn} ==="
|
||||||
rescue => e
|
rescue => e
|
||||||
|
report.update!(status: 'failed') if report
|
||||||
Rails.logger.error "=== ОШИБКА: #{e.message} ==="
|
Rails.logger.error "=== ОШИБКА: #{e.message} ==="
|
||||||
Rails.logger.error e.backtrace.first(5).join("\n")
|
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get_content(url, json_need = false)
|
def get_content(url, json_need = false)
|
||||||
|
|||||||
@@ -1,17 +1,25 @@
|
|||||||
<!-- app/views/reports/index.html.erb -->
|
<!-- 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>
|
<p style="color: green;"><%= notice %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= form_with url: reports_path, method: :post, local: true do |f| %>
|
<%= form_with url: reports_path, method: :post, local: true do |f| %>
|
||||||
<div style="margin-bottom: 15px;">
|
|
||||||
<%= f.label :inn, "Введите ИНН:" %>
|
<%= f.label :inn, "Введите ИНН:" %>
|
||||||
<%= f.text_field :inn, placeholder: "1234567890", required: true, style: "width: 100%; padding: 8px;" %>
|
<%= f.text_field :inn, required: true %>
|
||||||
</div>
|
<%= 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 %>
|
<% 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.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
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
|
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||||
end
|
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|
|
create_table "sessions", force: :cascade do |t|
|
||||||
t.string "session_id", null: false
|
t.string "session_id", null: false
|
||||||
t.string "cas_ticket"
|
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