MerchantCategory.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\system\merchant;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  14. use app\common\repositories\system\merchant\MerchantRepository;
  15. use app\validate\admin\MerchantCategoryValidate;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * Class MerchantCategory
  23. * @package app\controller\admin\system\merchant
  24. * @author xaboy
  25. * @day 2020-05-06
  26. */
  27. class MerchantCategory extends BaseController
  28. {
  29. /**
  30. * @var MerchantCategoryRepository
  31. */
  32. protected $repository;
  33. /**
  34. * MerchantCategory constructor.
  35. * @param App $app
  36. * @param MerchantCategoryRepository $repository
  37. */
  38. public function __construct(App $app, MerchantCategoryRepository $repository)
  39. {
  40. parent::__construct($app);
  41. $this->repository = $repository;
  42. }
  43. /**
  44. * @return mixed
  45. * @throws DbException
  46. * @throws DataNotFoundException
  47. * @throws ModelNotFoundException
  48. * @author xaboy
  49. * @day 2020-05-06
  50. */
  51. public function lst()
  52. {
  53. [$page, $limit] = $this->getPage();
  54. return app('json')->success($this->repository->getList([], $page, $limit));
  55. }
  56. /**
  57. * @param MerchantCategoryValidate $validate
  58. * @return mixed
  59. * @author xaboy
  60. * @day 2020-05-06
  61. */
  62. public function create(MerchantCategoryValidate $validate)
  63. {
  64. $data = $this->checkParams($validate);
  65. $data['commission_rate'] = bcdiv($data['commission_rate'], 100, 4);
  66. $this->repository->create($data);
  67. return app('json')->success('添加成功');
  68. }
  69. /**
  70. * @return mixed
  71. * @throws FormBuilderException
  72. * @author xaboy
  73. * @day 2020-05-06
  74. */
  75. public function createForm()
  76. {
  77. return app('json')->success(formToData($this->repository->form()));
  78. }
  79. /**
  80. * @param $id
  81. * @param MerchantCategoryValidate $validate
  82. * @return mixed
  83. * @throws DbException
  84. * @author xaboy
  85. * @day 2020-05-06
  86. */
  87. public function update($id, MerchantCategoryValidate $validate)
  88. {
  89. $data = $this->checkParams($validate);
  90. if (!$this->repository->exists($id))
  91. return app('json')->fail('数据不存在');
  92. $data['commission_rate'] = bcdiv($data['commission_rate'], 100, 4);
  93. $this->repository->update($id, $data);
  94. return app('json')->success('编辑成功');
  95. }
  96. /**
  97. * @param $id
  98. * @return mixed
  99. * @throws DataNotFoundException
  100. * @throws DbException
  101. * @throws ModelNotFoundException
  102. * @throws FormBuilderException
  103. * @author xaboy
  104. * @day 2020-05-06
  105. */
  106. public function updateForm($id)
  107. {
  108. if (!$this->repository->exists($id))
  109. return app('json')->fail('数据不存在');
  110. return app('json')->success(formToData($this->repository->updateForm($id)));
  111. }
  112. /**
  113. * @param $id
  114. * @param MerchantRepository $merchantRepository
  115. * @return mixed
  116. * @throws DbException
  117. * @author xaboy
  118. * @day 2020-05-06
  119. */
  120. public function delete($id, MerchantRepository $merchantRepository)
  121. {
  122. if (!$this->repository->exists($id))
  123. return app('json')->fail('数据不存在');
  124. if ($merchantRepository->fieldExists('category_id', $id))
  125. return app('json')->fail('存在商户,无法删除');
  126. $this->repository->delete($id);
  127. return app('json')->success('删除成功');
  128. }
  129. /**
  130. * @param MerchantCategoryValidate $validate
  131. * @return array
  132. * @author xaboy
  133. * @day 2020-05-06
  134. */
  135. public function checkParams(MerchantCategoryValidate $validate)
  136. {
  137. $data = $this->request->params(['category_name', ['commission_rate', 0]]);
  138. $validate->check($data);
  139. return $data;
  140. }
  141. }