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

View File

@ -9,31 +9,38 @@ class Coupon extends Controller
{
parent::__construct();
} //
final public function getCouponInfo($line, $field)
final public function getCouponInfo($data, $field)
{
list($clientData, $coupon) = explode("||", rtrim($line));
$client = $this->getModel("Client")->getData(sprintf("%s='%s'", $field, $clientData));
$service = $this->getModel("Service")->getData(sprintf("client_code='%s'", $client['Client_Code']));
$client = $this->getModel("Client")->getClient(sprintf("%s='%s'", $field, $data));
$service = $this->getModel("Service")->getOneTime($client->Client_Name);
return array(
"client" => $client,
"service" => $service,
"coupon" => $coupon,
);
} //
public function execute(array $lines, $field = "Client_Code")
public function execute(array $lines, $field = "Client_Code", $debug=false)
{
$results = array();
foreach ($this->getCouponInfo($lines, $field) as $coupon) {
//쿠폰 설정
$where = sprintf("coupon='%s'", $coupon['service']['service_code']);
$this->getModel("Service")->setCoupon($coupon['coupon'], $where);
$loop=1;
$successes = array();
$failes = array();
foreach ($lines as $line) {
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'],
$coupon['service']['server_code'], $coupon['service']['service_code'], $coupon['coupon']);
array_push($results, $result);
$result = sprintf("%20s[%20s:%15s]=>쿠폰 %3d개 설정 성공\n", $coupon['client']->Client_Name,
$coupon['service']->server_code, $coupon['service']->service_code, $coupon_cnt);
array_push($successes, $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;
protected function __construct()
{
try {
$envs = parse_ini_file("./env.ini", true);
if (!$envs) {
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->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\Exception $e) {
throw new \Exception("DB접속중 에러가 발생 하였습니다. : " . $e->getMessage());
}
} //
abstract public function getTable();

View File

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

View File

@ -15,24 +15,32 @@ class Service extends Model
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);
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);
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);
return $this->getRows($sql);
$sql = sprintf("SELECT * FROM %s WHERE server_code = '%s-일회성'", $this->getTable(), $client_name);
//echo $sql . "\n";
return $this->getRow($sql);
} //
final public function getCount($where = "")
{
$sql = sprintf("SELECT COUNT(*) FROM %s WHERE %s ", $this->getTable(), $where);