LabelRule.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\controller\merchant\user;
  3. use app\common\repositories\user\LabelRuleRepository;
  4. use app\common\repositories\user\UserLabelRepository;
  5. use app\validate\merchant\LabelRuleValidate;
  6. use ln\basic\BaseController;
  7. use think\App;
  8. use think\exception\ValidateException;
  9. class LabelRule extends BaseController
  10. {
  11. protected $repository;
  12. public function __construct(App $app, LabelRuleRepository $repository)
  13. {
  14. parent::__construct($app);
  15. $this->repository = $repository;
  16. }
  17. public function getList()
  18. {
  19. $where = $this->request->params(['keyword', 'type']);
  20. $where['mer_id'] = $this->request->merId();
  21. [$page, $limit] = $this->getPage();
  22. return app('json')->success($this->repository->getList($where, $page, $limit));
  23. }
  24. public function create()
  25. {
  26. $data = $this->checkParams();
  27. $data['mer_id'] = $this->request->merId();
  28. if (app()->make(UserLabelRepository::class)->existsName($data['label_name'], $data['mer_id'], 1))
  29. return app('json')->fail('标签名已存在');
  30. $this->repository->create($data);
  31. return app('json')->success('添加成功');
  32. }
  33. public function update($id)
  34. {
  35. $data = $this->checkParams();
  36. $mer_id = $this->request->merId();
  37. if (!$label = $this->repository->getWhere(['label_rule_id' => $id, 'mer_id' => $mer_id]))
  38. return app('json')->fail('数据不存在');
  39. if (app()->make(UserLabelRepository::class)->existsName($data['label_name'], $mer_id, 1, $label->label_id))
  40. return app('json')->fail('标签名已存在');
  41. $this->repository->update(intval($id), $data);
  42. return app('json')->success('编辑成功');
  43. }
  44. public function delete($id)
  45. {
  46. if (!$this->repository->existsWhere(['label_rule_id' => $id, 'mer_id' => $this->request->merId()]))
  47. return app('json')->fail('数据不存在');
  48. $this->repository->delete(intval($id));
  49. return app('json')->success('删除成功');
  50. }
  51. public function sync($id)
  52. {
  53. if (!$this->repository->existsWhere(['label_rule_id' => $id, 'mer_id' => $this->request->merId()]))
  54. return app('json')->fail('数据不存在');
  55. $this->repository->syncUserNum(intval($id));
  56. return app('json')->success('更新成功');
  57. }
  58. /**
  59. * @return array
  60. * @author zfy
  61. * @day 2020/10/21
  62. */
  63. public function checkParams()
  64. {
  65. $data = $this->request->params(['label_name', 'min', 'max', 'type', 'data']);
  66. app()->make(LabelRuleValidate::class)->check($data);
  67. if (!$data['type']) {
  68. if (false === filter_var($data['min'], FILTER_VALIDATE_INT) || false === filter_var($data['max'], FILTER_VALIDATE_INT)) {
  69. throw new ValidateException('数值必须为整数');
  70. }
  71. }
  72. return $data;
  73. }
  74. }