StoreCategory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\admin\model\store;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * Class StoreCategory
  7. * @package app\admin\model\store
  8. */
  9. class StoreCategory extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'store_category';
  21. use ModelTrait;
  22. /**
  23. * 异步获取分类列表
  24. * @param $where
  25. * @return array
  26. */
  27. public static function CategoryList($where)
  28. {
  29. $data = ($data = self::systemPage($where, true)->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  30. foreach ($data as &$item) {
  31. if ($item['pid']) {
  32. $item['pid_name'] = self::where('id', $item['pid'])->value('cate_name');
  33. } else {
  34. $item['pid_name'] = '顶级';
  35. }
  36. }
  37. $count = self::systemPage($where, true)->count();
  38. return compact('count', 'data');
  39. }
  40. /**
  41. * @param $where
  42. * @return array
  43. */
  44. public static function systemPage($where, $isAjax = false)
  45. {
  46. $model = new self;
  47. if ($where['pid'] != '') $model = $model->where('pid', $where['pid']);
  48. else if ($where['pid'] == '' && $where['cate_name'] == '') $model = $model->where('pid', 0);
  49. if ($where['is_show'] != '') $model = $model->where('is_show', $where['is_show']);
  50. if ($where['cate_name'] != '') $model = $model->where('cate_name', 'LIKE', "%$where[cate_name]%");
  51. if ($isAjax === true) {
  52. if (isset($where['order']) && $where['order'] != '') {
  53. $model = $model->order(self::setOrder($where['order']));
  54. } else {
  55. $model = $model->order('sort desc,id desc');
  56. }
  57. return $model;
  58. }
  59. return self::page($model, function ($item) {
  60. if ($item['pid']) {
  61. $item['pid_name'] = self::where('id', $item['pid'])->value('cate_name');
  62. } else {
  63. $item['pid_name'] = '顶级';
  64. }
  65. }, $where);
  66. }
  67. /**
  68. * 获取顶级分类
  69. * @return array
  70. */
  71. public static function getCategory()
  72. {
  73. return self::where('is_show', 1)->column('cate_name', 'id');
  74. }
  75. /**
  76. * 分级排序列表
  77. *
  78. * @param null $model 为 null 时构造本对象,不为 null 时用于提前构造过滤条件
  79. * @param int $type 为 0 时只查找根分类
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. public static function getTierList($model = null, $type = 0)
  86. {
  87. if ($model === null) $model = new self();
  88. if (!$type) return sort_list_tier($model->order('sort desc,id desc')->where('pid', 0)->select()->toArray());
  89. return sort_list_tier($model->order('sort desc,id desc')->select()->toArray());
  90. }
  91. public static function delCategory($id)
  92. {
  93. $count = self::where('pid', $id)->count();
  94. if ($count)
  95. return self::setErrorInfo('请先删除下级子分类');
  96. else {
  97. return self::del($id);
  98. }
  99. }
  100. /**
  101. * 产品分类隐藏显示
  102. * @param $id
  103. * @param $show
  104. * @return bool
  105. */
  106. public static function setCategoryShow($id, $show)
  107. {
  108. $count = self::where('id', $id)->count();
  109. if (!$count) return self::setErrorInfo('参数错误');
  110. $count = self::where('id', $id)->where('is_show', $show)->count();
  111. if ($count) return true;
  112. $pid = self::where('id', $id)->value('pid');
  113. self::beginTrans();
  114. $res1 = true;
  115. $res2 = self::where('id', $id)->update(['is_show' => $show]);
  116. if (!$pid) { //一级分类隐藏
  117. $count = self::where('pid', $id)->count();
  118. if ($count) {
  119. $count = self::where('pid', $id)->where('is_show', $show)->count();
  120. $countWhole = self::where('pid', $id)->count();
  121. if (!$count || $countWhole > $count) {
  122. $res1 = self::where('pid', $id)->update(['is_show' => $show]);
  123. }
  124. }
  125. }
  126. $res = $res1 && $res2;
  127. self::checkTrans($res);
  128. return $res;
  129. }
  130. }