class VolumePeriodique < ApplicationRecord
  def self.import 
    if Rails.env.development?
      mysql_path = "/usr/local/mysql/bin/"
    else
      mysql_path = ""
    end
    
    if true
    
      
    
    
      system "#{mysql_path}mysqldump -h 193.200.182.69 -u root -t -c -pmysqlAdmin vulcov2 volumeperiodique contact societe seuilsdeclencheur > #{Rails.root}/import_csv/vulco_to_import.sql "
  
      puts "Conversion du fichier"
  
      @sql = File.read("#{Rails.root}/import_csv/vulco_to_import.sql").encode('utf-8')
  
      @sql = @sql.gsub("`volumeperiodique`", "`volume_periodiques`")
      @sql = @sql.gsub("`contact`", "`v_contacts`")
      @sql = @sql.gsub("`societe`", "`societes`")
      @sql = @sql.gsub("`seuilsdeclencheur`", "`seuil_declencheurs`")
  
  
      @sql = @sql.gsub("`Type`","`soc_type`")
    
  
  
      vars = ["CodeManagInn","CodeIndicateur","DateDebut","DateFin","CodeIndicateur","Valeur","DateReception","Commentaire","CodeSociete"]
      vars.each do |var|
        @sql = @sql.gsub(var,var.downcase)
      end
  
  
  
      vars = ["typeUser", "CodeManagInn", "NomGroupe", "Societe", "Civilite", "Nom", "Prenom", "Titre", "MailTo", "MailCC", "MailBCC", "NumPortable", "NumTel", "NumFax", "Adresse1", "Adresse2", "CodePostal", "Ville", "Anniversaire", "Pays", "Photo", "Login", "Password", "typeUser", "DateCreation", "Commentaire", "CodeProprietaire", "TypePartage", "libre1", "libre2", "libre3", "libre4", "libre5", "AccesBloque", "Corbeille", "CodeCreateur", "PartageDefaut", "NPAI", "NpaiMail", "NpaiSms"]
 
      vars.each do |var|
        @sql = @sql.gsub(var,var.downcase)
      end
    
    
    
  
  
  
      vars = ["SocManagInn", "Nom", "Adresse1", "Adresse2", "CodePostal", "Ville", "Pays", "NumTel", "NumFax", "Mobile", "Mail", "MailCc", "MailBcc", "SiteWeb", "Statut", "RC", "NAF", "Type", "Enseigne", "DateCreation", "Activite1", "Activite2", "Activite3", "Activite4", "Activite5", "Login", "Password", "Photo", "TypeUser", "CodeProprietaire", "TypePartage", "Libre1", "Libre2", "Libre3", "Libre4", "Libre5", "AccesBloque", "Corbeille", "CodeTpe", "CodeCreateur", "PartageDefaut", "NPAI", "NpaiMail", "NpaiSms"]
      vars.each do |var|
        @sql = @sql.gsub(var,var.downcase)
      end
  
      vars = ["CodeDeclencheur", "SocManagInn", "ValSeuil", "Cadeau", "Media", "ParamMedia", "Parammedia", "DateModif", "Expression", "DateDeb", "DateFin", "EnvoiAuto", "CopieMail", "Copiemail", "ValMax", "SaisieCom", "ValBA", "UniteBA", "BonusActif"]
      vars.each do |var|
        @sql = @sql.gsub(var,var.downcase)
      end
    
      import_ok = "#{Rails.root}/import_csv/vulco-ok-#{Time.now.to_s.to_slug}.sql"
      puts "Enregistrement fichier traité"
      File.write("#{import_ok}", @sql)
    end
  
    #import_ok = "/Users/nico/Dev2/test6/vulco_app/import_csv/vulco-ok-18-12-2019-23-58-33.sql"
  
    VolumePeriodique.connection.truncate(VolumePeriodique.table_name)
    VContact.connection.truncate(VContact.table_name)
    Societe.connection.truncate(Societe.table_name)
    SeuilDeclencheur.connection.truncate(SeuilDeclencheur.table_name)
  
    puts "import dans la base"
    
    if Rails.env.development?
      system "#{mysql_path}mysql -u root vulco_app < #{import_ok}"
    else
      system "#{mysql_path}mysql -u vulco_app -pvulco_app vulco2_app < #{import_ok}"
    end
    puts "Fin de l'import"
    
    puts "Traitement sociétés"
    Societe.update_valids
    Societe.update_caches
    VContact.update_mvt
    puts "Fin du traitement"
  
  
  end
end
