MerchantCategory.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. public function getOptions()
  57. {
  58. return app('json')->success($this->repository->allOptions());
  59. }
  60. /**
  61. * @param MerchantCategoryValidate $validate
  62. * @return mixed
  63. * @author xaboy
  64. * @day 2020-05-06
  65. */
  66. public function create(MerchantCategoryValidate $validate)
  67. {
  68. $data = $this->checkParams($validate);
  69. $data['commission_rate'] = bcdiv($data['commission_rate'], 100, 4);
  70. $this->repository->create($data);
  71. return app('json')->success('添加成功');
  72. }
  73. /**
  74. * @return mixed
  75. * @throws FormBuilderException
  76. * @author xaboy
  77. * @day 2020-05-06
  78. */
  79. public function createForm()
  80. {
  81. return app('json')->success(formToData($this->repository->form()));
  82. }
  83. /**
  84. * @param $id
  85. * @param MerchantCategoryValidate $validate
  86. * @return mixed
  87. * @throws DbException
  88. * @author xaboy
  89. * @day 2020-05-06
  90. */
  91. public function update($id, MerchantCategoryValidate $validate)
  92. {
  93. $data = $this->checkParams($validate);
  94. if (!$this->repository->exists($id))
  95. return app('json')->fail('数据不存在');
  96. $data['commission_rate'] = bcdiv($data['commission_rate'], 100, 4);
  97. $this->repository->update($id, $data);
  98. return app('json')->success('编辑成功');
  99. }
  100. /**
  101. * @param $id
  102. * @return mixed
  103. * @throws DataNotFoundException
  104. * @throws DbException
  105. * @throws ModelNotFoundException
  106. * @throws FormBuilderException
  107. * @author xaboy
  108. * @day 2020-05-06
  109. */
  110. public function updateForm($id)
  111. {
  112. if (!$this->repository->exists($id))
  113. return app('json')->fail('数据不存在');
  114. return app('json')->success(formToData($this->repository->updateForm($id)));
  115. }
  116. /**
  117. * @param $id
  118. * @param MerchantRepository $merchantRepository
  119. * @return mixed
  120. * @throws DbException
  121. * @author xaboy
  122. * @day 2020-05-06
  123. */
  124. public function delete($id, MerchantRepository $merchantRepository)
  125. {
  126. if (!$this->repository->exists($id))
  127. return app('json')->fail('数据不存在');
  128. if ($merchantRepository->fieldExists('category_id', $id))
  129. return app('json')->fail('存在商户,无法删除');
  130. $this->repository->delete($id);
  131. return app('json')->success('删除成功');
  132. }
  133. /**
  134. * @param MerchantCategoryValidate $validate
  135. * @return array
  136. * @author xaboy
  137. * @day 2020-05-06
  138. */
  139. public function checkParams(MerchantCategoryValidate $validate)
  140. {
  141. $data = $this->request->params(['category_name', ['commission_rate', 0]]);
  142. $validate->check($data);
  143. return $data;
  144. }
  145. }