| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?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 Cat
- * @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 integer $shop_count
- * @property string $created_at
- * @property string $updated_at
- */
- class Cat 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 '{{%cat}}';
- }
- public function rules()
- {
- return [
- [['store_id', 'name',], 'required'],
- [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'is_show', 'shop_count'], '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' => '是否显示',
- 'shop_count' => '起购数量'
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- \app\modules\admin\models\jushuitan\JuShuiTanForm::afterCatSave($this);
- (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_PRODUCT_CAT, [$this->id]);
- }
- /**
- * 获取所有的分类
- * @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($val['children']){
- foreach ($val['children'] as &$v) {
- $v['children'] = self::getCatList($store_id, $is_show, $v['id'], $getGoods);
- }
- }
- //if($getGoods){
- // $val['children'] = self::getCatList($store_id, $is_show, $val['id']);
- // $goods_ids = GoodsCat::find()->where(['store_id' => $store_id, 'cat_id' => $val['id'], 'is_delete' => 0])->select('goods_id')->all();
- // $goods_ids = array_column($goods_ids, 'goods_id');
- // $val['goods_list'] = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1])->andWhere(['in', 'id', $goods_ids])
- // ->select('name, cover_pic, price, original_price, virtual_sales')->all();
- // }
- $res[] = $val;
- }
- // cache()->set($cat_list_cache_key, $res, 600);
- 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;
- }
- public function beforeSave($insert)
- {
- \queue_push(new \app\jobs\SyncDiyClassifyJob(['store_id' => $this->store_id]));
- return parent::beforeSave($insert); // TODO: Change the autogenerated stub
- }
- }
|