123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\agent;
- use app\dao\agent\AgentLevelDao;
- use app\services\BaseServices;
- use app\services\order\StoreOrderServices;
- use app\services\user\AwardIntegralServices;
- use app\services\user\UserAwardIntegralServices;
- use app\services\user\UserBrokerageServices;
- use app\services\user\UserExtractServices;
- use app\services\user\UserServices;
- use app\services\user\UserSpreadServices;
- use crmeb\exceptions\AdminException;
- use crmeb\services\FormBuilder as Form;
- use FormBuilder\Factory\Iview;
- use think\exception\ValidateException;
- use think\facade\Route as Url;
- /**
- * 分销等级
- * Class AgentLevelServices
- * @package app\services\agent
- * @mixin AgentLevelDao
- */
- class AgentLevelServices extends BaseServices
- {
- /**
- * AgentLevelServices constructor.
- * @param AgentLevelDao $dao
- */
- public function __construct(AgentLevelDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取某一个等级信息
- * @param int $id
- * @param string $field
- * @param array $with
- * @return array|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getLevelInfo(int $id, string $field = '*', array $with = [])
- {
- return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
- }
- /**
- * 获取等级列表
- * @param array $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getLevelList(array $where)
- {
- $where['is_del'] = 0;
- [$page, $limit] = $this->getPageValue();
- $count = $this->dao->count($where);
- $list = [];
- if ($count) {
- $list = $this->dao->getList($where, '*', ['task' => function ($query) {
- $query->field('count(*) as sum');
- }], $page, $limit);
- $store_brokerage_ratio = sys_config('store_brokerage_ratio');
- $store_brokerage_two = sys_config('store_brokerage_two');
- foreach ($list as &$item) {
- $item['one_brokerage_ratio'] = $this->compoteBrokerage($store_brokerage_ratio, $item['one_brokerage']);
- $item['two_brokerage_ratio'] = $this->compoteBrokerage($store_brokerage_two, $item['two_brokerage']);
- }
- }
- return compact('count', 'list');
- }
- /**
- * 商城获取分销员等级列表
- * @param int $uid
- * @return array
- */
- public function getUserlevelList(int $uid)
- {
- //商城分销是否开启
- if (!sys_config('brokerage_func_status')) {
- return [];
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserCacheInfo($uid);
- if (!$user) {
- throw new ValidateException('没有此用户');
- }
- try {
- //检测升级
- $this->checkUserLevelFinish($uid);
- } catch (\Throwable $e) {
- }
- $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
- $agent_level = $user['agent_level'] ?? 0;
- //没等级默认最低等级
- if (!$agent_level) {
- $levelInfo = [];
- } else {
- $levelInfo = $this->getLevelInfo($agent_level) ?: [];
- }
- $sum_task = $finish_task = 0;
- if ($levelInfo) {
- /** @var AgentLevelTaskServices $levelTaskServices */
- $levelTaskServices = app()->make(AgentLevelTaskServices::class);
- $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
- /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
- $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
- $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
- }
- $levelInfo['sum_task'] = $sum_task;
- $levelInfo['finish_task'] = $finish_task;
- //推广订单总数
- /** @var StoreOrderServices $orderServices */
- $orderServices = app()->make(StoreOrderServices::class);
- $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]);
- //冻结佣金和可提现金额
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
- $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
- //佣金排行
- $user['position_count'] = $userBrokerageServices->getUserBrokerageRank($uid, 'week');
- //推广人排行
- $startTime = strtotime('this week Monday');
- $endTime = time();
- $field = 'spread_uid,count(uid) AS count,spread_time';
- /** @var UserSpreadServices $userSpreadServices */
- $userSpreadServices = app()->make(UserSpreadServices::class);
- $rankList = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field);
- $rank = 0;
- foreach ($rankList as $key => $item) {
- if ($item['spread_uid'] == $uid) $rank = $key + 1;
- }
- $user['rank_count'] = $rank;
- $user['spread_count'] = $userServices->count(['spread_uid' => $uid]);
- /** @var UserExtractServices $extractService */
- $extractService = app()->make(UserExtractServices::class);
- $user['extract_price'] = $extractService->sum(['uid' => $uid, 'status' => 1], 'extract_price');
- $award_integral_service = app()->make(AwardIntegralServices::class);
- // $user['award_lack'] = $award_integral_service->getLake();
- // $user['integral_price'] = $award_integral_service->getPrice();
- // $user['static_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
- // $user['action_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
- $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
- $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
- return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
- }
- /**
- * 检测用户是否能升级
- * @param int $uid
- * @param array $uids
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function checkUserLevelFinish(int $uid, array $uids = [])
- {
- //商城分销是否开启
- if (!sys_config('brokerage_func_status')) {
- return false;
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userInfo = $userServices->getUserCacheInfo($uid);
- if (!$userInfo) {
- return false;
- }
- $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
- if (!$list) {
- return false;
- }
- if (!$uids) {
- //获取上级uid || 开启自购返回自己uid
- $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
- $two_spread_uid = 0;
- if ($spread_uid > 0 && $one_user_info = $userServices->getUserCacheInfo($spread_uid)) {
- $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
- }
- $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
- }
- foreach ($uids as $uid) {
- if ($uid <= 0) continue;
- if ($uid != $userInfo['uid']) {
- $userInfo = $userServices->getUserCacheInfo($uid);
- if (!$userInfo)
- continue;
- }
- $now_grade = 0;
- if ($userInfo['agent_level']) {
- $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
- }
- foreach ($list as $levelInfo) {
- if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
- continue;
- }
- /** @var AgentLevelTaskServices $levelTaskServices */
- $levelTaskServices = app()->make(AgentLevelTaskServices::class);
- $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
- if (!$task_list) {
- continue;
- }
- foreach ($task_list as $task) {
- $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
- }
- /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
- $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
- $ids = array_column($task_list, 'id');
- $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
- //任务完成升这一等级
- if ($finish_task >= count($task_list)) {
- $userServices->update($uid, ['agent_level' => $levelInfo['id']]);
- } else {
- break;
- }
- }
- }
- return true;
- }
- /**
- * 分销等级上浮
- * @param int $uid
- * @param array $userInfo
- * @return array|int[]
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getAgentLevelBrokerage(int $uid, $userInfo = [])
- {
- $one_brokerage_up = $two_brokerage_up = $spread_uid = $spread_two_uid = 0;
- $data = [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid];
- if (!$uid) {
- return $data;
- }
- //商城分销是否开启
- if (!sys_config('brokerage_func_status')) {
- return $data;
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if (!$userInfo) {
- $userInfo = $userServices->getUserCacheInfo($uid);
- }
- if (!$userInfo) {
- return $data;
- }
- //获取上级uid || 开启自购返回自己uid
- $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
- $one_agent_level = 0;
- $two_agent_level = 0;;
- if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
- $one_agent_level = $one_user_info['agent_level'] ?? 0;
- $spread_two_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
- if ($spread_two_uid > 0 && $two_user_info = $userServices->getUserInfo($spread_two_uid)) {
- $two_agent_level = $two_user_info['agent_level'] ?? 0;
- }
- }
- $one_brokerage_up = $one_agent_level ? ($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0) : 0;
- $two_brokerage_up = $two_agent_level ? ($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0) : 0;
- return [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid];
- }
- /**
- * 计算一二级返佣比率上浮
- * @param $ratio
- * @param $brokerage
- * @return int|string
- */
- public function compoteBrokerage($ratio, $brokerage)
- {
- if (!$ratio) {
- return 0;
- }
- $brokerage = bcdiv((string)$brokerage, '100', 4);
- if ($brokerage) {
- return bcmul((string)$ratio, bcadd('1', (string)$brokerage, 4), 2);
- }
- return $brokerage;
- }
- /**
- * 添加等级表单
- * @param int $id
- * @return array
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function createForm()
- {
- $store_brokerage_ratio = sys_config('store_brokerage_ratio');
- $store_brokerage_two = sys_config('store_brokerage_two');
- $field[] = Form::input('name', '等级名称')->col(24);
- $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
- $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('请选择背景图'));
- $field[] = Form::number('one_brokerage', '一级上浮', 0)->info('在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:' . $store_brokerage_ratio . '%,例如上浮10%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_ratio, 10) . '%')->min(0)->max(1000);
- $field[] = Form::number('two_brokerage', '二级上浮', 0)->info('在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:' . $store_brokerage_two . '%,例如上浮10%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_two, 10) . '%')->min(0)->max(1000);
- $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
- return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
- }
- /**
- * 获取修改等级表单
- * @param int $id
- * @return array
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function editForm(int $id)
- {
- $store_brokerage_ratio = sys_config('store_brokerage_ratio');
- $store_brokerage_two = sys_config('store_brokerage_two');
- $levelInfo = $this->getLevelInfo($id);
- if (!$levelInfo)
- throw new AdminException('数据不存在');
- $field = [];
- $field[] = Form::hidden('id', $id);
- $field[] = Form::input('name', '等级名称', $levelInfo['name'])->col(24);
- $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
- $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('请选择背景图'));
- $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);
- $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);
- $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
- return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
- }
- /**
- * 赠送分销等级表单
- * @param int $uid
- * @return array
- * @throws \FormBuilder\Exception\FormBuilderException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function levelForm(int $uid)
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userInfo = $userServices->getUserInfo($uid);
- if (!$userInfo) {
- throw new AdminException('分销员不存在');
- }
- $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1]);
- $setOptionLabel = function () use ($levelList) {
- $menus = [];
- foreach ($levelList as $level) {
- $menus[] = ['value' => $level['id'], 'label' => $level['name']];
- }
- return $menus;
- };
- $field[] = Form::hidden('uid', $uid);
- $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] ?? 0)->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
- return create_form('赠送分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
- }
- /**
- * 赠送分销等级
- * @param int $uid
- * @param int $id
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function givelevel(int $uid, int $id)
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userInfo = $userServices->getUserInfo($uid);
- if (!$userInfo) {
- throw new AdminException('分销员不存在');
- }
- $levelInfo = $this->getLevelInfo($id);
- if (!$levelInfo) {
- throw new AdminException('分销等级不存在');
- }
- if ($userServices->update($uid, ['agent_level' => $id]) === false) {
- throw new AdminException('赠送失败');
- }
- return true;
- }
- }
|