SystemStoreCategory.php 4.5 KB

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