AgentLevelServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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'] = bcdiv(bcmul((string)sys_config('store_brokerage_ratio'), bcadd('100', (string)$item['one_brokerage'], 2), 2), '100', 2);
  65. // $item['two_brokerage_ratio'] = bcdiv(bcmul((string)sys_config('store_brokerage_two'), bcadd('100', (string)$item['two_brokerage'], 2), 2), '100', 2);
  66. }
  67. return compact('count', 'list');
  68. }
  69. /**
  70. * 商城获取分销员等级列表
  71. * @param int $uid
  72. * @return array
  73. */
  74. public function getUserlevelList(int $uid)
  75. {
  76. //商城分销是否开启
  77. if (!sys_config('brokerage_func_status')) {
  78. return [];
  79. }
  80. /** @var UserServices $userServices */
  81. $userServices = app()->make(UserServices::class);
  82. $user = $userServices->getUserInfo($uid);
  83. if (!$user) {
  84. throw new ApiException(410032);
  85. }
  86. //检测升级
  87. $this->checkUserLevelFinish($uid);
  88. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  89. foreach ($list as &$item) {
  90. $item['image'] = set_file_url($item['image']);
  91. }
  92. $agent_level = $user['agent_level'] ?? 0;
  93. //没等级默认最低等级
  94. if (!$agent_level) {
  95. $levelInfo = $list[0] ?? [];
  96. $levelInfo['grade'] = -1;
  97. } else {
  98. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  99. }
  100. $sum_task = $finish_task = 0;
  101. if ($levelInfo) {
  102. /** @var AgentLevelTaskServices $levelTaskServices */
  103. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  104. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  105. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  106. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  107. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  108. }
  109. $levelInfo['sum_task'] = $sum_task;
  110. $levelInfo['finish_task'] = $finish_task;
  111. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  112. }
  113. /**
  114. * 获取下一等级
  115. * @param int $level_id
  116. * @return array|\think\Model|null
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getNextLevelInfo(int $level_id = 0)
  122. {
  123. $grade = 0;
  124. if ($level_id) {
  125. $grade = $this->dao->value(['id' => $level_id, 'is_del' => 0, 'status' => 1], 'grade') ?: 0;
  126. }
  127. return $this->dao->getOne([['grade', '>', $grade], ['is_del', '=', 0], ['status', '=', 1]]);
  128. }
  129. /**
  130. * 检测用户是否能升级
  131. * @param int $uid
  132. * @param array $uids
  133. * @return bool
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function checkUserLevelFinish(int $uid, array $uids = [])
  139. {
  140. //商城分销是否开启
  141. if (!sys_config('brokerage_func_status')) {
  142. return false;
  143. }
  144. /** @var UserServices $userServices */
  145. $userServices = app()->make(UserServices::class);
  146. $userInfo = $userServices->getUserInfo($uid);
  147. if (!$userInfo) {
  148. return false;
  149. }
  150. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  151. if (!$list) {
  152. return false;
  153. }
  154. if (!$uids) {
  155. //获取上级uid || 开启自购返回自己uid
  156. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  157. $two_spread_uid = 0;
  158. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  159. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  160. }
  161. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  162. }
  163. foreach ($uids as $uid) {
  164. if ($uid <= 0) continue;
  165. if ($uid != $userInfo['uid']) {
  166. $userInfo = $userServices->getUserInfo($uid);
  167. }
  168. $now_grade = 0;
  169. if ($userInfo['agent_level']) {
  170. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  171. }
  172. foreach ($list as $levelInfo) {
  173. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  174. continue;
  175. }
  176. /** @var AgentLevelTaskServices $levelTaskServices */
  177. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  178. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  179. if (!$task_list) {
  180. continue;
  181. }
  182. foreach ($task_list as $task) {
  183. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  184. }
  185. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  186. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  187. $ids = array_column($task_list, 'id');
  188. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  189. //任务完成升这一等级
  190. if ($finish_task >= count($task_list)) {
  191. $userServices->update($uid, ['agent_level' => $levelInfo['grade']]);
  192. } else {
  193. break;
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. /**
  200. * 分销等级上浮
  201. * @param int $uid
  202. * @param array $userInfo
  203. * @return array|int[]
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function getAgentLevelBrokerage(int $uid, $userInfo = [])
  209. {
  210. $one_brokerage_up = $two_brokerage_up = $spread_one_uid = $spread_two_uid = 0;
  211. if (!$uid) {
  212. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  213. }
  214. //商城分销是否开启
  215. if (!sys_config('brokerage_func_status')) {
  216. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  217. }
  218. /** @var UserServices $userServices */
  219. $userServices = app()->make(UserServices::class);
  220. if (!$userInfo) {
  221. $userInfo = $userServices->getUserInfo($uid);
  222. }
  223. if (!$userInfo) {
  224. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  225. }
  226. //获取上级uid || 开启自购返回自己uid
  227. $spread_one_uid = $userServices->getSpreadUid($uid, $userInfo);
  228. $one_agent_level = 0;
  229. $two_agent_level = 0;
  230. $spread_two_uid = 0;
  231. if ($spread_one_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_one_uid)) {
  232. $one_agent_level = $one_user_info['agent_level'] ?? 0;
  233. $spread_two_uid = $userServices->getSpreadUid($spread_one_uid, $one_user_info, false);
  234. if ($spread_two_uid > 0 && $two_user_info = $userServices->getUserInfo($spread_two_uid)) {
  235. $two_agent_level = $two_user_info['agent_level'] ?? 0;
  236. }
  237. }
  238. $one_brokerage_up = $one_agent_level ? ($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0) : 0;
  239. $two_brokerage_up = $two_agent_level ? ($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0) : 0;
  240. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  241. }
  242. /**
  243. * 添加等级表单
  244. * @param int $id
  245. * @return array
  246. * @throws \FormBuilder\Exception\FormBuilderException
  247. */
  248. public function createForm()
  249. {
  250. $field[] = Form::input('name', '等级名称')->maxlength(8)->col(24);
  251. $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
  252. $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]);
  253. // $field[] = Form::number('one_brokerage', '一级上浮', 0)->appendRule('suffix', [
  254. // 'type' => 'div',
  255. // 'class' => 'tips-info',
  256. // 'domProps' => ['innerHTML' => '在分销一级佣金基础上浮(0-100之间整数)百分比,目前一级返佣比率:10%,上浮5%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = 10.50%']
  257. // ])->max(1000)->precision(0);
  258. $field[] = Form::number('discount', '等级折扣', 0)->appendRule('suffix', [
  259. 'type' => 'div',
  260. 'class' => 'tips-info',
  261. 'domProps' => ['innerHTML' => '购买商品享受折扣(0-100之间整数)百分比']
  262. ])->max(100)->precision(0);
  263. $field[] = Form::number('free_issue', '优惠券id', 0)->appendRule('suffix', [
  264. 'type' => 'div',
  265. 'class' => 'tips-info',
  266. 'domProps' => ['innerHTML' => '下级购买礼包时赠送的优惠券ID']
  267. ])->precision(0);
  268. // $field[] = Form::number('verific_reward', '两线核销奖', 0)->appendRule('suffix', [
  269. // 'type' => 'div',
  270. // 'class' => 'tips-info',
  271. // 'domProps' => ['innerHTML' => '两条线各核销200单以上的奖励金额']
  272. // ])->precision(2);
  273. // $field[] = Form::number('more_reward', '三线核销奖', 0)->appendRule('suffix', [
  274. // 'type' => 'div',
  275. // 'class' => 'tips-info',
  276. // 'domProps' => ['innerHTML' => '三条线及以上各核销200单以上的奖励金额']
  277. // ])->precision(2);
  278. $field[] = Form::number('equal_award', '平级奖', 0)->appendRule('suffix', [
  279. 'type' => 'div',
  280. 'class' => 'tips-info',
  281. 'domProps' => ['innerHTML' => '平级奖励金额']
  282. ])->precision(2);
  283. $field[] = Form::number('spread_brokerage', '推荐下单佣金比例', 0)->appendRule('suffix', [
  284. 'type' => 'div',
  285. 'class' => 'tips-info',
  286. 'domProps' => ['innerHTML' => '推荐下单奖励佣金比例']
  287. ])->precision(2);
  288. // $field[] = Form::number('two_brokerage', '二级上浮', 0)->appendRule('suffix', [
  289. // 'type' => 'div',
  290. // 'class' => 'tips-info',
  291. // 'domProps' => ['innerHTML' => '在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:10%,上浮2%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = 5.10%']
  292. // ])->min(0)->max(1000)->precision(0);
  293. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  294. return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
  295. }
  296. /**
  297. * 获取修改等级表单
  298. * @param int $id
  299. * @return array
  300. * @throws \FormBuilder\Exception\FormBuilderException
  301. */
  302. public function editForm(int $id)
  303. {
  304. $levelInfo = $this->getLevelInfo($id);
  305. if (!$levelInfo)
  306. throw new AdminException(100026);
  307. $field = [];
  308. $field[] = Form::hidden('id', $id);
  309. $field[] = Form::input('name', '等级名称', $levelInfo['name'])->maxlength(8)->col(24);
  310. $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
  311. $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]);
  312. // $field[] = Form::number('one_brokerage', '一级上浮', $levelInfo['one_brokerage'])->appendRule('suffix', [
  313. // 'type' => 'div',
  314. // 'class' => 'tips-info',
  315. // 'domProps' => ['innerHTML' => '在分销一级佣金基础上浮(0-100之间整数)百分比,目前一级返佣比率:10%,上浮5%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = 10.50%']
  316. // ])->min(0)->max(1000)->precision(0);
  317. $field[] = Form::number('discount', '等级折扣', $levelInfo['discount'])->appendRule('suffix', [
  318. 'type' => 'div',
  319. 'class' => 'tips-info',
  320. 'domProps' => ['innerHTML' => '购买商品享受折扣(0-100之间整数)百分比']
  321. ])->max(100)->precision(0);
  322. $field[] = Form::number('free_issue', '优惠券id', $levelInfo['free_issue'])->appendRule('suffix', [
  323. 'type' => 'div',
  324. 'class' => 'tips-info',
  325. 'domProps' => ['innerHTML' => '下级购买礼包时赠送的优惠券ID']
  326. ])->precision(0);
  327. // $field[] = Form::number('verific_reward', '两线核销奖', $levelInfo['verific_reward'])->appendRule('suffix', [
  328. // 'type' => 'div',
  329. // 'class' => 'tips-info',
  330. // 'domProps' => ['innerHTML' => '两条线各核销200单以上的奖励金额']
  331. // ])->precision(2);
  332. // $field[] = Form::number('more_reward', '三线核销奖', $levelInfo['more_reward'])->appendRule('suffix', [
  333. // 'type' => 'div',
  334. // 'class' => 'tips-info',
  335. // 'domProps' => ['innerHTML' => '三条线及以上各核销200单以上的奖励金额']
  336. // ])->precision(2);
  337. $field[] = Form::number('equal_award', '平级奖', $levelInfo['equal_award'])->appendRule('suffix', [
  338. 'type' => 'div',
  339. 'class' => 'tips-info',
  340. 'domProps' => ['innerHTML' => '平级奖励金额']
  341. ])->precision(2);
  342. $field[] = Form::number('spread_brokerage', '推荐下单佣金比例', $levelInfo['spread_brokerage'])->appendRule('suffix', [
  343. 'type' => 'div',
  344. 'class' => 'tips-info',
  345. 'domProps' => ['innerHTML' => '推荐下单奖励佣金比例']
  346. ])->precision(2);
  347. // $field[] = Form::number('two_brokerage', '二级上浮', $levelInfo['two_brokerage'])->appendRule('suffix', [
  348. // 'type' => 'div',
  349. // 'class' => 'tips-info',
  350. // 'domProps' => ['innerHTML' => '在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:10%,上浮2%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = 5.10%']
  351. // ])->min(0)->max(1000)->precision(0);
  352. $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  353. return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
  354. }
  355. /**
  356. * 赠送分销等级表单
  357. * @param int $uid
  358. * @return array
  359. * @throws \FormBuilder\Exception\FormBuilderException
  360. * @throws \think\db\exception\DataNotFoundException
  361. * @throws \think\db\exception\DbException
  362. * @throws \think\db\exception\ModelNotFoundException
  363. */
  364. public function levelForm(int $uid)
  365. {
  366. /** @var UserServices $userServices */
  367. $userServices = app()->make(UserServices::class);
  368. $userInfo = $userServices->getUserInfo($uid);
  369. if (!$userInfo) {
  370. throw new AdminException(400214);
  371. }
  372. $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1], '*', [], 0, 0, $userInfo['agent_level']);
  373. $setOptionLabel = function () use ($levelList) {
  374. $menus = [];
  375. foreach ($levelList as $level) {
  376. $menus[] = ['value' => $level['id'], 'label' => $level['name']];
  377. }
  378. return $menus;
  379. };
  380. $field[] = Form::hidden('uid', $uid);
  381. $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] != 0 ? $userInfo['agent_level'] : '')->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  382. return create_form('修改分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
  383. }
  384. /**
  385. * 赠送分销等级
  386. * @param int $uid
  387. * @param int $id
  388. * @return bool
  389. * @throws \think\db\exception\DataNotFoundException
  390. * @throws \think\db\exception\DbException
  391. * @throws \think\db\exception\ModelNotFoundException
  392. */
  393. public function givelevel(int $uid, int $id)
  394. {
  395. /** @var UserServices $userServices */
  396. $userServices = app()->make(UserServices::class);
  397. $userInfo = $userServices->getUserInfo($uid, 'uid');
  398. if (!$userInfo) {
  399. throw new AdminException(400214);
  400. }
  401. $levelInfo = $this->getLevelInfo($id, 'id');
  402. if (!$levelInfo) {
  403. throw new AdminException(400442);
  404. }
  405. if ($userServices->update($uid, ['agent_level' => $id]) === false) {
  406. throw new AdminException(400219);
  407. }
  408. return true;
  409. }
  410. }