StoreCategory.php 4.3 KB

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