AgentLevelServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\order\StoreOrderServices;
  15. use app\services\user\UserAwardIntegralServices;
  16. use app\services\user\UserBrokerageServices;
  17. use app\services\user\UserExtractServices;
  18. use app\services\user\UserServices;
  19. use app\services\user\UserSpreadServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\FormBuilder as Form;
  22. use FormBuilder\Factory\Iview;
  23. use think\exception\ValidateException;
  24. use think\facade\Route as Url;
  25. /**
  26. * 分销等级
  27. * Class AgentLevelServices
  28. * @package app\services\agent
  29. * @mixin AgentLevelDao
  30. */
  31. class AgentLevelServices extends BaseServices
  32. {
  33. /**
  34. * AgentLevelServices constructor.
  35. * @param AgentLevelDao $dao
  36. */
  37. public function __construct(AgentLevelDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 获取某一个等级信息
  43. * @param int $id
  44. * @param string $field
  45. * @param array $with
  46. * @return array|\think\Model|null
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function getLevelInfo(int $id, string $field = '*', array $with = [])
  52. {
  53. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  54. }
  55. /**
  56. * 获取等级列表
  57. * @param array $where
  58. * @return array
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function getLevelList(array $where)
  64. {
  65. $where['is_del'] = 0;
  66. [$page, $limit] = $this->getPageValue();
  67. $count = $this->dao->count($where);
  68. $list = [];
  69. if ($count) {
  70. $list = $this->dao->getList($where, '*', ['task' => function ($query) {
  71. $query->field('count(*) as sum');
  72. }], $page, $limit);
  73. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  74. $store_brokerage_two = sys_config('store_brokerage_two');
  75. foreach ($list as &$item) {
  76. $item['one_brokerage_ratio'] = $this->compoteBrokerage($store_brokerage_ratio, $item['one_brokerage']);
  77. $item['two_brokerage_ratio'] = $this->compoteBrokerage($store_brokerage_two, $item['two_brokerage']);
  78. }
  79. }
  80. return compact('count', 'list');
  81. }
  82. /**
  83. * 商城获取分销员等级列表
  84. * @param int $uid
  85. * @return array
  86. */
  87. public function getUserlevelList(int $uid)
  88. {
  89. //商城分销是否开启
  90. if (!sys_config('brokerage_func_status')) {
  91. return [];
  92. }
  93. /** @var UserServices $userServices */
  94. $userServices = app()->make(UserServices::class);
  95. $user = $userServices->getUserCacheInfo($uid);
  96. if (!$user) {
  97. throw new ValidateException('没有此用户');
  98. }
  99. try {
  100. //检测升级
  101. $this->checkUserLevelFinish($uid);
  102. } catch (\Throwable $e) {
  103. }
  104. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  105. $agent_level = $user['agent_level'] ?? 0;
  106. //没等级默认最低等级
  107. if (!$agent_level) {
  108. $levelInfo = [];
  109. } else {
  110. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  111. }
  112. $sum_task = $finish_task = 0;
  113. if ($levelInfo) {
  114. /** @var AgentLevelTaskServices $levelTaskServices */
  115. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  116. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  117. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  118. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  119. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  120. }
  121. $levelInfo['sum_task'] = $sum_task;
  122. $levelInfo['finish_task'] = $finish_task;
  123. //推广订单总数
  124. /** @var StoreOrderServices $orderServices */
  125. $orderServices = app()->make(StoreOrderServices::class);
  126. $user['spread_order_count'] = $orderServices->count(['type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0, 'spread_or_uid' => $uid]);
  127. //冻结佣金和可提现金额
  128. /** @var UserBrokerageServices $userBrokerageServices */
  129. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  130. $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  131. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  132. //佣金排行
  133. $user['position_count'] = $userBrokerageServices->getUserBrokerageRank($uid, 'week');
  134. //推广人排行
  135. $startTime = strtotime('this week Monday');
  136. $endTime = time();
  137. $field = 'spread_uid,count(uid) AS count,spread_time';
  138. /** @var UserSpreadServices $userSpreadServices */
  139. $userSpreadServices = app()->make(UserSpreadServices::class);
  140. $rankList = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field);
  141. $rank = 0;
  142. foreach ($rankList as $key => $item) {
  143. if ($item['spread_uid'] == $uid) $rank = $key + 1;
  144. }
  145. $user['rank_count'] = $rank;
  146. $user['spread_count'] = $userServices->count(['spread_uid' => $uid]);
  147. /** @var UserExtractServices $extractService */
  148. $extractService = app()->make(UserExtractServices::class);
  149. $user['extract_price'] = $extractService->sum(['uid' => $uid, 'status' => 1], 'extract_price');
  150. $award_integral_service = app()->make(UserAwardIntegralServices::class);
  151. $user['award_lack'] = $award_integral_service->getLake();
  152. $user['integral_price'] = $award_integral_service->getPrice();
  153. $user['static_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
  154. $user['action_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
  155. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  156. $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  157. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  158. }
  159. /**
  160. * 检测用户是否能升级
  161. * @param int $uid
  162. * @param array $uids
  163. * @return bool
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public function checkUserLevelFinish(int $uid, array $uids = [])
  169. {
  170. //商城分销是否开启
  171. if (!sys_config('brokerage_func_status')) {
  172. return false;
  173. }
  174. /** @var UserServices $userServices */
  175. $userServices = app()->make(UserServices::class);
  176. $userInfo = $userServices->getUserCacheInfo($uid);
  177. if (!$userInfo) {
  178. return false;
  179. }
  180. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  181. if (!$list) {
  182. return false;
  183. }
  184. if (!$uids) {
  185. //获取上级uid || 开启自购返回自己uid
  186. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  187. $two_spread_uid = 0;
  188. if ($spread_uid > 0 && $one_user_info = $userServices->getUserCacheInfo($spread_uid)) {
  189. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  190. }
  191. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  192. }
  193. foreach ($uids as $uid) {
  194. if ($uid <= 0) continue;
  195. if ($uid != $userInfo['uid']) {
  196. $userInfo = $userServices->getUserCacheInfo($uid);
  197. if (!$userInfo)
  198. continue;
  199. }
  200. $now_grade = 0;
  201. if ($userInfo['agent_level']) {
  202. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  203. }
  204. foreach ($list as $levelInfo) {
  205. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  206. continue;
  207. }
  208. /** @var AgentLevelTaskServices $levelTaskServices */
  209. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  210. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  211. if (!$task_list) {
  212. continue;
  213. }
  214. foreach ($task_list as $task) {
  215. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  216. }
  217. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  218. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  219. $ids = array_column($task_list, 'id');
  220. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  221. //任务完成升这一等级
  222. if ($finish_task >= count($task_list)) {
  223. $userServices->update($uid, ['agent_level' => $levelInfo['id']]);
  224. } else {
  225. break;
  226. }
  227. }
  228. }
  229. return true;
  230. }
  231. /**
  232. * 分销等级上浮
  233. * @param int $uid
  234. * @param array $userInfo
  235. * @return array|int[]
  236. * @throws \think\db\exception\DataNotFoundException
  237. * @throws \think\db\exception\DbException
  238. * @throws \think\db\exception\ModelNotFoundException
  239. */
  240. public function getAgentLevelBrokerage(int $uid, $userInfo = [])
  241. {
  242. $one_brokerage_up = $two_brokerage_up = $spread_uid = $spread_two_uid = 0;
  243. $data = [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid];
  244. if (!$uid) {
  245. return $data;
  246. }
  247. //商城分销是否开启
  248. if (!sys_config('brokerage_func_status')) {
  249. return $data;
  250. }
  251. /** @var UserServices $userServices */
  252. $userServices = app()->make(UserServices::class);
  253. if (!$userInfo) {
  254. $userInfo = $userServices->getUserCacheInfo($uid);
  255. }
  256. if (!$userInfo) {
  257. return $data;
  258. }
  259. //获取上级uid || 开启自购返回自己uid
  260. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  261. $one_agent_level = 0;
  262. $two_agent_level = 0;;
  263. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  264. $one_agent_level = $one_user_info['agent_level'] ?? 0;
  265. $spread_two_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  266. if ($spread_two_uid > 0 && $two_user_info = $userServices->getUserInfo($spread_two_uid)) {
  267. $two_agent_level = $two_user_info['agent_level'] ?? 0;
  268. }
  269. }
  270. $one_brokerage_up = $one_agent_level ? ($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0) : 0;
  271. $two_brokerage_up = $two_agent_level ? ($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0) : 0;
  272. return [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid];
  273. }
  274. /**
  275. * 计算一二级返佣比率上浮
  276. * @param $ratio
  277. * @param $brokerage
  278. * @return int|string
  279. */
  280. public function compoteBrokerage($ratio, $brokerage)
  281. {
  282. if (!$ratio) {
  283. return 0;
  284. }
  285. $brokerage = bcdiv((string)$brokerage, '100', 4);
  286. if ($brokerage) {
  287. return bcmul((string)$ratio, bcadd('1', (string)$brokerage, 4), 2);
  288. }
  289. return $brokerage;
  290. }
  291. /**
  292. * 添加等级表单
  293. * @param int $id
  294. * @return array
  295. * @throws \FormBuilder\Exception\FormBuilderException
  296. */
  297. public function createForm()
  298. {
  299. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  300. $store_brokerage_two = sys_config('store_brokerage_two');
  301. $field[] = Form::input('name', '等级名称')->col(24);
  302. $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
  303. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择背景图'));
  304. $field[] = Form::number('one_brokerage', '一级上浮', 0)->info('在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:' . $store_brokerage_ratio . '%,例如上浮10%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_ratio, 10) . '%')->min(0)->max(1000);
  305. $field[] = Form::number('two_brokerage', '二级上浮', 0)->info('在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:' . $store_brokerage_two . '%,例如上浮10%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_two, 10) . '%')->min(0)->max(1000);
  306. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  307. return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
  308. }
  309. /**
  310. * 获取修改等级表单
  311. * @param int $id
  312. * @return array
  313. * @throws \FormBuilder\Exception\FormBuilderException
  314. */
  315. public function editForm(int $id)
  316. {
  317. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  318. $store_brokerage_two = sys_config('store_brokerage_two');
  319. $levelInfo = $this->getLevelInfo($id);
  320. if (!$levelInfo)
  321. throw new AdminException('数据不存在');
  322. $field = [];
  323. $field[] = Form::hidden('id', $id);
  324. $field[] = Form::input('name', '等级名称', $levelInfo['name'])->col(24);
  325. $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
  326. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')), $levelInfo['image'])->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择背景图'));
  327. $field[] = Form::number('one_brokerage', '一级上浮', $levelInfo['one_brokerage'])->info('在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:' . $store_brokerage_ratio . '%,上浮' . $levelInfo['one_brokerage'] . '%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_ratio, $levelInfo['one_brokerage']) . '%')->min(0)->max(1000);
  328. $field[] = Form::number('two_brokerage', '二级上浮', $levelInfo['two_brokerage'])->info('在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:' . $store_brokerage_two . '%,上浮' . $levelInfo['two_brokerage'] . '%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_two, $levelInfo['two_brokerage']) . '%')->min(0)->max(1000);
  329. $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  330. return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
  331. }
  332. /**
  333. * 赠送分销等级表单
  334. * @param int $uid
  335. * @return array
  336. * @throws \FormBuilder\Exception\FormBuilderException
  337. * @throws \think\db\exception\DataNotFoundException
  338. * @throws \think\db\exception\DbException
  339. * @throws \think\db\exception\ModelNotFoundException
  340. */
  341. public function levelForm(int $uid)
  342. {
  343. /** @var UserServices $userServices */
  344. $userServices = app()->make(UserServices::class);
  345. $userInfo = $userServices->getUserInfo($uid);
  346. if (!$userInfo) {
  347. throw new AdminException('分销员不存在');
  348. }
  349. $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  350. $setOptionLabel = function () use ($levelList) {
  351. $menus = [];
  352. foreach ($levelList as $level) {
  353. $menus[] = ['value' => $level['id'], 'label' => $level['name']];
  354. }
  355. return $menus;
  356. };
  357. $field[] = Form::hidden('uid', $uid);
  358. $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] ?? 0)->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  359. return create_form('赠送分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
  360. }
  361. /**
  362. * 赠送分销等级
  363. * @param int $uid
  364. * @param int $id
  365. * @return bool
  366. * @throws \think\db\exception\DataNotFoundException
  367. * @throws \think\db\exception\DbException
  368. * @throws \think\db\exception\ModelNotFoundException
  369. */
  370. public function givelevel(int $uid, int $id)
  371. {
  372. /** @var UserServices $userServices */
  373. $userServices = app()->make(UserServices::class);
  374. $userInfo = $userServices->getUserInfo($uid);
  375. if (!$userInfo) {
  376. throw new AdminException('分销员不存在');
  377. }
  378. $levelInfo = $this->getLevelInfo($id);
  379. if (!$levelInfo) {
  380. throw new AdminException('分销等级不存在');
  381. }
  382. if ($userServices->update($uid, ['agent_level' => $id]) === false) {
  383. throw new AdminException('赠送失败');
  384. }
  385. return true;
  386. }
  387. }