From 47f1a7c648beb41a2a28d9b712bfb95607e4f655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Mon, 20 May 2024 19:18:29 +0900 Subject: [PATCH] vhost init...4 --- app/Cells/ProductCell.php | 16 ++++++++ app/Controllers/Admin/ProductController.php | 40 +++++++++---------- app/Database/base.sql | 2 + app/Models/BaseModel.php | 43 +++++++++++++-------- app/Models/ProductDeviceModel.php | 7 +--- app/Views/cells/product/device.php | 2 +- 6 files changed, 67 insertions(+), 43 deletions(-) diff --git a/app/Cells/ProductCell.php b/app/Cells/ProductCell.php index b93e897..01340e5 100644 --- a/app/Cells/ProductCell.php +++ b/app/Cells/ProductCell.php @@ -4,20 +4,36 @@ namespace App\Cells; use App\Cells\BaseCell; use App\Models\DeviceModel; +use App\Models\ProductDeviceModel; class ProductCell extends BaseCell { private $_deviceModel = null; + private $_productDeviceModel = null; + final protected function getDeviceModel(): DeviceModel { return $this->_deviceModel = $this->_deviceModel ?: new DeviceModel(); } + final protected function getProductDeviceModel(): ProductDeviceModel + { + return $this->_productDeviceModel = $this->_productDeviceModel ?: new ProductDeviceModel(); + } public function device(array $cellDatas = []) { + $cellDatas['defaults'] = []; + if (array_key_exists('entity', $cellDatas)) { + foreach ($this->getProductDeviceModel()->getEntitys( + ['product_uid' => $cellDatas['entity']->getPrimaryKey()] + ) as $productDevieceEntity) { + $cellDatas['defaults'][$productDevieceEntity->device_uid] = $productDevieceEntity; + } + } $cellDatas['device'] = []; $cellDatas['device']['categorys'] = ['server', 'cpu', 'memory', 'disk', 'nic', 'publicip', 'os']; $cellDatas['device']['options'] = $this->getDeviceModel()->getOptions(); + //dd($cellDatas); return view( 'Views/cells/product/' . __FUNCTION__, ['cellDatas' => $cellDatas] diff --git a/app/Controllers/Admin/ProductController.php b/app/Controllers/Admin/ProductController.php index ac40fe0..329f1dd 100644 --- a/app/Controllers/Admin/ProductController.php +++ b/app/Controllers/Admin/ProductController.php @@ -132,23 +132,6 @@ class ProductController extends AdminController return array(); //return $this->add_procedure($product, 1, $this->_viewDatas['fieldDatas']['paymentday']); } - //실서버 - protected function beremetal_process(ProductEntity $entity, array $devices = []): array - { - $fields = ['server', 'cpu', 'memory', 'disk', 'nic', 'publicip', 'os']; - //fieldData 적용 - foreach ($fields as $field) { - $formDatas = ['product_uid' => $entity->getPrimaryKey()]; - $formDatas['device_uid'] = $this->request->getPost($field); - $formDatas['device_cnt'] = $this->request->getPost($field . "_cnt") ?: 1; - if (!is_null($formDatas['device_uid'])) { - $devices[$field] = $this->getProductDeviceModel()->create($formDatas); - } - } - dd($devices); - return $devices; - } - //주문처리 protected function device_process(ProductEntity $entity): array { @@ -157,7 +140,24 @@ class ProductController extends AdminController return $this->virtual_process($entity); break; case 'beremetal': - return $this->beremetal_process($entity); + //상품관련 tw_product_device 기존정보 삭제처리 + foreach ($this->getProductDeviceModel()->getEntitys( + ['product_uid' => $entity->getPrimaryKey()] + ) as $productDeviceEntity) { + $this->getProductDeviceModel()->delete($productDeviceEntity); + } + //상품관련 tw_product_device 기존정보 생성처리 + $devices = []; + foreach (['server', 'cpu', 'memory', 'disk', 'nic', 'publicip', 'os'] as $field) { + $formDatas = ['product_uid' => $entity->getPrimaryKey()]; + $formDatas['device_uid'] = $this->request->getPost($field); + $formDatas['device_cnt'] = $this->request->getPost($field . "_cnt") ?: 1; + if ($formDatas['product_uid'] && $formDatas['device_uid']) { + $devices[$field] = $this->getProductDeviceModel()->create($formDatas); + } + } + // dd($devices); + return $devices; break; default: throw new \Exception($this->_viewDatas['fieldDatas']['category'] . "는 알수없는 상품 구분입니다. 다시 확인 부탁드립니다."); @@ -178,10 +178,8 @@ class ProductController extends AdminController { $this->_viewDatas['fieldDatas']['price'] = $this->calculate_price(); $entity = parent::update_process($entity); - foreach ($this->getProductDeviceModel()->getEntitys(['product_uid' => $entity->getPrimaryKey()]) as $productDeviceEntity) { - $this->getProductDeviceModel()->delete($productDeviceEntity); - } $entity->setDevices($this->device_process($entity)); + // dd($entity); return $entity; } } diff --git a/app/Database/base.sql b/app/Database/base.sql index 48e5477..2803d2b 100644 --- a/app/Database/base.sql +++ b/app/Database/base.sql @@ -154,7 +154,9 @@ CREATE TABLE vhost.tw_product_device ( product_uid varchar(36) NOT NULL COMMENT '상품 정보', device_uid varchar(36) NOT NULL COMMENT '장비 정보', device_cnt int(3) unsigned NOT NULL DEFAULT 1 COMMENT '장비 갯수', + updated_at timestamp NULL DEFAULT NULL, created_at timestamp NOT NULL DEFAULT current_timestamp(), + deleted_at timestamp NULL DEFAULT NULL, PRIMARY KEY (uid), KEY product_uid (product_uid), KEY device_uid (device_uid), diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index c6bb384..e8cb10c 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -164,15 +164,12 @@ abstract class BaseModel extends Model } return $this->first() ?: throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 해당 데이터가 없습니다."); } - public function getEntitys(array $conditions = [], array $entitys = []): array + public function getEntitys(array $conditions = []): array { foreach ($conditions as $field => $value) { $this->where($field, $value); } - foreach ($this->findAll() as $entity) { - $entitys[$entity->getPrimaryKey()] = $entity; - } - return $entitys; + return $this->findAll(); } //Field별 Form Option용 public function getOptionLabel($entity) @@ -181,7 +178,10 @@ abstract class BaseModel extends Model } public function getOptions(array $conditions = array(), $options = array()): array { - foreach ($this->getEntitys($conditions, $options) as $entity) { + foreach ($conditions as $field => $value) { + $this->where($field, $value); + } + foreach ($this->findAll() as $entity) { $options[$entity->getPrimaryKey()] = $this->getOptionLabel($entity); } return $options; @@ -244,19 +244,32 @@ abstract class BaseModel extends Model return $entity; } + final protected function save_process($entity) + { + // echo var_export($entity, true); + // exit; + if ($entity->hasChanged()) { + if (!$this->save($entity)) { + log_message("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery()); + log_message("error", implode("\n", $this->errors())); + throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . $this->getLastQuery() . "\n" . var_export($this->errors(), true)); + } + } else { + throw new \Exception(__FUNCTION__ . " 오류 발생.\n 기존정보와 동일하여 수정되지 않았습니다."); + } + return $entity; + } protected function create_process($entity, array $formDatas) { foreach ($this->allowedFields as $field) { $entity = $this->changeFormData('create', $field, $formDatas, $entity); } - //primaryKey가 수동입력이면 - if (!$this->useAutoIncrement) { - // $pk = $this->primaryKey; - // $entity->$pk = $this->getUUID(); - //위 형식을 아래와같이 변경 - $entity->$$this->primaryKey = $this->getUUID(); + $entity = $this->save_process($entity); + //primaryKey가 자동입력이면 + if ($this->useAutoIncrement) { + $pk = $this->primaryKey; + $entity->$pk = $this->insertID(); } - $this->insert($entity); return $entity; } final protected function modify_process($entity, array $formDatas) @@ -265,9 +278,7 @@ abstract class BaseModel extends Model $entity = $this->changeFormData('modify', $field, $formDatas, $entity); } $entity->updated_at = time(); - //dd($entity); - $this->update($entity->getPrimaryKey(), $entity); - return $entity; + return $this->save_process($entity); } //Index관련 diff --git a/app/Models/ProductDeviceModel.php b/app/Models/ProductDeviceModel.php index 135f047..3c51778 100644 --- a/app/Models/ProductDeviceModel.php +++ b/app/Models/ProductDeviceModel.php @@ -3,7 +3,6 @@ namespace App\Models; use App\Entities\ProductDeviceEntity; -use App\Entities\ProductEntity; class ProductDeviceModel extends BaseModel { @@ -11,7 +10,7 @@ class ProductDeviceModel extends BaseModel protected $returnType = ProductDeviceEntity::class; public function __construct() { - parent::__construct('ProductDevic'); + parent::__construct('ProductDevice'); $this->allowedFields = [ ...$this->allowedFields, "product_uid", "device_uid", "device_cnt" ]; @@ -43,9 +42,7 @@ class ProductDeviceModel extends BaseModel } public function create(array $formDatas): ProductDeviceEntity { - $entity = $this->create_process(new ProductDeviceEntity(), $formDatas); - dd($entity); - return $entity; + return $this->create_process(new ProductDeviceEntity(), $formDatas); } public function modify(ProductDeviceEntity $entity, array $formDatas): ProductDeviceEntity { diff --git a/app/Views/cells/product/device.php b/app/Views/cells/product/device.php index ee24695..d9d939b 100644 --- a/app/Views/cells/product/device.php +++ b/app/Views/cells/product/device.php @@ -10,7 +10,7 @@ $category : DEFAULTS['EMPTY']), + $cellDatas['defaults'], [ 'id' => $category, 'size' => "6",