require 'csv'
class IotDatum < ActiveRecord::Base

validates_presence_of :part_number
	
  def self.import(file,bu)
    puts "----1---#{bu}------"
    spreadsheet = Roo::Spreadsheet.open(file.path)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      if bu == 'RE'
        puts "----2---#{bu}------"
        product = find_by(id: row["site"]) || new
      else
        puts "----3---#{bu}------"
        product = find_by(id: row["workbench_number"]) || new
      end
      product.attributes = row.to_hash
      return false unless product.valid?
      product.save!
    end
  end 

  #For RE

  def self.import_re(file)
    # puts "----2---------"
    spreadsheet = Roo::Spreadsheet.open(file.path)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      re_planner_data = find_by(id: row["site"]) || new
      re_planner_data.attributes = row.to_hash
      return false unless re_planner_data.valid?
      re_planner_data.save!
    end
  end 

  def self.to_csv(options = {})
    desired_columns = ["workbench_number", "part_number", "target", "lot_size", "employee_name", "employee_id", "shift", "device_id", "count", "status"]
    CSV.generate(options) do |csv|
      csv << desired_columns
      all.each do |product|
        csv << product.attributes.values_at(*desired_columns)
      end
    end
  end

  def self.to_report_csv
    attributes = %w{workbench_number employee_name shift part_number target count date start_time end_time duration}

    CSV.generate(headers: true) do |csv|
      csv << attributes

      all.each do |ds|
        csv << attributes.map{ |attr| ds.send(attr) }
      end
    end
  end 

  def self.to_ds_csv
    attributes = %w{dates day_name workbench shift target count duration}

    CSV.generate(headers: true) do |csv|
      csv << attributes

      all.each do |ds|
        csv << attributes.map{ |attr| ds.send(attr) }
      end
    end
  end

  def self.to_sp_csv
    attributes = %w{dates shift workbench part_number duration}

    CSV.generate(headers: true) do |csv|
      csv << attributes

      all.each do |sp|
        csv << attributes.map{ |attr| sp.send(attr) }
      end
    end
  end
end