AgentLevelTask.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 AgentLevelTask
  19. * @package app\controller\admin\v1\agent
  20. */
  21. class AgentLevelTask extends AuthController
  22. {
  23. /**
  24. * @var AgentLevelTaskServices
  25. */
  26. protected $services;
  27. /**
  28. * AgentLevelTask constructor.
  29. * @param App $app
  30. * @param AgentLevelTaskServices $services
  31. */
  32. public function __construct(App $app, AgentLevelTaskServices $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. // 获取请求参数:等级ID、状态、关键词
  47. $where = $this->request->getMore([
  48. ['id', 0],
  49. ['status', ''],
  50. ['keyword', '']
  51. ]);
  52. if (!$where['id']) {
  53. return app('json')->fail('参数错误');
  54. }
  55. $where['level_id'] = $where['id'];
  56. unset($where['id']);
  57. // 调用服务层获取等级任务列表
  58. return app('json')->success($this->services->getLevelTaskList($where));
  59. }
  60. /**
  61. * 等级任务添加表单
  62. * @return mixed
  63. * @throws \FormBuilder\Exception\FormBuilderException
  64. */
  65. public function create()
  66. {
  67. // 获取等级ID
  68. [$level_id] = $this->request->postMore([
  69. ['level_id', 0]], true);
  70. if (!$level_id) {
  71. return app('json')->fail('参数错误');
  72. }
  73. // 调用服务层创建添加表单
  74. return app('json')->success($this->services->createForm((int)$level_id));
  75. }
  76. /**
  77. * 保存等级任务
  78. * @return mixed
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function save()
  84. {
  85. // 获取并验证请求数据
  86. $data = $this->request->postMore([
  87. ['level_id', 0],
  88. ['name', ''],
  89. ['type', ''],
  90. ['number', 0],
  91. ['desc', 0],
  92. ['sort', 0],
  93. ['status', 0]]);
  94. if (!$data['level_id']) return app('json')->fail('参数错误');
  95. if (!$data['name']) return app('json')->fail('请输入任务名称');
  96. if (!$data['type']) return app('json')->fail('请选择任务类型');
  97. if (!$data['number']) return app('json')->fail('请输入限定数量');
  98. // 检查任务类型是否有效
  99. $this->services->checkTypeTask(0, $data);
  100. $data['add_time'] = time();
  101. // 保存任务数据
  102. $this->services->save($data);
  103. // 更新等级的任务数量
  104. $levelInfo = app()->make(AgentLevelServices::class)->get((int)$data['level_id']);
  105. $levelInfo->task_num = $levelInfo->task_num + 1;
  106. $levelInfo->task_total_num = $levelInfo->task_total_num + 1;
  107. $levelInfo->save();
  108. return app('json')->success('添加任务成功');
  109. }
  110. /**
  111. * 显示指定的资源
  112. * @param $id
  113. */
  114. public function read($id)
  115. {
  116. }
  117. /**
  118. * 等级任务修改表单
  119. * @param $id
  120. * @return mixed
  121. * @throws \FormBuilder\Exception\FormBuilderException
  122. */
  123. public function edit($id)
  124. {
  125. // 调用服务层创建编辑表单
  126. return app('json')->success($this->services->editForm((int)$id));
  127. }
  128. /**
  129. * 修改等级任务
  130. * @param $id
  131. * @return mixed
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function update($id)
  137. {
  138. // 获取并验证请求数据
  139. $data = $this->request->postMore([
  140. ['name', ''],
  141. ['type', ''],
  142. ['number', 0],
  143. ['desc', 0],
  144. ['sort', 0],
  145. ['status', 0]]);
  146. if (!$data['name']) return app('json')->fail('请输入任务名称');
  147. if (!$data['type']) return app('json')->fail('请选择任务类型');
  148. if (!$data['number']) return app('json')->fail('请输入限定数量');
  149. // 检查任务是否存在
  150. if (!$levelTaskInfo = $this->services->getLevelTaskInfo((int)$id)) return app('json')->fail('编辑的任务不存在');
  151. // 检查任务类型是否有效
  152. $this->services->checkTypeTask((int)$id, $data);
  153. // 更新任务信息
  154. $levelTaskInfo->name = $data['name'];
  155. $levelTaskInfo->type = $data['type'];
  156. $levelTaskInfo->number = $data['number'];
  157. $levelTaskInfo->desc = $data['desc'];
  158. $levelTaskInfo->sort = $data['sort'];
  159. $levelTaskInfo->status = $data['status'];
  160. $levelTaskInfo->save();
  161. return app('json')->success('修改成功');
  162. }
  163. /**
  164. * 删除等级任务
  165. * @param $id
  166. * @return mixed
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. */
  171. public function delete($id)
  172. {
  173. if (!$id) return app('json')->fail('参数错误');
  174. $levelTaskInfo = $this->services->getLevelTaskInfo((int)$id);
  175. if ($levelTaskInfo) {
  176. // 标记删除
  177. $res = $this->services->update($id, ['is_del' => 1]);
  178. if ($res) {
  179. // 更新等级的任务数量
  180. $levelInfo = app()->make(AgentLevelServices::class)->get((int)$levelTaskInfo['level_id']);
  181. $levelInfo->task_num = $levelInfo->task_num - 1;
  182. $levelInfo->task_total_num = $levelInfo->task_total_num - 1;
  183. if ($levelInfo->task_num <= 0) $levelInfo->task_num = $levelInfo->task_total_num;
  184. $levelInfo->save();
  185. } else {
  186. return app('json')->fail('删除失败');
  187. }
  188. }
  189. return app('json')->success('删除成功');
  190. }
  191. /**
  192. * 修改状态
  193. * @param int $id
  194. * @param string $status
  195. * @return mixed
  196. */
  197. public function set_status($id = 0, $status = '')
  198. {
  199. if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  200. // 更新状态
  201. $this->services->update($id, ['status' => $status]);
  202. return app('json')->success('设置成功');
  203. }
  204. }