AgentLevel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 AgentLevel
  19. * @package app\controller\admin\v1\agent
  20. */
  21. class AgentLevel extends AuthController
  22. {
  23. /**
  24. * AgentLevel constructor.
  25. * @param App $app
  26. * @param AgentLevelServices $services
  27. */
  28. public function __construct(App $app, AgentLevelServices $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. ['status', ''],
  44. ['keyword', '']
  45. ]);
  46. return app('json')->success($this->services->getLevelList($where));
  47. }
  48. /**
  49. * 添加分销等级表单
  50. * @return mixed
  51. * @throws \FormBuilder\Exception\FormBuilderException
  52. */
  53. public function create()
  54. {
  55. return app('json')->success($this->services->createForm());
  56. }
  57. /**
  58. * 保存分销等级
  59. * @return mixed
  60. */
  61. public function save()
  62. {
  63. $data = $this->request->postMore([
  64. ['name', ''],
  65. ['grade', 0],
  66. ['image', ''],
  67. ['one_brokerage', 0],
  68. ['two_brokerage', 0],
  69. ['discount', 0],
  70. ['free_issue', 0],
  71. // ['verific_reward', 0],
  72. // ['more_reward', 0],
  73. ['equal_award', 0],
  74. ['spread_brokerage', 0],
  75. ['status', 0]]);
  76. if (!$data['name']) return app('json')->fail(400200);
  77. if (!$data['grade']) return app('json')->fail(400201);
  78. if (!$data['image']) return app('json')->fail(400202);
  79. if ($data['two_brokerage'] > $data['one_brokerage']) {
  80. return app('json')->fail(400203);
  81. }
  82. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  83. if ($grade) {
  84. return app('json')->fail(400204);
  85. }
  86. $data['add_time'] = time();
  87. $this->services->save($data);
  88. return app('json')->success(400205);
  89. }
  90. /**
  91. * 显示指定的资源
  92. * @param $id
  93. */
  94. public function read($id)
  95. {
  96. }
  97. /**
  98. * 编辑分销等级表单
  99. * @param $id
  100. * @return mixed
  101. * @throws \FormBuilder\Exception\FormBuilderException
  102. */
  103. public function edit($id)
  104. {
  105. return app('json')->success($this->services->editForm((int)$id));
  106. }
  107. /**
  108. * 修改分销等级
  109. * @param $id
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function update($id)
  116. {
  117. $data = $this->request->postMore([
  118. ['name', ''],
  119. ['grade', 0],
  120. ['image', ''],
  121. ['one_brokerage', 0],
  122. ['two_brokerage', 0],
  123. ['discount', 0],
  124. ['free_issue', 0],
  125. // ['verific_reward', 0],
  126. // ['more_reward', 0],
  127. ['equal_award', 0],
  128. ['spread_brokerage', 0],
  129. ['status', 0]]);
  130. if (!$data['name']) return app('json')->fail(400200);
  131. if (!$data['grade']) return app('json')->fail(400201);
  132. if (!$data['image']) return app('json')->fail(400202);
  133. if ($data['two_brokerage'] > $data['one_brokerage']) {
  134. return app('json')->fail(400203);
  135. }
  136. if (!$levelInfo = $this->services->getLevelInfo((int)$id)) return app('json')->fail(400206);
  137. $grade = $this->services->get(['grade' => $data['grade'], 'is_del' => 0]);
  138. if ($grade && $grade['id'] != $id) {
  139. return app('json')->fail(400204);
  140. }
  141. $levelInfo->name = $data['name'];
  142. $levelInfo->grade = $data['grade'];
  143. $levelInfo->image = $data['image'];
  144. $levelInfo->one_brokerage = $data['one_brokerage'];
  145. $levelInfo->two_brokerage = $data['two_brokerage'];
  146. $levelInfo->discount = $data['discount'];
  147. $levelInfo->free_issue = $data['free_issue'];
  148. // $levelInfo->verific_reward = $data['verific_reward'];
  149. // $levelInfo->more_reward = $data['more_reward'];
  150. $levelInfo->equal_award = $data['equal_award'];
  151. $levelInfo->spread_brokerage = $data['spread_brokerage'];
  152. $levelInfo->status = $data['status'];
  153. $levelInfo->save();
  154. return app('json')->success(100001);
  155. }
  156. /**
  157. * 删除分销等级
  158. * @param $id
  159. * @return mixed
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function delete($id)
  165. {
  166. if (!$id) return app('json')->fail(100100);
  167. //检查分销等级数据是否存在
  168. $levelInfo = $this->services->getLevelInfo((int)$id);
  169. if ($levelInfo) {
  170. //更新数据为已删除
  171. $res = $this->services->update($id, ['is_del' => 1]);
  172. if (!$res)
  173. return app('json')->fail(100008);
  174. //删除该等级的任务为已删除
  175. /** @var AgentLevelTaskServices $agentLevelTaskServices */
  176. $agentLevelTaskServices = app()->make(AgentLevelTaskServices::class);
  177. $agentLevelTaskServices->update(['level_id' => $id], ['is_del' => 1]);
  178. }
  179. return app('json')->success(100002);
  180. }
  181. /**
  182. * 修改状态
  183. * @param int $id
  184. * @param string $status
  185. * @return mixed
  186. */
  187. public function set_status($id = 0, $status = '')
  188. {
  189. if ($status == '' || $id == 0) return app('json')->fail(100100);
  190. $this->services->update($id, ['status' => $status]);
  191. return app('json')->success(100014);
  192. }
  193. }