chore: 添加静态资源文件和版本配置文件
添加大量图片、字体、音频等静态资源文件,包括用户界面图片、模板文件和安装界面素材 添加系统版本配置文件(.version)和常量定义文件(.constant) 更新供应商依赖文件,包括Symfony VarDumper组件和相关许可证文件 添加验证码配置文件(captcha.php)和.gitignore规则
This commit is contained in:
443
app/services/agent/AgentLevelServices.php
Normal file
443
app/services/agent/AgentLevelServices.php
Normal file
@@ -0,0 +1,443 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 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\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\annotation\Inject;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Route as Url;
|
||||
|
||||
|
||||
/**
|
||||
* 分销等级
|
||||
* Class AgentLevelServices
|
||||
* @package app\services\agent
|
||||
* @mixin AgentLevelDao
|
||||
*/
|
||||
class AgentLevelServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var AgentLevelDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected AgentLevelDao $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['commissionCount'] = max($user['commissionCount'], 0);
|
||||
|
||||
$whereType = [['type', 'in', ['self_brokerage', 'one_brokerage', 'two_brokerage', 'brokerage_user', 'division_brokerage', 'agent_brokerage', 'staff_brokerage']]];
|
||||
$brokerage = $userBrokerageServices->sum($whereType + ['uid' => $uid, 'pm' => 1], 'number', true);
|
||||
$refund = $userBrokerageServices->sum(['uid' => $uid, 'pm' => 0, 'type' => 'refund'], 'number', true);
|
||||
$user['accumulate'] = bcsub((string)$brokerage, (string)$refund, 2);
|
||||
//佣金排行
|
||||
$user['position_count'] = $userBrokerageServices->getUserBrokerageRank($uid, 'week')['position'];
|
||||
//推广人排行
|
||||
$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;
|
||||
if ($rankList) {
|
||||
$key = array_search($uid, array_column($rankList, 'spread_uid'));
|
||||
if ($key !== false) {
|
||||
$rank = (int)$key + 1;
|
||||
}
|
||||
}
|
||||
foreach ($rankList as $key => $item) {
|
||||
if ($item['spread_uid'] == $uid) $rank = $key + 1;
|
||||
}
|
||||
$user['rank_count'] = $rank;
|
||||
$user['division_type'] = (int)sys_config('division_open') ? $user['division_type'] : 0;
|
||||
$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');
|
||||
$user['division_apply_open'] = (int)sys_config('division_apply_open');
|
||||
|
||||
if($user['agent_id'] == 0 && $user['division_type'] == 2){
|
||||
$user['agent_id'] = $user['uid'];
|
||||
}
|
||||
|
||||
|
||||
return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一等级
|
||||
* @param int $level_id
|
||||
* @return array|\think\Model|null
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getNextLevelInfo(int $level_id = 0)
|
||||
{
|
||||
$grade = 0;
|
||||
if ($level_id) {
|
||||
$grade = $this->dao->value(['id' => $level_id, 'is_del' => 0, 'status' => 1], 'grade') ?: 0;
|
||||
}
|
||||
return $this->dao->getOne([['grade', '>', $grade], ['is_del', '=', 0], ['status', '=', 1]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户是否能升级
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
//获取后台一级返佣比例
|
||||
$storeBrokerageRatio = sys_config('store_brokerage_ratio');
|
||||
//一级上浮之后的反佣比例
|
||||
$storeBrokerageRatio = $one_agent_level ? bcadd($storeBrokerageRatio, bcmul($storeBrokerageRatio, bcdiv(($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0), 100, 2), 2), 2) : $storeBrokerageRatio;
|
||||
//获取二级返佣比例
|
||||
$storeBrokerageTwo = sys_config('store_brokerage_two');
|
||||
//二级上浮之后的反佣比例
|
||||
$storeBrokerageTwo = $two_agent_level ? bcadd($storeBrokerageTwo, bcmul($storeBrokerageTwo, bcdiv(($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0), 100, 2), 2), 2) : $storeBrokerageTwo;
|
||||
return [$storeBrokerageRatio, $storeBrokerageTwo, $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('admin/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择背景图'));
|
||||
$field[] = Form::color('color', '字体颜色:')->required('请选择字体颜色');
|
||||
$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('admin/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::color('color', '字体颜色', $levelInfo['color'] ?? '')->required('请选择字体颜色');
|
||||
$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;
|
||||
}
|
||||
}
|
||||
33
app/services/agent/AgentLevelTaskRecordServices.php
Normal file
33
app/services/agent/AgentLevelTaskRecordServices.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\agent;
|
||||
|
||||
use app\dao\agent\AgentLevelTaskRecordDao;
|
||||
use app\services\BaseServices;
|
||||
use think\annotation\Inject;
|
||||
|
||||
|
||||
/**
|
||||
* 分销等级任务完成记录
|
||||
* Class AgentLevelTaskRecordServices
|
||||
* @package app\services\agent
|
||||
* @mixin AgentLevelTaskRecordDao
|
||||
*/
|
||||
class AgentLevelTaskRecordServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var AgentLevelTaskRecordDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected AgentLevelTaskRecordDao $dao;
|
||||
|
||||
}
|
||||
421
app/services/agent/AgentLevelTaskServices.php
Normal file
421
app/services/agent/AgentLevelTaskServices.php
Normal file
@@ -0,0 +1,421 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\agent;
|
||||
|
||||
use app\dao\agent\AgentLevelTaskDao;
|
||||
use app\services\BaseServices;
|
||||
use app\services\order\StoreOrderServices;
|
||||
use app\services\user\UserServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\FormBuilder as Form;
|
||||
use FormBuilder\Factory\Iview;
|
||||
use think\annotation\Inject;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Route as Url;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Class AgentLevelTaskServices
|
||||
* @package app\services\agent
|
||||
* @mixin AgentLevelTaskDao
|
||||
*/
|
||||
class AgentLevelTaskServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* 任务类型
|
||||
* type 记录在数据库中用来区分任务
|
||||
* name 任务名 (任务名中的{$num}会自动替换成设置的数字 + 单位)
|
||||
* max_number 最大设定数值 0为不限定
|
||||
* min_number 最小设定数值
|
||||
* unit 单位
|
||||
* */
|
||||
protected array $TaskType = [
|
||||
[
|
||||
'type' => 1,
|
||||
'method' => 'spread',
|
||||
'name' => '邀请好友{$num}成为下级',
|
||||
'real_name' => '邀请好友成为下级',
|
||||
'max_number' => 0,
|
||||
'min_number' => 1,
|
||||
'unit' => '人',
|
||||
'image' => '/uploads/system/agent_spread.png'
|
||||
],
|
||||
[
|
||||
'type' => 2,
|
||||
'method' => 'consumePrice',
|
||||
'name' => '自身消费满{$num}',
|
||||
'real_name' => '自身消费金额',
|
||||
'max_number' => 0,
|
||||
'min_number' => 0,
|
||||
'unit' => '元',
|
||||
'image' => '/uploads/system/agent_self_order_price.png'
|
||||
],
|
||||
[
|
||||
'type' => 3,
|
||||
'method' => 'consumeCount',
|
||||
'name' => '自身消费满{$num}',
|
||||
'real_name' => '自身消费单数',
|
||||
'max_number' => 0,
|
||||
'min_number' => 0,
|
||||
'unit' => '单',
|
||||
'image' => '/uploads/system/agent_self_order.png'
|
||||
],
|
||||
[
|
||||
'type' => 4,
|
||||
'method' => 'spreadConsumePrice',
|
||||
'name' => '下级消费满{$num}',
|
||||
'real_name' => '下级消费金额',
|
||||
'max_number' => 0,
|
||||
'min_number' => 0,
|
||||
'unit' => '元',
|
||||
'image' => '/uploads/system/agent_spread_order_price.png'
|
||||
],
|
||||
[
|
||||
'type' => 5,
|
||||
'method' => 'spreadConsumeCount',
|
||||
'name' => '下级消费满{$num}',
|
||||
'real_name' => '下级消费单数',
|
||||
'max_number' => 0,
|
||||
'min_number' => 0,
|
||||
'unit' => '单',
|
||||
'image' => '/uploads/system/agent_spread_order.png'
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var AgentLevelTaskDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected AgentLevelTaskDao $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 getLevelTaskInfo(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 getLevelTaskList(array $where)
|
||||
{
|
||||
$where['is_del'] = 0;
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$list = $this->dao->getTaskList($where, '*', [], $page, $limit);
|
||||
if ($list) {
|
||||
$allTyep = $this->getTaskTypeAll();
|
||||
$allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
|
||||
foreach ($list as &$item) {
|
||||
$item['type_name'] = $allTyep[$item['type']]['real_name'] ?? '';
|
||||
}
|
||||
}
|
||||
$count = $this->dao->count($where);
|
||||
return compact('count', 'list');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个等级某个类型任务
|
||||
* @param int $level_id
|
||||
* @param int $type
|
||||
* @return array|\think\Model|null
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getLevelTypeTask(int $level_id, int $type = 1)
|
||||
{
|
||||
return $this->dao->get(['level_id' => $level_id, 'type' => $type, 'is_del' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加等级任务表单
|
||||
* @param int $id
|
||||
* @return array
|
||||
* @throws \FormBuilder\Exception\FormBuilderException
|
||||
*/
|
||||
public function createForm(int $level_id)
|
||||
{
|
||||
/** @var AgentLevelServices $levelServices */
|
||||
$levelServices = app()->make(AgentLevelServices::class);
|
||||
if (!$levelServices->getLevelInfo($level_id)) {
|
||||
throw new AdminException('选择的等级不存在,请返回重新选择');
|
||||
}
|
||||
$taskList = $this->getTaskTypeAll();
|
||||
$setOptionLabel = function () use ($taskList) {
|
||||
$menus = [];
|
||||
foreach ($taskList as $task) {
|
||||
$menus[] = ['value' => $task['type'], 'label' => ($task['real_name'] ?? '') . '(' . ($task['unit'] ?? '') . ')'];
|
||||
}
|
||||
return $menus;
|
||||
};
|
||||
$field[] = Form::hidden('level_id', $level_id);
|
||||
$field[] = Form::select('type', '任务类型:')->setOptions(Form::setOptions($setOptionLabel))->filterable(true)->info('请选择需要达成的任务类型')->appendValidate(Iview::validateInt()->message('请选择任务类型')->required());
|
||||
$field[] = Form::number('number', '任务要求:', 0)->info('达成任务需要完成的人数、金额或订单数')->required('请填写任务要求')->min(0);
|
||||
$field[] = Form::input('name', '任务名称:')->col(24)->required('请填写任务名称');
|
||||
$field[] = Form::textarea('desc', '任务描述:');
|
||||
$field[] = Form::number('sort', '排序:', 0)->min(0);
|
||||
$field[] = Form::radio('status', '是否显示:', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
|
||||
return create_form('添加等级任务', $field, Url::buildUrl('/agent/level_task'), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取修改任务数据
|
||||
* @param int $id
|
||||
* @return array
|
||||
* @throws \FormBuilder\Exception\FormBuilderException
|
||||
*/
|
||||
public function editForm(int $id)
|
||||
{
|
||||
$levelTaskInfo = $this->getLevelTaskInfo($id);
|
||||
if (!$levelTaskInfo)
|
||||
throw new AdminException('数据不存在');
|
||||
$field = [];
|
||||
$field[] = Form::hidden('id', $id);
|
||||
$taskList = $this->getTaskTypeAll();
|
||||
$setOptionLabel = function () use ($taskList) {
|
||||
$menus = [];
|
||||
foreach ($taskList as $task) {
|
||||
$menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
|
||||
}
|
||||
return $menus;
|
||||
};
|
||||
$field[] = Form::select('type', '任务类型', $levelTaskInfo['type'])->setOptions(Form::setOptions($setOptionLabel))->filterable(true)->info('请选择需要达成的任务类型')->appendValidate(Iview::validateInt()->message('请选择任务类型')->required());
|
||||
$field[] = Form::number('number', '任务要求', $levelTaskInfo['number'])->min(0)->info('达成任务需要完成的人数、金额或订单数')->required('请填写任务要求');
|
||||
$field[] = Form::input('name', '任务名称', $levelTaskInfo['name'])->required('请填写任务名称');
|
||||
$field[] = Form::textarea('desc', '任务描述', $levelTaskInfo['desc']);
|
||||
$field[] = Form::number('sort', '排序', $levelTaskInfo['sort'])->min(0);
|
||||
$field[] = Form::radio('status', '是否显示', $levelTaskInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
|
||||
|
||||
return create_form('编辑等级任务', $field, Url::buildUrl('/agent/level_task/' . $id), 'PUT');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务类型
|
||||
* @return array[]
|
||||
*/
|
||||
public function getTaskTypeAll()
|
||||
{
|
||||
return $this->TaskType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个任务
|
||||
* @param string $type 任务类型
|
||||
* @return array
|
||||
* */
|
||||
public static function getTaskType($type)
|
||||
{
|
||||
foreach (self::$TaskType as $item) {
|
||||
if ($item['type'] == $type) return $item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户某一个分销等级任务情况
|
||||
* @param int $uid
|
||||
* @param int $level_id
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getUserLevelTaskList(int $uid, int $level_id)
|
||||
{
|
||||
//商城分销是否开启
|
||||
if (!sys_config('brokerage_func_status')) {
|
||||
return [];
|
||||
}
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$user = $userServices->getUserInfo($uid);
|
||||
if (!$user) {
|
||||
throw new ValidateException('没有此用户');
|
||||
}
|
||||
/** @var AgentLevelServices $levelServices */
|
||||
$levelServices = app()->make(AgentLevelServices::class);
|
||||
$levelInfo = $levelServices->getLevelInfo($level_id);
|
||||
if (!$levelInfo) {//没有默认最低等级
|
||||
$list = $levelServices->getList(['is_del' => 0, 'status' => 1]);
|
||||
$levelInfo = $list[0] ?? [];
|
||||
$level_id = $levelInfo['id'] ?? 0;
|
||||
}
|
||||
$taskList = $this->dao->getTaskList(['level_id' => $level_id, 'is_del' => 0, 'status' => 1]);
|
||||
$speedAll = $speedCount = 0;
|
||||
if ($taskList) {
|
||||
$userLevel = [];
|
||||
if ($user['agent_level'] ?? 0) $userLevel = $levelServices->getLevelInfo($user['agent_level']);
|
||||
$allTyep = $this->getTaskTypeAll();
|
||||
$allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
|
||||
$baseUrl = sys_config('site_url');
|
||||
foreach ($taskList as &$task) {
|
||||
$task['finish'] = 1;
|
||||
$task['task_type_title'] = '已完成';
|
||||
$task['speed'] = 100;
|
||||
$task['new_number'] = $task['number'];
|
||||
//当前等级之前的等级任务 全部为完成
|
||||
if (!$userLevel || $userLevel['grade'] < $levelInfo['grade']) {
|
||||
[$title, $num, $isComplete] = $this->checkLevelTaskFinish($uid, (int)$task['id']);
|
||||
if (!$isComplete) {
|
||||
$scale = in_array($task['type'], [2, 4]) ? 2 : 0;
|
||||
$task['finish'] = 0;
|
||||
$numdata = bcsub($task['number'], $num, $scale);
|
||||
$task['task_type_title'] = '还需' . str_replace('{$num}', $numdata . $allTyep[$task['type']]['unit'] ?? '', $title);
|
||||
$task['speed'] = bcmul((string)bcdiv((string)$num, (string)$task['number'], 2), '100', 0);
|
||||
$task['new_number'] = $num;
|
||||
$task['image'] = $baseUrl . $allTyep[$task['type']]['image'] ?? '';
|
||||
}
|
||||
}
|
||||
$speedCount = bcadd((string)$speedCount, $task['speed'], 0);
|
||||
}
|
||||
$speedAll = count($taskList) > 0 ? floatval(bcdiv($speedCount, (string)count($taskList), 2)) : 0;
|
||||
}
|
||||
return ['list' => $taskList, 'speedAll' => $speedAll];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测某个任务完成情况
|
||||
* @param int $uid
|
||||
* @param int $task_id
|
||||
* @return array|false
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function checkLevelTaskFinish(int $uid, int $task_id, $levelTaskInfo = [])
|
||||
{
|
||||
if (!$levelTaskInfo) {
|
||||
$levelTaskInfo = $this->getLevelTaskInfo($task_id);
|
||||
}
|
||||
if (!$levelTaskInfo) return false;
|
||||
$allTyep = $this->getTaskTypeAll();
|
||||
$allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
|
||||
$userNumber = 0;
|
||||
$msg = $allTyep[$levelTaskInfo['type']]['name'] ?? '';
|
||||
switch ($levelTaskInfo['type']) {
|
||||
case 1:
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userNumber = $userServices->count(['spread_uid' => $uid]);
|
||||
break;
|
||||
case 2:
|
||||
/** @var StoreOrderServices $storeOrderServices */
|
||||
$storeOrderServices = app()->make(StoreOrderServices::class);
|
||||
$where = ['pid' => 0, 'uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
|
||||
$userNumber = $storeOrderServices->sum($where, 'pay_price');
|
||||
break;
|
||||
case 3:
|
||||
/** @var StoreOrderServices $storeOrderServices */
|
||||
$storeOrderServices = app()->make(StoreOrderServices::class);
|
||||
$where = ['pid' => 0, 'uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
|
||||
$userNumber = $storeOrderServices->count($where);
|
||||
break;
|
||||
case 4:
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
|
||||
if ($spread_uids) {
|
||||
/** @var StoreOrderServices $storeOrderServices */
|
||||
$storeOrderServices = app()->make(StoreOrderServices::class);
|
||||
$where = ['pid' => 0, 'uid' => $spread_uids, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
|
||||
$userNumber = $storeOrderServices->sum($where, 'pay_price');
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
|
||||
if ($spread_uids) {
|
||||
/** @var StoreOrderServices $storeOrderServices */
|
||||
$storeOrderServices = app()->make(StoreOrderServices::class);
|
||||
$where = ['pid' => 0, 'uid' => $spread_uids, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
|
||||
$userNumber = $storeOrderServices->count($where);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
$isComplete = false;
|
||||
if ($userNumber >= $levelTaskInfo['number']) {
|
||||
/** @var AgentLevelTaskRecordServices $agentLevelTaskRecordServices */
|
||||
$agentLevelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
|
||||
$isComplete = true;
|
||||
if (!$agentLevelTaskRecordServices->get(['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id']])) {
|
||||
$data = ['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id'], 'add_time' => time()];
|
||||
$isComplete = $agentLevelTaskRecordServices->save($data);
|
||||
}
|
||||
}
|
||||
return [$msg, $userNumber, $isComplete];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测等级任务
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function checkTypeTask(int $id, array $data)
|
||||
{
|
||||
if (!$id && (!isset($data['level_id']) || !$data['level_id'])) {
|
||||
throw new ValidateException('缺少等级任务必要参数');
|
||||
}
|
||||
if ($id) {
|
||||
$task = $this->getLevelTaskInfo($id);
|
||||
if (!$task) {
|
||||
throw new ValidateException('编辑的任务不存在');
|
||||
}
|
||||
$data['level_id'] = $task['level_id'];
|
||||
}
|
||||
/** @var AgentLevelServices $agentLevelServices */
|
||||
$agentLevelServices = app()->make(AgentLevelServices::class);
|
||||
$levelInfo = $agentLevelServices->getLevelInfo($data['level_id']);
|
||||
if (!$levelInfo) {
|
||||
throw new ValidateException('所选择的分销等级不存在或者删除,请重新选择');
|
||||
}
|
||||
$task = $this->dao->getOne(['level_id' => $data['level_id'], 'type' => $data['type'], 'is_del' => 0]);
|
||||
if (($id && $task && $task['id'] != $id) || (!$id && $task)) {
|
||||
throw new ValidateException('该等级已存在此类型任务');
|
||||
}
|
||||
$taskList = $this->dao->getTypTaskList($data['type']);
|
||||
if ($taskList) {
|
||||
foreach ($taskList as $taskInfo) {
|
||||
if (is_null($taskInfo['grade'])) continue;
|
||||
if ($levelInfo['grade'] > $taskInfo['grade'] && $data['number'] <= $taskInfo['number']) {
|
||||
throw new ValidateException('不能小于低等级同类型任务限定数量');
|
||||
}
|
||||
if ($levelInfo['grade'] < $taskInfo['grade'] && $data['number'] >= $taskInfo['number']) {
|
||||
throw new ValidateException('不能大于高等级同类型任务限定数量');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
410
app/services/agent/AgentManageServices.php
Normal file
410
app/services/agent/AgentManageServices.php
Normal file
@@ -0,0 +1,410 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\services\agent;
|
||||
|
||||
use app\jobs\agent\AutoAgentJob;
|
||||
use app\services\BaseServices;
|
||||
use app\services\order\StoreOrderServices;
|
||||
use app\services\order\StoreOrderStatusServices;
|
||||
use app\services\other\QrcodeServices;
|
||||
use app\services\user\UserBrokerageServices;
|
||||
use app\services\user\UserExtractServices;
|
||||
use app\services\user\UserServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
|
||||
/**
|
||||
* 分销员
|
||||
* Class AgentManageServices
|
||||
* @package app\services\agent
|
||||
*/
|
||||
class AgentManageServices extends BaseServices
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $where
|
||||
* @return array
|
||||
*/
|
||||
public function agentSystemPage(array $where, $is_page = true)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$data = $userServices->getAgentUserList($where, '*', $is_page);
|
||||
/** @var UserBrokerageServices $userBrokerageServices */
|
||||
$userBrokerageServices = app()->make(UserBrokerageServices::class);
|
||||
|
||||
$uids = array_column($data['list'], 'uid');
|
||||
$spreadCount = $userServices->search(['spread_uid'=>$uids])
|
||||
->group('spread_uid')
|
||||
->field('uid,count(*) as spread_count,spread_uid')
|
||||
->select()->toArray();
|
||||
$spreadCount = array_column($spreadCount, 'spread_count', 'spread_uid');
|
||||
|
||||
foreach ($data['list'] as &$item) {
|
||||
$item['headimgurl'] = $item['avatar'];
|
||||
$item['extract_count_price'] = $item['extract'][0]['extract_count_price'] ?? 0;
|
||||
$item['extract_count_num'] = $item['extract'][0]['extract_count_num'] ?? 0;
|
||||
$item['spread_name'] = $item['spreadUser']['nickname'] ?? '';
|
||||
if ($item['spread_name']) {
|
||||
$item['spread_name'] .= '/' . $item['spread_uid'];
|
||||
}
|
||||
$item['spread_count'] = $spreadCount[$item['uid']] ?? 0 ;
|
||||
$item['order_price'] = $item['order'][0]['order_price'] ?? 0;
|
||||
$item['order_count'] = $item['order'][0]['order_count'] ?? 0;
|
||||
$item['broken_commission'] = $userBrokerageServices->getUserFrozenPrice((int)$item['uid']);
|
||||
if ($item['broken_commission'] < 0)
|
||||
$item['broken_commission'] = 0;
|
||||
$item['brokerage_money'] = $item['brokerage'][0]['brokerage_money'] ?? 0;
|
||||
if ($item['brokerage_price'] > $item['broken_commission'])
|
||||
$item['brokerage_money'] = bcsub($item['brokerage_price'], $item['broken_commission'], 2);
|
||||
else
|
||||
$item['brokerage_money'] = 0;
|
||||
$item['new_money'] = $item['brokerage_price'];
|
||||
unset($item['extract'], $item['order'], $item['bill'], $item['spreadUser'], $item['spreadCount']);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分销头部信息
|
||||
* @param $where
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getSpreadBadge($where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$uids = $userServices->getAgentUserIds($where);
|
||||
|
||||
//分销员人数
|
||||
$data['uids'] = $uids;
|
||||
$data['sum_count'] = count($uids);
|
||||
$data['spread_sum'] = 0;
|
||||
if ($data['sum_count']) {
|
||||
//发展会员人数
|
||||
$data['spread_sum'] = $userServices->getCount([['spread_uid', '<>', 0]]);
|
||||
}
|
||||
/** @var UserBrokerageServices $userBrokerageServices */
|
||||
$userBrokerageServices = app()->make(UserBrokerageServices::class);
|
||||
$data['brokerage_price'] = $userBrokerageServices->getUsersBokerageSum(array_merge($where, ['pm' => 1, 'not_type' => ['extract_fail', 'refund']]));
|
||||
//分销员人数
|
||||
$data['order_count'] = 0;
|
||||
$data['pay_price'] = 0;
|
||||
$data['pay_price'] = 0;
|
||||
$data['extract_count'] = 0;
|
||||
if ($data['sum_count']) {
|
||||
/** @var StoreOrderServices $storeOrder */
|
||||
$storeOrder = app()->make(StoreOrderServices::class);
|
||||
$order_where = ['uid' => $uids, 'pid' => 0, 'paid' => 1, 'refund_status' => [0, 3]];
|
||||
//订单总数
|
||||
$data['order_count'] = $storeOrder->count($order_where);
|
||||
//订单金额
|
||||
$data['pay_price'] = $storeOrder->sum($order_where, 'pay_price', true);
|
||||
//提现次数
|
||||
$data['extract_count'] = app()->make(UserExtractServices::class)->getCount(['status' => 1]);
|
||||
}
|
||||
return [
|
||||
[
|
||||
'name' => '分销员人数(人)',
|
||||
'count' => $data['sum_count'],
|
||||
'className' => 'md-contacts',
|
||||
'col' => 8,
|
||||
],
|
||||
[
|
||||
'name' => '发展会员人数(人)',
|
||||
'count' => $data['spread_sum'],
|
||||
'className' => 'md-contact',
|
||||
'col' => 8,
|
||||
],
|
||||
[
|
||||
'name' => '订单数(单)',
|
||||
'count' => $data['order_count'],
|
||||
'className' => 'md-cart',
|
||||
'col' => 8,
|
||||
],
|
||||
[
|
||||
'name' => '订单金额(元)',
|
||||
'count' => $data['pay_price'],
|
||||
'className' => 'md-bug',
|
||||
'col' => 8,
|
||||
],
|
||||
[
|
||||
'name' => '已提现次数(次)',
|
||||
'count' => $data['extract_count'],
|
||||
'className' => 'md-basket',
|
||||
'col' => 8,
|
||||
],
|
||||
[
|
||||
'name' => '佣金金额(元)',
|
||||
'count' => $data['brokerage_price'],
|
||||
'className' => 'ios-at-outline',
|
||||
'col' => 8,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 推广人列表
|
||||
* @param array $where
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStairList(array $where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$data = $userServices->getSairList($where);
|
||||
$store_brokerage_statu = sys_config('store_brokerage_statu');
|
||||
foreach ($data['list'] as &$item) {
|
||||
$item['spread_count'] = $item['spreadCount'][0]['spread_count'] ?? 0;
|
||||
$item['order_count'] = $item['order'][0]['order_count'] ?? 0;
|
||||
$item['promoter_name'] = $item['is_promoter'] || $store_brokerage_statu == 2 ? '是' : '否';
|
||||
$item['add_time'] = $item['add_time'] ? date("Y-m-d H:i:s", $item['add_time']) : '';
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 推广人头部信息
|
||||
* @param array $where
|
||||
* @return array[]
|
||||
*/
|
||||
public function getSairBadge(array $where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$data['number'] = $userServices->getSairCount($where);
|
||||
$where['type'] = 1;
|
||||
$data['one_number'] = $userServices->getSairCount($where);
|
||||
$where['type'] = 2;
|
||||
$data['two_number'] = $userServices->getSairCount($where);
|
||||
|
||||
$col = $data['two_number'] > 0 ? 4 : 6;
|
||||
return [
|
||||
[
|
||||
'name' => '总人数(人)',
|
||||
'count' => $data['number'],
|
||||
'col' => $col,
|
||||
],
|
||||
[
|
||||
'name' => '一级人数(人)',
|
||||
'count' => $data['one_number'],
|
||||
'col' => $col,
|
||||
],
|
||||
[
|
||||
'name' => '二级人数(人)',
|
||||
'count' => $data['two_number'],
|
||||
'col' => $col,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 推广订单
|
||||
* @param array $where
|
||||
* @return array
|
||||
*/
|
||||
public function getStairOrderList(int $uid, array $where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->getUserInfo($uid);
|
||||
if (!$userInfo) {
|
||||
return ['count' => 0, 'list' => []];
|
||||
}
|
||||
/** @var StoreOrderServices $storeOrder */
|
||||
$storeOrder = app()->make(StoreOrderServices::class);
|
||||
$data = $storeOrder->getUserStairOrderList($uid, $where);
|
||||
if ($data['list']) {
|
||||
$uids = array_unique(array_column($data['list'], 'uid'));
|
||||
$userList = [];
|
||||
if ($uids) {
|
||||
$userList = $userServices->getColumn([['uid', 'IN', $uids]], 'nickname,phone,avatar,real_name', 'uid');
|
||||
}
|
||||
$orderIds = array_column($data['list'], 'id');
|
||||
$orderChangTimes = [];
|
||||
if ($orderIds) {
|
||||
/** @var StoreOrderStatusServices $storeOrderStatus */
|
||||
$storeOrderStatus = app()->make(StoreOrderStatusServices::class);
|
||||
$orderChangTimes = $storeOrderStatus->getColumn([['oid', 'IN', $orderIds], ['change_type', '=', 'user_take_delivery']], 'change_time', 'oid');
|
||||
}
|
||||
foreach ($data['list'] as &$item) {
|
||||
$user = $userList[$item['uid']] ?? [];
|
||||
$item['user_info'] = '';
|
||||
$item['avatar'] = '';
|
||||
if (count($user)) {
|
||||
$item['user_info'] = $user['nickname'] . '|' . ($user['phone'] ? $user['phone'] . '|' : '') . $user['real_name'];
|
||||
$item['avatar'] = $user['avatar'];
|
||||
}
|
||||
$item['brokerage_price'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
|
||||
$item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
|
||||
$item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
|
||||
$item['take_time'] = ($change_time = $orderChangTimes[$item['id']] ?? '') ? date('Y-m-d H:i:s', $change_time) : '暂无';
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取永久二维码
|
||||
* @param $type
|
||||
* @param $id
|
||||
* @return array|false|\PDOStatement|string|\think\Model
|
||||
*/
|
||||
public function wechatCode(int $uid)
|
||||
{
|
||||
/** @var QrcodeServices $qrcode */
|
||||
$qrcode = app()->make(QrcodeServices::class);
|
||||
$code = $qrcode->getForeverQrcode('spread', $uid);
|
||||
if (!$code['ticket']) exception('永久二维码获取错误');
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看小程序推广二维码
|
||||
* @param string $uid
|
||||
*/
|
||||
public function lookXcxCode(int $uid)
|
||||
{
|
||||
$userInfo = app()->make(UserServices::class)->getUserInfo($uid);
|
||||
if (!$userInfo) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
$name = $userInfo['uid'] . '_' . $userInfo['is_promoter'] . '_user.jpg';
|
||||
/** @var QrcodeServices $qrcode */
|
||||
$qrcode = app()->make(QrcodeServices::class);
|
||||
$resForever = $qrcode->qrCodeForever($uid, 'spread_routine');
|
||||
$id = (int)$resForever['id'];
|
||||
/** @var QrcodeServices $QrcodeService */
|
||||
$QrcodeService = app()->make(QrcodeServices::class);
|
||||
//生成小程序地址
|
||||
$routineQrcode = $QrcodeService->getRoutineQrcodePath($id, $uid, -1, $name);
|
||||
if (!$routineQrcode) {
|
||||
$qrcode->update($id, ['status' => 1, 'time' => time(), 'qrcode_url' => $routineQrcode]);
|
||||
}
|
||||
return ['code_src' => $routineQrcode];
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看H5推广二维码
|
||||
* @param string $uid
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function lookH5Code(int $uid)
|
||||
{
|
||||
$userInfo = app()->make(UserServices::class)->getUserInfo($uid);
|
||||
if (!$userInfo) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
$name = $uid . '_h5_' . $userInfo['is_promoter'] . '_user.jpg';
|
||||
/** @var QrcodeServices $qrcodeServices */
|
||||
$qrcodeServices = app()->make(QrcodeServices::class);
|
||||
$urlCode = $qrcodeServices->getWechatQrcodePath($name, '?spread=' . $uid . '&spid=' . $uid);
|
||||
return ['code_src' => $urlCode];
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除推广关系
|
||||
* @param int $uid
|
||||
* @return mixed
|
||||
*/
|
||||
public function delSpread(int $uid)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if (!$userServices->userExist($uid)) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
if ($userServices->update($uid, ['spread_uid' => 0]) !== false)
|
||||
return true;
|
||||
else
|
||||
throw new AdminException('解除失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消推广资格
|
||||
* @param int $uid
|
||||
* @return mixed
|
||||
*/
|
||||
public function delSystemSpread(int $uid)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if (!$userServices->userExist($uid)) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
if ($userServices->update($uid, ['spread_uid' => 0, 'spread_time' => 0]) !== false)
|
||||
return true;
|
||||
else
|
||||
throw new AdminException('取消失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
* @param $limit
|
||||
* @param $where
|
||||
*/
|
||||
public function startRemoveSpread($page, $limit, $where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$list = $userServices->getList($where, 'uid,spread_uid,spread_time', $page, $limit);
|
||||
foreach ($list as $userInfo) {
|
||||
$userServices->update($userInfo['uid'], ['spread_uid' => 0, 'spread_time' => 0], 'uid');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消绑定上级
|
||||
* @return bool
|
||||
*/
|
||||
public function removeSpread()
|
||||
{
|
||||
//商城分销功能是否开启 0关闭1开启
|
||||
if (!sys_config('brokerage_func_status')) return true;
|
||||
|
||||
//绑定类型
|
||||
$store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
|
||||
if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
|
||||
return true;
|
||||
} else {
|
||||
//分销绑定类型为时间段且没过期
|
||||
$store_brokerage_binding_time = (int)sys_config('store_brokerage_binding_time', 30) * 24 * 3600;
|
||||
$spread_time = bcsub((string)time(), (string)$store_brokerage_binding_time, 0);
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$where = ['not_spread_uid' => 0, 'status' => 1, 'time' => [0, $spread_time], 'time_key' => 'spread_time'];
|
||||
$count = $userServices->count($where);
|
||||
$pages = ceil($count / 100);
|
||||
for ($i = 1; $i <= $pages; $i++) {
|
||||
AutoAgentJob::dispatch([$i, 100, $where]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置绑定类型切换重置绑定时间
|
||||
* @return bool
|
||||
*/
|
||||
public function resetSpreadTime()
|
||||
{
|
||||
//商城分销功能是否开启 0关闭1开启
|
||||
if (!sys_config('brokerage_func_status')) return true;
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userServices->update(['not_spread_uid' => 0, 'status' => 1], ['spread_time' => time()]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
187
app/services/agent/DivisionApplyServices.php
Normal file
187
app/services/agent/DivisionApplyServices.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\agent;
|
||||
|
||||
use app\dao\agent\DivisionApplyDao;
|
||||
use app\services\BaseServices;
|
||||
use app\services\user\UserServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\exceptions\ApiException;
|
||||
use think\annotation\Inject;
|
||||
use crmeb\services\FormBuilder as Form;
|
||||
|
||||
class DivisionApplyServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var DivisionApplyDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected DivisionApplyDao $dao;
|
||||
|
||||
/**
|
||||
* 申请列表
|
||||
* @param $where
|
||||
* @return array
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function applyList($where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$list = $this->dao->applyList($where, $page, $limit);
|
||||
$divisionIds = array_column($list, 'division_id');
|
||||
$divisionNames = app()->make(UserServices::class)->getColumn([['uid', 'in', $divisionIds]], 'division_name', 'uid');
|
||||
foreach ($list as &$item) {
|
||||
$item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
|
||||
$item['status_time'] = date('Y-m-d H:i:s', $item['status_time']);
|
||||
$item['images'] = json_decode($item['images'], true);
|
||||
$item['spread_name'] = $divisionNames[$item['division_id']] ?? '';
|
||||
}
|
||||
$count = $this->dao->applyCount($where);
|
||||
return compact('list', 'count');
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请审核
|
||||
* @param $id
|
||||
* @param $type
|
||||
* @return mixed
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function applyExamine($id, $type)
|
||||
{
|
||||
$field = [];
|
||||
$field[] = Form::hidden('type', $type);
|
||||
$field[] = Form::hidden('id', $id);
|
||||
if ($type) {
|
||||
$field[] = Form::number('division_percent', '佣金比例', '')->placeholder('代理商佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
|
||||
$field[] = Form::date('division_end_time', '到期时间', '')->placeholder('代理商代理到期时间');
|
||||
$field[] = Form::radio('division_status', '代理状态', 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
|
||||
$title = '同意申请';
|
||||
} else {
|
||||
$field[] = Form::textarea('refusal_reason', '拒绝原因', '')->rows(5);
|
||||
$title = '拒绝申请';
|
||||
}
|
||||
return create_form($title, $field, $this->url('/agent/division/apply/examine/save'), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请审核保存
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function applyExamineSave($data)
|
||||
{
|
||||
return $this->transaction(function () use ($data) {
|
||||
$applyInfo = $this->dao->get($data['id']);
|
||||
if ($data['type'] == 1) {
|
||||
$agentData = [
|
||||
'spread_uid' => $applyInfo['division_id'],
|
||||
'spread_time' => time(),
|
||||
'division_name' => $applyInfo['division_name'],
|
||||
'division_id' => $applyInfo['division_id'],
|
||||
'division_status' => 1,
|
||||
'division_percent' => $data['division_percent'],
|
||||
'division_change_time' => time(),
|
||||
'division_end_time' => strtotime($data['division_end_time']),
|
||||
'division_type' => 2,
|
||||
'agent_id' => $applyInfo['uid'],
|
||||
'is_promoter' => 1,
|
||||
];
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$divisionInfo = $userServices->getUserInfo($applyInfo['division_id'], 'division_end_time,division_percent');
|
||||
if ($divisionInfo) {
|
||||
if ($agentData['division_percent'] > $divisionInfo['division_percent']) throw new AdminException('代理商比例不能大于上级区域代理');
|
||||
if ($agentData['division_end_time'] > $divisionInfo['division_end_time']) throw new AdminException('代理商到期时间不能大于上级区域代理');
|
||||
}
|
||||
$applyInfo->status = 1;
|
||||
$applyInfo->status_time = time();
|
||||
$res = $applyInfo->save();
|
||||
$res = $res && $userServices->update($applyInfo['uid'], $agentData);
|
||||
} else {
|
||||
$applyInfo->status = 2;
|
||||
$applyInfo->refusal_reason = $data['refusal_reason'];
|
||||
$applyInfo->status_time = time();
|
||||
$res = $applyInfo->save();
|
||||
}
|
||||
if (!$res) throw new AdminException($data['type'] == 1 ? '通过失败' : '拒绝失败');
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除申请
|
||||
* @param $id
|
||||
* @return bool
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function applyDelete($id)
|
||||
{
|
||||
$res = $this->dao->update(['id' => $id], ['is_del' => 1]);
|
||||
if (!$res) throw new AdminException('删除失败');
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请代理商
|
||||
* @param $data
|
||||
* @param $id
|
||||
* @return bool
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function applyAgent($data, $id)
|
||||
{
|
||||
$data['images'] = json_encode($data['images']);
|
||||
$data['add_time'] = time();
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$divisionId = $userServices->value(['division_invite' => $data['division_invite']], 'division_id');
|
||||
if (!$divisionId) throw new ApiException('区域代理不存在');
|
||||
$data['division_id'] = $divisionId;
|
||||
if ($id) {
|
||||
$data['status'] = 0;
|
||||
$res = $this->dao->update(['id' => $id], $data);
|
||||
} else {
|
||||
$this->dao->update(['uid' => $data['uid']], ['is_del' => 1]);
|
||||
$res = $this->dao->save($data);
|
||||
}
|
||||
if (!$res) throw new ApiException('申请失败');
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请代理商详情
|
||||
* @param $uid
|
||||
* @return array|int[]
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function applyInfo($uid)
|
||||
{
|
||||
$data = $this->dao->get(['uid' => $uid, 'is_del' => 0]);
|
||||
if (!$data) return ['status' => -1];
|
||||
$data = $data->toArray();
|
||||
$data['images'] = json_decode($data['images'], true);
|
||||
$data['add_time'] = date('Y/m/d H:i', $data['add_time']);
|
||||
$data['status_time'] = $data['status_time'] ? date('Y/m/d H:i', $data['status_time']) : '';
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
983
app/services/agent/DivisionServices.php
Normal file
983
app/services/agent/DivisionServices.php
Normal file
@@ -0,0 +1,983 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\agent;
|
||||
|
||||
use app\services\BaseServices;
|
||||
use app\services\order\StoreOrderServices;
|
||||
use app\services\other\QrcodeServices;
|
||||
use app\services\system\admin\SystemAdminServices;
|
||||
use app\services\system\SystemRoleServices;
|
||||
use app\services\user\UserServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\exceptions\ApiException;
|
||||
use crmeb\services\FormBuilder as Form;
|
||||
use think\facade\Log;
|
||||
|
||||
class DivisionServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* 获取事业部列表
|
||||
* @param $where
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function getDivisionList($where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$data = $userServices->getUserList($where + ['status' => 1, 'is_del' => 0],
|
||||
'uid,nickname,avatar,division_id,division_name,division_type,division_percent,division_end_time,division_change_time,division_status,division_invite',
|
||||
'division_change_time desc'
|
||||
);
|
||||
if ($where['division_type'] == 2) {
|
||||
$divisionUpIds = array_column($data['list'], 'division_id');
|
||||
$divisionUpIds = array_unique($divisionUpIds);
|
||||
$divisionUpInfos = $userServices->getColumn(['division_id' => $divisionUpIds, 'division_type' => 1], 'division_name', 'division_id');
|
||||
}
|
||||
$divisionCount = $userServices->getDivisionCount($where['division_type'], array_column($data['list'], 'uid'));
|
||||
foreach ($data['list'] as &$item) {
|
||||
$item['division_end_time'] = $item['division_end_time'] ? date('Y-m-d', $item['division_end_time']) : '';
|
||||
$item['division_change_time'] = $item['division_change_time'] ? date('Y-m-d', $item['division_change_time']) : '';
|
||||
$item['down_num'] = $divisionCount[$item['uid']] ?? 0;
|
||||
$item['division_percent'] = $item['division_percent'] . '%';
|
||||
if ($where['division_type'] == 2) {
|
||||
$item['division_up_name'] = $divisionUpInfos[$item['division_id']] ?? '';
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事业部表单
|
||||
* @param $uid
|
||||
* @return mixed
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function getDivisionForm($uid)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
/** @var SystemAdminServices $adminService */
|
||||
$adminService = app()->make(SystemAdminServices::class);
|
||||
$userInfo = $userServices->get($uid);
|
||||
if ($uid && !$userInfo) throw new AdminException('用户数据不存在');
|
||||
if ($uid) {
|
||||
$adminInfo = $adminService->get(['division_id' => $uid]);
|
||||
if ($adminInfo) $adminInfo = $adminInfo->toArray();
|
||||
if (isset($adminInfo['roles'])) {
|
||||
foreach ($adminInfo['roles'] as &$item) {
|
||||
$item = intval($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
$field = [];
|
||||
$field[] = Form::input('division_name', '区域代理名称:', $userInfo['division_name'] ?? '')->required('请输入区域代理名称');
|
||||
if ($uid) {
|
||||
$field[] = Form::hidden('uid', $uid);
|
||||
} else {
|
||||
$field[] = Form::frameImage('image', '关联用户:', $this->url('admin/system.user/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
|
||||
}
|
||||
$field[] = Form::hidden('aid', $adminInfo['id'] ?? 0);
|
||||
$field[] = Form::number('division_percent', '佣金比例:', $userInfo['division_percent'] ?? '')->placeholder('区域代理佣金比例1-100')->info('填写1-100,如填写50代表返佣50%')->style(['width' => '173px'])->min(0)->max(100)->required();
|
||||
$field[] = Form::date('division_end_time', '到期时间:', isset($userInfo['division_end_time']) && $userInfo['division_end_time'] != 0 ? date('Y-m-d', $userInfo['division_end_time']) : '')->placeholder('区域代理到期时间')->required();
|
||||
$field[] = Form::radio('division_status', '代理状态:', $userInfo['division_status'] ?? 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
|
||||
$field[] = Form::input('account', '管理员账号:', $adminInfo['account'] ?? '')->required('请填写管理员账号');
|
||||
$field[] = Form::input('phone', '管理员手机号:', $adminInfo['phone'] ?? '')->required('请填写管理员手机号');
|
||||
$field[] = Form::input('pwd', '管理密码:')->type('password')->required('请填写管理员密码');
|
||||
$field[] = Form::input('conf_pwd', '确认密码:')->type('password')->required('请输入确认密码');
|
||||
$options = app()->make(SystemRoleServices::class)->getRoleFormSelect(1);
|
||||
$field[] = Form::select('roles', '管理员身份:', $adminInfo['roles'] ?? [])->setOptions(Form::setOptions($options))->multiple(true)->required('请选择管理员身份');
|
||||
return create_form('区域代理', $field, $this->url('/agent/division/save'), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存事业部
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function saveDivision($data)
|
||||
{
|
||||
if ((int)$data['uid'] == 0) $data['uid'] = $data['image']['uid'] ?? 0;
|
||||
if ((int)$data['uid'] == 0) throw new AdminException('请选择用户');
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if ($data['aid'] == 0) {
|
||||
$divisionType = $userServices->value(['uid' => $data['uid']], 'division_type');
|
||||
if (is_null($divisionType)) throw new AdminException('用户不存在');
|
||||
if ($divisionType == 1) throw new AdminException('此用户是区域代理,请勿重复添加');
|
||||
if ($divisionType == 2) throw new AdminException('此用户是代理商,无法添加为区域代理');
|
||||
if ($divisionType == 3) throw new AdminException('此用户是下级员工,无法添加为区域代理');
|
||||
}
|
||||
$uid = $data['uid'];
|
||||
$aid = $data['aid'];
|
||||
$divisionData = [
|
||||
'division_name' => $data['division_name'],
|
||||
'division_type' => 1,
|
||||
'division_status' => $data['division_status'],
|
||||
'division_id' => $uid,
|
||||
'agent_id' => 0,
|
||||
'staff_id' => 0,
|
||||
'division_percent' => $data['division_percent'],
|
||||
'division_end_time' => strtotime($data['division_end_time']),
|
||||
'division_change_time' => time(),
|
||||
'division_invite' => $userServices->value(['uid' => $uid], 'division_invite') ?: rand(10000000, 99999999),
|
||||
'spread_uid' => 0,
|
||||
'spread_time' => 0,
|
||||
'is_promoter' => 1,
|
||||
];
|
||||
$adminData = [
|
||||
'account' => $data['account'],
|
||||
'phone' => $data['phone'],
|
||||
'pwd' => $data['pwd'],
|
||||
'conf_pwd' => $data['conf_pwd'],
|
||||
'real_name' => $data['division_name'],
|
||||
'roles' => $data['roles'],
|
||||
'status' => 1,
|
||||
'level' => 1,
|
||||
'division_id' => $uid
|
||||
];
|
||||
return $this->transaction(function () use ($uid, $divisionData, $adminData, $aid, $userServices) {
|
||||
$userServices->update($uid, $divisionData);
|
||||
/** @var SystemAdminServices $adminService */
|
||||
$adminService = app()->make(SystemAdminServices::class);
|
||||
if (!$aid) {
|
||||
if (!$adminData['pwd']) throw new AdminException('请输入管理员密码');
|
||||
if (!$adminData['conf_pwd']) throw new AdminException('请输入确认密码');
|
||||
if ($adminData['pwd'] != $adminData['conf_pwd']) throw new AdminException('两次密码不一致');
|
||||
$adminService->create($adminData);
|
||||
} else {
|
||||
$adminInfo = $adminService->get($aid);
|
||||
if (!$adminInfo) throw new AdminException('管理员不存在');
|
||||
if ($adminData['pwd']) {
|
||||
if (!$adminData['conf_pwd']) throw new AdminException('请输入确认密码');
|
||||
if ($adminData['pwd'] != $adminData['conf_pwd']) throw new AdminException('两次密码不一致');
|
||||
$adminInfo->pwd = $this->passwordHash($adminData['pwd']);
|
||||
}
|
||||
$adminInfo->real_name = $adminData['real_name'];
|
||||
$adminInfo->account = $adminData['account'];
|
||||
$adminInfo->roles = implode(',', $adminData['roles']);
|
||||
if (!$adminInfo->save()) throw new AdminException('保存管理员失败');
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代理商表单
|
||||
* @param $uid
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function getDivisionAgentForm($uid)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->get($uid);
|
||||
if ($uid && !$userInfo) throw new AdminException(400214);
|
||||
$field = [];
|
||||
$options = [];
|
||||
$divisionList = $userServices->getUserList(['status' => 1, 'is_del' => 0, 'division_type' => 1], 'uid,division_name');
|
||||
foreach ($divisionList['list'] as $item) {
|
||||
$options[] = ['value' => $item['uid'], 'label' => $item['division_name']];
|
||||
}
|
||||
$field[] = Form::input('division_name', '代理商名称:', $userInfo['division_name'] ?? '')->required('请输入代理商名称');
|
||||
if ($uid) {
|
||||
$field[] = Form::hidden('uid', $uid);
|
||||
$field[] = Form::hidden('division_id', $userInfo['division_id']);
|
||||
} else {
|
||||
$field[] = Form::select('division_id', '区域代理:', $info['file_name'] ?? '')->setOptions(Form::setOptions($options))->filterable(1);
|
||||
$field[] = Form::frameImage('image', '关联用户:', $this->url('admin/system.user/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
|
||||
}
|
||||
$field[] = Form::number('division_percent', '佣金比例:', $userInfo['division_percent'] ?? '')->placeholder('代理商佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级区域代理的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
|
||||
$field[] = Form::date('division_end_time', '到期时间:', isset($userInfo['division_end_time']) && $userInfo['division_end_time'] != 0 ? date('Y-m-d', $userInfo['division_end_time']) : '')->placeholder('代理商代理到期时间');
|
||||
$field[] = Form::radio('division_status', '代理状态:', $userInfo['division_status'] ?? 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
|
||||
return create_form('代理商', $field, $this->url('/agent/division_agent/save'), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存代理商
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function saveDivisionAgent($data)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if ((int)$data['uid'] == 0) {
|
||||
$data['uid'] = $data['image']['uid'] ?? 0;
|
||||
if ((int)$data['uid'] == 0) throw new AdminException('请选择用户');
|
||||
$divisionType = $userServices->value(['uid' => $data['uid']], 'division_type');
|
||||
if ($divisionType == 1) throw new AdminException('此用户是区域代理,无法添加为代理商');
|
||||
if ($divisionType == 2) throw new AdminException('此用户是代理商,无法重复添加');
|
||||
if ($divisionType == 3) throw new AdminException('此用户是下级员工,无法添加为代理商');
|
||||
}
|
||||
$uid = $data['uid'];
|
||||
$agentData = [
|
||||
'spread_uid' => $data['division_id'],
|
||||
'spread_time' => time(),
|
||||
'division_name' => $data['division_name'],
|
||||
'division_id' => $data['division_id'],
|
||||
'division_status' => $data['division_status'],
|
||||
'division_percent' => $data['division_percent'],
|
||||
'division_change_time' => time(),
|
||||
'division_end_time' => strtotime($data['division_end_time']),
|
||||
'division_type' => 2,
|
||||
'agent_id' => $uid,
|
||||
'is_promoter' => 1,
|
||||
];
|
||||
$division_info = $userServices->getUserInfo($data['division_id'], 'division_end_time,division_percent');
|
||||
if ($division_info) {
|
||||
if ($agentData['division_percent'] > $division_info['division_percent']) throw new AdminException('代理商比例不能大于上级区域代理');
|
||||
if ($agentData['division_end_time'] > $division_info['division_end_time']) throw new AdminException('代理商到期时间不能大于上级区域代理');
|
||||
}
|
||||
$res = $userServices->update($uid, $agentData);
|
||||
if ($res) return true;
|
||||
throw new AdminException('保存失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取员工表单
|
||||
* @param $uid
|
||||
* @return mixed
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function getDivisionStaffForm($uid)
|
||||
{
|
||||
$field = [];
|
||||
$field[] = Form::frameImage('image', '关联用户:', $this->url('admin/system.user/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
|
||||
$field[] = Form::number('division_percent', '佣金比例:', '')->placeholder('员工佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级代理商的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
|
||||
$field[] = Form::hidden('agent_id', $uid);
|
||||
return create_form('添加员工', $field, $this->url('/agent/division_staff/save'), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存员工
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function saveDivisionStaff($data)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if ((int)$data['uid'] == 0) {
|
||||
$data['uid'] = $data['image']['uid'] ?? 0;
|
||||
if ((int)$data['uid'] == 0) throw new AdminException('请选择用户');
|
||||
$divisionType = $userServices->value(['uid' => $data['uid']], 'division_type');
|
||||
if ($divisionType == 1) throw new AdminException('此用户是区域代理,无法添加为员工');
|
||||
if ($divisionType == 2) throw new AdminException('此用户是代理商,无法添加为员工');
|
||||
if ($divisionType == 3) throw new AdminException('此用户是员工,无法重复添加');
|
||||
}
|
||||
$agentInfo = $userServices->getUserInfo($data['agent_id'], 'division_id,agent_id,division_end_time,division_percent');
|
||||
$staffData = [
|
||||
'spread_uid' => $data['agent_id'],
|
||||
'spread_time' => time(),
|
||||
'division_type' => 3,
|
||||
'division_status' => 1,
|
||||
'division_id' => $agentInfo['division_id'],
|
||||
'agent_id' => $agentInfo['agent_id'],
|
||||
'staff_id' => $data['uid'],
|
||||
'division_percent' => $data['division_percent'],
|
||||
'division_change_time' => time(),
|
||||
'division_end_time' => $agentInfo['division_end_time'],
|
||||
'is_promoter' => 1,
|
||||
];
|
||||
if ($staffData['division_percent'] > $agentInfo['division_percent']) throw new AdminException('员工比例不能大于上级代理商');
|
||||
$res = $userServices->update($data['uid'], $staffData);
|
||||
if ($res) return true;
|
||||
throw new AdminException('保存失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除事业部/代理商/员工
|
||||
* @param $uid
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function deleteDivision($uid)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->getUserInfo($uid);
|
||||
if (!$userInfo) throw new AdminException('用户不存在');
|
||||
$userInfo = $userInfo->toArray();
|
||||
$data = [
|
||||
'division_name' => '',
|
||||
'division_type' => 0,
|
||||
'division_status' => 0,
|
||||
'division_id' => 0,
|
||||
'agent_id' => 0,
|
||||
'staff_id' => 0,
|
||||
'division_percent' => 0,
|
||||
'division_end_time' => 0,
|
||||
'division_change_time' => time(),
|
||||
'division_invite' => 0
|
||||
];
|
||||
$userServices->update(['uid' => $uid], $data);
|
||||
if ($userInfo['division_type'] == 1) {
|
||||
app()->make(SystemAdminServices::class)->delete(['division_id' => $uid]);
|
||||
$userServices->update(['division_id' => $uid], $data);
|
||||
} elseif ($userInfo['division_type'] == 2) {
|
||||
app()->make(DivisionApplyServices::class)->delete(['uid' => $uid]);
|
||||
$userServices->update(['agent_id' => $uid], $data);
|
||||
} elseif ($userInfo['division_type'] == 3) {
|
||||
$userServices->update(['staff_id' => $uid], $data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置状态
|
||||
* @param $uid
|
||||
* @param $status
|
||||
* @return bool
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function setStatus($uid, $status)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userServices->update($uid, ['division_status' => $status]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分成比例
|
||||
* @param $uid
|
||||
* @param $storeBrokerageRatio
|
||||
* @param $storeBrokerageRatioTwo
|
||||
* @return array
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function divisionBrokerage($uid, $storeBrokerageRatio, $storeBrokerageRatioTwo)
|
||||
{
|
||||
if (!sys_config('brokerage_func_status')) {
|
||||
return [0, 0, 0, 0, 0];
|
||||
}
|
||||
$isSelfBrokerage = (int)sys_config('is_self_brokerage', 0);
|
||||
$division_open = (int)sys_config('division_status', 1);
|
||||
if (!$division_open) {
|
||||
/** 代理商关闭 */
|
||||
$storeBrokerageOne = $storeBrokerageRatio;
|
||||
$storeBrokerageTwo = $storeBrokerageRatioTwo;
|
||||
$staffPercent = 0;
|
||||
$agentPercent = 0;
|
||||
$divisionPercent = 0;
|
||||
} else {
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->get($uid);
|
||||
if ($userInfo['division_type'] == 1) {
|
||||
/** 自己是事业部 */
|
||||
$storeBrokerageOne = 0;
|
||||
$storeBrokerageTwo = 0;
|
||||
$staffPercent = 0;
|
||||
$agentPercent = 0;
|
||||
if ($userInfo['division_status'] == 1 && $userInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = $isSelfBrokerage ? $userInfo['division_percent'] : 0;
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
} elseif ($userInfo['division_type'] == 2) {
|
||||
/** 自己是代理商 */
|
||||
$divisionInfo = $userServices->get($userInfo['division_id']);
|
||||
$storeBrokerageOne = 0;
|
||||
$storeBrokerageTwo = 0;
|
||||
$staffPercent = 0;
|
||||
if ($userInfo['division_status'] == 1 && $userInfo['division_end_time'] > time()) {
|
||||
$agentPercent = $isSelfBrokerage ? $userInfo['division_percent'] : 0;
|
||||
} else {
|
||||
$agentPercent = 0;
|
||||
}
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], $agentPercent, 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
} elseif ($userInfo['division_type'] == 3) { // 自己是员工
|
||||
/** 自己是员工 */
|
||||
$agentInfo = $userServices->get($userInfo['agent_id']);
|
||||
$divisionInfo = $userServices->get($userInfo['division_id']);
|
||||
$storeBrokerageOne = 0;
|
||||
$storeBrokerageTwo = 0;
|
||||
if ($userInfo['division_status'] == 1 && $userInfo['division_end_time'] > time()) {
|
||||
$staffPercent = $isSelfBrokerage ? $userInfo['division_percent'] : 0;
|
||||
} else {
|
||||
$staffPercent = 0;
|
||||
}
|
||||
if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
|
||||
$agentPercent = bcsub($agentInfo['division_percent'], $staffPercent, 2);
|
||||
} else {
|
||||
$agentPercent = 0;
|
||||
}
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], bcadd($staffPercent, $agentPercent, 2), 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
} else {
|
||||
/** 自己是普通用户 */
|
||||
$staffInfo = $userServices->get($userInfo['staff_id']);
|
||||
$agentInfo = $userServices->get($userInfo['agent_id']);
|
||||
$divisionInfo = $userServices->get($userInfo['division_id']);
|
||||
|
||||
if ($userInfo['staff_id']) {
|
||||
/** 该用户为员工推广 */
|
||||
if ($userInfo['staff_id'] == $userInfo['spread_uid']) {
|
||||
/** 员工直接下级 */
|
||||
$storeBrokerageOne = $isSelfBrokerage ? $storeBrokerageRatio : 0;
|
||||
$storeBrokerageTwo = 0;
|
||||
if ($staffInfo['division_status'] == 1 && $staffInfo['division_end_time'] > time()) {
|
||||
$staffPercent = bcsub($staffInfo['division_percent'], $storeBrokerageOne, 2);
|
||||
} else {
|
||||
$staffPercent = 0;
|
||||
}
|
||||
if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
|
||||
$agentPercent = bcsub($agentInfo['division_percent'], bcadd($storeBrokerageOne, $staffPercent, 2), 2);
|
||||
} else {
|
||||
$agentPercent = 0;
|
||||
}
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], bcadd(bcadd($storeBrokerageOne, $staffPercent, 2), $agentPercent, 2), 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
} else {
|
||||
$storeBrokerageOne = $storeBrokerageRatio;
|
||||
$storeBrokerageTwo = $userServices->value(['uid' => $userInfo['spread_uid']], 'spread_uid') == $userInfo['staff_id'] && !$isSelfBrokerage ? 0 : $storeBrokerageRatioTwo;
|
||||
|
||||
$brokerageOneTwo = bcadd($storeBrokerageRatio, $storeBrokerageTwo, 2);
|
||||
|
||||
if ($staffInfo['division_status'] == 1 && $staffInfo['division_end_time'] > time()) {
|
||||
$staffPercent = bcsub($staffInfo['division_percent'], $brokerageOneTwo, 2);
|
||||
} else {
|
||||
$staffPercent = 0;
|
||||
}
|
||||
if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
|
||||
$agentPercent = bcsub($agentInfo['division_percent'], bcadd($brokerageOneTwo, $staffPercent, 2), 2);
|
||||
} else {
|
||||
$agentPercent = 0;
|
||||
}
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], bcadd(bcadd($brokerageOneTwo, $staffPercent, 2), $agentPercent, 2), 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
}
|
||||
} elseif ($userInfo['agent_id']) {
|
||||
/** 该用户为代理商推广 */
|
||||
if ($userInfo['agent_id'] == $userInfo['spread_uid']) {
|
||||
$storeBrokerageOne = $isSelfBrokerage ? $storeBrokerageRatio : 0;
|
||||
$storeBrokerageTwo = 0;
|
||||
$staffPercent = 0;
|
||||
if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
|
||||
$agentPercent = bcsub($agentInfo['division_percent'], $storeBrokerageOne, 2);
|
||||
} else {
|
||||
$agentPercent = 0;
|
||||
}
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], bcadd($storeBrokerageOne, $agentPercent, 2), 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
} else {
|
||||
$storeBrokerageOne = $storeBrokerageRatio;
|
||||
$storeBrokerageTwo = $userServices->value(['uid' => $userInfo['spread_uid']], 'spread_uid') == $userInfo['agent_id'] && !$isSelfBrokerage ? 0 : $storeBrokerageRatioTwo;
|
||||
$brokerageOneTwo = bcadd($storeBrokerageRatio, $storeBrokerageTwo, 2);
|
||||
$staffPercent = 0;
|
||||
if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
|
||||
$agentPercent = bcsub($agentInfo['division_percent'], $brokerageOneTwo, 2);
|
||||
} else {
|
||||
$agentPercent = 0;
|
||||
}
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], bcadd($brokerageOneTwo, $agentPercent, 2), 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
}
|
||||
} elseif ($userInfo['division_id']) {
|
||||
/** 该用户为事业部推广 */
|
||||
if ($userInfo['division_id'] == $userInfo['spread_uid']) {
|
||||
/** 事业部直接下级 */
|
||||
$storeBrokerageOne = $isSelfBrokerage ? $storeBrokerageRatio : 0;
|
||||
$storeBrokerageTwo = 0;
|
||||
$staffPercent = 0;
|
||||
$agentPercent = 0;
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], $storeBrokerageOne, 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
} else {
|
||||
$storeBrokerageOne = $storeBrokerageRatio;
|
||||
$storeBrokerageTwo = $userServices->value(['uid' => $userInfo['spread_uid']], 'spread_uid') == $userInfo['division_id'] && !$isSelfBrokerage ? 0 : $storeBrokerageRatioTwo;
|
||||
$brokerageOneTwo = bcadd($storeBrokerageRatio, $storeBrokerageTwo, 2);
|
||||
$staffPercent = 0;
|
||||
$agentPercent = 0;
|
||||
if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
|
||||
$divisionPercent = bcsub($divisionInfo['division_percent'], $brokerageOneTwo, 2);
|
||||
} else {
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/** 没有任何代理商关系 */
|
||||
$storeBrokerageOne = $storeBrokerageRatio;
|
||||
$storeBrokerageTwo = $storeBrokerageRatioTwo;
|
||||
$staffPercent = 0;
|
||||
$agentPercent = 0;
|
||||
$divisionPercent = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return [max($storeBrokerageOne, 0), max($storeBrokerageTwo, 0), max($staffPercent, 0), max($agentPercent, 0), max($divisionPercent, 0)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代理商下级员工列表
|
||||
* @param $where
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function staffList($where)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
/** @var StoreOrderServices $orderService */
|
||||
$orderService = app()->make(StoreOrderServices::class);
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$whereData = [
|
||||
'agent_id' => $where['agent_id'],
|
||||
'division_type' => 3,
|
||||
'is_del' => 0,
|
||||
'nickname' => $where['keyword'],
|
||||
];
|
||||
if ($where['start'] != 0) {
|
||||
$whereData['spread_time'] = [$where['start'], $where['stop']];
|
||||
}
|
||||
$count = $userServices->count($whereData);
|
||||
$brokerage = $orderService->sum(['division_agent_id' => $where['agent_id']], 'division_agent_brokerage');
|
||||
$list = $userServices->getList($whereData, 'avatar,nickname,uid,spread_uid,spread_time,division_percent,pay_count', $page, $limit);
|
||||
foreach ($list as &$item) {
|
||||
$item['spread_time'] = date('Y/m/d', $item['spread_time']);
|
||||
$item['childCount'] = $userServices->getCount(['agent_id' => $where['agent_id'], 'spread_uid' => $item['uid']]);
|
||||
$item['orderCount'] = $orderService->count(['uid' => $item['uid'], 'refund_status' => 0]);
|
||||
$item['numberCount'] = $orderService->sum(['uid' => $item['uid'], 'refund_status' => 0], 'pay_price');
|
||||
}
|
||||
return compact('list', 'count', 'brokerage');
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改员工返佣比例
|
||||
* @param $uid
|
||||
* @param $agentId
|
||||
* @param $divisionPercent
|
||||
* @return bool|\think\Response
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function staffPercent($uid, $agentId, $divisionPercent)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if ($divisionPercent >= $userServices->value(['uid' => $agentId], 'division_percent')) {
|
||||
throw new ApiException('返佣比例不能大于代理商返佣比例');
|
||||
}
|
||||
$userServices->update(['uid' => $uid, 'agent_id' => $agentId], ['division_percent' => $divisionPercent]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除员工
|
||||
* @param $uid
|
||||
* @param $agentId
|
||||
* @return bool
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function delStaff($uid, $agentId)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$data = [
|
||||
'division_name' => '',
|
||||
'division_type' => 0,
|
||||
'division_status' => 0,
|
||||
'division_id' => 0,
|
||||
'agent_id' => 0,
|
||||
'staff_id' => 0,
|
||||
'division_percent' => 0,
|
||||
'division_end_time' => 0,
|
||||
'division_change_time' => time(),
|
||||
'division_invite' => 0
|
||||
];
|
||||
$userServices->update(['uid' => $uid, 'agent_id' => $agentId], $data);
|
||||
$userServices->update(['staff_id' => $uid, 'agent_id' => $agentId], $data);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事业部订单列表
|
||||
* @param $adminInfo
|
||||
* @param $where
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function divisionOrder($adminInfo, $where)
|
||||
{
|
||||
if ($adminInfo['level'] != 0) {
|
||||
$where['division_id'] = $adminInfo['division_id'] ?: -1;
|
||||
}
|
||||
$where['division_id'] = $where['division_id'] != '' ? $where['division_id'] : -1;
|
||||
$where['division_agent_id'] = $where['division_agent_id'] != '' ? $where['division_agent_id'] : -1;
|
||||
$where['pid'] = 0;
|
||||
$where['is_del'] = 0;
|
||||
/** @var StoreOrderServices $orderServices */
|
||||
$orderServices = app()->make(StoreOrderServices::class);
|
||||
return $orderServices->getOrderList($where);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取区域代理下拉选项
|
||||
* @return array
|
||||
*/
|
||||
public function divisionOption()
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$list = $userServices->getColumn(['division_type' => 1], 'division_name', 'uid');
|
||||
$data = [];
|
||||
foreach ($list as $key => $value) {
|
||||
$data[] = ['value' => $key, 'label' => $value];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代理商下拉选项
|
||||
* @param int $division_id 区域代理ID
|
||||
* @return array
|
||||
*/
|
||||
public function agentOption($division_id)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$list = $userServices->getColumn(['division_type' => 2, 'division_id' => $division_id], 'division_name', 'uid');
|
||||
$data = [];
|
||||
foreach ($list as $key => $value) {
|
||||
$data[] = ['value' => $key, 'label' => $value];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事业部统计数据
|
||||
* @param $divisionId
|
||||
* @return array
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function divisionStatistics($divisionId)
|
||||
{
|
||||
$where = $divisionId != 0 ? ['division_id' => $divisionId] : [];
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$divisionNum = $userServices->count(['division_type' => 1]);
|
||||
$agentNum = $userServices->count(['division_type' => 2] + $where);
|
||||
$staffNum = $userServices->count(['division_type' => 3] + $where);
|
||||
/** @var StoreOrderServices $orderServices */
|
||||
$orderServices = app()->make(StoreOrderServices::class);
|
||||
$divisionOrderNum = $orderServices->count(['division_type' => 1, 'pid' => 0, 'paid' => 1] + $where);
|
||||
$orderPrice = $orderServices->sum(['division_type' => 1, 'pid' => 0, 'paid' => 1] + $where, 'pay_price', true);
|
||||
$divisionBrokerage = $orderServices->sum(['division_type' => 1, 'pid' => 0, 'paid' => 1] + $where, 'division_brokerage', true);
|
||||
$divisionAgentBrokerage = $orderServices->sum(['division_type' => 1, 'pid' => 0, 'paid' => 1] + $where, 'division_agent_brokerage', true);
|
||||
$divisionStaffBrokerage = $orderServices->sum(['division_type' => 1, 'pid' => 0, 'paid' => 1] + $where, 'division_staff_brokerage', true);
|
||||
$divisionBrokeragePrice = bcadd(bcadd($divisionBrokerage, $divisionAgentBrokerage, 2), $divisionStaffBrokerage, 2);
|
||||
return [
|
||||
[
|
||||
'name' => '区域代理数量',
|
||||
'count' => $divisionNum,
|
||||
'className' => 'md-contacts',
|
||||
'col' => 4,
|
||||
],
|
||||
[
|
||||
'name' => '代理商数量',
|
||||
'count' => $agentNum,
|
||||
'className' => 'md-contact',
|
||||
'col' => 4,
|
||||
],
|
||||
[
|
||||
'name' => '员工数量',
|
||||
'count' => $staffNum,
|
||||
'className' => 'md-cart',
|
||||
'col' => 4,
|
||||
],
|
||||
[
|
||||
'name' => '总订单数',
|
||||
'count' => $divisionOrderNum,
|
||||
'className' => 'md-bug',
|
||||
'col' => 4,
|
||||
],
|
||||
[
|
||||
'name' => '总订单金额',
|
||||
'count' => $orderPrice,
|
||||
'className' => 'md-basket',
|
||||
'col' => 4,
|
||||
],
|
||||
[
|
||||
'name' => '获得佣金金额',
|
||||
'count' => $divisionBrokeragePrice,
|
||||
'className' => 'ios-at-outline',
|
||||
'col' => 4,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事业部趋势数据
|
||||
* @param $time
|
||||
* @param $divisionId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function divisionTrend($time, $divisionId)
|
||||
{
|
||||
$timeArr = explode('-', $time);
|
||||
if (count($timeArr) != 2) throw new AdminException('参数错误');
|
||||
$dayCount = (strtotime($timeArr[1]) - strtotime($timeArr[0])) / 86400 + 1;
|
||||
$data = [];
|
||||
if ($dayCount == 1) {
|
||||
$data = $this->trend($timeArr, 0, $divisionId);
|
||||
} elseif ($dayCount > 1 && $dayCount <= 31) {
|
||||
$data = $this->trend($timeArr, 1, $divisionId);
|
||||
} elseif ($dayCount > 31 && $dayCount <= 92) {
|
||||
$data = $this->trend($timeArr, 3, $divisionId);
|
||||
} elseif ($dayCount > 92) {
|
||||
$data = $this->trend($timeArr, 30, $divisionId);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事业部趋势图数据
|
||||
* @param $time
|
||||
* @param $num
|
||||
* @param $divisionId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function trend($time, $num, $divisionId)
|
||||
{
|
||||
/** @var StoreOrderServices $storeOrder */
|
||||
$storeOrder = app()->make(StoreOrderServices::class);
|
||||
|
||||
if ($num == 0) {
|
||||
$xAxis = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
|
||||
$timeType = '%H';
|
||||
} elseif ($num != 0) {
|
||||
$dt_start = strtotime($time[0]);
|
||||
$dt_end = strtotime($time[1]);
|
||||
while ($dt_start <= $dt_end) {
|
||||
if ($num == 30) {
|
||||
$xAxis[] = date('Y-m', $dt_start);
|
||||
$dt_start = strtotime("+1 month", $dt_start);
|
||||
$timeType = '%Y-%m';
|
||||
} else {
|
||||
$xAxis[] = date('Y-m-d', $dt_start);
|
||||
$dt_start = strtotime("+$num day", $dt_start);
|
||||
$timeType = '%Y-%m-%d';
|
||||
}
|
||||
}
|
||||
}
|
||||
$pay_price = array_column($storeOrder->getDivisionTrend($time, $timeType, $divisionId, 'sum(pay_price)'), 'num', 'days');
|
||||
$pay_count = array_column($storeOrder->getDivisionTrend($time, $timeType, $divisionId, 'count(id)'), 'num', 'days');
|
||||
$data = $series = [];
|
||||
foreach ($xAxis as $item) {
|
||||
$data['订单金额'][] = isset($pay_price[$item]) ? floatval($pay_price[$item]) : 0;
|
||||
$data['订单量'][] = isset($pay_count[$item]) ? floatval($pay_count[$item]) : 0;
|
||||
}
|
||||
foreach ($data as $key => $item) {
|
||||
$series[] = [
|
||||
'name' => $key,
|
||||
'data' => $item,
|
||||
'type' => 'line',
|
||||
];
|
||||
}
|
||||
return compact('xAxis', 'series');
|
||||
}
|
||||
|
||||
/**
|
||||
* 事业部统计排行
|
||||
* @param $divisionId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/26
|
||||
*/
|
||||
public function divisionRanking($divisionId)
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
/** @var StoreOrderServices $orderServices */
|
||||
$orderServices = app()->make(StoreOrderServices::class);
|
||||
if ($divisionId == 0) {
|
||||
$data = $userServices->getUserList(['division_type' => 1, 'status' => 1, 'is_del' => 0], 'uid,nickname');
|
||||
} else {
|
||||
$data = $userServices->getUserList(['division_type' => 2, 'division_id' => $divisionId, 'status' => 1, 'is_del' => 0], 'uid,nickname');
|
||||
}
|
||||
$list = [];
|
||||
foreach ($data['list'] as $item) {
|
||||
$divisionBrokerage = $orderServices->sum(['division_id' => $item['uid'], 'pid' => 0, 'paid' => 1], 'division_brokerage', true);
|
||||
$divisionAgentBrokerage = $orderServices->sum(['division_id' => $item['uid'], 'pid' => 0, 'paid' => 1], 'division_agent_brokerage', true);
|
||||
$divisionStaffBrokerage = $orderServices->sum(['division_id' => $item['uid'], 'pid' => 0, 'paid' => 1], 'division_staff_brokerage', true);
|
||||
$list[] = [
|
||||
'nickname' => $item['nickname'],
|
||||
'spread_agent' => $userServices->count(['division_type' => 2, 'division_id' => $item['uid'], 'status' => 1, 'is_del' => 0]),
|
||||
'spread_staff' => $userServices->count(['division_type' => 3, 'division_id' => $item['uid'], 'status' => 1, 'is_del' => 0]),
|
||||
'order_price' => $orderServices->sum(['division_id' => $item['uid'], 'pid' => 0, 'paid' => 1], 'pay_price', true),
|
||||
'brokerage_price' => bcadd(bcadd($divisionBrokerage, $divisionAgentBrokerage, 2), $divisionStaffBrokerage, 2),
|
||||
'order_num' => $orderServices->count(['division_id' => $item['uid'], 'pid' => 0, 'paid' => 1])
|
||||
];
|
||||
}
|
||||
return compact('list');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代理商小程序推广码
|
||||
* @param $uid
|
||||
* @return false|mixed|string
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function agentSpreadCode($uid)
|
||||
{
|
||||
$userInfo = app()->make(UserServices::class)->getUserInfo($uid);
|
||||
if (!$userInfo) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
$name = 'division_agent_' . $userInfo['uid'] . '.jpg';
|
||||
/** @var QrcodeServices $QrcodeService */
|
||||
$QrcodeService = app()->make(QrcodeServices::class);
|
||||
$resForever = $QrcodeService->qrCodeForever($uid, 'division_agent');
|
||||
$url = $QrcodeService->getRoutineQrcodePath((int)$resForever['id'], $uid, 110, $name);
|
||||
return compact('url');
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定员工
|
||||
* @param $uid
|
||||
* @param int $agentId
|
||||
* @param int $agentCode
|
||||
* @return false|string
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2024/8/21
|
||||
*/
|
||||
public function agentSpreadStaff($uid, int $agentId = 0, int $agentCode = 0)
|
||||
{
|
||||
if ($agentCode && !$agentId) {
|
||||
/** @var QrcodeServices $qrCode */
|
||||
$qrCode = app()->make(QrcodeServices::class);
|
||||
if ($info = $qrCode->getOne(['id' => $agentCode, 'third_type' => 'division_agent', 'status' => 1])) {
|
||||
$agentId = $info['third_id'];
|
||||
}
|
||||
}
|
||||
if (!$agentId) throw new ApiException('上级代理商不存在');
|
||||
if ($uid == $agentId) throw new ApiException('自己不能推荐自己');
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$agentInfo = $userServices->getUserInfo($agentId, 'division_id,agent_id,division_type,division_end_time');
|
||||
if (!$agentInfo) throw new ApiException('上级用户不存在');
|
||||
$userInfo = $userServices->getUserInfo($uid, 'division_type');
|
||||
if (!$userInfo) throw new ApiException('用户不存在');
|
||||
if ($userInfo['division_type'] == 1) throw new ApiException('您是区域代理,不能绑定成为别人的员工');
|
||||
if ($userInfo['division_type'] == 2) throw new ApiException('您是代理商,不能绑定成为别人的员工');
|
||||
if ($userInfo['division_type'] == 3) throw new ApiException('您已经是员工,不能绑定成为别人的员工');
|
||||
$staffData = [
|
||||
'spread_uid' => $agentId,
|
||||
'spread_time' => time(),
|
||||
'division_type' => 3,
|
||||
'division_status' => 1,
|
||||
'division_id' => $agentInfo['division_id'],
|
||||
'agent_id' => $agentInfo['agent_id'],
|
||||
'staff_id' => $uid,
|
||||
'division_end_time' => $agentInfo['division_end_time'],
|
||||
'division_change_time' => time(),
|
||||
'is_promoter' => 1,
|
||||
];
|
||||
$res = $userServices->update($uid, $staffData);
|
||||
if ($res) return true;
|
||||
throw new ApiException('绑定员工失败');
|
||||
}
|
||||
}
|
||||
133
app/services/agent/PromoterApplyServices.php
Normal file
133
app/services/agent/PromoterApplyServices.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace app\services\agent;
|
||||
|
||||
use app\dao\agent\PromoterApplyDao;
|
||||
use app\services\BaseServices;
|
||||
use app\services\other\AgreementServices;
|
||||
use app\services\user\UserServices;
|
||||
use crmeb\exceptions\ApiException;
|
||||
use crmeb\services\FormBuilder as Form;
|
||||
use think\annotation\Inject;
|
||||
|
||||
class PromoterApplyServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var PromoterApplyDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected PromoterApplyDao $dao;
|
||||
|
||||
/**
|
||||
* 获取推广员申请信息
|
||||
* @param int $uid 用户ID
|
||||
* @param array $user 用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function applyInfo($uid, $user)
|
||||
{
|
||||
$applyInfo = $this->dao->get(['uid' => $uid, 'is_del' => 0]);
|
||||
$user = [
|
||||
'id' => $applyInfo['id'] ?? 0,
|
||||
'uid' => $uid,
|
||||
'nickname' => $user['nickname'] ?? '',
|
||||
'real_name' => $user['real_name'] ?? '',
|
||||
'phone' => $user['phone'] ?? '',
|
||||
'status' => $applyInfo ? $applyInfo['status'] : -1,
|
||||
'refusal_reason' => $applyInfo ? $applyInfo['refusal_reason'] : '',
|
||||
'add_time' => $applyInfo ? date('Y/m/d H:i', $applyInfo['add_time']) : '',
|
||||
'status_time' => $applyInfo && $applyInfo['status_time'] ? date('Y/m/d H:i', $applyInfo['status_time']) : '',
|
||||
];
|
||||
$agreement = app()->make(AgreementServices::class)->getAgreementBytype(2);
|
||||
return compact('user', 'agreement');
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请成为推广员
|
||||
* @param array $data 申请数据
|
||||
* @param int $id 申请记录ID
|
||||
* @param array $userInfo 用户信息
|
||||
* @return int
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function applyPromoter($data, $id, $userInfo)
|
||||
{
|
||||
if (!sys_config('brokerage_func_status')) throw new ApiException('未开启推广功能');
|
||||
if (sys_config('store_brokerage_statu') != 1) throw new ApiException('非指定分销模式无需申请推广员');
|
||||
if ($userInfo['is_promoter']) throw new ApiException('您已经是推广员');
|
||||
if ($data['phone'] != $userInfo['phone']) {
|
||||
$phoneUsed = app()->make(UserServices::class)->count(['phone' => $data['phone']]);
|
||||
if ($phoneUsed) throw new ApiException('该手机号已被使用');
|
||||
}
|
||||
if ($id) {
|
||||
$data['status'] = 0;
|
||||
$res = $this->dao->update(['id' => $id], $data);
|
||||
} else {
|
||||
$data['add_time'] = time();
|
||||
$this->dao->update(['uid' => $data['uid']], ['is_del' => 1]);
|
||||
$res = $this->dao->save($data);
|
||||
$id = $res->id;
|
||||
}
|
||||
if (!$res) throw new ApiException('申请失败');
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推广员申请列表
|
||||
* @param array $where 查询条件
|
||||
* @return array
|
||||
*/
|
||||
public function applyList($where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$list = $this->dao->applyList($where, $page, $limit);
|
||||
foreach ($list as &$item) {
|
||||
$item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
|
||||
$item['status_time'] = date('Y-m-d H:i:s', $item['status_time']);
|
||||
}
|
||||
$count = $this->dao->applyCount($where);
|
||||
return compact('list', 'count');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除推广员申请记录
|
||||
* @param int $id 申请记录ID
|
||||
* @return bool
|
||||
*/
|
||||
public function applyDelete($id)
|
||||
{
|
||||
$this->dao->update(['id' => $id], ['is_del' => 1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推广员审核表单
|
||||
* @param int $id 申请记录ID
|
||||
* @return array
|
||||
*/
|
||||
public function applyExamine($id)
|
||||
{
|
||||
$field = [];
|
||||
$field[] = Form::radio('status', '状态:', 1)->options([['label' => '通过', 'value' => 1], ['label' => '拒绝', 'value' => 2]]);
|
||||
$field[] = Form::textarea('refusal_reason', '备注:', '')->rows(5);
|
||||
return create_form('分销员审核', $field, $this->url('/agent/promoter/apply/examine/' . $id), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存推广员审核结果
|
||||
* @param int $id 申请记录ID
|
||||
* @param array $data 审核数据
|
||||
* @return bool
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function applyExamineSave($id, $data)
|
||||
{
|
||||
$info = $this->dao->get($id);
|
||||
if (!$info) throw new ApiException('申请不存在');
|
||||
$this->dao->update(['id' => $id], ['status' => $data['status'], 'refusal_reason' => $data['refusal_reason'], 'status_time' => time()]);
|
||||
if ($data['status'] == 1) {
|
||||
app()->make(UserServices::class)->update(['uid' => $info['uid']], ['is_promoter' => 1]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user