StoreBrand.php 3.6 KB

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