AgentLevel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\agent;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\agent\AgentLevelServices;
  14. use app\services\agent\AgentLevelTaskServices;
  15. use think\facade\App;
  16. /**
  17. * 分销等级控制器
  18. * Class AgentLevel
  19. * @package app\controller\admin\v1\agent
  20. */
  21. class AgentLevel extends AuthController
  22. {
  23. /**
  24. * AgentLevel constructor.
  25. * @param App $app
  26. * @param AgentLevelServices $services
  27. */
  28. public function __construct(App $app, AgentLevelServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 后台分销等级列表
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['status', ''],
  44. ['keyword', '']
  45. ]);
  46. return app('json')->success($this->services->getLevelList($where));
  47. }
  48. /**
  49. * 添加分销等级表单
  50. * @return mixed
  51. * @throws \FormBuilder\Exception\FormBuilderException
  52. */
  53. public function create()
  54. {
  55. return app('json')->success($this->services->createForm());
  56. }
  57. /**
  58. * 保存分销等级
  59. * @return mixed
  60. */
  61. public function save()
  62. {
  63. $data = $this->request->postMore([
  64. ['name', ''],
  65. ['grade', 0],
  66. ['image', ''],
  67. ['one_brokerage_percent', 0],
  68. ['two_brokerage_percent', 0],
  69. ['status', 0]]);
  70. if (!$data['name']) return app('json')->fail(400200);
  71. if (!$data['grade']) return app('json')->fail(400201);
  72. if (!$data['image']) return app('json')->fail(400202);
  73. if ($data['two_brokerage_percent'] > $data['one_brokerage_percent']) {
  74. return app('json')->fail(400203);
  75. }
  76. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  77. if ($grade) {
  78. return app('json')->fail(400204);
  79. }
  80. $data['add_time'] = time();
  81. $this->services->save($data);
  82. return app('json')->success(400205);
  83. }
  84. /**
  85. * 显示指定的资源
  86. * @param $id
  87. */
  88. public function read($id)
  89. {
  90. }
  91. /**
  92. * 编辑分销等级表单
  93. * @param $id
  94. * @return mixed
  95. * @throws \FormBuilder\Exception\FormBuilderException
  96. */
  97. public function edit($id)
  98. {
  99. return app('json')->success($this->services->editForm((int)$id));
  100. }
  101. /**
  102. * 修改分销等级
  103. * @param $id
  104. * @return mixed
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public function update($id)
  110. {
  111. $data = $this->request->postMore([
  112. ['name', ''],
  113. ['grade', 0],
  114. ['image', ''],
  115. ['one_brokerage_percent', 0],
  116. ['two_brokerage_percent', 0],
  117. ['status', 0]]);
  118. if (!$data['name']) return app('json')->fail(400200);
  119. if (!$data['grade']) return app('json')->fail(400201);
  120. if (!$data['image']) return app('json')->fail(400202);
  121. if ($data['two_brokerage_percent'] > $data['one_brokerage_percent']) {
  122. return app('json')->fail(400203);
  123. }
  124. if (!$levelInfo = $this->services->getLevelInfo((int)$id)) return app('json')->fail(400206);
  125. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  126. if ($grade && $grade['id'] != $id) {
  127. return app('json')->fail(400204);
  128. }
  129. $levelInfo->name = $data['name'];
  130. $levelInfo->grade = $data['grade'];
  131. $levelInfo->image = $data['image'];
  132. $levelInfo->one_brokerage_percent = $data['one_brokerage_percent'];
  133. $levelInfo->two_brokerage_percent = $data['two_brokerage_percent'];
  134. $levelInfo->status = $data['status'];
  135. $levelInfo->save();
  136. return app('json')->success(100001);
  137. }
  138. /**
  139. * 删除分销等级
  140. * @param $id
  141. * @return mixed
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function delete($id)
  147. {
  148. if (!$id) return app('json')->fail(100100);
  149. //检查分销等级数据是否存在
  150. $levelInfo = $this->services->getLevelInfo((int)$id);
  151. if ($levelInfo) {
  152. //更新数据为已删除
  153. $res = $this->services->update($id, ['is_del' => 1]);
  154. if (!$res)
  155. return app('json')->fail(100008);
  156. //删除该等级的任务为已删除
  157. /** @var AgentLevelTaskServices $agentLevelTaskServices */
  158. $agentLevelTaskServices = app()->make(AgentLevelTaskServices::class);
  159. $agentLevelTaskServices->update(['level_id' => $id], ['is_del' => 1]);
  160. }
  161. return app('json')->success(100002);
  162. }
  163. /**
  164. * 修改状态
  165. * @param int $id
  166. * @param string $status
  167. * @return mixed
  168. */
  169. public function set_status($id = 0, $status = '')
  170. {
  171. if ($status == '' || $id == 0) return app('json')->fail(100100);
  172. $this->services->update($id, ['status' => $status]);
  173. return app('json')->success(100014);
  174. }
  175. }