57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
class BatchActionManager
|
|
{
|
|
private PDO $pdo;
|
|
private int $companyId;
|
|
private int $userId;
|
|
|
|
public function __construct(PDO $pdo, int $company_id, int $user_id)
|
|
{
|
|
$this->pdo = $pdo;
|
|
$this->companyId = $company_id;
|
|
$this->userId = $user_id;
|
|
}
|
|
|
|
public function log(array $data): void
|
|
{
|
|
$type = trim((string)($data['type'] ?? ''));
|
|
$doc_type = trim((string)($data['doc_type'] ?? ''));
|
|
$total = (int)($data['total'] ?? 0);
|
|
$succeeded = (int)($data['succeeded'] ?? 0);
|
|
$failed = (int)($data['failed'] ?? 0);
|
|
$failed_ids = $this->normalizeFailedIds($data['failed_ids'] ?? '[]');
|
|
|
|
if ($type === '') {
|
|
throw new Exception('type is required.');
|
|
}
|
|
|
|
$this->pdo->prepare(
|
|
"INSERT INTO td_batch_action
|
|
(company_id, type, doc_type, total, succeeded, failed, failed_ids, user_id, created_at)
|
|
VALUES
|
|
(:company_id, :type, :doc_type, :total, :succeeded, :failed, :failed_ids, :user_id, :created_at)"
|
|
)->execute([
|
|
':company_id' => $this->companyId,
|
|
':type' => $type,
|
|
':doc_type' => $doc_type,
|
|
':total' => $total,
|
|
':succeeded' => $succeeded,
|
|
':failed' => $failed,
|
|
':failed_ids' => $failed_ids,
|
|
':user_id' => $this->userId,
|
|
':created_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
}
|
|
|
|
private function normalizeFailedIds(mixed $failed_ids): string
|
|
{
|
|
if (!is_string($failed_ids)) {
|
|
return json_encode([]);
|
|
}
|
|
|
|
$decoded = json_decode($failed_ids, true);
|
|
return json_encode(is_array($decoded) ? $decoded : []);
|
|
}
|
|
}
|
|
?>
|