136 lines
3.9 KiB
PHP
136 lines
3.9 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2021年6月29日
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\models\common;
|
||
|
||
use app\models\Faq;
|
||
use app\models\FaqMeta;
|
||
use app\models\Cat;
|
||
use app\models\User;
|
||
use app\models\Model;
|
||
|
||
|
||
class CommonFaqEditForm extends Model
|
||
{
|
||
public $cx_mch_id;
|
||
public $title;
|
||
public $content;
|
||
public $status;
|
||
public $sort;
|
||
public $is_hot;
|
||
|
||
public $model;
|
||
public $cat_ids;
|
||
|
||
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['title', 'content',], 'trim'],
|
||
[['title', 'content',], 'string'],
|
||
[['cx_mch_id', 'status', 'sort', 'is_hot'], 'integer'],
|
||
[['is_hot'], 'in', 'range' => [0, 1]],
|
||
[['status'], 'in', 'range' => [0, 1, 2]],
|
||
[['model', 'cat_ids'], 'safe'],
|
||
[['sort'], 'default', 'value' => 100],
|
||
[['title', 'content', 'cx_mch_id', 'status', 'is_hot', 'model'], 'required'],
|
||
];
|
||
}
|
||
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'cx_mch_id' => '平台商户ID',
|
||
'title' => '标题',
|
||
'content' => '内容',
|
||
'status' => '状态:0=草稿,1=已发布,2=回收站',
|
||
'sort' => '排序:默认100,升序',
|
||
'is_hot' => '是否热门:0=否,1=是',
|
||
];
|
||
}
|
||
|
||
public function save()
|
||
{
|
||
if(!$this->validate()){
|
||
return $this->getModelError();
|
||
}
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
if($this->model->isNewRecord){
|
||
$this->model->cx_mch_id = $this->cx_mch_id;
|
||
$this->model->is_delete = 0;
|
||
}
|
||
$this->model->title = $this->title;
|
||
$this->model->content = $this->content;
|
||
$this->model->status = $this->status;
|
||
$this->model->sort = $this->sort;
|
||
$this->model->is_hot = $this->is_hot;
|
||
if($this->status == Faq::STATUS_PUBLISHED && empty($this->model->published_at)){
|
||
$this->model->published_at = time();
|
||
}
|
||
if(!$this->model->save()){
|
||
$t->rollBack();
|
||
return $this->getModelError($this->model);
|
||
}
|
||
//保存分类
|
||
$res = $this->saveCat($this->model->id, $this->cat_ids, $this->cx_mch_id);
|
||
if($res['code'] != 0){
|
||
$t->rollBack();
|
||
return $res;
|
||
}
|
||
$t->commit();
|
||
return $this->apiReturnSuccess("保存成功");
|
||
}
|
||
|
||
|
||
private function saveCat($faq_id, $cat_ids, $cx_mch_id = 0)
|
||
{
|
||
//删除所有分类
|
||
FaqMeta::updateAll(['is_delete' => 1],[
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'type' => FaqMeta::TYPE_CAT,
|
||
'is_delete' => 0,
|
||
'faq_id' => $faq_id,
|
||
]);
|
||
foreach ($cat_ids as $cat_id){
|
||
//分类是否存在
|
||
$temp = Cat::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'type' => Cat::TYPE_FAQ,
|
||
'is_delete' => 0,
|
||
'id' => $cat_id,
|
||
]);
|
||
if($temp == null){
|
||
return $this->apiReturnError("ID为{$cat_id}的分类不存在");
|
||
}
|
||
$model = FaqMeta::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'type' => FaqMeta::TYPE_CAT,
|
||
'faq_id' => $faq_id,
|
||
'value' => $cat_id
|
||
]);
|
||
if($model == null){
|
||
$model = new FaqMeta();
|
||
$model->cx_mch_id = $cx_mch_id;
|
||
$model->type = FaqMeta::TYPE_CAT;
|
||
$model->faq_id = $faq_id;
|
||
$model->value = $cat_id;
|
||
}
|
||
$model->is_delete = 0;
|
||
if(!$model->save()){
|
||
return $this->getModelError($model);
|
||
}
|
||
}
|
||
return $this->apiReturnSuccess();
|
||
}
|
||
}
|
||
|