Cat.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\jobs\storeSync\DiyCommon;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * Class Cat
  13. * @package app\modules\models
  14. * @property integer $id
  15. * @property integer $store_id
  16. * @property integer $parent_id
  17. * @property string $name
  18. * @property string $pic_url
  19. * @property integer $sort
  20. * @property integer $is_delete
  21. * @property string $is_show
  22. * @property integer $shop_count
  23. * @property string $created_at
  24. * @property string $updated_at
  25. */
  26. class Cat extends ActiveRecord
  27. {
  28. /**
  29. * 分类是否显示:显示
  30. */
  31. const IS_SHOW_TRUE = 1;
  32. /**
  33. * 分类是否显示:不显示
  34. */
  35. const IS_SHOW_FALSE = 0;
  36. public function behaviors()
  37. {
  38. return [
  39. [
  40. // 自动更新创建和更新时间
  41. 'class' => TimestampBehavior::class,
  42. 'value' => time()
  43. ]
  44. ];
  45. }
  46. public static function tableName()
  47. {
  48. return '{{%cat}}';
  49. }
  50. public function rules()
  51. {
  52. return [
  53. [['store_id', 'name',], 'required'],
  54. [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'is_show', 'shop_count'], 'integer'],
  55. [['pic_url'], 'string'],
  56. [['name'], 'string', 'max' => 255],
  57. ];
  58. }
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'id' => 'ID',
  63. 'store_id' => '商城id',
  64. 'parent_id' => '上级分类id',
  65. 'name' => '分类名称',
  66. 'pic_url' => '分类图片url',
  67. 'sort' => '排序,升序',
  68. 'created_at' => '创建时间',
  69. 'updated_at' => '更新时间',
  70. 'is_delete' => 'Is Delete',
  71. 'is_show' => '是否显示',
  72. 'shop_count' => '起购数量'
  73. ];
  74. }
  75. public function afterSave($insert, $changedAttributes)
  76. {
  77. parent::afterSave($insert, $changedAttributes);
  78. \app\modules\admin\models\jushuitan\JuShuiTanForm::afterCatSave($this);
  79. (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_PRODUCT_CAT, [$this->id]);
  80. }
  81. /**
  82. * 获取所有的分类
  83. * @param int $store_id
  84. * @param int $parent_id
  85. * @return array
  86. */
  87. public static function getCatList($store_id = 1, $is_show = -1, $parent_id = 0, $getGoods = 1)
  88. {
  89. // $cat_list_cache_key = 'goods_cat_list_cache_' . $store_id;
  90. // if ($list = cache()->get($cat_list_cache_key)) {
  91. // return $list;
  92. // }
  93. $res = [];
  94. $query = self::find()->where([
  95. 'parent_id' => $parent_id,
  96. 'is_delete' => 0,
  97. 'store_id' => $store_id
  98. ])->orderBy(['sort'=>SORT_ASC]);
  99. if ($is_show > -1) {
  100. $query->andWhere([
  101. 'is_show' => $is_show
  102. ]);
  103. }
  104. $cat_list = $query->asArray()->all();
  105. if (empty($cat_list)) {
  106. return $res;
  107. }
  108. foreach ($cat_list as &$val) {
  109. $val['children'] = self::getCatList($store_id, $is_show, $val['id'], $getGoods);
  110. if($val['children']){
  111. foreach ($val['children'] as &$v) {
  112. $v['children'] = self::getCatList($store_id, $is_show, $v['id'], $getGoods);
  113. }
  114. }
  115. //if($getGoods){
  116. // $val['children'] = self::getCatList($store_id, $is_show, $val['id']);
  117. // $goods_ids = GoodsCat::find()->where(['store_id' => $store_id, 'cat_id' => $val['id'], 'is_delete' => 0])->select('goods_id')->all();
  118. // $goods_ids = array_column($goods_ids, 'goods_id');
  119. // $val['goods_list'] = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1])->andWhere(['in', 'id', $goods_ids])
  120. // ->select('name, cover_pic, price, original_price, virtual_sales')->all();
  121. // }
  122. $res[] = $val;
  123. }
  124. // cache()->set($cat_list_cache_key, $res, 600);
  125. return $res;
  126. }
  127. public static function getCatId($parent_id, &$cat_list = [])
  128. {
  129. $cat_list[] = $parent_id;
  130. $query = self::find()->where([
  131. 'parent_id' => $parent_id,
  132. 'is_delete' => 0,
  133. 'store_id' => get_store_id()
  134. ])->orderBy(['sort'=>SORT_DESC]);
  135. $query->andWhere([
  136. 'is_show' => 1
  137. ]);
  138. $cat = $query->asArray()->all();
  139. if (empty($cat)) {
  140. return $cat_list;
  141. }
  142. foreach ($cat as &$val) {
  143. self::getCatId($val['id'], $cat_list);
  144. }
  145. return $cat_list;
  146. }
  147. public static function getCatListByPids($store_id = 0, $pids = []) {
  148. $list = self::find()->where([
  149. 'is_delete' => 0,
  150. 'is_show' => 1,
  151. 'store_id' => $store_id
  152. ])->orderBy(['sort'=>SORT_ASC])->all();
  153. foreach($list as $item){
  154. if(in_array($item['parent_id'], $pids)){
  155. $pids[] = $item['id'];
  156. }
  157. }
  158. return $pids;
  159. }
  160. public function beforeSave($insert)
  161. {
  162. \queue_push(new \app\jobs\SyncDiyClassifyJob(['store_id' => $this->store_id]));
  163. return parent::beforeSave($insert); // TODO: Change the autogenerated stub
  164. }
  165. }