From e496d6dbcbbd83218ad2e1de952cffe403dfff7c Mon Sep 17 00:00:00 2001 From: Osamu Takiya Date: Tue, 21 Jun 2022 21:32:07 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20=E3=80=8C=E9=96=8B?= =?UTF-8?q?=E7=A5=A8=E3=82=A4=E3=83=A9=E3=82=B9=E3=83=88=E7=8A=B6=E6=B3=81?= =?UTF-8?q?=E3=80=8D=E3=81=AE=E3=82=A4=E3=83=B3=E3=83=9D=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=81=AB=E3=81=A6=E3=80=81=E9=A0=85=E7=9B=AE=E3=81=AB=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=81=8C=E3=81=AA=E3=81=91=E3=82=8C=E3=81=B0=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BD=9C=E6=A5=AD=E3=82=92=E8=A1=8C=E3=82=8F=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F=20(#113)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../result_illustration_applications.rb | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/app/services/sheets/result_illustration_applications.rb b/app/services/sheets/result_illustration_applications.rb index 2ec01f15..c1fb9f5f 100644 --- a/app/services/sheets/result_illustration_applications.rb +++ b/app/services/sheets/result_illustration_applications.rb @@ -14,14 +14,11 @@ def self.import_statuses_data_to_database range: "開票イラスト!A1:P" ) - # TODO: ここは切り出しできそう - # シート と DBカラム の対応表を自作しておく sheet_headers_and_column_names = YAML.load_file( Rails.root.join("config/sheet_headers_and_column_names_relations/on_raw_sheet_result_illustration_statuses.yml") ) cloumn_names_and_sheet_index_number = {} - # TODO: ここは切り出しできそう # 実際のシートのヘッダーを取得し、自作の対応表からインデックス番号を算出する source_sheet_headers = rows[0] source_sheet_headers.each_with_index do |header, i| @@ -32,10 +29,8 @@ def self.import_statuses_data_to_database end end - # TODO: バルクインサートする ActiveRecord::Base.transaction do rows.each_with_index do |row, i| - # TODO: このスキップ条件は切り出せそう next if i == 0 || row[cloumn_names_and_sheet_index_number['id_on_sheet']].blank? || row[cloumn_names_and_sheet_index_number['character_name']].blank? next if OnRawSheetResultIllustrationStatus.where( @@ -63,17 +58,13 @@ def self.import_totallings_data_to_database range: "開票イラスト!A1:P" ) - # TODO: ここは切り出しできそう - # シート と DBカラム の対応表を自作しておく sheet_headers_and_column_names = YAML.load_file( Rails.root.join("config/sheet_headers_and_column_names_relations/on_raw_sheet_result_illustration_totallings.yml") ) cloumn_names_and_sheet_index_number = {} - - # TODO: ここは切り出しできそう - # 実際のシートのヘッダーを取得し、自作の対応表からインデックス番号を算出する source_sheet_headers = rows[0] + # 実際のシートのヘッダーを取得し、自作の対応表からインデックス番号を算出する source_sheet_headers.each_with_index do |header, i| sheet_headers_and_column_names.find { |e| e['sheet_header'] == header }.tap do |e| next if e.nil? @@ -82,18 +73,22 @@ def self.import_totallings_data_to_database end end - # binding.irb - # {"character_name_for_sheet_totalling"=>7, - # "number_of_applications"=>8, - # "character_name_for_public"=>10} + # シートから持ってきたレコードのうち、無効なレコードを除外する + valid_rows = [] + rows.each_with_index do |row, i| + next if i == 0 || row[cloumn_names_and_sheet_index_number['character_name_for_sheet_totalling']].blank? || row[cloumn_names_and_sheet_index_number['character_name_for_public']].blank? + + valid_rows << row + end + + # 更新がなければメソッドを抜ける + return 'Not Modified.' if valid_rows.count == OnRawSheetResultIllustrationTotalling.count - # TODO: バルクインサートする ActiveRecord::Base.transaction do # 主キーがないから問答無用で全削除して入れ直す OnRawSheetResultIllustrationTotalling.destroy_all rows.each_with_index do |row, i| - # TODO: このスキップ条件は切り出せそう next if i == 0 || row[cloumn_names_and_sheet_index_number['character_name_for_sheet_totalling']].blank? || row[cloumn_names_and_sheet_index_number['character_name_for_public']].blank? obj = OnRawSheetResultIllustrationTotalling.new(