Dmitry Yu Okunev лет назад: 7
Родитель
Сommit
d01ffc0b5f
1 измененных файлов с 82 добавлено и 0 удалено
  1. 82 0
      3rdparty/json2sql.php

+ 82 - 0
3rdparty/json2sql.php

@@ -0,0 +1,82 @@
+<?php
+
+// dyokunev@mephi.ru
+
+$db = new mysqli("localhost", "api", "", "lesnoy");
+
+$data = json_decode(file_get_contents("/tmp/test.json"), 1);
+
+$persons = array();
+$appointments = array();
+$units = array();
+
+foreach ($data as &$row) {
+	if (!isset($persons[$row['photo']])) {
+		$persons[$row['photo']] = &$row;
+	}
+
+	if (!isset($units[$row['unit']])) {
+		$units[$row['unit']] = &$row;
+	}
+
+	$appointments[] = &$row;
+}
+
+$person_id = 1;
+foreach ($persons as &$row) {
+	$row['login'] = split('@', $row['mail'])[0];
+
+	print 'INSERT INTO `people` SET '.
+		'`id` = '.$person_id.', '.
+		'`login` = '.(empty($row['login'])?'NULL':'"'.$db->real_escape_string($row['login']).'"').', '.
+		'`firstname` = "'.$db->real_escape_string($row['name']).'", '.
+		'`lastname` = "'.$db->real_escape_string($row['surname']).'", '.
+		'`patronymic` = '.(empty($row['middle_name'])?'NULL':'"'.$db->real_escape_string($row['middle_name']).'"').', '.
+		'`displayname` = NULL, '.
+		'`displayname_latin` = NULL, '.
+		'`photo_url` = "'.$db->real_escape_string($row['photo']).'",'.
+		"created_at=NOW(), updated_at=NOW();\n";
+
+	foreach ($appointments as &$app_row) {
+		if ($app_row['photo'] === $row['photo']) {
+			$app_row['person_id'] = $person_id;
+		}
+	}
+
+	$person_id++;
+}
+
+print 'INSERT INTO `units` SET `code` = NULL, `name` = "Технологический институт", `fullname` = "Технологический институт (г. Лесной)", `shortname` = "фил_ти_лесной", parent_id=0'.";\n";
+
+$unit_id = 2;
+foreach ($units as &$row) {
+	print 'INSERT INTO `units` SET '.
+		'`id` = '.$unit_id.','.
+		'`code` = NULL,'.
+		'`name` = "'.$db->real_escape_string($row['unit']).'", '.
+		'`fullname` = NULL, '.
+		'`shortname` = NULL, '.
+		'`parent_id` = 1,'.
+		"created_at=NOW(), updated_at=NOW();\n";
+
+	foreach ($appointments as &$app_row) {
+		if ($app_row['unit'] === $row['unit']) {
+			$app_row['unit_id'] = $unit_id;
+		}
+	}
+
+	$unit_id++;
+}
+
+foreach ($appointments as &$row) {
+	print 'INSERT INTO `appointments` SET '.
+		'`person_id` = "'.$db->real_escape_string($row['person_id']).'", '.
+		'`unit_id` = "'.$db->real_escape_string($row['unit_id']).'", '.
+		'`name` = "'.$db->real_escape_string($row['position']).'", '.
+		'`line` = "'.$db->real_escape_string($row['phone']).'", '.
+		'`description` = "'.$db->real_escape_string($row['charge']).'", '.
+		'`room` = "'.$db->real_escape_string($row['room']).'",'.
+		"created_at=NOW(), updated_at=NOW();\n";
+}
+
+?>