IntelligentMatchCat.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\db\ActiveRecord;
  10. /**
  11. * Class Cat
  12. * @package app\modules\models
  13. * @property integer $id
  14. * @property integer $store_id
  15. * @property integer $parent_id
  16. * @property string $cat_name
  17. * @property integer $sort
  18. * @property integer $is_delete
  19. * @property string $is_show
  20. * @property string $created_at
  21. * @property string $updated_at
  22. */
  23. class IntelligentMatchCat extends ActiveRecord
  24. {
  25. /**
  26. * 分类是否显示:显示
  27. */
  28. const IS_SHOW_TRUE = 1;
  29. /**
  30. * 分类是否显示:不显示
  31. */
  32. const IS_SHOW_FALSE = 0;
  33. public function behaviors()
  34. {
  35. return [
  36. [
  37. // 自动更新创建和更新时间
  38. 'class' => TimestampBehavior::class,
  39. 'value' => time()
  40. ]
  41. ];
  42. }
  43. public static function tableName()
  44. {
  45. return '{{%intelligent_match_cat}}';
  46. }
  47. public function rules()
  48. {
  49. return [
  50. [['store_id', 'cat_name',], 'required'],
  51. [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'is_show'], 'integer'],
  52. [['cat_name'], 'string', 'max' => 255],
  53. ];
  54. }
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'store_id' => '商城id',
  60. 'parent_id' => '上级分类id',
  61. 'cat_name' => '分类名称',
  62. 'sort' => '排序,升序',
  63. 'created_at' => '创建时间',
  64. 'updated_at' => '更新时间',
  65. 'is_delete' => 'Is Delete',
  66. 'is_show' => '是否显示',
  67. ];
  68. }
  69. /**
  70. * 获取所有的分类
  71. * @param int $store_id
  72. * @param int $parent_id
  73. * @return array
  74. */
  75. public static function getCatList($store_id = 1, $is_show = -1, $parent_id = 0, $getGoods = 1)
  76. {
  77. // $cat_list_cache_key = 'goods_cat_list_cache_' . $store_id;
  78. // if ($list = cache()->get($cat_list_cache_key)) {
  79. // return $list;
  80. // }
  81. $res = [];
  82. $query = self::find()->where([
  83. 'parent_id' => $parent_id,
  84. 'is_delete' => 0,
  85. 'store_id' => $store_id
  86. ])->orderBy(['sort'=>SORT_ASC]);
  87. if ($is_show > -1) {
  88. $query->andWhere([
  89. 'is_show' => $is_show
  90. ]);
  91. }
  92. $cat_list = $query->asArray()->all();
  93. if (empty($cat_list)) {
  94. return $res;
  95. }
  96. foreach ($cat_list as &$val) {
  97. $val['children'] = self::getCatList($store_id, $is_show, $val['id'], $getGoods);
  98. if($getGoods){
  99. $val['children'] = self::getCatList($store_id, $is_show, $val['id']);
  100. $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();
  101. }
  102. $res[] = $val;
  103. }
  104. return $res;
  105. }
  106. public static function getCatId($parent_id, &$cat_list = [])
  107. {
  108. $cat_list[] = $parent_id;
  109. $query = self::find()->where([
  110. 'parent_id' => $parent_id,
  111. 'is_delete' => 0,
  112. 'store_id' => get_store_id()
  113. ])->orderBy(['sort'=>SORT_DESC]);
  114. $query->andWhere([
  115. 'is_show' => 1
  116. ]);
  117. $cat = $query->asArray()->all();
  118. if (empty($cat)) {
  119. return $cat_list;
  120. }
  121. foreach ($cat as &$val) {
  122. self::getCatId($val['id'], $cat_list);
  123. }
  124. return $cat_list;
  125. }
  126. public static function getCatListByPids($store_id = 0, $pids = []) {
  127. $list = self::find()->where([
  128. 'is_delete' => 0,
  129. 'is_show' => 1,
  130. 'store_id' => $store_id
  131. ])->orderBy(['sort'=>SORT_ASC])->all();
  132. foreach($list as $item){
  133. if(in_array($item['parent_id'], $pids)){
  134. $pids[] = $item['id'];
  135. }
  136. }
  137. return $pids;
  138. }
  139. }