Category.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace app\admin\model;
  3. use support\Db;
  4. use support\Model;
  5. /**
  6. * 分类模型
  7. * Class Users
  8. * @package app\admin\model
  9. */
  10. class Category extends Model
  11. {
  12. const STATUS_DEL_NO = '0';
  13. const STATUS_DEL_YES = '1';
  14. const STATUS_DEL = [
  15. self::STATUS_DEL_NO => '正常',
  16. self::STATUS_DEL_YES => '删除'
  17. ];
  18. const CATEGORY_RECOMMEND_NO = '0';
  19. const CATEGORY_RECOMMEND_YES = '1';
  20. const CATEGORY_SHOW = [
  21. self::CATEGORY_RECOMMEND_NO => '不推荐',
  22. self::CATEGORY_RECOMMEND_YES => '推荐'
  23. ];
  24. /**
  25. * The table associated with the model.
  26. *
  27. * @var string
  28. */
  29. protected $table = 'category';
  30. public $timestamps = false;
  31. /**
  32. * Notes:模型关联
  33. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  34. * User: yym
  35. * Date: 2022/11/4
  36. */
  37. public function children()
  38. {
  39. return $this->hasMany(Category::class, 'category_pid', 'category_id')->where(['category_del' => Category::STATUS_DEL_NO])->select(['category_id', 'category_pid', 'category_name', 'category_icon', 'category_type', 'category_open_type', 'category_open_url', 'category_recommend', 'category_sort', Db::raw('FROM_UNIXTIME(category_create_time) as category_create_time')])->orderBy('category_sort', 'desc');
  40. }
  41. /**
  42. * Notes:获取菜单列表
  43. * @param string $keywords
  44. * @param int $page
  45. * @param int $limit
  46. * @return array
  47. * User: ZQ
  48. * Date: 2022/9/14
  49. */
  50. public static function getCategoryList(string $keywords)
  51. {
  52. return static::select(['*', Db::raw('FROM_UNIXTIME(category_create_time) as category_create_time')])->where(['category_del' => static::STATUS_DEL_NO])
  53. ->when($keywords != '', function ($query) use ($keywords){
  54. $query->where('category_name', 'like', '%' . $keywords . '%');
  55. })
  56. ->when($keywords == '', function ($query) use ($keywords){
  57. $query->where('category_pid', 0);
  58. })
  59. ->with(['children' => function($query){
  60. $query->with(['children' => function($query1){
  61. $query1->with(['children' => function($query2){
  62. $query2->with(['children' => function($query3){
  63. $query3->with(['children' => function($query4){
  64. $query4->with(['children']);
  65. }]);
  66. }]);
  67. }]);
  68. }]);
  69. }])
  70. ->orderBy('category_sort','DESC')
  71. ->orderBy('category_create_time','DESC')
  72. ->get()
  73. ->toArray();
  74. }
  75. /**
  76. * Notes:获取菜单
  77. * @param int $category_pid
  78. * @return array
  79. * User: ZQ
  80. * Date: 2022/9/13
  81. */
  82. public static function getCategorys(int $category_pid)
  83. {
  84. $list = static::select('*')
  85. ->where(['category_pid' => $category_pid, 'category_del' => static::STATUS_DEL_NO])
  86. ->orderBy('category_sort','DESC')
  87. ->get();
  88. return $list;
  89. }
  90. /**
  91. * Notes:三级菜单
  92. * @param int $category_pid
  93. * @return array
  94. * User: ZQ
  95. * Date: 2022/9/15
  96. */
  97. public static function getLevel($category_pid)
  98. {
  99. $list = static::select(['category_id as value','category_name as label'])
  100. ->where(['category_pid' => $category_pid, 'category_del' => static::STATUS_DEL_NO])
  101. ->orderBy('category_sort','DESC')
  102. ->get();
  103. return $list;
  104. }
  105. /**
  106. * Notes:获取菜单
  107. * @param int $categorys
  108. * @return array
  109. * User: ZQ
  110. * Date: 2022/9/13
  111. */
  112. public static function categorysMation($categorys)
  113. {
  114. $list = static::where(['category_del'=>static::STATUS_DEL_NO])
  115. ->when($categorys != '', function ($query) use ($categorys){
  116. $query->whereIn('category_id', $categorys);
  117. })
  118. ->get();
  119. return $list;
  120. }
  121. /**
  122. * Notes:获取角色对应权限菜单
  123. * @param array $category_ids
  124. * @return array
  125. * User: yym
  126. * Date: 2022/9/21
  127. */
  128. public static function getCategoryListRule(array $category_ids)
  129. {
  130. return static::where(['category_del' => static::STATUS_DEL_NO])
  131. ->whereIn('category_id', $category_ids)
  132. ->get()
  133. ->toArray();
  134. }
  135. /**
  136. * Notes:菜单列表
  137. * @param array $category_list_ids
  138. * @return array
  139. * User: yym
  140. * Date: 2022/9/21
  141. */
  142. public static function getHomeCategoryList(array $category_list_ids)
  143. {
  144. $where = array(
  145. 'category_del' => static::STATUS_DEL_NO,
  146. 'level' => STATIC::MENU_LEVEL_ONE
  147. );
  148. return static::where($where)
  149. ->select(['category_id', 'category_pid', 'category_path', 'category_icon', 'category_name', 'category_route'])
  150. ->when(!empty($category_list_ids), function ($query) use ($category_list_ids){
  151. $query->whereIn('category_id', $category_list_ids);
  152. })
  153. ->with(['children' => function($query) use ($category_list_ids){
  154. $query->with('children')->when(!empty($category_list_ids), function ($query1) use ($category_list_ids){
  155. $query1->whereIn('category_id', $category_list_ids);
  156. });
  157. }])
  158. ->get()
  159. ->toArray();
  160. }
  161. /**
  162. * Notes:查询指定条件下的某个字段内容
  163. * @param int $category_id
  164. * @param string $field
  165. * User: yym
  166. * Date: 2022/9/27
  167. */
  168. public static function getParentValue(int $category_id, string $field = 'level')
  169. {
  170. return static::where(['category_id' => $category_id])->value($field);
  171. }
  172. /**
  173. * Notes:获取父级菜单编号 处理成数组形式
  174. * @param int $category_pid
  175. * @return array|false|mixed
  176. * User: yym
  177. * Date: 2022/9/26
  178. */
  179. public function getParentCategoryId(int $category_pid)
  180. {
  181. $parent = array();
  182. $parent = $this->getParentPid($category_pid);
  183. if(!empty($parent))
  184. {
  185. $parent = array_merge([$category_pid], $parent);
  186. array_pop($parent);
  187. }
  188. return array_reverse($parent);
  189. }
  190. /**
  191. * Notes:递归查询父级ID
  192. * @param $category_id
  193. * @param array $date
  194. * @return array|false|mixed
  195. * User: yym
  196. * Date: 2022/9/26
  197. */
  198. public function getParentPid($category_id, $date=[]){
  199. if(empty($category_id) && empty($date))
  200. {
  201. return array();
  202. }
  203. if(empty($category_id) && !empty($date)){
  204. return $date;
  205. }
  206. $parentIds = array();
  207. $parentIds = Category::select('category_pid')->where(['category_id' => $category_id, 'category_del' => Category::STATUS_DEL_NO])->get()->toArray();
  208. $subIds = array();
  209. $subIds = array_column($parentIds, 'category_pid');
  210. $subIds = array_unique($subIds);
  211. if(count($subIds)>0)
  212. {
  213. foreach($subIds as $val)
  214. {
  215. $date[] = $val;
  216. $date = $this->getParentPid($val, $date); //注意写$date 返回给上级
  217. }
  218. }
  219. if(count($date)>0)
  220. {
  221. return $date;
  222. }else{
  223. return false;
  224. }
  225. }
  226. }