AgentLevel.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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. * @var AgentLevelServices
  25. */
  26. protected $services;
  27. /**
  28. * AgentLevel constructor.
  29. * @param App $app
  30. * @param AgentLevelServices $services
  31. */
  32. public function __construct(App $app, AgentLevelServices $services)
  33. {
  34. parent::__construct($app);
  35. $this->services = $services;
  36. }
  37. /**
  38. * 后台分销等级列表
  39. * @return mixed
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function index()
  45. {
  46. // 获取请求参数,包括状态和关键词
  47. $where = $this->request->getMore([
  48. ['status', ''],
  49. ['keyword', '']
  50. ]);
  51. // 调用服务层获取等级列表
  52. return app('json')->success($this->services->getLevelList($where));
  53. }
  54. /**
  55. * 添加分销等级表单
  56. * @return mixed
  57. * @throws \FormBuilder\Exception\FormBuilderException
  58. */
  59. public function create()
  60. {
  61. // 调用服务层创建添加表单
  62. return app('json')->success($this->services->createForm());
  63. }
  64. /**
  65. * 保存分销等级
  66. * @return mixed
  67. */
  68. public function save()
  69. {
  70. // 获取并验证请求数据
  71. $data = $this->request->postMore([
  72. ['name', ''],
  73. ['grade', 0],
  74. ['image', ''],
  75. ['one_brokerage_percent', 0],
  76. ['two_brokerage_percent', 0],
  77. ['status', 0]]);
  78. if (!$data['name']) return app('json')->fail('请输入等级名称');
  79. if (!$data['grade']) return app('json')->fail('请输入等级');
  80. if (!$data['image']) return app('json')->fail('请选择等级图标');
  81. // 验证二级返佣比例是否大于一级
  82. if ($data['two_brokerage_percent'] > $data['one_brokerage_percent']) {
  83. return app('json')->fail('二级返佣比例不能大于一级');
  84. }
  85. // 检查等级是否已存在
  86. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  87. if ($grade) {
  88. return app('json')->fail('当前等级已存在');
  89. }
  90. $data['add_time'] = time();
  91. // 保存数据
  92. $this->services->save($data);
  93. return app('json')->success('添加等级成功');
  94. }
  95. /**
  96. * 显示指定的资源
  97. * @param $id
  98. */
  99. public function read($id)
  100. {
  101. }
  102. /**
  103. * 编辑分销等级表单
  104. * @param $id
  105. * @return mixed
  106. * @throws \FormBuilder\Exception\FormBuilderException
  107. */
  108. public function edit($id)
  109. {
  110. // 调用服务层创建编辑表单
  111. return app('json')->success($this->services->editForm((int)$id));
  112. }
  113. /**
  114. * 修改分销等级
  115. * @param $id
  116. * @return mixed
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function update($id)
  122. {
  123. // 获取并验证请求数据
  124. $data = $this->request->postMore([
  125. ['name', ''],
  126. ['grade', 0],
  127. ['image', ''],
  128. ['one_brokerage_percent', 0],
  129. ['two_brokerage_percent', 0],
  130. ['status', 0]]);
  131. if (!$data['name']) return app('json')->fail('请输入等级名称');
  132. if (!$data['grade']) return app('json')->fail('请输入等级');
  133. if (!$data['image']) return app('json')->fail('请选择等级图标');
  134. // 验证二级返佣比例是否大于一级
  135. if ($data['two_brokerage_percent'] > $data['one_brokerage_percent']) {
  136. return app('json')->fail('二级返佣比例不能大于一级');
  137. }
  138. // 检查编辑的等级是否存在
  139. if (!$levelInfo = $this->services->getLevelInfo((int)$id)) return app('json')->fail('编辑的等级不存在');
  140. // 检查等级是否重复
  141. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  142. if ($grade && $grade['id'] != $id) {
  143. return app('json')->fail('当前等级已存在');
  144. }
  145. // 更新等级信息
  146. $levelInfo->name = $data['name'];
  147. $levelInfo->grade = $data['grade'];
  148. $levelInfo->image = $data['image'];
  149. $levelInfo->one_brokerage_percent = $data['one_brokerage_percent'];
  150. $levelInfo->two_brokerage_percent = $data['two_brokerage_percent'];
  151. $levelInfo->status = $data['status'];
  152. $levelInfo->save();
  153. return app('json')->success('修改成功');
  154. }
  155. /**
  156. * 删除分销等级
  157. * @param $id
  158. * @return mixed
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function delete($id)
  164. {
  165. if (!$id) return app('json')->fail('参数错误');
  166. //检查分销等级数据是否存在
  167. $levelInfo = $this->services->getLevelInfo((int)$id);
  168. if ($levelInfo) {
  169. //更新数据为已删除
  170. $res = $this->services->update($id, ['is_del' => 1]);
  171. if (!$res)
  172. return app('json')->fail('删除失败');
  173. //删除该等级的任务为已删除
  174. /** @var AgentLevelTaskServices $agentLevelTaskServices */
  175. $agentLevelTaskServices = app()->make(AgentLevelTaskServices::class);
  176. $agentLevelTaskServices->update(['level_id' => $id], ['is_del' => 1]);
  177. }
  178. return app('json')->success('删除成功');
  179. }
  180. /**
  181. * 修改状态
  182. * @param int $id
  183. * @param string $status
  184. * @return mixed
  185. */
  186. public function set_status($id = 0, $status = '')
  187. {
  188. if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  189. // 更新状态
  190. $this->services->update($id, ['status' => $status]);
  191. return app('json')->success('设置成功');
  192. }
  193. /**
  194. * 获取任务表单数量
  195. * @param int $id 任务ID
  196. * @return \think\response\Json
  197. */
  198. public function getTaskNumForm($id)
  199. {
  200. // 判断任务ID是否为0,若为0则返回错误信息
  201. if ($id == 0) return app('json')->fail('参数错误');
  202. // 调用服务层获取任务表单数量
  203. $result = $this->services->getTaskNumForm($id);
  204. // 返回成功信息和任务表单数量
  205. return app('json')->success($result);
  206. }
  207. /**
  208. * 设置任务数量
  209. * @param int $id 任务ID
  210. * @return \think\response\Json
  211. */
  212. public function setTaskNum($id)
  213. {
  214. // 从请求中获取任务数量
  215. $data = $this->request->postMore([
  216. ['task_num', 0]
  217. ]);
  218. // 调用服务层设置任务数量
  219. $res = $this->services->setTaskNum($id, $data);
  220. return app('json')->success('设置成功');
  221. }
  222. }