114 lines
4.0 KiB
PHP
114 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\ProductEntity;
|
|
|
|
class ProductModel extends BaseModel
|
|
{
|
|
protected $table = "tw_product";
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = ProductEntity::class;
|
|
protected $useSoftDeletes = false;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->allowedFields = ["uid", "user_uid", ...$this->allowedFields, ...$this->getFields(),];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
$fields = ["category_uid", $this->getTitleField(), "photo", "cost", "price", "sale", "stock", "view_cnt", "status", "content",];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
return ["category_uid", "user_uid", $this->getTitleField(), "photo", "cost", "price", "sale", "stock", "view_cnt", "status", "created_at"];
|
|
break;
|
|
case "view":
|
|
return [...$fields, "created_at"];
|
|
break;
|
|
default:
|
|
return $fields;
|
|
break;
|
|
}
|
|
}
|
|
public function getFieldFilters(): array
|
|
{
|
|
return ["category_uid", "user_uid", "status"];
|
|
}
|
|
public function getFieldBatchFilters(): array
|
|
{
|
|
return parent::getFieldBatchFilters();
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "category_uid":
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case "user_uid":
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case 'photo':
|
|
$rules[$field] = !$action ? "if_exist|string" : "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},100]|max_dims[{$field},1024,768]";
|
|
break;
|
|
case 'cost':
|
|
case 'price':
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case 'sale':
|
|
case 'stock':
|
|
$rules[$field] = "if_exist|numeric";
|
|
break;
|
|
case "content":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
public function getEntity($conditions): ProductEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
public function create(array $formDatas): ProductEntity
|
|
{
|
|
return $this->create_process(new ProductEntity(), $formDatas);
|
|
}
|
|
public function modify(ProductEntity $entity, array $formDatas): ProductEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both");
|
|
$this->orLike("content", $word, "both"); //befor , after , both
|
|
}
|
|
|
|
//추가Action
|
|
//장바구니에 넣은 갯수만큼 차감
|
|
final public function decreaseStock(ProductEntity $entity, int $cnt)
|
|
{
|
|
if ($entity->getStock() == $cnt) {
|
|
$this->builder()->set('status', "outofstock");
|
|
}
|
|
//escape -> false옵션 반드시 있어야함
|
|
$this->builder()->set('stock', "stock-{$cnt}", false);
|
|
$this->builder()->where($this->primaryKey, $entity->getPrimaryKey());
|
|
$this->builder()->update();
|
|
}
|
|
}
|