| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\jobs\storeSync\DiyCommon;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * Class CloudInventoryGoodsCat
- * @package app\modules\models
- * @property integer $id
- * @property integer $store_id
- * @property integer $parent_id
- * @property string $name
- * @property string $pic_url
- * @property integer $sort
- * @property integer $is_delete
- * @property string $is_show
- * @property string $created_at
- * @property string $updated_at
- */
- class CloudInventoryGoodsCat extends ActiveRecord
- {
- /**
- * 分类是否显示:显示
- */
- const IS_SHOW_TRUE = 1;
- /**
- * 分类是否显示:不显示
- */
- const IS_SHOW_FALSE = 0;
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- public static function tableName()
- {
- return '{{%cloud_inventory_goods_cat}}';
- }
- public function rules()
- {
- return [
- [['store_id', 'name',], 'required'],
- [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'is_show'], 'integer'],
- [['pic_url'], 'string'],
- [['name'], 'string', 'max' => 255],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => '商城id',
- 'parent_id' => '上级分类id',
- 'name' => '分类名称',
- 'pic_url' => '分类图片url',
- '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)
- {
- $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']);
- }
- $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;
- }
- }
|