CloudInventoryGoodsCat.php 3.9 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 app\jobs\storeSync\DiyCommon;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * Class CloudInventoryGoodsCat
  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 string $created_at
  23. * @property string $updated_at
  24. */
  25. class CloudInventoryGoodsCat extends ActiveRecord
  26. {
  27. /**
  28. * 分类是否显示:显示
  29. */
  30. const IS_SHOW_TRUE = 1;
  31. /**
  32. * 分类是否显示:不显示
  33. */
  34. const IS_SHOW_FALSE = 0;
  35. public function behaviors()
  36. {
  37. return [
  38. [
  39. // 自动更新创建和更新时间
  40. 'class' => TimestampBehavior::class,
  41. 'value' => time()
  42. ]
  43. ];
  44. }
  45. public static function tableName()
  46. {
  47. return '{{%cloud_inventory_goods_cat}}';
  48. }
  49. public function rules()
  50. {
  51. return [
  52. [['store_id', 'name',], 'required'],
  53. [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'is_show'], 'integer'],
  54. [['pic_url'], 'string'],
  55. [['name'], 'string', 'max' => 255],
  56. ];
  57. }
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'store_id' => '商城id',
  63. 'parent_id' => '上级分类id',
  64. 'name' => '分类名称',
  65. 'pic_url' => '分类图片url',
  66. 'sort' => '排序,升序',
  67. 'created_at' => '创建时间',
  68. 'updated_at' => '更新时间',
  69. 'is_delete' => 'Is Delete',
  70. 'is_show' => '是否显示'
  71. ];
  72. }
  73. /**
  74. * 获取所有的分类
  75. * @param int $store_id
  76. * @param int $parent_id
  77. * @return array
  78. */
  79. public static function getCatList($store_id = 1, $is_show = -1, $parent_id = 0, $getGoods = 1)
  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. }
  101. $res[] = $val;
  102. }
  103. return $res;
  104. }
  105. public static function getCatId($parent_id, &$cat_list = [])
  106. {
  107. $cat_list[] = $parent_id;
  108. $query = self::find()->where([
  109. 'parent_id' => $parent_id,
  110. 'is_delete' => 0,
  111. 'store_id' => get_store_id()
  112. ])->orderBy(['sort'=>SORT_DESC]);
  113. $query->andWhere([
  114. 'is_show' => 1
  115. ]);
  116. $cat = $query->asArray()->all();
  117. if (empty($cat)) {
  118. return $cat_list;
  119. }
  120. foreach ($cat as &$val) {
  121. self::getCatId($val['id'], $cat_list);
  122. }
  123. return $cat_list;
  124. }
  125. public static function getCatListByPids($store_id = 0, $pids = []) {
  126. $list = self::find()->where([
  127. 'is_delete' => 0,
  128. 'is_show' => 1,
  129. 'store_id' => $store_id
  130. ])->orderBy(['sort'=>SORT_ASC])->all();
  131. foreach($list as $item){
  132. if(in_array($item['parent_id'], $pids)){
  133. $pids[] = $item['id'];
  134. }
  135. }
  136. return $pids;
  137. }
  138. }