import-system-dev.pl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. #Обновляем БД устрйств
  6. use utf8;
  7. use warnings;
  8. use Encode;
  9. use open qw(:std :encoding(UTF-8));
  10. no warnings 'utf8';
  11. use FindBin '$Bin';
  12. use lib "$Bin/";
  13. use Data::Dumper;
  14. use eyelib::config;
  15. use eyelib::main;
  16. use eyelib::logconfig;
  17. use eyelib::database;
  18. use eyelib::net_utils;
  19. use strict;
  20. use warnings;
  21. print "Stage 0: Read system devices\n";
  22. my @user_devices=();
  23. if (-e "system-devs.csv") {
  24. my @nSQL=read_file("system-devs.csv");
  25. foreach my $row (@nSQL) {
  26. my ($dev_id,$dev_model,$dev_vendor) = split(/;/,$row);
  27. my $device;
  28. $device->{id}=$dev_id;
  29. $device->{model_name}=$dev_model;
  30. $device->{vendor_id}=$dev_vendor;
  31. push(@user_devices,$device);
  32. }
  33. }
  34. print "Stage 1: Read system vendors\n";
  35. my @user_vendors=();
  36. if (-e "system-vendors.csv") {
  37. my @nSQL=read_file("system-vendors.csv");
  38. foreach my $row (@nSQL) {
  39. my ($vendor_id,$vendor_name) = split(/;/,$row);
  40. my $vendor;
  41. $vendor->{id}=$vendor_id;
  42. $vendor->{name}=$vendor_name;
  43. push(@user_vendors,$vendor);
  44. }
  45. }
  46. print "Done!\n";
  47. my %vendor_migration;
  48. print "Import Vendors\n";
  49. foreach my $vendor (@user_vendors) {
  50. #seach exists vendor created by user
  51. my $vendor_exist = get_record_sql($dbh,"SELECT * FROM vendors WHERE id>=10000 and LOWER(name)= ?", lc(trim($vendor->{name})));
  52. print "Check: $vendor->{name} id: $vendor->{id} ...";
  53. if ($vendor_exist) {
  54. if ($vendor_exist->{id} == $vendor->{id}) { print "OK\n"; next; }
  55. print " created by user. Switch to system pool.";
  56. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$vendor->{id},$vendor_exist->{id});
  57. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$vendor->{id}, $vendor_exist->{id});
  58. do_sql($dbh,"UPDATE devices SET vendor_id=? WHERE vendor_id=?", $vendor->{id}, $vendor_exist->{id});
  59. print " Migrated.\n";
  60. next;
  61. }
  62. #check system pool
  63. $vendor_exist = get_record_sql($dbh,"SELECT * FROM vendors WHERE id<10000 and LOWER(name)=?",lc(trim($vendor->{name})));
  64. if ($vendor_exist) {
  65. if ($vendor_exist->{id} == $vendor->{id}) { print "OK\n"; next ; }
  66. print "Warning! System vendor mismatch! ";
  67. my $vendor2 = get_record_sql($dbh,"SELECT * FROM vendors WHERE id=?",$vendor->{id});
  68. if ($vendor2) {
  69. print "Found another vendor with this id =>".$vendor2->{name};
  70. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM vendors");
  71. my $new_vendor_id = $last_id->{'last'}+1;
  72. if ($new_vendor_id <=10000 ) { $new_vendor_id = 10001; }
  73. print " Move vendor $vendor2->{name} to user custom block. Run script again\n";
  74. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$new_vendor_id, $vendor2->{id});
  75. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$new_vendor_id,$vendor2->{id});
  76. next;
  77. }
  78. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$vendor->{id},$vendor_exist->{id});
  79. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$vendor->{id},$vendor_exist->{id});
  80. do_sql($dbh,"UPDATE devices SET vendor_id=? WHERE vendor_id=?",$vendor->{id},$vendor_exist->{id});
  81. print "Fixed.\n";
  82. next;
  83. }
  84. #check another record with this id
  85. my $vendor2 = get_record_sql($dbh,"SELECT * FROM vendors WHERE id=?",$vendor->{id});
  86. if ($vendor2) {
  87. print "Found another vendor with this id =>".$vendor2->{name};
  88. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM vendors");
  89. my $new_vendor_id = $last_id->{'last'}+1;
  90. if ($new_vendor_id <=10000 ) { $new_vendor_id = 10001; }
  91. print " Move vendor $vendor2->{name} to user custom block. Run script again\n";
  92. do_sql($dbh,"UPDATE vendors SET id=? WHERE id=?",$new_vendor_id,$vendor2->{id});
  93. do_sql($dbh,"UPDATE device_models SET vendor_id=? WHERE vendor_id=?",$new_vendor_id,$vendor2->{id});
  94. next;
  95. }
  96. insert_record($dbh,"vendors",$vendor);
  97. print " Imported.\n";
  98. }
  99. print "Done!\n";
  100. print "Import devices\n";
  101. foreach my $device (@user_devices) {
  102. #seach exists device created by user
  103. print "Check id: $device->{id} name: $device->{model_name}..";
  104. my $device_exist = get_record_sql($dbh,"SELECT * FROM device_models WHERE id>=10000 AND vendor_id=? AND LOWER(model_name)=?",$device->{vendor_id},lc(trim($device->{model_name})));
  105. if ($device_exist) {
  106. if ($device_exist->{id} == $device->{id}) { print "OK\n"; next; }
  107. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$device->{id},$device_exist->{id});
  108. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$device->{id},$device_exist->{id});
  109. print "Migrated\n";
  110. next;
  111. }
  112. #system model table
  113. $device_exist = get_record_sql($dbh,"SELECT * FROM device_models WHERE id<10000 AND vendor_id=? AND LOWER(model_name)=?",$device->{vendor_id},lc(trim($device->{model_name})));
  114. if ($device_exist) {
  115. if ($device_exist->{id} == $device->{id}) { print "OK\n"; next; }
  116. print "Warning! System device model mismatch! ";
  117. my $device2 = get_record_sql($dbh,"SELECT * FROM device_models WHERE id=?",$device->{id});
  118. if ($device2) {
  119. print "Found another device model with this id =>".$device2->{model_name};
  120. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM device_models");
  121. my $new_model_id = $last_id->{'last'}+1;
  122. if ($new_model_id <=10000 ) { $new_model_id = 10001; }
  123. print " Move device model $device2->{model_name} to user custom block. Run script again\n";
  124. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$new_model_id,$device2->{id});
  125. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$new_model_id,$device2->{id});
  126. next;
  127. }
  128. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$device->{id},$device_exist->{id});
  129. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$device->{id},$device_exist->{id});
  130. print "Migrated\n";
  131. next;
  132. }
  133. #check another record with this id
  134. my $device2 = get_record_sql($dbh,"SELECT * FROM device_models WHERE id=?",$device->{id});
  135. if ($device2) {
  136. print "Found another device model with this id =>".$device2->{model_name};
  137. my $last_id = get_record_sql($dbh,"SELECT MAX(id) as last FROM device_models");
  138. my $new_model_id = $last_id->{'last'}+1;
  139. if ($new_model_id <=10000 ) { $new_model_id = 10001; }
  140. print " Move device model $device2->{model_name} to user custom block. Run script again\n";
  141. do_sql($dbh,"UPDATE device_models SET id=? WHERE id=?",$new_model_id,$device2->{id});
  142. do_sql($dbh,"UPDATE devices SET device_model_id=? WHERE device_model_id=?",$new_model_id,$device2->{id});
  143. next;
  144. }
  145. insert_record($dbh,"device_models",$device);
  146. print " Imported.\n";
  147. }
  148. print "Done!\n";
  149. exit;