cxfoot/models/common/CommonCatActionForm.php
2023-10-27 14:25:12 +08:00

229 lines
7.1 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月29日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Cat;
use app\models\Model;
class CommonCatActionForm extends Model
{
public $cx_mch_id;
public $cat_id;
public $type;
public function rules()
{
return [
[['cx_mch_id', 'type'], 'integer'],
[['cat_id'], 'safe'],
[['cx_mch_id', 'type', 'cat_id'], 'required'],
];
}
public function delete()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->cat_id)){
foreach ($this->cat_id as $cat_id){
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $cat_id,
'is_delete' => 0,
'type' => $this->type
]);
if($model == null)
continue;
$model->is_delete = 1;
$model->deleted_at = time();
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $this->cat_id,
'is_delete' => 0,
'type' => $this->type
]);
if($model == null)
return $this->apiReturnError('分类不存在或已经删除');
$model->is_delete = 1;
$model->deleted_at = time();
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess('操作成功');
}
public function open()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->cat_id)){
foreach ($this->cat_id as $cat_id){
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $cat_id,
'is_delete' => 0,
'type' => $this->type,
'status' => 0
]);
if($model == null)
continue;
$model->status = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $this->cat_id,
'is_delete' => 0,
'type' => $this->type,
'status' => 0
]);
if($model == null)
return $this->apiReturnError('分类不存在或已经删除');
$model->status = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess('操作成功');
}
public function close()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->cat_id)){
foreach ($this->cat_id as $cat_id){
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $cat_id,
'is_delete' => 0,
'type' => $this->type,
'status' => 1
]);
if($model == null)
continue;
$model->status = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $this->cat_id,
'is_delete' => 0,
'type' => $this->type,
'status' => 1
]);
if($model == null)
return $this->apiReturnError('分类不存在或已经删除');
$model->status = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess('操作成功');
}
public function show()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->cat_id)){
foreach ($this->cat_id as $cat_id){
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $cat_id,
'is_delete' => 0,
'type' => $this->type,
'is_hide' => 1
]);
if($model == null)
continue;
$model->is_hide = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $this->cat_id,
'is_delete' => 0,
'type' => $this->type,
'is_hide' => 1
]);
if($model == null)
return $this->apiReturnError('分类不存在或已经删除');
$model->is_hide = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess('操作成功');
}
public function hide()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->cat_id)){
foreach ($this->cat_id as $cat_id){
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $cat_id,
'is_delete' => 0,
'type' => $this->type,
'is_hide' => 0
]);
if($model == null)
continue;
$model->is_hide = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'id' => $this->cat_id,
'is_delete' => 0,
'type' => $this->type,
'is_hide' => 0
]);
if($model == null)
return $this->apiReturnError('分类不存在或已经删除');
$model->is_hide = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess('操作成功');
}
}