Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.12 KB | None | 0 0
  1. <?php
  2.  
  3. function getArrayElementByKey($array, $key) {
  4.     if (array_key_exists($key, $array)) {
  5.         return $array[$key];
  6.     } else {
  7.         return null;
  8.     }
  9. }
  10.  
  11. $csv = array_map('str_getcsv', file('1.1.csv'));
  12.  
  13. $utf_csv = [];
  14. $utf_rows = [];
  15.  
  16. // меняем кодировку на UTF-8, парсим
  17. foreach ($csv as $rows) {
  18.     $utf_row = mb_convert_encoding(implode(',', $rows), 'UTF-8', 'Windows-1251');
  19.     $utf_rows = explode(';', $utf_row);
  20.     array_push($utf_csv, $utf_rows);
  21. }
  22.  
  23. unset($csv);
  24. unset($rows);
  25. unset($utf_row);
  26. unset($utf_rows);
  27.  
  28. //убираем первый элемент массива - заголовки
  29. array_shift($utf_csv);
  30.  
  31. $temp = [];
  32. $parsed_data = [];
  33.  
  34. // оставляем только город Хабаровск, адрес + money_collecting_way, bank_bik, money_ppl_collected
  35. foreach ($utf_csv as $rows) {
  36.     // удаляем ненужную нам информацию из массива
  37.     array_splice($rows, 0, 5);
  38.     array_splice($rows, 1, 10);
  39.     array_splice($rows, 2, 1);
  40.     array_splice($rows, 4, 13);
  41.  
  42.     // делим адрес на составные части
  43.     $address_split = explode(', ', $rows[0]);
  44.  
  45.     // город
  46.     $city = getArrayElementByKey(explode(' ', $address_split[0]), 0);
  47.  
  48.     // улица
  49.     $street_name = getArrayElementByKey(explode(' ', $address_split[1]), 0);
  50.  
  51.     // кв-л, ул, б-р и т.д.
  52.     $street_type = getArrayElementByKey(explode(' ', $address_split[1]), 1);
  53.  
  54.     // номер дома (если есть корпус, то преобразуем)
  55.     $house_num = getArrayElementByKey(explode(' ', $address_split[2]), 2);
  56.  
  57.     if (array_key_exists(3, $address_split)) {
  58.         if (0 < substr_count($address_split[3], 'корпус')) {
  59.             $house_num = $house_num . 'к' . getArrayElementByKey(explode(' ', $address_split[3]), 1);
  60.         }
  61.     }
  62.  
  63.     $money_collecting_way = (int)$rows[1];
  64.  
  65.     $bank_bik = (int)$rows[2];
  66.  
  67.     // меняем запятую на точку и превращаем в число
  68.     $money_ppl_collected = str_replace(',', '.', $rows[3]);
  69.     $money_ppl_collected = (float)$money_ppl_collected;
  70.  
  71.     if ($city == 'Хабаровск') {
  72.         array_push($temp, $city, $street_name, $street_type, mb_strtolower($house_num), $money_collecting_way, $bank_bik, $money_ppl_collected);
  73.         array_push($parsed_data, $temp);
  74.         $temp = [];
  75.     }
  76. }
  77.  
  78. unset($utf_csv);
  79. unset($rows);
  80. unset($address_split);
  81. unset($city);
  82. unset($street_name);
  83. unset($street_type);
  84. unset($house_num);
  85. unset($money_collecting_way);
  86. unset($bank_bik);
  87. unset($money_ppl_collected);
  88. unset($temp);
  89.  
  90. $host = '127.0.0.1';
  91. $db   = 'narayone';
  92. $user = 'root';
  93. $pass = '123';
  94. $charset = 'utf8';
  95.  
  96. $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  97. $opt = [
  98.     PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  99.     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  100.     PDO::ATTR_EMULATE_PREPARES   => false,
  101. ];
  102. $pdo = new PDO($dsn, $user, $pass, $opt);
  103.  
  104. $query = 'SELECT house.id, house_restore_budget.house_id, house.aoguid, ' .
  105.             'new.FORMALNAME, new.SHORTNAME, house.HOUSENUM, house_restore_budget.money_ppl_collected ' .
  106.             'FROM narayone.house INNER JOIN narayone.new ON house.AOGUID = new.AOGUID ' .
  107.             'LEFT JOIN narayone.house_restore_budget ON house.id = house_restore_budget.house_id';
  108.  
  109. $db_data = [];
  110.  
  111. $stmt = $pdo->query($query);
  112.  
  113. while ($row = $stmt->fetch()) {
  114.     array_push($db_data, $row);
  115. }
  116.  
  117. $pdo = null;
  118. unset($query);
  119. unset($row);
  120.  
  121. $has_data = [];
  122. $has_no_data = [];
  123. foreach ($db_data as $data) {
  124.     if ($data['house_id'] === null) {
  125.         array_push($has_no_data, $data);
  126.     } else {
  127.         array_push($has_data, $data);
  128.     }
  129. }
  130.  
  131. unset($db_data);
  132. unset($data);
  133.  
  134. $pdo = new PDO($dsn, $user, $pass, $opt);
  135. $query = 'UPDATE narayone.house_restore_budget SET house_restore_budget.money_ppl_collected = ? WHERE house_restore_budget.house_id = ?';
  136. $stmt = $pdo->prepare($query);
  137.  
  138. $updated = [];
  139. $updated_data = [];
  140.  
  141. // будет перезаписывать постоянно, потому что БД округляет числа (всегда по-разному)
  142. foreach ($has_data as $data) {
  143.     foreach ($parsed_data as $rows) {
  144.         if ($rows[1] == $data['FORMALNAME'] && $rows[2] == $data['SHORTNAME'] && $rows[3] == mb_strtolower($data['HOUSENUM']) && $rows[6] != $data['money_ppl_collected']) {
  145.             $updated_data['id'] = $data['id'];
  146.             $updated_data['money_ppl_collected_old'] = $data['money_ppl_collected'];
  147.             $updated_data['money_ppl_collected_new'] = $rows[6];
  148.             array_push($updated, $updated_data);
  149.             try {
  150.                 $stmt->execute(array($rows[6], $data['house_id']));
  151.             } catch (PDOException $e) {
  152.                 echo $e->getMessage();
  153.             }
  154.         }
  155.     }
  156. }
  157.  
  158. $pdo = null;
  159. unset($query);
  160. unset($data);
  161. unset($rows);
  162. unset($updated_data);
  163.  
  164. $pdo = new PDO($dsn, $user, $pass, $opt);
  165. $query = 'INSERT INTO narayone.house_restore_budget ' .
  166.             '(' .
  167.                 'house_restore_budget.house_id, ' .
  168.                 'house_restore_budget.demolition, ' .
  169.                 'house_restore_budget.money_collecting_way, ' .
  170.                 'house_restore_budget.bank_bik, ' .
  171.                 'house_restore_budget.money_ppl_collected, ' .
  172.                 'house_restore_budget.cofunding_total, ' .
  173.                 'house_restore_budget.cofunding_mo_budget, ' .
  174.                 'house_restore_budget.cofunding_subject_rf_budget, ' .
  175.                 'house_restore_budget.cofunding_rf_budget, ' .
  176.                 'house_restore_budget.cofunding_fond_budget' .
  177.             ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  178. $stmt = $pdo->prepare($query);
  179.  
  180. $created = [];
  181. $created_data = [];
  182.  
  183. foreach ($has_no_data as $data) {
  184.     foreach ($parsed_data as $rows) {
  185.         if ($rows[1] == $data['FORMALNAME'] && $rows[2] == $data['SHORTNAME'] && $rows[3] == mb_strtolower($data['HOUSENUM'])) {
  186.             $created_data['id'] = $data['id'];
  187.             $created_data['money_ppl_collected_old'] = $data['money_ppl_collected'];
  188.             $created_data['money_ppl_collected_new'] = $rows[6];
  189.             array_push($created, $created_data);
  190.             try {
  191.                 $stmt->execute([
  192.                     $data['id'], // house_id
  193.                     0, // demolition
  194.                     $rows[4], // money_collecting_way
  195.                     $rows[5], // bank_bik
  196.                     $rows[6], // money_ppl_collected
  197.                     0, // cofunding_total
  198.                     0, // cofunding_mo_budget
  199.                     0, // cofunding_subject_rf_budget
  200.                     0, // cofunding_rf_budget
  201.                     0  // cofunding_fond_budget
  202.                 ]);
  203.             } catch (PDOException $e) {
  204.                 echo $e->getMessage();
  205.             }
  206.         }
  207.     }
  208. }
  209.  
  210. $pdo = null;
  211. unset($query);
  212. unset($data);
  213. unset($rows);
  214. unset($created_data);
  215.  
  216. unset($host);
  217. unset($db);
  218. unset($user);
  219. unset($pass);
  220. unset($charset);
  221. unset($dsn);
  222. unset($opt);
  223. unset($pdo);
  224.  
  225. echo 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement