AgentLevelTask.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 AgentLevelTask
  19. * @package app\controller\admin\v1\agent
  20. */
  21. class AgentLevelTask extends AuthController
  22. {
  23. /**
  24. * AgentLevelTask constructor.
  25. * @param App $app
  26. * @param AgentLevelTaskServices $services
  27. */
  28. public function __construct(App $app, AgentLevelTaskServices $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. ['id', 0],
  44. ['status', ''],
  45. ['keyword', '']
  46. ]);
  47. if (!$where['id']) {
  48. return app('json')->fail(100100);
  49. }
  50. $where['level_id'] = $where['id'];
  51. unset($where['id']);
  52. return app('json')->success($this->services->getLevelTaskList($where));
  53. }
  54. /**
  55. * 等级任务添加表单
  56. * @return mixed
  57. * @throws \FormBuilder\Exception\FormBuilderException
  58. */
  59. public function create()
  60. {
  61. [$level_id] = $this->request->postMore([
  62. ['level_id', 0]], true);
  63. if (!$level_id) {
  64. return app('json')->fail(100100);
  65. }
  66. return app('json')->success($this->services->createForm((int)$level_id));
  67. }
  68. /**
  69. * 保存等级任务
  70. * @return mixed
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function save()
  76. {
  77. $data = $this->request->postMore([
  78. ['level_id', 0],
  79. ['name', ''],
  80. ['type', ''],
  81. ['number', 0],
  82. ['desc', 0],
  83. ['sort', 0],
  84. ['status', 0]]);
  85. if (!$data['level_id']) return app('json')->fail(100100);
  86. if (!$data['name']) return app('json')->fail(400207);
  87. if (!$data['type']) return app('json')->fail(400208);
  88. if (!$data['number']) return app('json')->fail(400209);
  89. $this->services->checkTypeTask(0, $data);
  90. $data['add_time'] = time();
  91. $this->services->save($data);
  92. $levelInfo = app()->make(AgentLevelServices::class)->get((int)$data['level_id']);
  93. $levelInfo->task_num = $levelInfo->task_num + 1;
  94. $levelInfo->task_total_num = $levelInfo->task_total_num + 1;
  95. $levelInfo->save();
  96. return app('json')->success(400210);
  97. }
  98. /**
  99. * 显示指定的资源
  100. * @param $id
  101. */
  102. public function read($id)
  103. {
  104. }
  105. /**
  106. * 等级任务修改表单
  107. * @param $id
  108. * @return mixed
  109. * @throws \FormBuilder\Exception\FormBuilderException
  110. */
  111. public function edit($id)
  112. {
  113. return app('json')->success($this->services->editForm((int)$id));
  114. }
  115. /**
  116. * 修改等级任务
  117. * @param $id
  118. * @return mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function update($id)
  124. {
  125. $data = $this->request->postMore([
  126. ['name', ''],
  127. ['type', ''],
  128. ['number', 0],
  129. ['desc', 0],
  130. ['sort', 0],
  131. ['status', 0]]);
  132. if (!$data['name']) return app('json')->fail(400207);
  133. if (!$data['type']) return app('json')->fail(400208);
  134. if (!$data['number']) return app('json')->fail(400209);
  135. if (!$levelTaskInfo = $this->services->getLevelTaskInfo((int)$id)) return app('json')->fail(400211);
  136. $this->services->checkTypeTask((int)$id, $data);
  137. $levelTaskInfo->name = $data['name'];
  138. $levelTaskInfo->type = $data['type'];
  139. $levelTaskInfo->number = $data['number'];
  140. $levelTaskInfo->desc = $data['desc'];
  141. $levelTaskInfo->sort = $data['sort'];
  142. $levelTaskInfo->status = $data['status'];
  143. $levelTaskInfo->save();
  144. return app('json')->success(100001);
  145. }
  146. /**
  147. * 删除等级任务
  148. * @param $id
  149. * @return mixed
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function delete($id)
  155. {
  156. if (!$id) return app('json')->fail(100100);
  157. $levelTaskInfo = $this->services->getLevelTaskInfo((int)$id);
  158. if ($levelTaskInfo) {
  159. $res = $this->services->update($id, ['is_del' => 1]);
  160. if ($res) {
  161. $levelInfo = app()->make(AgentLevelServices::class)->get((int)$levelTaskInfo['level_id']);
  162. $levelInfo->task_num = $levelInfo->task_num - 1;
  163. $levelInfo->task_total_num = $levelInfo->task_total_num - 1;
  164. if ($levelInfo->task_num <= 0) $levelInfo->task_num = $levelInfo->task_total_num;
  165. $levelInfo->save();
  166. } else {
  167. return app('json')->fail(100008);
  168. }
  169. }
  170. return app('json')->success(100002);
  171. }
  172. /**
  173. * 修改状态
  174. * @param int $id
  175. * @param string $status
  176. * @return mixed
  177. */
  178. public function set_status($id = 0, $status = '')
  179. {
  180. if ($status == '' || $id == 0) return app('json')->fail(100100);
  181. $this->services->update($id, ['status' => $status]);
  182. return app('json')->success(100014);
  183. }
  184. }