giftcon_dev/app/Services/QnaService.php

150 lines
5.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\CounselingOneOnOne;
use App\Jobs\SendAdminQnaCreatedMailJob;
use App\Support\LegacyCrypto\CiSeedCrypto;
use Illuminate\Support\Str;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
final class QnaService
{
public function paginateMyQna(int $memNo, int $perPage = 10, int $year = 0): LengthAwarePaginator
{
$year = $year > 0 ? $year : (int) date('Y');
$table = "counseling_one_on_one_{$year}";
if (!Schema::hasTable($table)) {
return new Paginator([], 0, $perPage, 1, [
'path' => request()->url(),
'query' => request()->query(),
]);
}
return CounselingOneOnOne::queryForYear($year)
->from("{$table} as q")
->leftJoin('counseling_code as cc', function ($join) {
$join->on('cc.enquiry_code', '=', 'q.enquiry_code')
->where('cc.enquiry_type', '=', 'e'); // ✅ 접수분류 타입
})
->where('q.member_num', $memNo)
->select([
'q.seq',
'q.state',
'q.enquiry_code',
'q.enquiry_title',
'q.regdate',
// ✅ 코드 대신 보여줄 분류명
'cc.enquiry_title as enquiry_code_name',
])
->orderByDesc('q.seq')
->paginate($perPage)
->withQueryString();
}
public function findMyQna(int $memNo, int $seq, int $year = 0)
{
$year = $year > 0 ? $year : (int) date('Y');
$table = "counseling_one_on_one_{$year}";
if (!Schema::hasTable($table)) {
abort(404);
}
return CounselingOneOnOne::queryForYear($year)
->from("{$table} as q")
->leftJoin('counseling_code as cc', function ($join) {
$join->on('cc.enquiry_code', '=', 'q.enquiry_code')
->where('cc.enquiry_type', '=', 'e');
})
->where('q.member_num', $memNo)
->where('q.seq', $seq)
->select([
'q.seq',
'q.state',
'q.enquiry_code',
'q.enquiry_title',
'q.enquiry_content',
'q.answer_content',
'q.regdate',
'cc.enquiry_title as enquiry_code_name',
])
->firstOrFail();
}
public function getEnquiryCodes(): array
{
return DB::table('counseling_code')
->where('enquiry_type', 'e')
->orderBy('enquiry_code', 'asc')
->get(['enquiry_code', 'enquiry_title'])
->map(fn($r) => ['code' => (string)$r->enquiry_code, 'title' => (string)$r->enquiry_title])
->all();
}
/**
* 1:1 문의 등록 (연도 테이블: counseling_one_on_one_YYYY)
* 반환: insert seq(id)
*/
public function createQna(int $memNo, array $payload, ?int $year = null): int
{
$year = $year ?: (int)date('Y');
$table = "counseling_one_on_one_{$year}";
// 테이블이 없으면(혹시 모를 경우) 생성은 다음 단계로. 지금은 실패 처리
if (!Schema::hasTable($table)) {
throw new \RuntimeException("qna_table_not_found: {$table}");
}
$enquiryCode = (string)($payload['enquiry_code'] ?? '');
$enquiryTitle = strip_tags(trim((string)($payload['enquiry_title'] ?? '')));
$enquiryContent = strip_tags(trim((string)($payload['enquiry_content'] ?? '')));
$returnType = (string)($payload['return_type'] ?? 'web');
$id = (int) DB::transaction(function () use ($table, $memNo, $enquiryCode, $enquiryTitle, $enquiryContent, $returnType) {
return DB::table($table)->insertGetId([
'state' => 'a',
'member_num' => $memNo,
'enquiry_code' => $enquiryCode,
'enquiry_title' => $enquiryTitle,
'enquiry_content' => $enquiryContent,
'return_type' => $returnType,
'user_upload' => null,
'regdate' => now()->format('Y-m-d H:i:s'),
'receipt_date' => "1900-01-01 00:00:00",
'defer_date' => "1900-01-01 00:00:00",
'completion_date' => "1900-01-01 00:00:00",
]);
});
$userEmail = (string)($payload['_user_email'] ?? '');
$userName = (string)($payload['_user_name'] ?? '');
$ip = (string)($payload['_ip'] ?? '');
$cellEnc = (string)($payload['_user_cell_enc'] ?? '');
$seed = app(CiSeedCrypto::class);
$cellPlain = (string) $seed->decrypt($cellEnc);
SendAdminQnaCreatedMailJob::dispatch($memNo, $id, [
'name' => $userName,
'email' => $userEmail,
'cell' => $cellPlain,
'ip' => $ip,
'year' => $year,
'enquiry_code' => $enquiryCode,
'enquiry_title' => $enquiryTitle,
'enquiry_content' => $enquiryContent,
'return_type' => $returnType,
'created_at' => now()->toDateTimeString(),
])->onConnection('redis'); // 네 QUEUE_CONNECTION=redis일 때 명시해도 좋음
return $id;
}
}