StoreBrand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. 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 as repository;
  16. class StoreBrand extends BaseController
  17. {
  18. protected $repository;
  19. /**
  20. * ArticleCategory constructor.
  21. * @param App $app
  22. * @param StoreBrandCategoryRepository $repository
  23. */
  24. public function __construct(App $app, repository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * 列表
  31. * @return mixed
  32. * @author Qinii
  33. */
  34. public function lst()
  35. {
  36. [$page , $limit] = $this->getPage();
  37. $where = $this->request->params(['brand_category_id','brand_name']);
  38. return app('json')->success($this->repository->getList($where, $page, $limit));
  39. }
  40. public function create(validate $validate)
  41. {
  42. $data = $this->checkParams($validate);
  43. if (!$this->repository->parentExists($data['brand_category_id']))
  44. return app('json')->fail('上级分类不存在');
  45. if ($this->repository->merExistsBrand($data['brand_name']))
  46. return app('json')->fail('该品牌已存在');
  47. $this->repository->create($data);
  48. return app('json')->success('添加成功');
  49. }
  50. public function update($id,validate $validate)
  51. {
  52. $data = $this->checkParams($validate);
  53. if(!$this->repository->meExists($id))
  54. return app('json')->fail('数据不存在');
  55. if (!$this->repository->parentExists($data['brand_category_id']))
  56. return app('json')->fail('上级分类不存在');
  57. $this->repository->update($id,$data);
  58. return app('json')->success('编辑成功');
  59. }
  60. public function delete($id)
  61. {
  62. if(!$this->repository->meExists($id))
  63. return app('json')->fail('数据不存在');
  64. if($this->repository->getBrandHasProduct($id))
  65. return app('json')->fail('该品牌下存在商品');
  66. $this->repository->delete($id);
  67. return app('json')->success('删除成功');
  68. }
  69. public function detail($id)
  70. {
  71. if (!$this->repository->meExists($id))
  72. return app('json')->fail('数据不存在');
  73. return app('json')->success($this->repository->get($id));
  74. }
  75. /**
  76. * 验证
  77. * @param validate $validate
  78. * @param bool $isCreate
  79. * @return array
  80. * @author Qinii
  81. */
  82. public function checkParams(validate $validate)
  83. {
  84. $data = $this->request->params(['brand_category_id','brand_name','is_show','sort','pic']);
  85. $validate->check($data);
  86. return $data;
  87. }
  88. /**
  89. * @Author:Qinii
  90. * @Date: 2020/5/27
  91. * @return mixed
  92. */
  93. public function createForm()
  94. {
  95. return app('json')->success(formToData($this->repository->form()));
  96. }
  97. /**
  98. * @Author:Qinii
  99. * @Date: 2020/5/27
  100. * @param $id
  101. * @return mixed
  102. */
  103. public function updateForm($id)
  104. {
  105. if (!$this->repository->meExists($id))
  106. return app('json')->fail('数据不存在');
  107. return app('json')->success(formToData($this->repository->updateForm($id)));
  108. }
  109. /**
  110. * @Author:Qinii
  111. * @Date: 2020/5/27
  112. * @param int $id
  113. * @return mixed
  114. */
  115. public function switchStatus($id)
  116. {
  117. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  118. if (!$this->repository->meExists($id))
  119. return app('json')->fail('数据不存在');
  120. $this->repository->update($id, ['is_show' => $status]);
  121. return app('json')->success('修改成功');
  122. }
  123. }