119 lines
3.2 KiB
PHP
119 lines
3.2 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
|
||
/**
|
||
* This is the model class for table "{{%faq_meta}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $type 类型:0=分类,1=浏览量
|
||
* @property int $faq_id 帮助ID
|
||
* @property string|null $key KEY
|
||
* @property string|null $value VALUE
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $is_delete 是否删除:0=否,1=是
|
||
* @property int $deleted_at 删除时间
|
||
*/
|
||
class FaqMeta extends \yii\db\ActiveRecord
|
||
{
|
||
const TYPE_CAT = 0;
|
||
const TYPE_VIEWED_NUM = 1;
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%faq_meta}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['key', 'value'], 'trim'],
|
||
[['cx_mch_id', 'type', 'faq_id', 'created_at', 'updated_at', 'is_delete', 'deleted_at'], 'integer'],
|
||
[['faq_id'], 'required'],
|
||
[['key'], 'string', 'max' => 50],
|
||
[['value'], 'string', 'max' => 256],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'type' => '类型:0=分类,1=浏览量',
|
||
'faq_id' => '帮助ID',
|
||
'key' => 'KEY',
|
||
'value' => 'VALUE',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'is_delete' => '是否删除:0=否,1=是',
|
||
'deleted_at' => '删除时间',
|
||
];
|
||
}
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static function getViewedNum($faq_id, $cx_mch_id = 0)
|
||
{
|
||
$model = FaqMeta::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'type' => self::TYPE_VIEWED_NUM,
|
||
'faq_id' => $faq_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if($model == null)
|
||
return 0;
|
||
return $model->value;
|
||
}
|
||
|
||
public static function setViewedNum($faq_id, $cx_mch_id = 0)
|
||
{
|
||
$model = FaqMeta::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'type' => self::TYPE_VIEWED_NUM,
|
||
'faq_id' => $faq_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if($model == null){
|
||
$model = new FaqMeta();
|
||
$model->cx_mch_id = $cx_mch_id;
|
||
$model->type = self::TYPE_VIEWED_NUM;
|
||
$model->faq_id = $faq_id;
|
||
$model->value = 0;
|
||
$model->is_delete = 0;
|
||
}
|
||
$val = (int)$model->value;
|
||
$val += 1;
|
||
$model->value = (string)$val;
|
||
if(!$model->save()){
|
||
return (new Model())->getModelError($model);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
}
|