UserLabelServices.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserLabelDao;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. *
  20. * Class UserLabelServices
  21. * @package app\services\user
  22. * * @method getColumn(array $where, string $field, string $key = '') 获取某个字段数组
  23. */
  24. class UserLabelServices extends BaseServices
  25. {
  26. /**
  27. * UserLabelServices constructor.
  28. * @param UserLabelDao $dao
  29. */
  30. public function __construct(UserLabelDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取某一本标签
  36. * @param $id
  37. * @return array|\think\Model|null
  38. */
  39. public function getLable($id)
  40. {
  41. return $this->dao->get($id);
  42. }
  43. /**
  44. * 获取所有用户标签
  45. * @param array $where
  46. * @param array|string[] $field
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getLabelList(array $where = [], array $field = ['*'])
  53. {
  54. return $this->dao->getList(0, 0, $where, $field);
  55. }
  56. /**
  57. * 获取列表
  58. * @return array
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function getList(array $where)
  64. {
  65. [$page, $limit] = $this->getPageValue();
  66. $list = $this->dao->getList($page, $limit, $where);
  67. $count = $this->dao->count($where);
  68. return compact('list', 'count');
  69. }
  70. /**
  71. * 添加修改标签表单
  72. * @param int $id
  73. * @param int $cateId
  74. * @return array
  75. * @throws \FormBuilder\Exception\FormBuilderException
  76. */
  77. public function add(int $id, int $cateId)
  78. {
  79. $label = $this->getLable($id);
  80. $field = array();
  81. /** @var UserLabelCateServices $service */
  82. $service = app()->make(UserLabelCateServices::class);
  83. $options = [];
  84. foreach ($service->getLabelCateAll() as $item) {
  85. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  86. }
  87. if (!$label) {
  88. $title = '添加标签';
  89. $field[] = Form::select('label_cate', '标签分类', $cateId)->setOptions($options);
  90. $field[] = Form::input('label_name', '标签名称', '')->required();
  91. } else {
  92. $title = '修改标签';
  93. $field[] = Form::select('label_cate', '分类', (int)$label->getData('label_cate'))->setOptions($options);
  94. $field[] = Form::hidden('id', $label->getData('id'));
  95. $field[] = Form::input('label_name', '标签名称', $label->getData('label_name'))->required('请填写标签名称');
  96. }
  97. return create_form($title, $field, Url::buildUrl('/user/user_label/save'), 'POST');
  98. }
  99. /**
  100. * 保存标签表单数据
  101. * @param int $id
  102. * @param array $data
  103. * @return mixed
  104. */
  105. public function save(int $id, array $data)
  106. {
  107. if (!$data['label_cate']) {
  108. throw new AdminException(400669);
  109. }
  110. $levelName = $this->dao->getOne(['label_name' => $data['label_name'], 'label_cate' => $data['label_cate']]);
  111. if ($id) {
  112. if (!$this->getLable($id)) {
  113. throw new AdminException(100026);
  114. }
  115. if ($levelName && $id != $levelName['id']) {
  116. throw new AdminException(400670);
  117. }
  118. if ($this->dao->update($id, $data)) {
  119. return true;
  120. } else {
  121. throw new AdminException(100007);
  122. }
  123. } else {
  124. unset($data['id']);
  125. if ($levelName) {
  126. throw new AdminException(400670);
  127. }
  128. if ($this->dao->save($data)) {
  129. return true;
  130. } else {
  131. throw new AdminException(100022);
  132. }
  133. }
  134. }
  135. /**
  136. * 删除
  137. * @param $id
  138. * @throws \Exception
  139. */
  140. public function delLabel(int $id)
  141. {
  142. if ($this->getLable($id)) {
  143. if (!$this->dao->delete($id)) {
  144. throw new AdminException(100008);
  145. }
  146. }
  147. return true;
  148. }
  149. /**
  150. * tree处理 分类、标签数据
  151. * @param array $cate
  152. * @param array $label
  153. * @return array
  154. */
  155. public function get_tree_children(array $cate, array $label)
  156. {
  157. if ($cate) {
  158. foreach ($cate as $key => $value) {
  159. if ($label) {
  160. foreach ($label as $k => $item) {
  161. if ($value['id'] == $item['label_cate']) {
  162. $cate[$key]['children'][] = $item;
  163. unset($label[$k]);
  164. }
  165. }
  166. } else {
  167. $cate[$key]['children'] = [];
  168. }
  169. }
  170. }
  171. return $cate;
  172. }
  173. }