Initial commit: 교육 프로젝트 배포

This commit is contained in:
송대일
2026-07-01 18:32:42 +09:00
commit be6dccd120
1483 changed files with 5082202 additions and 0 deletions
+157
View File
@@ -0,0 +1,157 @@
<?php
// 에러 출력 설정
ini_set('display_errors', 1);
error_reporting(E_ALL);
set_time_limit(0); // 12,650명 처리를 위해 스크립트 실행 시간 제한 해제
extract($_REQUEST);
$url = "http://erp.hanmaceng.co.kr/intranet/sys/model/EduJsonAPI.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120); // 데이터 양이 많으므로 2분으로 연장
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept: application/json"]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
die("CURL 에러: " . curl_error($ch));
}
curl_close($ch);
if ($httpCode !== 200) {
die("API 호출 실패 (HTTP 상태 코드: $httpCode)");
}
$data = json_decode($response, true);
if (!isset($data['data']) || !is_array($data['data'])) {
die("유효한 데이터가 없습니다.");
}
// DB 연결
require_once __DIR__ . '/../bbs/db_conn.php';
$conn = db_conn();
$user_list = $data['data'];
$total_count = count($user_list);
$success_count = 0;
$error_count = 0;
echo "<h3>인사 정보 동기화 시작 (PDO 방식)</h3>";
echo "총 대상 인원: {$total_count}명<br><hr>";
$SET_injection_at = date('Y-m-d H:i:s');
$backup_log = "";
// ---------------------------------------------------------
// [추가] 데이터 주입 전 기존 데이터 백업 (삭제 후 재입력 방식)
// ---------------------------------------------------------
try {
$bk_stmt = $conn->prepare("CALL proc_edu_users_backup(?)");
$bk_stmt->execute([$SET_injection_at]);
echo "기존 데이터 백업 완료 (이전 백업 삭제 후 재생성)...<br><br>";
$backup_log .= "기존 데이터 백업 완료 (이전 백업 삭제 후 재생성) : 백업일시".$SET_injection_at;
flush();
} catch (PDOException $e) {
// 백업 실패 시 안전을 위해 스크립트를 중단하거나 경고를 띄울 수 있습니다.
echo "<span style='color:red;'>백업 실패: " . $e->getMessage() . "</span><br>작업을 중단합니다.";
$backup_log .= "기존 데이터 백업 실패 : 백업일시".$SET_injection_at;
exit;
}
// ---------------------------------------------------------
// 1. PDO Prepared Statement 준비 (위치 보유자 '?' 사용)
$sql = "CALL proc_edu_users_in(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
foreach ($user_list as $row) {
// 데이터 정리 (null 처리)
$params = [
$row['member_id'] ?? '',
$row['sys_comp_code'] ?? '',
$row['name'] ?? '',
$row['dept_name'] ?? '',
$row['rank_name'] ?? '',
(!empty($row['join_date'])) ? $row['join_date'] : null,
$row['belong_comp'] ?? '',
$row['working_comp'] ?? '',
$row['intra_pw'] ?? '',
$row['auth_level'] ?? '',
(!empty($row['end_date'])) ? $row['end_date'] : null,
$row['intro_flag'] ?? '0',
$row['belong_comp_id'] ?? '',
$row['working_comp_id'] ?? '',
$row['created_at'] ?? date('Y-m-d H:i:s'),
$row['updated_at'] ?? date('Y-m-d H:i:s'),
$SET_injection_at // injection_at
];
// 2. 실행 (PDO는 execute에 배열을 바로 넣으면 바인딩됩니다)
try {
if ($stmt->execute($params)) {
$success_count++;
// 대량 데이터 처리 시 진행 상황 출력 (100명 단위)
if ($success_count % 1000 == 0) {
echo "현재 {$success_count}명 처리 중...<br>";
flush(); // 화면에 즉시 반영
}
} else {
$error_count++;
}
} catch (PDOException $e) {
$error_count++;
// 프로시저 내부 에러 로그는 테이블에 쌓이겠지만 PHP 에러도 확인용 출력
echo "에러(ID: " . ($row['member_id'] ?? 'unknown') . "): " . $e->getMessage() . "<br>";
}
}
// ---------------------------------------------------------
// 로그 파일 생성 및 저장 로직 (한글 깨짐 방지 적용)
// ---------------------------------------------------------
$log_dir = $_SERVER['DOCUMENT_ROOT'] . "/log/";
$log_file_name = "users_info_batch_" . date('Y') . ".txt";
$log_path = $log_dir . $log_file_name;
// 폴더 생성
if (!is_dir($log_dir)) {
mkdir($log_dir, 0777, true);
}
// 로그 내용 구성
if($backup_log==""){
$new_log_entry = "주입일자= {$SET_injection_at} , 성공카운트 = {$success_count} , 실패카운트= {$error_count}" . PHP_EOL;
}else{
$new_log_entry = $backup_log."\n";
$new_log_entry .= "주입일자= {$SET_injection_at} , 성공카운트 = {$success_count} , 실패카운트= {$error_count}" . PHP_EOL;
}
// UTF-8 BOM (한글 깨짐 방지용 식별코드)
$bom = "\xEF\xBB\xBF";
if (file_exists($log_path)) {
// 기존 내용을 읽어옴
$existing_content = file_get_contents($log_path);
// 기존 내용에 BOM이 이미 있다면 제거 (내용 결합 시 중복 방지)
$existing_content = str_replace($bom, '', $existing_content);
// [BOM] + [새 로그] + [기존 로그] 순서로 저장
file_put_contents($log_path, $bom . $new_log_entry . $existing_content);
} else {
// 파일이 처음 생성될 때 BOM과 함께 저장
file_put_contents($log_path, $bom . $new_log_entry);
}
// ---------------------------------------------------------
echo "<hr>";
echo "<strong>처리 완료!</strong><br>";
echo "성공: {$success_count}건 / 실패: {$error_count}";
?>