AgentLevelServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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\services\agent;
  12. use app\dao\agent\AgentLevelDao;
  13. use app\services\BaseServices;
  14. use app\services\user\UserServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\services\FormBuilder as Form;
  18. use think\facade\Route as Url;
  19. /**
  20. * Class AgentLevelServices
  21. * @package app\services\agent
  22. */
  23. class AgentLevelServices extends BaseServices
  24. {
  25. /**
  26. * AgentLevelServices constructor.
  27. * @param AgentLevelDao $dao
  28. */
  29. public function __construct(AgentLevelDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取某一个等级信息
  35. * @param int $id
  36. * @param string $field
  37. * @param array $with
  38. * @return array|\think\Model|null
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getLevelInfo(int $id, string $field = '*', array $with = [])
  44. {
  45. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  46. }
  47. /**
  48. * 获取等级列表
  49. * @param array $where
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getLevelList(array $where)
  56. {
  57. $where['is_del'] = 0;
  58. [$page, $limit] = $this->getPageValue();
  59. $list = $this->dao->getList($where, '*', ['task' => function ($query) {
  60. $query->field('count(*) as sum');
  61. }], $page, $limit);
  62. $count = $this->dao->count($where);
  63. foreach ($list as &$item) {
  64. $item['one_brokerage_ratio'] = $item['one_brokerage_percent'];
  65. $item['two_brokerage_ratio'] = $item['two_brokerage_percent'];
  66. if (strpos($item['image'], '/statics/system_images/') !== false) {
  67. $item['image'] = set_file_url($item['image']);
  68. }
  69. }
  70. return compact('count', 'list');
  71. }
  72. /**
  73. * 商城获取分销员等级列表
  74. * @param int $uid
  75. * @return array
  76. */
  77. public function getUserlevelList(int $uid)
  78. {
  79. //商城分销是否开启
  80. if (!sys_config('brokerage_func_status')) {
  81. return [];
  82. }
  83. /** @var UserServices $userServices */
  84. $userServices = app()->make(UserServices::class);
  85. $user = $userServices->getUserInfo($uid);
  86. if (!$user) {
  87. throw new ApiException(410032);
  88. }
  89. //检测升级
  90. $this->checkUserLevelFinish($uid);
  91. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  92. foreach ($list as &$item) {
  93. $item['image'] = set_file_url($item['image']);
  94. }
  95. $agent_level = $user['agent_level'] ?? 0;
  96. //没等级默认最低等级
  97. if (!$agent_level) {
  98. $levelInfo = $list[0] ?? [];
  99. $levelInfo['grade'] = -1;
  100. } else {
  101. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  102. }
  103. $sum_task = $finish_task = 0;
  104. if ($levelInfo) {
  105. /** @var AgentLevelTaskServices $levelTaskServices */
  106. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  107. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  108. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  109. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  110. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  111. }
  112. $levelInfo['sum_task'] = $sum_task;
  113. $levelInfo['finish_task'] = $finish_task;
  114. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  115. }
  116. /**
  117. * 获取下一等级
  118. * @param int $level_id
  119. * @return array|\think\Model|null
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function getNextLevelInfo(int $level_id = 0)
  125. {
  126. $grade = 0;
  127. if ($level_id) {
  128. $grade = $this->dao->value(['id' => $level_id, 'is_del' => 0, 'status' => 1], 'grade') ?: 0;
  129. }
  130. return $this->dao->getOne([['grade', '>', $grade], ['is_del', '=', 0], ['status', '=', 1]]);
  131. }
  132. /**
  133. * 检测用户是否能升级
  134. * @param int $uid
  135. * @param array $uids
  136. * @return bool
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. public function checkUserLevelFinish(int $uid, array $uids = [])
  142. {
  143. //商城分销是否开启
  144. if (!sys_config('brokerage_func_status')) {
  145. return false;
  146. }
  147. /** @var UserServices $userServices */
  148. $userServices = app()->make(UserServices::class);
  149. $userInfo = $userServices->getUserInfo($uid);
  150. if (!$userInfo) {
  151. return false;
  152. }
  153. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  154. if (!$list) {
  155. return false;
  156. }
  157. if (!$uids) {
  158. //获取上级uid || 开启自购返回自己uid
  159. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  160. $two_spread_uid = 0;
  161. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  162. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  163. }
  164. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  165. }
  166. foreach ($uids as $uid) {
  167. if ($uid <= 0) continue;
  168. if ($uid != $userInfo['uid']) {
  169. $userInfo = $userServices->getUserInfo($uid);
  170. }
  171. $now_grade = 0;
  172. if ($userInfo['agent_level']) {
  173. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  174. }
  175. foreach ($list as $levelInfo) {
  176. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  177. continue;
  178. }
  179. /** @var AgentLevelTaskServices $levelTaskServices */
  180. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  181. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  182. if (!$task_list) {
  183. continue;
  184. }
  185. foreach ($task_list as $task) {
  186. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  187. }
  188. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  189. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  190. $ids = array_column($task_list, 'id');
  191. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  192. //任务完成升这一等级
  193. if ($finish_task >= $levelInfo['task_num']) {
  194. $userServices->update($uid, ['agent_level' => $levelInfo['id']]);
  195. } else {
  196. break;
  197. }
  198. }
  199. }
  200. return true;
  201. }
  202. /**
  203. * 分销等级上浮
  204. * @param $storeBrokerageRatio
  205. * @param $storeBrokerageTwo
  206. * @param $spread_one_uid
  207. * @param $spread_two_uid
  208. * @return array|int[]
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\DbException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. */
  213. public function getAgentLevelBrokerage($storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid)
  214. {
  215. /** @var UserServices $userServices */
  216. $userServices = app()->make(UserServices::class);
  217. $one_agent_level = $userServices->value(['uid' => $spread_one_uid], 'agent_level') ?? 0;
  218. $two_agent_level = $userServices->value(['uid' => $spread_two_uid], 'agent_level') ?? 0;
  219. if ($one_agent_level) {
  220. $oneLevelInfo = $this->getLevelInfo($one_agent_level);
  221. if ($oneLevelInfo && $oneLevelInfo['status'] == 1) {
  222. $storeBrokerageRatio = $oneLevelInfo['one_brokerage_percent'];
  223. }
  224. }
  225. if ($two_agent_level) {
  226. $twoLevelInfo = $this->getLevelInfo($two_agent_level);
  227. if ($twoLevelInfo && $twoLevelInfo['status'] == 1) {
  228. $storeBrokerageTwo = $twoLevelInfo['two_brokerage_percent'];
  229. }
  230. }
  231. return [$storeBrokerageRatio, $storeBrokerageTwo];
  232. }
  233. /**
  234. * 添加等级表单
  235. * @param int $id
  236. * @return array
  237. * @throws \FormBuilder\Exception\FormBuilderException
  238. */
  239. public function createForm()
  240. {
  241. $field[] = Form::input('name', '等级名称')->maxlength(8)->col(24);
  242. $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
  243. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'image')))->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  244. $field[] = Form::number('one_brokerage_percent', '一级佣金比例', 0)->appendRule('suffix', [
  245. 'type' => 'div',
  246. 'class' => 'tips-info',
  247. 'domProps' => ['innerHTML' => '到达该等级之后,一级分佣按照此比例计算佣金']
  248. ])->max(100)->precision(2);
  249. $field[] = Form::number('two_brokerage_percent', '二级佣金比例', 0)->appendRule('suffix', [
  250. 'type' => 'div',
  251. 'class' => 'tips-info',
  252. 'domProps' => ['innerHTML' => '到达该等级之后,二级分佣按照此比例计算佣金']
  253. ])->min(0)->max(100)->precision(2);
  254. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  255. return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
  256. }
  257. /**
  258. * 获取修改等级表单
  259. * @param int $id
  260. * @return array
  261. * @throws \FormBuilder\Exception\FormBuilderException
  262. */
  263. public function editForm(int $id)
  264. {
  265. $levelInfo = $this->getLevelInfo($id);
  266. if (!$levelInfo)
  267. throw new AdminException(100026);
  268. $field = [];
  269. $field[] = Form::hidden('id', $id);
  270. $field[] = Form::input('name', '等级名称', $levelInfo['name'])->maxlength(8)->col(24);
  271. $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
  272. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'image')), $levelInfo['image'])->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  273. $field[] = Form::number('one_brokerage_percent', '一级佣金比例', $levelInfo['one_brokerage_percent'])->appendRule('suffix', [
  274. 'type' => 'div',
  275. 'class' => 'tips-info',
  276. 'domProps' => ['innerHTML' => '到达该等级之后,一级分佣按照此比例计算佣金']
  277. ])->max(100)->precision(2);
  278. $field[] = Form::number('two_brokerage_percent', '二级佣金比例', $levelInfo['two_brokerage_percent'])->appendRule('suffix', [
  279. 'type' => 'div',
  280. 'class' => 'tips-info',
  281. 'domProps' => ['innerHTML' => '到达该等级之后,二级分佣按照此比例计算佣金']
  282. ])->min(0)->max(100)->precision(2);
  283. $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  284. return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
  285. }
  286. /**
  287. * 赠送分销等级表单
  288. * @param int $uid
  289. * @return array
  290. * @throws \FormBuilder\Exception\FormBuilderException
  291. * @throws \think\db\exception\DataNotFoundException
  292. * @throws \think\db\exception\DbException
  293. * @throws \think\db\exception\ModelNotFoundException
  294. */
  295. public function levelForm(int $uid)
  296. {
  297. /** @var UserServices $userServices */
  298. $userServices = app()->make(UserServices::class);
  299. $userInfo = $userServices->getUserInfo($uid);
  300. if (!$userInfo) {
  301. throw new AdminException(400214);
  302. }
  303. $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1], '*', [], 0, 0, $userInfo['agent_level']);
  304. $setOptionLabel = function () use ($levelList) {
  305. $menus = [];
  306. foreach ($levelList as $level) {
  307. $menus[] = ['value' => $level['id'], 'label' => $level['name']];
  308. }
  309. return $menus;
  310. };
  311. $field[] = Form::hidden('uid', $uid);
  312. $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] != 0 ? $userInfo['agent_level'] : '')->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  313. return create_form('修改分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
  314. }
  315. /**
  316. * 赠送分销等级
  317. * @param int $uid
  318. * @param int $id
  319. * @return bool
  320. * @throws \think\db\exception\DataNotFoundException
  321. * @throws \think\db\exception\DbException
  322. * @throws \think\db\exception\ModelNotFoundException
  323. */
  324. public function givelevel(int $uid, int $id)
  325. {
  326. /** @var UserServices $userServices */
  327. $userServices = app()->make(UserServices::class);
  328. $userInfo = $userServices->getUserInfo($uid, 'uid');
  329. if (!$userInfo) {
  330. throw new AdminException(400214);
  331. }
  332. $levelInfo = $this->getLevelInfo($id, 'id');
  333. if (!$levelInfo) {
  334. throw new AdminException(400442);
  335. }
  336. if ($userServices->update($uid, ['agent_level' => $id]) === false) {
  337. throw new AdminException(400219);
  338. }
  339. return true;
  340. }
  341. /**
  342. * 获取指定分销等级的任务数量表单
  343. * @param int $id 分销等级ID
  344. * @return array|string
  345. */
  346. public function getTaskNumForm($id)
  347. {
  348. // 获取指定分销等级的信息
  349. $levelInfo = $this->getLevelInfo($id);
  350. // 构建任务数量输入框
  351. $field[] = Form::input('task_num', '完成任务数量', $levelInfo['task_num'])->maxlength(8)->col(24)->info('默认全部完成升级,可设置升级任务数量');
  352. // 创建表单并返回HTML字符串
  353. return create_form('设置完成任务数量', $field, Url::buildUrl('/agent/set_task_num/' . $id), 'post');
  354. }
  355. /**
  356. * 设置指定分销等级的任务数量
  357. * @param int $id 分销等级ID
  358. * @param array $data 包含任务数量的数组
  359. * @return bool 返回true表示设置成功
  360. * @throws AdminException 如果分销等级不存在或任务数量为空或任务数量大于已有任务数量,则抛出异常
  361. */
  362. public function setTaskNum($id, $data)
  363. {
  364. // 判断分销等级是否存在
  365. if (!$id) throw new AdminException('分销等级不存在');
  366. // 判断任务数量是否为空
  367. if (!$data['task_num']) throw new AdminException('请输入任务数量');
  368. // 获取当前分销等级已有的任务数量
  369. $count = app()->make(AgentLevelTaskServices::class)->count(['level_id' => $id, 'is_del' => 0, 'status' => 1]);
  370. // 判断任务数量是否大于已有任务数量
  371. if ($data['task_num'] > $count) throw new AdminException('任务数量不能大于已有任务数量');
  372. // 更新分销等级的任务数量
  373. $this->dao->update($id, ['task_num' => $data['task_num']]);
  374. // 返回true表示设置成功
  375. return true;
  376. }
  377. /**
  378. * 获取分销等级数组
  379. * @return array
  380. * @author wuhaotian
  381. * @email 442384644@qq.com
  382. * @date 2025/6/16
  383. */
  384. public function getAgentLevelArr()
  385. {
  386. return $this->dao->getColumn(['status'=>1,'is_del'=>0], 'name', 'grade');
  387. }
  388. }