MerchantCategory.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\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. * 商户分类
  23. */
  24. class MerchantCategory extends BaseController
  25. {
  26. /**
  27. * @var MerchantCategoryRepository
  28. */
  29. protected $repository;
  30. /**
  31. * MerchantCategory constructor.
  32. * @param App $app
  33. * @param MerchantCategoryRepository $repository
  34. */
  35. public function __construct(App $app, MerchantCategoryRepository $repository)
  36. {
  37. parent::__construct($app);
  38. $this->repository = $repository;
  39. }
  40. /**
  41. * 列表
  42. * @return mixed
  43. * @throws DbException
  44. * @throws DataNotFoundException
  45. * @throws ModelNotFoundException
  46. * @author xaboy
  47. * @day 2020-05-06
  48. */
  49. public function lst()
  50. {
  51. [$page, $limit] = $this->getPage();
  52. return app('json')->success($this->repository->getList([], $page, $limit));
  53. }
  54. /**
  55. * 获取所有分类
  56. * @return mixed
  57. * @author xaboy
  58. * @day 2020-05-06
  59. */
  60. public function getOptions()
  61. {
  62. return app('json')->success($this->repository->allOptions());
  63. }
  64. /**
  65. * 创建
  66. * @param MerchantCategoryValidate $validate
  67. * @return mixed
  68. * @author xaboy
  69. * @day 2020-05-06
  70. */
  71. public function create(MerchantCategoryValidate $validate)
  72. {
  73. $data = $this->checkParams($validate);
  74. $data['commission_rate'] = bcdiv($data['commission_rate'], 100, 4);
  75. $this->repository->create($data);
  76. return app('json')->success('添加成功');
  77. }
  78. /**
  79. * 创建表单
  80. * @return mixed
  81. * @throws FormBuilderException
  82. * @author xaboy
  83. * @day 2020-05-06
  84. */
  85. public function createForm()
  86. {
  87. return app('json')->success(formToData($this->repository->form()));
  88. }
  89. /**
  90. * 修改
  91. * @param $id
  92. * @param MerchantCategoryValidate $validate
  93. * @return mixed
  94. * @throws DbException
  95. * @author xaboy
  96. * @day 2020-05-06
  97. */
  98. public function update($id, MerchantCategoryValidate $validate)
  99. {
  100. $data = $this->checkParams($validate);
  101. if (!$this->repository->exists($id))
  102. return app('json')->fail('数据不存在');
  103. $data['commission_rate'] = bcdiv($data['commission_rate'], 100, 4);
  104. $this->repository->update($id, $data);
  105. return app('json')->success('编辑成功');
  106. }
  107. /**
  108. * 修改表单
  109. * @param $id
  110. * @return mixed
  111. * @throws DataNotFoundException
  112. * @throws DbException
  113. * @throws ModelNotFoundException
  114. * @throws FormBuilderException
  115. * @author xaboy
  116. * @day 2020-05-06
  117. */
  118. public function updateForm($id)
  119. {
  120. if (!$this->repository->exists($id))
  121. return app('json')->fail('数据不存在');
  122. return app('json')->success(formToData($this->repository->updateForm($id)));
  123. }
  124. /**
  125. * 删除
  126. * @param $id
  127. * @param MerchantRepository $merchantRepository
  128. * @return mixed
  129. * @throws DbException
  130. * @author xaboy
  131. * @day 2020-05-06
  132. */
  133. public function delete($id, MerchantRepository $merchantRepository)
  134. {
  135. if (!$this->repository->exists($id))
  136. return app('json')->fail('数据不存在');
  137. if ($merchantRepository->fieldExists('category_id', $id))
  138. return app('json')->fail('存在商户,无法删除');
  139. $this->repository->delete($id);
  140. return app('json')->success('删除成功');
  141. }
  142. /**
  143. * 验证参数
  144. * @param MerchantCategoryValidate $validate
  145. * @return array
  146. * @author xaboy
  147. * @day 2020-05-06
  148. */
  149. public function checkParams(MerchantCategoryValidate $validate)
  150. {
  151. $data = $this->request->params(['category_name', ['commission_rate', 0]]);
  152. $validate->check($data);
  153. return $data;
  154. }
  155. }