163 lines
7.1 KiB
PHP
163 lines
7.1 KiB
PHP
<?php
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
try {
|
|
$pdo = db_connect();
|
|
|
|
switch ($method) {
|
|
case 'GET':
|
|
if ($action === 'list') {
|
|
// 전체 목록 조회
|
|
$stmt = $pdo->prepare("SELECT * FROM package_pricing WHERE is_active = 1 ORDER BY item_type, item_id");
|
|
$stmt->execute();
|
|
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// JSON 필드 파싱
|
|
foreach ($items as &$item) {
|
|
if ($item['commission_rates']) {
|
|
$item['commission_rates'] = json_decode($item['commission_rates'], true);
|
|
}
|
|
$item['join_fee'] = floatval($item['join_fee']);
|
|
$item['subscription_fee'] = floatval($item['subscription_fee']);
|
|
$item['total_amount'] = $item['total_amount'] ? floatval($item['total_amount']) : null;
|
|
$item['allow_flexible_pricing'] = (bool)$item['allow_flexible_pricing'];
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'data' => $items], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
} elseif ($action === 'get') {
|
|
// 단일 항목 조회
|
|
$item_type = $_GET['item_type'] ?? '';
|
|
$item_id = $_GET['item_id'] ?? '';
|
|
|
|
if (!$item_type || !$item_id) {
|
|
throw new Exception("item_type과 item_id가 필요합니다.");
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM package_pricing WHERE item_type = ? AND item_id = ? AND is_active = 1");
|
|
$stmt->execute([$item_type, $item_id]);
|
|
$item = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($item) {
|
|
if ($item['commission_rates']) {
|
|
$item['commission_rates'] = json_decode($item['commission_rates'], true);
|
|
}
|
|
$item['join_fee'] = floatval($item['join_fee']);
|
|
$item['subscription_fee'] = floatval($item['subscription_fee']);
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'data' => $item], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
} else {
|
|
throw new Exception("잘못된 action입니다.");
|
|
}
|
|
break;
|
|
|
|
case 'POST':
|
|
// 새 항목 생성
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!isset($data['item_type']) || !isset($data['item_id']) || !isset($data['item_name'])) {
|
|
throw new Exception("필수 필드가 누락되었습니다.");
|
|
}
|
|
|
|
$item_type = $data['item_type'];
|
|
$item_id = $data['item_id'];
|
|
$item_name = $data['item_name'];
|
|
$sub_name = $data['sub_name'] ?? null;
|
|
$join_fee = floatval($data['join_fee'] ?? 0);
|
|
$subscription_fee = floatval($data['subscription_fee'] ?? 0);
|
|
$commission_rates = isset($data['commission_rates']) ? json_encode($data['commission_rates'], JSON_UNESCAPED_UNICODE) : null;
|
|
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO package_pricing (item_type, item_id, item_name, sub_name, join_fee, subscription_fee, commission_rates)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
");
|
|
$stmt->execute([$item_type, $item_id, $item_name, $sub_name, $join_fee, $subscription_fee, $commission_rates]);
|
|
|
|
echo json_encode(['success' => true, 'message' => '항목이 생성되었습니다.', 'id' => $pdo->lastInsertId()], JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
|
|
case 'PUT':
|
|
// 항목 수정
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!isset($data['item_type']) || !isset($data['item_id'])) {
|
|
throw new Exception("item_type과 item_id가 필요합니다.");
|
|
}
|
|
|
|
$item_type = $data['item_type'];
|
|
$item_id = $data['item_id'];
|
|
$updates = [];
|
|
$params = [];
|
|
|
|
if (isset($data['join_fee'])) {
|
|
$updates[] = "join_fee = ?";
|
|
$params[] = floatval($data['join_fee']);
|
|
}
|
|
if (isset($data['subscription_fee'])) {
|
|
$updates[] = "subscription_fee = ?";
|
|
$params[] = floatval($data['subscription_fee']);
|
|
}
|
|
if (isset($data['total_amount'])) {
|
|
$updates[] = "total_amount = ?";
|
|
$params[] = $data['total_amount'] !== null ? floatval($data['total_amount']) : null;
|
|
}
|
|
if (isset($data['allow_flexible_pricing'])) {
|
|
$updates[] = "allow_flexible_pricing = ?";
|
|
$params[] = intval($data['allow_flexible_pricing']);
|
|
}
|
|
if (isset($data['commission_rates'])) {
|
|
$updates[] = "commission_rates = ?";
|
|
$params[] = json_encode($data['commission_rates'], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
if (isset($data['item_name'])) {
|
|
$updates[] = "item_name = ?";
|
|
$params[] = $data['item_name'];
|
|
}
|
|
if (isset($data['sub_name'])) {
|
|
$updates[] = "sub_name = ?";
|
|
$params[] = $data['sub_name'];
|
|
}
|
|
|
|
if (empty($updates)) {
|
|
throw new Exception("수정할 필드가 없습니다.");
|
|
}
|
|
|
|
$params[] = $item_type;
|
|
$params[] = $item_id;
|
|
|
|
$sql = "UPDATE package_pricing SET " . implode(", ", $updates) . " WHERE item_type = ? AND item_id = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
|
|
echo json_encode(['success' => true, 'message' => '항목이 수정되었습니다.'], JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
|
|
case 'DELETE':
|
|
// 항목 삭제 (soft delete)
|
|
$item_type = $_GET['item_type'] ?? '';
|
|
$item_id = $_GET['item_id'] ?? '';
|
|
|
|
if (!$item_type || !$item_id) {
|
|
throw new Exception("item_type과 item_id가 필요합니다.");
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE package_pricing SET is_active = 0 WHERE item_type = ? AND item_id = ?");
|
|
$stmt->execute([$item_type, $item_id]);
|
|
|
|
echo json_encode(['success' => true, 'message' => '항목이 삭제되었습니다.'], JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception("지원하지 않는 HTTP 메서드입니다.");
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|