UserLabelServices.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 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()
  53. {
  54. return $this->dao->getList();
  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. * @return mixed
  74. */
  75. public function add(int $id)
  76. {
  77. $label = $this->getLable($id);
  78. $field = array();
  79. /** @var UserLabelCateServices $service */
  80. $service = app()->make(UserLabelCateServices::class);
  81. $options = [];
  82. foreach ($service->getLabelCateAll() as $item) {
  83. $options[] = ['value' => $item['id'], 'label' => $item['name']];;
  84. }
  85. if (!$label) {
  86. $title = '添加标签';
  87. $field[] = Form::select('label_cate', '标签分类')->setOptions($options);
  88. $field[] = Form::input('label_name', '标签名称', '')->required();
  89. } else {
  90. $title = '修改标签';
  91. $field[] = Form::select('label_cate', '分类', (int)$label->getData('label_cate'))->setOptions($options);
  92. $field[] = Form::hidden('id', $label->getData('id'));
  93. $field[] = Form::input('label_name', '标签名称', $label->getData('label_name'))->required('请填写标签名称');
  94. }
  95. return create_form($title, $field, Url::buildUrl('/user/user_label/save'), 'POST');
  96. }
  97. /**
  98. * 保存标签表单数据
  99. * @param int $id
  100. * @param array $data
  101. * @return mixed
  102. */
  103. public function save(int $id, array $data)
  104. {
  105. if (!$data['label_cate']) {
  106. throw new ValidateException('请选择标签分类');
  107. }
  108. $levelName = $this->dao->getOne(['label_name' => $data['label_name']]);
  109. if ($id) {
  110. if (!$this->getLable($id)) {
  111. throw new AdminException('数据不存在');
  112. }
  113. if ($levelName && $id != $levelName['id']) {
  114. throw new AdminException('该标签已经存在');
  115. }
  116. if ($this->dao->update($id, $data)) {
  117. return true;
  118. } else {
  119. throw new AdminException('修改失败或者您没有修改什么!');
  120. }
  121. } else {
  122. unset($data['id']);
  123. if ($levelName) {
  124. throw new AdminException('该标签已经存在');
  125. }
  126. if ($this->dao->save($data)) {
  127. return true;
  128. } else {
  129. throw new AdminException('添加失败!');
  130. }
  131. }
  132. }
  133. /**
  134. * 删除
  135. * @param $id
  136. * @throws \Exception
  137. */
  138. public function delLabel(int $id)
  139. {
  140. if ($this->getLable($id)) {
  141. if (!$this->dao->delete($id)) {
  142. throw new AdminException('删除失败,请稍候再试!');
  143. }
  144. }
  145. return true;
  146. }
  147. }