MchCat.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. /**
  11. * This is the model class for table "{{%mch_cat}}".
  12. *
  13. * @property integer $id
  14. * @property integer $parent_id
  15. * @property integer $mch_id
  16. * @property string $name
  17. * @property string $icon
  18. * @property integer $sort
  19. * @property integer $is_delete
  20. * @property string $created_at
  21. * @property string $updated_at
  22. */
  23. class MchCat extends \yii\db\ActiveRecord
  24. {
  25. /**
  26. * @inheritdoc
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%mch_cat}}';
  31. }
  32. public function behaviors()
  33. {
  34. return [
  35. [
  36. // 自动更新创建和更新时间
  37. 'class' => TimestampBehavior::class,
  38. 'value' => time()
  39. ]
  40. ];
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['parent_id', 'mch_id', 'sort', 'is_delete', 'created_at', 'updated_at',], 'integer'],
  49. [['icon'], 'string'],
  50. [['name'], 'string', 'max' => 255],
  51. ];
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'parent_id' => 'Parent ID',
  61. 'mch_id' => 'Mch ID',
  62. 'name' => '分类名称',
  63. 'icon' => '分类图标',
  64. 'sort' => 'Sort',
  65. 'is_delete' => 'Is Delete',
  66. 'created_at' => '创建时间',
  67. 'updated_at' => '更新时间'
  68. ];
  69. }
  70. /**
  71. * 获取所有的分类
  72. * @param int $store_id
  73. * @param int $parent_id
  74. * @return array
  75. */
  76. public static function getCatList($mch_id = 1, $is_show = -1, $parent_id = 0)
  77. {
  78. $res = [];
  79. $query = self::find()->where([
  80. 'parent_id' => $parent_id,
  81. 'is_delete' => 0,
  82. 'mch_id' => $mch_id
  83. ])->orderBy(['sort'=>SORT_DESC]);
  84. $cat_list = $query->asArray()->all();
  85. if (empty($cat_list)) {
  86. return $res;
  87. }
  88. foreach ($cat_list as &$val) {
  89. $val['children'] = self::getCatList($mch_id, $is_show, $val['id']);
  90. $res[] = $val;
  91. }
  92. return $res;
  93. }
  94. public static function getCatId($parent_id, &$cat_list = [])
  95. {
  96. $cat_list[] = $parent_id;
  97. $query = self::find()->where([
  98. 'parent_id' => $parent_id,
  99. 'is_delete' => 0,
  100. 'mch_id' => get_mch_id()
  101. ])->orderBy(['sort'=>SORT_DESC]);
  102. $cat = $query->asArray()->all();
  103. if (empty($cat)) {
  104. return $cat_list;
  105. }
  106. foreach ($cat as &$val) {
  107. self::getCatId($val['id'], $cat_list);
  108. }
  109. return $cat_list;
  110. }
  111. }