170 lines
6.8 KiB
PHP
170 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin\Product;
|
|
|
|
use App\Repositories\Admin\Product\AdminSaleCodeRepository;
|
|
use App\Services\Admin\AdminAuditService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class AdminSaleCodeService
|
|
{
|
|
public function __construct(
|
|
private readonly AdminSaleCodeRepository $repo,
|
|
private readonly AdminAuditService $audit,
|
|
) {}
|
|
|
|
/**
|
|
* 화면 출력용 트리 구조 생성 (연동사 -> 하위 상품코드)
|
|
*/
|
|
public function getGroupedTree(): array
|
|
{
|
|
$providers = $this->repo->getAllProviders();
|
|
$codes = $this->repo->getAllCodes();
|
|
|
|
$tree = [];
|
|
foreach ($providers as $p) {
|
|
$p['children'] = [];
|
|
$tree[$p['id']] = $p;
|
|
}
|
|
|
|
foreach ($codes as $c) {
|
|
if (isset($tree[$c['provider_id']])) {
|
|
$tree[$c['provider_id']]['children'][] = $c;
|
|
}
|
|
}
|
|
|
|
return array_values($tree);
|
|
}
|
|
|
|
// --- Provider ---
|
|
public function storeProvider(array $input, int $actorAdminId, string $ip, string $ua): array
|
|
{
|
|
try {
|
|
return DB::transaction(function () use ($input, $actorAdminId, $ip, $ua) {
|
|
$data = [
|
|
'code' => strtoupper(trim($input['code'])),
|
|
'name' => trim($input['name']),
|
|
'is_active' => (int)$input['is_active'],
|
|
'sort_order' => 0,
|
|
];
|
|
|
|
$newId = $this->repo->insertProvider($data);
|
|
$this->audit->log($actorAdminId, 'admin.sale_code.provider.create', 'api_provider', $newId, null, $data, $ip, $ua);
|
|
|
|
return ['ok' => true, 'message' => '연동사가 등록되었습니다.'];
|
|
});
|
|
} catch (\Throwable $e) {
|
|
return ['ok' => false, 'message' => '연동사 코드가 중복되거나 저장 오류가 발생했습니다.'];
|
|
}
|
|
}
|
|
|
|
public function updateProvider(int $id, array $input, int $actorAdminId, string $ip, string $ua): array
|
|
{
|
|
try {
|
|
return DB::transaction(function () use ($id, $input, $actorAdminId, $ip, $ua) {
|
|
$before = $this->repo->findProvider($id);
|
|
if (!$before) return ['ok' => false, 'message' => '연동사를 찾을 수 없습니다.'];
|
|
|
|
$data = [
|
|
'name' => trim($input['name']),
|
|
'is_active' => (int)$input['is_active'],
|
|
];
|
|
|
|
$this->repo->updateProvider($id, $data);
|
|
$this->audit->log($actorAdminId, 'admin.sale_code.provider.update', 'api_provider', $id, (array)$before, array_merge((array)$before, $data), $ip, $ua);
|
|
|
|
return ['ok' => true, 'message' => '연동사가 수정되었습니다.'];
|
|
});
|
|
} catch (\Throwable $e) {
|
|
return ['ok' => false, 'message' => '수정 중 오류가 발생했습니다.'];
|
|
}
|
|
}
|
|
|
|
public function deleteProvider(int $id, int $actorAdminId, string $ip, string $ua): array
|
|
{
|
|
$before = $this->repo->findProvider($id);
|
|
if (!$before) return ['ok' => false, 'message' => '존재하지 않는 연동사입니다.'];
|
|
|
|
if ($this->repo->countCodesByProvider($id) > 0) {
|
|
return ['ok' => false, 'message' => '하위 상품 코드가 존재하여 삭제할 수 없습니다. 상품 코드를 먼저 삭제해주세요.'];
|
|
}
|
|
|
|
$this->repo->deleteProvider($id);
|
|
$this->audit->log($actorAdminId, 'admin.sale_code.provider.delete', 'api_provider', $id, (array)$before, null, $ip, $ua);
|
|
|
|
return ['ok' => true, 'message' => '삭제되었습니다.'];
|
|
}
|
|
|
|
// --- Product Code ---
|
|
public function storeCode(array $input, int $actorAdminId, string $ip, string $ua): array
|
|
{
|
|
try {
|
|
return DB::transaction(function () use ($input, $actorAdminId, $ip, $ua) {
|
|
$data = [
|
|
'provider_id' => (int)$input['provider_id'],
|
|
'api_code' => strtoupper(trim($input['api_code'])),
|
|
'name' => trim($input['name']),
|
|
'is_active' => (int)$input['is_active'],
|
|
'sort_order' => 0,
|
|
];
|
|
|
|
$newId = $this->repo->insertCode($data);
|
|
$this->audit->log($actorAdminId, 'admin.sale_code.code.create', 'api_code', $newId, null, $data, $ip, $ua);
|
|
|
|
return ['ok' => true, 'message' => '상품 코드가 등록되었습니다.'];
|
|
});
|
|
} catch (\Throwable $e) {
|
|
return ['ok' => false, 'message' => '해당 연동사에 중복된 코드가 있거나 저장 오류가 발생했습니다.'];
|
|
}
|
|
}
|
|
|
|
public function updateCode(int $id, array $input, int $actorAdminId, string $ip, string $ua): array
|
|
{
|
|
try {
|
|
return DB::transaction(function () use ($id, $input, $actorAdminId, $ip, $ua) {
|
|
$before = $this->repo->findCode($id);
|
|
if (!$before) return ['ok' => false, 'message' => '상품 코드를 찾을 수 없습니다.'];
|
|
|
|
$data = [
|
|
'provider_id' => (int)$input['provider_id'],
|
|
'api_code' => strtoupper(trim($input['api_code'])),
|
|
'name' => trim($input['name']),
|
|
'is_active' => (int)$input['is_active'],
|
|
];
|
|
|
|
$this->repo->updateCode($id, $data);
|
|
$this->audit->log($actorAdminId, 'admin.sale_code.code.update', 'api_code', $id, (array)$before, array_merge((array)$before, $data), $ip, $ua);
|
|
|
|
return ['ok' => true, 'message' => '상품 코드가 수정되었습니다.'];
|
|
});
|
|
} catch (\Throwable $e) {
|
|
return ['ok' => false, 'message' => '수정 중 중복 오류가 발생했습니다.'];
|
|
}
|
|
}
|
|
|
|
public function updateCodeSort(array $ids, int $actorAdminId): array
|
|
{
|
|
try {
|
|
DB::transaction(function () use ($ids) {
|
|
foreach ($ids as $index => $id) {
|
|
$this->repo->updateCodeSortOrder((int)$id, $index + 1);
|
|
}
|
|
});
|
|
return ['ok' => true];
|
|
} catch (\Throwable $e) {
|
|
return ['ok' => false, 'message' => '정렬 중 오류가 발생했습니다.'];
|
|
}
|
|
}
|
|
|
|
public function deleteCode(int $id, int $actorAdminId, string $ip, string $ua): array
|
|
{
|
|
$before = $this->repo->findCode($id);
|
|
if (!$before) return ['ok' => false, 'message' => '상품 코드를 찾을 수 없습니다.'];
|
|
|
|
$this->repo->deleteCode($id);
|
|
$this->audit->log($actorAdminId, 'admin.sale_code.code.delete', 'api_code', $id, (array)$before, null, $ip, $ua);
|
|
|
|
return ['ok' => true, 'message' => '상품 코드가 삭제되었습니다.'];
|
|
}
|
|
}
|