AgentLevel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\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. * Class AgentLevel
  18. * @package app\controller\admin\v1\agent
  19. */
  20. class AgentLevel extends AuthController
  21. {
  22. /**
  23. * AgentLevel constructor.
  24. * @param App $app
  25. * @param AgentLevelServices $services
  26. */
  27. public function __construct(App $app, AgentLevelServices $services)
  28. {
  29. parent::__construct($app);
  30. $this->services = $services;
  31. }
  32. /**
  33. * 显示资源列表
  34. *
  35. * @return \think\Response
  36. */
  37. public function index()
  38. {
  39. $where = $this->request->getMore([
  40. ['status', ''],
  41. ['keyword', '']
  42. ]);
  43. return app('json')->success($this->services->getLevelList($where));
  44. }
  45. /**
  46. * 显示创建资源表单页.
  47. *
  48. * @return \think\Response
  49. */
  50. public function create()
  51. {
  52. return app('json')->success($this->services->createForm());
  53. }
  54. /**
  55. * 保存新建的资源
  56. *
  57. * @return \think\Response
  58. */
  59. public function save()
  60. {
  61. $data = $this->request->postMore([
  62. ['name', ''],
  63. ['grade', 0],
  64. ['image', ''],
  65. ['one_brokerage', 0],
  66. ['two_brokerage', 0],
  67. ['status', 0]]);
  68. if (!$data['name']) return app('json')->fail('请输入等级名称');
  69. if (!$data['grade']) return app('json')->fail('请输入等级');
  70. if (!$data['image']) return app('json')->fail('请选择等级图标');
  71. if ($data['two_brokerage'] > $data['one_brokerage']) {
  72. return app('json')->fail('二级返佣比例不能大于一级');
  73. }
  74. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  75. if ($grade) {
  76. return app('json')->fail('当前等级已存在');
  77. }
  78. $data['add_time'] = time();
  79. $this->services->save($data);
  80. return app('json')->success('添加等级成功!');
  81. }
  82. /**
  83. * 显示指定的资源
  84. *
  85. * @param int $id
  86. * @return \think\Response
  87. */
  88. public function read($id)
  89. {
  90. //
  91. }
  92. /**
  93. * 显示编辑资源表单页.
  94. *
  95. * @param int $id
  96. * @return \think\Response
  97. */
  98. public function edit($id)
  99. {
  100. return app('json')->success($this->services->editForm((int)$id));
  101. }
  102. /**
  103. * 保存更新的资源
  104. *
  105. * @param int $id
  106. * @return \think\Response
  107. */
  108. public function update($id)
  109. {
  110. $data = $this->request->postMore([
  111. ['name', ''],
  112. ['grade', 0],
  113. ['image', ''],
  114. ['one_brokerage', 0],
  115. ['two_brokerage', 0],
  116. ['status', 0]]);
  117. if (!$data['name']) return app('json')->fail('请输入等级名称');
  118. if (!$data['grade']) return app('json')->fail('请输入等级');
  119. if (!$data['image']) return app('json')->fail('请选择等级图标');
  120. if ($data['two_brokerage'] > $data['one_brokerage']) {
  121. return app('json')->fail('二级分拥比例不能大于一级');
  122. }
  123. if (!$levelInfo = $this->services->getLevelInfo((int)$id)) return app('json')->fail('编辑的等级不存在!');
  124. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  125. if ($grade && $grade['id'] != $id) {
  126. return app('json')->fail('当前等级已存在');
  127. }
  128. $levelInfo->name = $data['name'];
  129. $levelInfo->grade = $data['grade'];
  130. $levelInfo->image = $data['image'];
  131. $levelInfo->one_brokerage = $data['one_brokerage'];
  132. $levelInfo->two_brokerage = $data['two_brokerage'];
  133. $levelInfo->status = $data['status'];
  134. $levelInfo->save();
  135. return app('json')->success('修改成功!');
  136. }
  137. /**
  138. * 删除指定资源
  139. * @param $id
  140. * @return mixed
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function delete($id)
  146. {
  147. if (!$id) return app('json')->fail('参数错误,请重新打开');
  148. $levelInfo = $this->services->getLevelInfo((int)$id);
  149. if ($levelInfo) {
  150. $res = $this->services->update($id, ['is_del' => 1]);
  151. if (!$res)
  152. return app('json')->fail('删除失败,请稍候再试!');
  153. /** @var AgentLevelTaskServices $agentLevelTaskServices */
  154. $agentLevelTaskServices = app()->make(AgentLevelTaskServices::class);
  155. $agentLevelTaskServices->update(['level_id' => $id], ['is_del' => 1]);
  156. }
  157. return app('json')->success('删除成功!');
  158. }
  159. /**
  160. * 修改状态
  161. * @param int $id
  162. * @param string $status
  163. * @return mixed
  164. */
  165. public function set_status($id = 0, $status = '')
  166. {
  167. if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  168. $this->services->update($id, ['status' => $status]);
  169. return app('json')->success($status == 0 ? '隐藏成功' : '显示成功');
  170. }
  171. }