TimestampBehavior::class, 'value' => time() ] ]; } public static function tableName() { return '{{%intelligent_match_cat}}'; } public function rules() { return [ [['store_id', 'cat_name',], 'required'], [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'is_show'], 'integer'], [['cat_name'], 'string', 'max' => 255], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => '商城id', 'parent_id' => '上级分类id', 'cat_name' => '分类名称', 'sort' => '排序,升序', 'created_at' => '创建时间', 'updated_at' => '更新时间', 'is_delete' => 'Is Delete', 'is_show' => '是否显示', ]; } /** * 获取所有的分类 * @param int $store_id * @param int $parent_id * @return array */ public static function getCatList($store_id = 1, $is_show = -1, $parent_id = 0, $getGoods = 1) { // $cat_list_cache_key = 'goods_cat_list_cache_' . $store_id; // if ($list = cache()->get($cat_list_cache_key)) { // return $list; // } $res = []; $query = self::find()->where([ 'parent_id' => $parent_id, 'is_delete' => 0, 'store_id' => $store_id ])->orderBy(['sort'=>SORT_ASC]); if ($is_show > -1) { $query->andWhere([ 'is_show' => $is_show ]); } $cat_list = $query->asArray()->all(); if (empty($cat_list)) { return $res; } foreach ($cat_list as &$val) { $val['children'] = self::getCatList($store_id, $is_show, $val['id'], $getGoods); if($getGoods){ $val['children'] = self::getCatList($store_id, $is_show, $val['id']); $val['scene_list'] = IntelligentScene::find()->where(['store_id' => $store_id, 'cat_id' => $val['id'], 'is_delete' => 0,'status'=>1])->select('name,id,pic_url')->all(); } $res[] = $val; } return $res; } public static function getCatId($parent_id, &$cat_list = []) { $cat_list[] = $parent_id; $query = self::find()->where([ 'parent_id' => $parent_id, 'is_delete' => 0, 'store_id' => get_store_id() ])->orderBy(['sort'=>SORT_DESC]); $query->andWhere([ 'is_show' => 1 ]); $cat = $query->asArray()->all(); if (empty($cat)) { return $cat_list; } foreach ($cat as &$val) { self::getCatId($val['id'], $cat_list); } return $cat_list; } public static function getCatListByPids($store_id = 0, $pids = []) { $list = self::find()->where([ 'is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id ])->orderBy(['sort'=>SORT_ASC])->all(); foreach($list as $item){ if(in_array($item['parent_id'], $pids)){ $pids[] = $item['id']; } } return $pids; } }