LinkCategory.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace common\models\link;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%link_category}}".
  7. *
  8. *
  9. * @property int $id 主键
  10. * @property string $name 分类key
  11. * @property string $title 中文名
  12. * @property string $pid 父级ID
  13. * @property string $is_list 是否是列表数据
  14. * @property string $remark 注意事项
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int|null $created_at 创建时间
  17. * @property int|null $updated_at 修改时间
  18. */
  19. class LinkCategory extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%link_category}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['status', 'is_list', 'created_at', 'updated_at'], 'integer'],
  35. [['title'], 'string', 'max' => 20],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => '主键',
  45. 'name' => '分类key',
  46. 'pid' => '父级ID',
  47. 'title' => '中文名',
  48. 'is_list' => '是否是列表数据',
  49. 'remark' => '注意事项',
  50. 'status' => '状态[-1:删除;0:禁用;1启用]',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => '修改时间',
  53. ];
  54. }
  55. /**
  56. * 获取分类名称
  57. * @param $mall_id
  58. * @return array
  59. */
  60. public static function getCategoryName($is_format = true)
  61. {
  62. $data = [];
  63. $field = "id,name,title,remark";
  64. $list = self::find()->select($field)->where(['status' => StatusEnum::ENABLED])->asArray()->all();
  65. if ($is_format) {
  66. foreach ($list as $item) {
  67. $data[$item['id']] = $item['title'];
  68. }
  69. } else {
  70. $data = $list;
  71. }
  72. return $data;
  73. }
  74. /**
  75. * 获取树形结构
  76. * @Author: lun
  77. * @DateTime: 2021/10/13 0013 9:17
  78. * @Copyright: copyright (c) 2021 广东七件事集团
  79. * @param array $menu
  80. * @param int $pid
  81. * @return array
  82. */
  83. public static function getTreeMenu(array $menu, int $pid = 0)
  84. {
  85. $tree = [];
  86. foreach ($menu as $k => $v) {
  87. $v['value'] = $v['id'];
  88. $v['label'] = $v['title'];
  89. if ($v['pid'] == $pid) {
  90. $tmp = $v;
  91. // 当前路由的菜单被选中
  92. $children = self::getTreeMenu($menu, $v['value']);
  93. $children && $tmp['children'] = $children;
  94. $tree[] = $tmp;
  95. }
  96. }
  97. return $tree;
  98. }
  99. /**
  100. * 保存数据
  101. * @Author: lun
  102. * @DateTime: 2021/10/13 0013 18:56
  103. * @Copyright: copyright (c) 2021 广东七件事集团
  104. * @param $params
  105. * @return bool
  106. */
  107. public static function setData($params)
  108. {
  109. try{
  110. if (!empty($params['id'])) {
  111. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED]]);
  112. if (empty($model)) throw new \Exception('分类不存在或已删除');
  113. } else {
  114. $model = new self();
  115. $model->loadDefaultValues();
  116. }
  117. $model->name = $params['name'];
  118. $model->title = $params['title'];
  119. $model->pid = $params['pid'];
  120. $model->is_list = $params['is_list'];
  121. $model->remark = $params['remark'];
  122. if(!$model->save(false)){
  123. throw new \Exception($model->getErrorMessage());
  124. }
  125. return true;
  126. }catch(\Exception $e){
  127. self::$static_error = $e->getMessage();
  128. return false;
  129. }
  130. }
  131. /**
  132. * 获取分类列表
  133. * @Author: lun
  134. * @DateTime: 2021/10/13 0013 9:34
  135. * @Copyright: copyright (c) 2021 广东七件事集团
  136. * @param array $cond
  137. * @param array $with
  138. * @param bool $is_array
  139. * @param string $select
  140. * @return array|\yii\db\ActiveRecord[]
  141. */
  142. public static function getLinkCategory(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
  143. $default_cond = [['<>','status',StatusEnum::DELETE]];
  144. $cond = array_merge($default_cond,$cond);
  145. $query = self::find()->select($select);
  146. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  147. $data = $query->asArray($is_array)->all();
  148. return $data;
  149. }
  150. }