AgentLevel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\controller\admin\v1\agent;
  12. use app\controller\admin\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 $this->success($this->services->getLevelList($where));
  44. }
  45. /**
  46. * 显示创建资源表单页.
  47. *
  48. * @return \think\Response
  49. */
  50. public function create()
  51. {
  52. return $this->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. $data['name'] = trim($data['name']);
  69. if (!$data['name']) return $this->fail('请输入等级名称');
  70. if (!$data['grade']) return $this->fail('请输入等级');
  71. if (!$data['image']) return $this->fail('请选择等级图标');
  72. if ((int)$data['two_brokerage'] > (int)$data['one_brokerage']) {
  73. return $this->fail('二级上浮整体返佣比例不能大于一级');
  74. }
  75. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  76. if ($grade) {
  77. return $this->fail('当前等级已存在');
  78. }
  79. $data['add_time'] = time();
  80. $this->services->save($data);
  81. return $this->success('添加等级成功!');
  82. }
  83. /**
  84. * 显示指定的资源
  85. *
  86. * @param int $id
  87. * @return \think\Response
  88. */
  89. public function read($id)
  90. {
  91. //
  92. }
  93. /**
  94. * 显示编辑资源表单页.
  95. *
  96. * @param int $id
  97. * @return \think\Response
  98. */
  99. public function edit($id)
  100. {
  101. return $this->success($this->services->editForm((int)$id));
  102. }
  103. /**
  104. * 保存更新的资源
  105. *
  106. * @param int $id
  107. * @return \think\Response
  108. */
  109. public function update($id)
  110. {
  111. $data = $this->request->postMore([
  112. ['name', ''],
  113. ['grade', 0],
  114. ['image', ''],
  115. ['one_brokerage', 0],
  116. ['two_brokerage', 0],
  117. ['status', 0]]);
  118. $data['name'] = trim($data['name']);
  119. if (!$data['name']) return $this->fail('请输入等级名称');
  120. if (!$data['grade']) return $this->fail('请输入等级');
  121. if (!$data['image']) return $this->fail('请选择等级图标');
  122. if ((int)$data['two_brokerage'] > (int)$data['one_brokerage']) {
  123. return $this->fail('二级上浮整体返佣比例不能大于一级');
  124. }
  125. if (!$levelInfo = $this->services->getLevelInfo((int)$id)) return $this->fail('编辑的等级不存在!');
  126. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  127. if ($grade && $grade['id'] != $id) {
  128. return $this->fail('当前等级已存在');
  129. }
  130. $levelInfo->name = $data['name'];
  131. $levelInfo->grade = $data['grade'];
  132. $levelInfo->image = $data['image'];
  133. $levelInfo->one_brokerage = $data['one_brokerage'];
  134. $levelInfo->two_brokerage = $data['two_brokerage'];
  135. $levelInfo->status = $data['status'];
  136. $levelInfo->save();
  137. return $this->success('修改成功!');
  138. }
  139. /**
  140. * 删除指定资源
  141. * @param $id
  142. * @return mixed
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. */
  147. public function delete($id)
  148. {
  149. if (!$id) return $this->fail('参数错误,请重新打开');
  150. $levelInfo = $this->services->getLevelInfo((int)$id);
  151. if ($levelInfo) {
  152. $res = $this->services->update($id, ['is_del' => 1]);
  153. if (!$res)
  154. return $this->fail('删除失败,请稍候再试!');
  155. /** @var AgentLevelTaskServices $agentLevelTaskServices */
  156. $agentLevelTaskServices = app()->make(AgentLevelTaskServices::class);
  157. $agentLevelTaskServices->update(['level_id' => $id], ['is_del' => 1]);
  158. }
  159. return $this->success('删除成功!');
  160. }
  161. /**
  162. * 修改状态
  163. * @param int $id
  164. * @param string $status
  165. * @return mixed
  166. */
  167. public function set_status($id = 0, $status = '')
  168. {
  169. if ($status == '' || $id == 0) return $this->fail('参数错误');
  170. $this->services->update($id, ['status' => $status]);
  171. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  172. }
  173. }