UserLabelServices.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getLable($id)
  43. {
  44. return $this->dao->get($id);
  45. }
  46. /**
  47. * 获取所有用户标签
  48. * @param array $where
  49. * @param array|string[] $field
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getLabelList(array $where = [], array $field = ['*'])
  56. {
  57. return $this->dao->getList(0, 0, $where, $field);
  58. }
  59. /**
  60. * 获取列表
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function getList(array $where)
  67. {
  68. [$page, $limit] = $this->getPageValue();
  69. $list = $this->dao->getList($page, $limit, $where);
  70. $count = $this->dao->count($where);
  71. return compact('list', 'count');
  72. }
  73. /**
  74. * 添加修改标签表单
  75. * @param int $id
  76. * @param int $cateId
  77. * @return array
  78. * @throws \FormBuilder\Exception\FormBuilderException
  79. */
  80. public function add(int $id, int $cateId)
  81. {
  82. $label = $this->getLable($id);
  83. $field = array();
  84. /** @var UserLabelCateServices $service */
  85. $service = app()->make(UserLabelCateServices::class);
  86. $options[] = ['value' => 0, 'label' => '全部'];
  87. foreach ($service->getLabelCateAll() as $item) {
  88. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  89. }
  90. if (!$label) {
  91. $title = '添加标签';
  92. $field[] = Form::select('label_cate', '标签分类', $cateId)->setOptions($options);
  93. $field[] = Form::input('label_name', '标签名称', '')->required();
  94. } else {
  95. $title = '修改标签';
  96. $field[] = Form::select('label_cate', '分类', (int)$label->getData('label_cate'))->setOptions($options);
  97. $field[] = Form::hidden('id', $label->getData('id'));
  98. $field[] = Form::input('label_name', '标签名称', $label->getData('label_name'))->required('请填写标签名称');
  99. }
  100. return create_form($title, $field, Url::buildUrl('/user/user_label/save'), 'POST');
  101. }
  102. /**
  103. * 保存标签表单数据
  104. * @param int $id
  105. * @param array $data
  106. * @return mixed
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function save(int $id, array $data)
  112. {
  113. if (!$data['label_cate']) {
  114. throw new AdminException(400669);
  115. }
  116. $levelName = $this->dao->getOne(['label_name' => $data['label_name'], 'label_cate' => $data['label_cate']]);
  117. if ($id) {
  118. if (!$this->getLable($id)) {
  119. throw new AdminException(100026);
  120. }
  121. if ($levelName && $id != $levelName['id']) {
  122. throw new AdminException(400670);
  123. }
  124. if ($this->dao->update($id, $data)) {
  125. return true;
  126. } else {
  127. throw new AdminException(100007);
  128. }
  129. } else {
  130. unset($data['id']);
  131. if ($levelName) {
  132. throw new AdminException(400670);
  133. }
  134. if ($this->dao->save($data)) {
  135. return true;
  136. } else {
  137. throw new AdminException(100022);
  138. }
  139. }
  140. }
  141. /**
  142. * 删除
  143. * @param int $id
  144. * @return bool
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. */
  149. public function delLabel(int $id)
  150. {
  151. if ($this->getLable($id)) {
  152. if (!$this->dao->delete($id)) {
  153. throw new AdminException(100008);
  154. }
  155. }
  156. return true;
  157. }
  158. /**
  159. * tree处理 分类、标签数据
  160. * @param array $cate
  161. * @param array $label
  162. * @return array
  163. */
  164. public function get_tree_children(array $cate, array $label)
  165. {
  166. if ($cate) {
  167. foreach ($cate as $key => $value) {
  168. if ($label) {
  169. foreach ($label as $k => $item) {
  170. if ($value['id'] == $item['label_cate']) {
  171. $cate[$key]['children'][] = $item;
  172. unset($label[$k]);
  173. }
  174. }
  175. } else {
  176. $cate[$key]['children'] = [];
  177. }
  178. }
  179. }
  180. return $cate;
  181. }
  182. }