dbms/app/Controllers/CLI/DBMigration/DBMigration.php
2025-07-07 19:20:00 +09:00

72 lines
2.5 KiB
PHP

<?php
namespace App\Controllers\CLI\DBMigration;
use App\Controllers\BaseController;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
abstract class DBMigration extends BaseController
{
private $_targetDB = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
abstract protected function getSourceDB(): BaseConnection;
abstract protected function getClient(): array;
final protected function getTargetDB(): BaseConnection
{
if ($this->_targetDB === null) {
$this->_targetDB = \Config\Database::connect('default');
}
return $this->_targetDB;
}
private function convertClient(array $row): array
{
$temps = [];
$temps['code'] = $row['Client_Code'];
$temps['role'] = $row['Client_Reseller'] == 30 ? "reseller" : "user";
$temps['name'] = $row['Client_Name'];
$temps['phone'] = empty($row['Client_Phone1']) ? "" : $row['Client_Phone1'];
if (empty($temps['phone'])) {
$temps['phone'] = empty($row['Client_Phone2']) ? "" : $row['Client_Phone2'];
}
$temps['email'] = empty($row['Client_EMail1']) ? "" : $row['Client_EMail1'];
if (empty($temps['email'])) {
$temps['email'] = empty($row['Client_EMail2']) ? "" : $row['Client_EMail2'];
}
$temps['account_balance'] = intval($row['Client_Money']);
$temps['coupon_balance'] = 0;
$temps['point_balance'] = 0;
$temps['status'] = 'default';
return $temps;
}
private function setClient(mixed $rows): void
{
foreach ($rows as $row) {
$datas = $this->convertClient($row);
$this->getTargetDB()->table('clientinfo')->insert($datas);
}
}
final public function client(): void
{
//Transaction Start
$this->getTargetDB()->transStart();
try {
$this->setClient($this->getClient());
echo " 작업을 완료하였습니다.";
$this->getTargetDB()->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
$this->getTargetDB()->transRollback();
echo " 작업을 실패하였습니다.\n--------------\n" . $e->getMessage() . "\n--------------\n";
}
}
}