This commit is contained in:
최준흠 2022-03-25 10:17:30 +09:00
commit 33b8ad0723
5 changed files with 54 additions and 41 deletions

View File

@ -3,25 +3,21 @@ namespace lib\Controller;
require_once "lib/autoload.php"; require_once "lib/autoload.php";
if ($argc < 1) { if ($argc < 2) {
die("사용법 : php main.php 데이타파일명 \n"); die("사용법 : php main.php 데이타파일명 [Type]\n");
} }
if (!is_file($argv[1])) { if (!is_file($argv[1])) {
die($argv[1] . "는 파일형식이 아닙니다. \n"); die($argv[1] . "는 파일형식이 아닙니다. \n");
} }
$client_type = isset($argv[2]) ? $argv[2]:'Client_Code';
$lines = file($argv[1]); $lines = file($argv[1]);
if (!is_array($lines)) { if (!is_array($lines)) {
die($argv[1] . "는 파일데이터에 오류가 있습니다. \n"); die($argv[1] . "는 파일데이터에 오류가 있습니다. \n");
} }
try { $debug = false;
$coupon = new Coupon(); $coupon = new Coupon();
$results = $coupon->execute($lines); list($successes,$failes) = $coupon->execute($lines,$client_type,$debug);
print("Coupon 설정이 완료되었습니다.\n");
print("Coupon 설정이 완료되었습니다.\n"); print_r($successes);
foreach ($results as $result) { print_r($failes);
echo $result . "\n";
}
} catch (\Exception $e) {
echo $e->getMessage();
} //

View File

@ -9,31 +9,38 @@ class Coupon extends Controller
{ {
parent::__construct(); parent::__construct();
} // } //
final public function getCouponInfo($line, $field) final public function getCouponInfo($data, $field)
{ {
list($clientData, $coupon) = explode("||", rtrim($line)); $client = $this->getModel("Client")->getClient(sprintf("%s='%s'", $field, $data));
$client = $this->getModel("Client")->getData(sprintf("%s='%s'", $field, $clientData)); $service = $this->getModel("Service")->getOneTime($client->Client_Name);
$service = $this->getModel("Service")->getData(sprintf("client_code='%s'", $client['Client_Code']));
return array( return array(
"client" => $client, "client" => $client,
"service" => $service, "service" => $service,
"coupon" => $coupon,
); );
} // } //
public function execute(array $lines, $field = "Client_Code") public function execute(array $lines, $field = "Client_Code", $debug=false)
{ {
$results = array(); $loop=1;
foreach ($this->getCouponInfo($lines, $field) as $coupon) { $successes = array();
//쿠폰 설정 $failes = array();
$where = sprintf("coupon='%s'", $coupon['service']['service_code']); foreach ($lines as $line) {
$this->getModel("Service")->setCoupon($coupon['coupon'], $where); list($info, $coupon_cnt) = explode("||", rtrim($line));
$coupon = $this->getCouponInfo($info, $field);
if(isset($coupon['service']->service_code)){
//쿠폰 설정
$where = sprintf("service_code='%s'", $coupon['service']->service_code);
$this->getModel("Service")->setCoupon($coupon_cnt, $where, $debug);
//결과정리 $result = sprintf("%20s[%20s:%15s]=>쿠폰 %3d개 설정 성공\n", $coupon['client']->Client_Name,
$result = sprintf("%20s[%20s:%15s]=>쿠폰 %3d개 설정\n", $coupon['client']['Client_Name'], $coupon['service']->server_code, $coupon['service']->service_code, $coupon_cnt);
$coupon['service']['server_code'], $coupon['service']['service_code'], $coupon['coupon']); array_push($successes, $result);
array_push($results, $result); } else {
$result = sprintf("%5d번째: %s 설정 실패\n", $loop,rtrim($line));
array_push($failes, $result);
}
$loop++;
} }
return $results; return array($successes,$failes);
} }
} }

View File

@ -10,7 +10,6 @@ abstract class Model
private $_mode = PDO::FETCH_OBJ; private $_mode = PDO::FETCH_OBJ;
protected function __construct() protected function __construct()
{ {
try {
$envs = parse_ini_file("./env.ini", true); $envs = parse_ini_file("./env.ini", true);
if (!$envs) { if (!$envs) {
throw new \Exception(var_export($envs, true)); throw new \Exception(var_export($envs, true));
@ -20,9 +19,6 @@ abstract class Model
$this->db = new PDO($dsn, $envs['db']['id'], $envs['db']['passwd']); $this->db = new PDO($dsn, $envs['db']['id'], $envs['db']['passwd']);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\Exception $e) {
throw new \Exception("DB접속중 에러가 발생 하였습니다. : " . $e->getMessage());
}
} // } //
abstract public function getTable(); abstract public function getTable();

View File

@ -14,4 +14,10 @@ class Client extends Model
{ {
return 'clientdb'; return 'clientdb';
} }
final public function getClient($where)
{
$sql = sprintf("SELECT * FROM %s WHERE %s", $this->getTable(), $where);
return $this->getRow($sql);
} //
} //Class } //Class

View File

@ -15,24 +15,32 @@ class Service extends Model
return 'servicedb'; return 'servicedb';
} }
//쿠폰 추가 //쿠폰 추가
final public function addCoupon($coupon, $where = "") final public function addCoupon($coupon, $where = "",$debug=false)
{ {
$sql = sprintf("UPDATE %s SET coupon = coupon+%d %s", $this->getTable(), $coupon, $where ? "WHERE " . $where : $where); $sql = sprintf("UPDATE %s SET coupon = coupon+%d %s", $this->getTable(), $coupon, $where ? "WHERE " . $where : $where);
return $this->execute($sql); if($debug){
echo $sql."\n";
}else{
return $this->execute($sql);
}
} // } //
//쿠폰 설정 //쿠폰 설정
final public function setCoupon($coupon, $where = "") final public function setCoupon($coupon, $where = "",$debug=false)
{ {
$sql = sprintf("UPDATE %s SET coupon = %d %s", $this->getTable(), $coupon, $where ? "WHERE " . $where : $where); $sql = sprintf("UPDATE %s SET coupon = %d %s", $this->getTable(), $coupon, $where ? "WHERE " . $where : $where);
return $this->execute($sql); if($debug){
echo $sql."\n";
}else{
return $this->execute($sql);
}
} // } //
//일회성서비스정보 가져오기 //일회성서비스정보 가져오기
final public function getOneTimes($where = "") final public function getOneTime($client_name)
{ {
$sql = sprintf("SELECT * FROM %s WHERE server_code LIKE '%일회성%' %s", $this->getTable(), $where ? "WHERE " . $where : $where); $sql = sprintf("SELECT * FROM %s WHERE server_code = '%s-일회성'", $this->getTable(), $client_name);
return $this->getRows($sql); //echo $sql . "\n";
return $this->getRow($sql);
} // } //
final public function getCount($where = "") final public function getCount($where = "")
{ {
$sql = sprintf("SELECT COUNT(*) FROM %s WHERE %s ", $this->getTable(), $where); $sql = sprintf("SELECT COUNT(*) FROM %s WHERE %s ", $this->getTable(), $where);