StoreBrand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\admin\store;
  12. use think\App;
  13. use crmeb\basic\BaseController;
  14. use app\validate\admin\StoreBrandValidate as validate;
  15. use app\common\repositories\store\StoreBrandRepository;
  16. /**
  17. * 商品品牌
  18. */
  19. class StoreBrand extends BaseController
  20. {
  21. protected $repository;
  22. /**
  23. * ArticleCategory constructor.
  24. * @param App $app
  25. * @param StoreBrandRepository $repository
  26. */
  27. public function __construct(App $app, StoreBrandRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 列表
  34. * @return mixed
  35. * @author Qinii
  36. */
  37. public function lst()
  38. {
  39. [$page, $limit] = $this->getPage();
  40. $where = $this->request->params(['brand_category_id', 'brand_name']);
  41. return app('json')->success($this->repository->getList($where, $page, $limit));
  42. }
  43. /**
  44. * 创建
  45. * @param validate $validate
  46. * @return \think\response\Json
  47. * @author Qinii
  48. */
  49. public function create(validate $validate)
  50. {
  51. $data = $this->checkParams($validate);
  52. if (!$this->repository->parentExists($data['brand_category_id']))
  53. return app('json')->fail('上级分类不存在');
  54. if ($this->repository->merExistsBrand($data['brand_name']))
  55. return app('json')->fail('该品牌已存在');
  56. $this->repository->create($data);
  57. return app('json')->success('添加成功');
  58. }
  59. /**
  60. * 修改
  61. * @param $id
  62. * @param validate $validate
  63. * @return \think\response\Json
  64. * @author Qinii
  65. */
  66. public function update($id, validate $validate)
  67. {
  68. $data = $this->checkParams($validate);
  69. if (!$this->repository->meExists($id))
  70. return app('json')->fail('数据不存在');
  71. if (!$this->repository->parentExists($data['brand_category_id']))
  72. return app('json')->fail('上级分类不存在');
  73. $this->repository->update($id, $data);
  74. return app('json')->success('编辑成功');
  75. }
  76. /**
  77. * 删除
  78. * @param $id
  79. * @return \think\response\Json
  80. * @author Qinii
  81. */
  82. public function delete($id)
  83. {
  84. if (!$this->repository->meExists($id))
  85. return app('json')->fail('数据不存在');
  86. if ($this->repository->getBrandHasProduct($id))
  87. return app('json')->fail('该品牌下存在商品');
  88. $this->repository->delete($id);
  89. return app('json')->success('删除成功');
  90. }
  91. /**
  92. * 详情
  93. * @param $id
  94. * @return \think\response\Json
  95. * @author Qinii
  96. */
  97. public function detail($id)
  98. {
  99. if (!$this->repository->meExists($id))
  100. return app('json')->fail('数据不存在');
  101. return app('json')->success($this->repository->get($id));
  102. }
  103. /**
  104. * 验证
  105. * @param validate $validate
  106. * @param bool $isCreate
  107. * @return array
  108. * @author Qinii
  109. */
  110. public function checkParams(validate $validate)
  111. {
  112. $data = $this->request->params(['brand_category_id', 'brand_name', 'is_show', 'sort', 'pic']);
  113. $validate->check($data);
  114. return $data;
  115. }
  116. /**
  117. * 创建
  118. * @Author:Qinii
  119. * @Date: 2020/5/27
  120. * @return mixed
  121. */
  122. public function createForm()
  123. {
  124. return app('json')->success(formToData($this->repository->form()));
  125. }
  126. /**
  127. * 修改表单
  128. * @Author:Qinii
  129. * @Date: 2020/5/27
  130. * @param $id
  131. * @return mixed
  132. */
  133. public function updateForm($id)
  134. {
  135. if (!$this->repository->meExists($id))
  136. return app('json')->fail('数据不存在');
  137. return app('json')->success(formToData($this->repository->updateForm($id)));
  138. }
  139. /**
  140. * 状态修改
  141. * @Author:Qinii
  142. * @Date: 2020/5/27
  143. * @param int $id
  144. * @return mixed
  145. */
  146. public function switchStatus($id)
  147. {
  148. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  149. if (!$this->repository->meExists($id))
  150. return app('json')->fail('数据不存在');
  151. $this->repository->update($id, ['is_show' => $status]);
  152. return app('json')->success('修改成功');
  153. }
  154. }