MerchantCategory.php 4.0 KB

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