StoreCategoryServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\store;
  12. use app\dao\other\CategoryDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. use crmeb\traits\ServicesTrait;
  17. /**
  18. * 门店分类
  19. * Class StoreCategoryServices
  20. * @package app\services\store
  21. * @mixin CategoryDao
  22. */
  23. class StoreCategoryServices extends BaseServices
  24. {
  25. /**
  26. * 在分类库中
  27. */
  28. const GROUP = 5;
  29. use ServicesTrait;
  30. /**
  31. * UserLabelCateServices constructor.
  32. * @param CategoryDao $dao
  33. */
  34. public function __construct(CategoryDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取标签分类
  40. * @param array $where
  41. * @return array
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getList(array $where)
  47. {
  48. $where['type'] = 0;
  49. $where['group'] = self::GROUP;
  50. $list = $this->dao->getCateList($where, 0, 0, ['*'], ['children']);
  51. if ($list) {
  52. foreach ($list as $key => &$item) {
  53. if (isset($item['children']) && $item['children']) {
  54. $item['children'] = [];
  55. $item['loading'] = false;
  56. $item['_loading'] = false;
  57. } else {
  58. unset($item['children']);
  59. }
  60. }
  61. }
  62. $count = $this->dao->count($where);
  63. return compact('list', 'count');
  64. }
  65. /**
  66. * 设置分类状态
  67. * @param $id
  68. * @param $is_show
  69. */
  70. public function setShow(int $id, int $is_show)
  71. {
  72. $res = $this->dao->update($id, ['is_show' => $is_show]);
  73. $res = $res && $this->dao->update($id, ['is_show' => $is_show], 'pid');
  74. if (!$res) {
  75. throw new AdminException('设置失败');
  76. }
  77. return true;
  78. }
  79. /**
  80. * 商品分类搜索下拉
  81. * @param string $show
  82. * @param string $type
  83. * @return array
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function getTierList($show = '', $type = 0)
  89. {
  90. $where = ['type' => 0, 'group' => self::GROUP];
  91. if ($show !== '') $where['is_show'] = $show;
  92. if (!$type) $where['pid'] = 0;
  93. return sort_list_tier($this->dao->getCateList($where));
  94. }
  95. /**
  96. * 获取分类cascader
  97. * @param int $type
  98. * @param int $relation_id
  99. * @param bool $isPid
  100. * @return mixed
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\DbException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. */
  105. public function cascaderList(bool $isPid = false)
  106. {
  107. $where = ['is_show' => 1, 'type' => 0, 'group' => self::GROUP];
  108. if ($isPid) $where['pid'] = 0;
  109. $data = get_tree_children($this->dao->getCateList($where, 0, 0, ['id as value', 'name as label', 'pid']), 'children', 'value');
  110. return $data;
  111. }
  112. /**
  113. * 获取一级分类组合数据
  114. * @return array[]
  115. */
  116. public function menus()
  117. {
  118. $list = $this->dao->getMenus(['pid' => 0, 'group' => self::GROUP, 'type' => 0]);
  119. $menus = [['value' => 0, 'label' => '顶级菜单']];
  120. foreach ($list as $menu) {
  121. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  122. }
  123. return $menus;
  124. }
  125. /**
  126. * 创建表单
  127. * @return array
  128. * @throws \FormBuilder\Exception\FormBuilderException
  129. */
  130. public function createForm($id = 0)
  131. {
  132. $info = [];
  133. if ($id) {
  134. $info = $this->dao->get($id);
  135. }
  136. if (isset($info['pid'])) {
  137. if ($info['pid']) {
  138. $f[] = Form::select('pid', '父级', (int)($info['pid'] ?? ''))->setOptions($this->menus())->filterable(1);
  139. } else {
  140. $f[] = Form::select('pid', '父级', (int)($info['pid'] ?? ''))->setOptions($this->menus())->filterable(1)->disabled(true);
  141. }
  142. } else {
  143. $f[] = Form::select('pid', '父级', (int)($info['pid'] ?? ''))->setOptions($this->menus())->filterable(1);
  144. }
  145. $f[] = Form::input('name', '分类名称', $info['name'] ?? '')->maxlength(30)->required();
  146. $f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->min(0)->min(0);
  147. $f[] = Form::radio('is_show', '状态', $info['is_show'] ?? 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]]);
  148. return create_form($id ? '编辑' : '添加分类', $f, $this->url('/store/category/' . $id), 'POST');
  149. }
  150. }