LabelRule.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\user;
  12. use app\common\repositories\user\LabelRuleRepository;
  13. use app\common\repositories\user\UserLabelRepository;
  14. use app\validate\merchant\LabelRuleValidate;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\exception\ValidateException;
  18. class LabelRule extends BaseController
  19. {
  20. protected $repository;
  21. /**
  22. * 构造函数
  23. *
  24. * @param App $app 应用实例
  25. * @param LabelRuleRepository $repository 标签规则仓库实例
  26. */
  27. public function __construct(App $app, LabelRuleRepository $repository)
  28. {
  29. // 调用父类构造函数
  30. parent::__construct($app);
  31. // 初始化标签规则仓库实例
  32. $this->repository = $repository;
  33. }
  34. /**
  35. * 获取标签列表
  36. *
  37. * @return mixed
  38. */
  39. public function getList()
  40. {
  41. // 从请求参数中获取查询条件
  42. $where = $this->request->params(['keyword', 'type']);
  43. // 设置商家ID
  44. $where['mer_id'] = $this->request->merId();
  45. // 获取分页参数
  46. [$page, $limit] = $this->getPage();
  47. // 调用标签规则仓库的获取标签列表方法并返回结果
  48. return app('json')->success($this->repository->getList($where, $page, $limit));
  49. }
  50. /**
  51. * 创建标签
  52. *
  53. * @return mixed
  54. */
  55. public function create()
  56. {
  57. // 检查参数
  58. $data = $this->checkParams();
  59. $data['mer_id'] = $this->request->merId();
  60. // 判断标签名是否已存在
  61. if (app()->make(UserLabelRepository::class)->existsName($data['label_name'], $data['mer_id'], 1))
  62. return app('json')->fail('标签名已存在');
  63. // 调用标签规则仓库的创建标签方法
  64. $this->repository->create($data);
  65. // 返回操作成功的结果
  66. return app('json')->success('添加成功');
  67. }
  68. /**
  69. * 更新标签规则
  70. *
  71. * @param int $id 标签规则ID
  72. * @return \think\response\Json
  73. */
  74. public function update($id)
  75. {
  76. // 获取参数并校验
  77. $data = $this->checkParams();
  78. // 获取商家ID
  79. $mer_id = $this->request->merId();
  80. // 根据条件查询标签规则
  81. if (!$label = $this->repository->getWhere(['label_rule_id' => $id, 'mer_id' => $mer_id]))
  82. return app('json')->fail('数据不存在');
  83. // 判断标签名是否已存在
  84. if (app()->make(UserLabelRepository::class)->existsName($data['label_name'], $mer_id, 1, $label->label_id))
  85. return app('json')->fail('标签名已存在');
  86. // 更新标签规则
  87. $this->repository->update(intval($id), $data);
  88. return app('json')->success('编辑成功');
  89. }
  90. /**
  91. * 删除标签规则
  92. *
  93. * @param int $id 标签规则ID
  94. * @return \think\response\Json
  95. */
  96. public function delete($id)
  97. {
  98. // 判断标签规则是否存在
  99. if (!$this->repository->existsWhere(['label_rule_id' => $id, 'mer_id' => $this->request->merId()]))
  100. return app('json')->fail('数据不存在');
  101. // 删除标签规则
  102. $this->repository->delete(intval($id));
  103. return app('json')->success('删除成功');
  104. }
  105. /**
  106. * 同步标签规则用户数量
  107. * @param int $id 标签规则ID
  108. * @return \think\response\Json
  109. */
  110. public function sync($id)
  111. {
  112. // 判断标签规则是否存在
  113. if (!$this->repository->existsWhere(['label_rule_id' => $id, 'mer_id' => $this->request->merId()]))
  114. return app('json')->fail('数据不存在');
  115. // 同步标签规则用户数量
  116. $this->repository->syncUserNum(intval($id));
  117. // 返回操作结果
  118. return app('json')->success('更新成功');
  119. }
  120. /**
  121. * 校验参数
  122. * @return array
  123. * @throws \app\common\exception\ValidateException
  124. */
  125. public function checkParams()
  126. {
  127. // 获取请求参数
  128. $data = $this->request->params(['label_name', 'min', 'max', 'type', 'data']);
  129. // 校验参数
  130. app()->make(LabelRuleValidate::class)->check($data);
  131. // 如果类型为空,则校验最小值和最大值是否为整数
  132. if (!$data['type']) {
  133. if (false === filter_var($data['min'], FILTER_VALIDATE_INT) || false === filter_var($data['max'], FILTER_VALIDATE_INT)) {
  134. throw new ValidateException('数值必须为整数');
  135. }
  136. }
  137. // 返回校验后的参数
  138. return $data;
  139. }
  140. }