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

190 lines
6.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models;
use Yii;
use app\models\Model;
/**
* This is the model class for table "{{%cat}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property int $type 类型
* @property string $name 名称
* @property string|null $desc 描述
* @property string|null $img_url 图片
* @property int $parent_id 父ID
* @property int $sort 排序默认100升序
* @property int $is_delete 是否删除0=否1=是
* @property int $created_at 添加时间
* @property int $updated_at 更新时间
* @property int $deleted_at 删除时间
* @property int $status 是否启用0=否1=是
* @property int $is_hide 是否隐藏0=否1=是
* @property string|null $big_img_url 大图
* @property string|null $advert_pic 广告图
* @property string|null $advert_url 广告链接
* @property int $advert_url_type 广告链接类型
* @property string|null $advert_open_params 广告链接打开参数
*/
class Cat extends \yii\db\ActiveRecord
{
/** ---TYPE START--- */
const TYPE_GOODS = 0; //普通商品
const TYPE_POST_DEFAULT = 2; //文章分类
const TYPE_FEEDBACK = 3; //意见反馈分类
const TYPE_FAQ = 5; //帮助文档分类
const TYPE_INTEGRAL_GOODS = 7; //积分商品分类
/** ---TYPE END--- */
const IS_YES = 1;
const IS_NO = 0;
/**
*
* @var 按照parent建立索引的标签数组提高效率无需重复获取数据库数据
*/
public static $parentTags = [];
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%cat}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'desc', 'img_url', 'big_img_url', 'advert_pic', 'advert_url', 'advert_open_params'], 'trim'],
[['cx_mch_id', 'type', 'parent_id', 'sort', 'is_delete', 'created_at', 'updated_at', 'deleted_at', 'status', 'is_hide', 'advert_url_type'], 'integer'],
[['name'], 'required'],
[['name'], 'string', 'max' => 128],
[['desc'], 'string', 'max' => 256],
[['img_url', 'big_img_url', 'advert_pic', 'advert_url', 'advert_open_params'], 'string', 'max' => 2048],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'type' => '类型',
'name' => '名称',
'desc' => '描述',
'img_url' => '图片',
'parent_id' => '父ID',
'sort' => '排序默认100升序',
'is_delete' => '是否删除0=否1=是',
'created_at' => '添加时间',
'updated_at' => '更新时间',
'deleted_at' => '删除时间',
'status' => '是否启用0=否1=是',
'is_hide' => '是否隐藏0=否1=是',
'big_img_url' => '大图',
'advert_pic' => '广告图',
'advert_url' => '广告链接',
'advert_url_type' => '广告链接类型',
'advert_open_params' => '广告链接打开参数',
];
}
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();
$this->htmlTagFilter();
return true;
} else {
return false;
}
}
public function htmlTagFilter()
{
$this->name = Model::htmlTagFilter($this->name);
$this->desc = Model::htmlTagFilter($this->desc);
$this->img_url = Model::htmlTagFilter($this->img_url);
$this->big_img_url = Model::htmlTagFilter($this->big_img_url);
$this->advert_pic = Model::htmlTagFilter($this->advert_pic);
$this->advert_url = Model::htmlTagFilter($this->advert_url);
}
/**
* 获取分类层次数组可以默认获取整个分类树的数组也可以获取指定父id的分类树数组
* @param integer $parent
* @return array
* @throws
*/
public static function getTagTreeArray($parent = 0, $type = 0, $cx_mch_id = 0)
{
$parent = intval($parent);
$topLevelTags = [];
//首先确认数据是否已经获取,不需要重复获取数据
if(count(self::$parentTags) == 0)
{
$allTags = Cat::find()->where(['cx_mch_id' => $cx_mch_id, 'type' => $type, 'is_delete' => 0])->select('id,type,name,desc,img_url,parent_id,sort,created_at,big_img_url,status,is_hide,advert_pic,advert_url,advert_url_type,advert_open_params')->orderBy(['sort' => SORT_ASC])->asArray()->all();
//先把获取的全量数组按照parent为数组键值重新排列使用引用的方式提高效率
foreach(array_keys($allTags) as $key)
{
self::$parentTags[$allTags[$key]['parent_id']][] = &$allTags[$key];
}
}
//从parent为键值的数组中获取带有层次信息的数组$level为层级
$level = 0;
$result = self::getChildTags($parent, $level, $cx_mch_id);
if(is_array($result))
$topLevelTags = array_merge($topLevelTags, $result);
return $topLevelTags;
}
/**
* 获取某个父节点下面的所有子节点的数组
* @param integer $parent
* @param integer $level
* @return array
* @throws
*/
private static function getChildTags($parent = 0, $level = 0, $cx_mch_id = 0)
{
$list = [];
if(!array_key_exists($parent, self::$parentTags))
return null;
foreach(self::$parentTags[$parent] as $tag)
{
$tag['level'] = $level;
$list[] = $tag;
//递归调用,直到获取所有的子节点
$result = self::getChildTags($tag['id'], $level + 1);
if(is_array($result))
$list = array_merge($list, $result);
}
return $list;
}
}