UserLabelServices.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 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\exception\ValidateException;
  18. use think\facade\Route as Url;
  19. /**
  20. *
  21. * Class UserLabelServices
  22. * @package app\services\user
  23. * * @method getColumn(array $where, string $field, string $key = '') 获取某个字段数组
  24. */
  25. class UserLabelServices extends BaseServices
  26. {
  27. /**
  28. * UserLabelServices constructor.
  29. * @param UserLabelDao $dao
  30. */
  31. public function __construct(UserLabelDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取某一本标签
  37. * @param $id
  38. * @return array|\think\Model|null
  39. */
  40. public function getLable($id)
  41. {
  42. return $this->dao->get($id);
  43. }
  44. /**
  45. * 获取所有用户标签
  46. * @param array $where
  47. * @param array|string[] $field
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getLabelList(array $where = [], array $field = ['*'])
  54. {
  55. return $this->dao->getList(0, 0, $where, $field);
  56. }
  57. /**
  58. * 获取列表
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function getList(array $where)
  65. {
  66. [$page, $limit] = $this->getPageValue();
  67. $list = $this->dao->getList($page, $limit, $where);
  68. $count = $this->dao->count($where);
  69. return compact('list', 'count');
  70. }
  71. /**
  72. * 添加修改标签表单
  73. * @param int $id
  74. * @param int $cateId
  75. * @return array
  76. * @throws \FormBuilder\Exception\FormBuilderException
  77. */
  78. public function add(int $id, int $cateId)
  79. {
  80. $label = $this->getLable($id);
  81. $field = array();
  82. /** @var UserLabelCateServices $service */
  83. $service = app()->make(UserLabelCateServices::class);
  84. $options = [];
  85. foreach ($service->getLabelCateAll() as $item) {
  86. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  87. }
  88. if (!$label) {
  89. $title = '添加标签';
  90. $field[] = Form::select('label_cate', '标签分类', $cateId)->setOptions($options);
  91. $field[] = Form::input('label_name', '标签名称', '')->required();
  92. } else {
  93. $title = '修改标签';
  94. $field[] = Form::select('label_cate', '分类', (int)$label->getData('label_cate'))->setOptions($options);
  95. $field[] = Form::hidden('id', $label->getData('id'));
  96. $field[] = Form::input('label_name', '标签名称', $label->getData('label_name'))->required('请填写标签名称');
  97. }
  98. return create_form($title, $field, Url::buildUrl('/user/user_label/save'), 'POST');
  99. }
  100. /**
  101. * 保存标签表单数据
  102. * @param int $id
  103. * @param array $data
  104. * @return mixed
  105. */
  106. public function save(int $id, array $data)
  107. {
  108. if (!$data['label_cate']) {
  109. throw new ValidateException('请选择标签分类');
  110. }
  111. $levelName = $this->dao->getOne(['label_name' => $data['label_name']]);
  112. if ($id) {
  113. if (!$this->getLable($id)) {
  114. throw new AdminException('数据不存在');
  115. }
  116. if ($levelName && $id != $levelName['id']) {
  117. throw new AdminException('该标签已经存在');
  118. }
  119. if ($this->dao->update($id, $data)) {
  120. return true;
  121. } else {
  122. throw new AdminException('修改失败或者您没有修改什么!');
  123. }
  124. } else {
  125. unset($data['id']);
  126. if ($levelName) {
  127. throw new AdminException('该标签已经存在');
  128. }
  129. if ($this->dao->save($data)) {
  130. return true;
  131. } else {
  132. throw new AdminException('添加失败!');
  133. }
  134. }
  135. }
  136. /**
  137. * 删除
  138. * @param $id
  139. * @throws \Exception
  140. */
  141. public function delLabel(int $id)
  142. {
  143. if ($this->getLable($id)) {
  144. if (!$this->dao->delete($id)) {
  145. throw new AdminException('删除失败,请稍候再试!');
  146. }
  147. }
  148. return true;
  149. }
  150. /**
  151. * tree处理 分类、标签数据
  152. * @param array $cate
  153. * @param array $label
  154. * @return array
  155. */
  156. public function get_tree_children(array $cate, array $label)
  157. {
  158. if ($cate) {
  159. foreach ($cate as $key => $value) {
  160. if ($label) {
  161. foreach ($label as $k => $item) {
  162. if ($value['id'] == $item['label_cate']) {
  163. $cate[$key]['children'][] = $item;
  164. unset($label[$k]);
  165. }
  166. }
  167. } else {
  168. $cate[$key]['children'] = [];
  169. }
  170. }
  171. }
  172. return $cate;
  173. }
  174. }