auth->id; $agents = AgentModel::with(['agentLevel']) ->where('user_id', $userId) ->order('id desc') ->select(); $delayMoney = RewardModel::waiting()->where('agent_user_id', $userId)->sum('commission'); $payload = [ 'agents' => $agents, 'delay_money' => round($delayMoney, 2), 'apply_form' => $this->_config->getApplyForm(), ]; if (class_exists('\\addons\\shopro_huifu\\library\\onboard\\HuifuOnboardGate')) { $latestAgent = AgentModel::where('user_id', $userId)->order('id desc')->find(); $refId = $latestAgent ? $latestAgent['id'] : 0; $onboardRow = $refId ? \addons\shopro_huifu\model\Onboard::findByRole($userId, 'regional_agent', $refId) : null; $agentStatus = $latestAgent ? ($latestAgent['status'] ?? '') : ''; $payload['huifu_onboard'] = \addons\shopro_huifu\library\onboard\HuifuOnboardGate::publicStatus( $onboardRow, $userId, 'regional_agent', $refId, $agentStatus ); $payload['agent_check'] = $this->_config->isAgentCheck(); } $this->success('获取成功', $payload); } /** * 申请成为区域代理 */ public function apply() { $areaId = intval($this->request->post('area_id')); $areaLevel = intval($this->request->post('area_level')); $applyData = $this->request->post('data/a', []); if (!$areaId || !$areaLevel) { $this->error('请选择公司区域'); } $exists = AgentModel::where('user_id', $this->auth->id) ->where('area_id', $areaId) ->where('status', '<>', AgentModel::STATUS_FORBIDDEN) ->find(); if ($exists) { $this->error('您已申请或已是该区域公司'); } $conflict = AgentModel::available() ->where('area_id', $areaId) ->where('area_level', $areaLevel) ->find(); if ($conflict) { $this->error('该区域已有公司'); } $area = Area::get($areaId); $level = LevelModel::where('area_level', $areaLevel)->order('level asc')->find(); if (!$level) { $this->error('公司等级不存在'); } $huifuRequired = class_exists('\\addons\\shopro_huifu\\library\\onboard\\HuifuOnboardGate') && \addons\shopro_huifu\library\onboard\HuifuOnboardGate::isOnboardRequired(); // 汇付分账模式下必须后台审核;否则遵循区域公司申请审核开关 $status = ($this->_config->isAgentCheck() || $huifuRequired) ? AgentModel::STATUS_PENDING : AgentModel::STATUS_NORMAL; $userId = $this->auth->id; $huifuOnboard = $this->request->post('huifu_onboard/a', []); \think\Db::transaction(function () use ($level, $areaId, $areaLevel, $area, $applyData, $status, $userId, $huifuOnboard) { $agentRow = AgentModel::create([ 'user_id' => $userId, 'level' => $level['level'], 'area_id' => $areaId, 'area_level' => $areaLevel, 'area_name' => $area ? $area['name'] : '', 'apply_info' => json_encode($applyData, JSON_UNESCAPED_UNICODE), 'status' => $status, 'becometime' => $status === AgentModel::STATUS_NORMAL ? time() : null, ]); if (class_exists('\\addons\\shopro_huifu\\library\\Bridge') && !empty($huifuOnboard)) { $hookParams = [ 'user_id' => $userId, 'regional_agent_id' => $agentRow['id'], 'huifu_onboard' => $huifuOnboard, ]; try { \think\Hook::listen('shopro_regional_agent_apply_after', $hookParams); } catch (\Throwable $e) { $this->error($e->getMessage() ?: '汇付进件提交失败'); } } }); if ($status === AgentModel::STATUS_PENDING) { $msg = $huifuRequired ? '申请已提交,请等待后台审核;审核通过后请到「提现」页绑定进件资料' : '申请已提交,请等待后台审核'; } else { $msg = '申请已通过,您已成为该区域公司'; if ($huifuRequired && class_exists('\\addons\\shopro_huifu\\library\\onboard\\HuifuOnboardGate')) { \addons\shopro_huifu\library\onboard\HuifuOnboardGate::submitOnBusinessApproved( $userId, 'regional_agent', AgentModel::where('user_id', $userId)->order('id desc')->value('id') ); } } $this->success($msg, ['status' => $status]); } /** * 区域业绩统计 */ public function stats() { $userId = $this->auth->id; $startTime = $this->request->get('start_time'); $endTime = $this->request->get('end_time'); $start = $startTime ? (is_numeric($startTime) ? intval($startTime) : strtotime($startTime)) : 0; $end = $endTime ? (is_numeric($endTime) ? intval($endTime) : strtotime($endTime . ' 23:59:59')) : 0; $agents = AgentModel::where('user_id', $userId)->where('status', AgentModel::STATUS_NORMAL)->select(); $stats = []; $totalOrderCount = 0; $totalOrderMoney = 0; $totalIncome = 0; $totalDelay = 0; foreach ($agents as $agent) { $orderCountQuery = RegionalOrderModel::where('agent_id', $agent['id']); $orderSumQuery = RegionalOrderModel::where('agent_id', $agent['id']); $rewardSumQuery = RewardModel::where('agent_id', $agent['id'])->where('status', 1); if ($start && $end) { $orderCountQuery->where('createtime', 'between', [$start, $end]); $orderSumQuery->where('createtime', 'between', [$start, $end]); $rewardSumQuery->where('createtime', 'between', [$start, $end]); } $orderCount = $orderCountQuery->count(); $orderMoney = round($orderSumQuery->sum('amount'), 2); $income = round($rewardSumQuery->sum('commission'), 2); $delayMoney = round(RewardModel::waiting()->where('agent_user_id', $userId)->where('agent_id', $agent['id'])->sum('commission'), 2); $stats[] = [ 'agent' => $agent, 'order_count' => $orderCount, 'order_money' => $orderMoney, 'total_income' => $income, 'delay_money' => $delayMoney, ]; $totalOrderCount += $orderCount; $totalOrderMoney += $orderMoney; $totalIncome += $income; $totalDelay += $delayMoney; } $this->success('获取成功', [ 'list' => $stats, 'summary' => [ 'order_count' => $totalOrderCount, 'order_money' => round($totalOrderMoney, 2), 'total_income' => round($totalIncome, 2), 'delay_money' => round($totalDelay, 2), ], ]); } /** * 区域订单列表 */ public function orders() { $userId = $this->auth->id; $perPage = $this->request->get('per_page', 10); $startTime = $this->request->get('start_time'); $endTime = $this->request->get('end_time'); $query = RegionalOrderModel::where('agent_user_id', $userId); if ($startTime && $endTime) { $start = is_numeric($startTime) ? intval($startTime) : strtotime($startTime); $end = is_numeric($endTime) ? intval($endTime) : strtotime($endTime . ' 23:59:59'); $query->where('createtime', 'between', [$start, $end]); } $orders = $query->order('id desc')->paginate($perPage); $this->success('获取成功', $orders); } /** * 佣金明细 */ public function rewards() { $userId = $this->auth->id; $perPage = $this->request->get('per_page', 10); $startTime = $this->request->get('start_time'); $endTime = $this->request->get('end_time'); $status = $this->request->get('status'); $query = RewardModel::where('agent_user_id', $userId); if ($startTime && $endTime) { $start = is_numeric($startTime) ? intval($startTime) : strtotime($startTime); $end = is_numeric($endTime) ? intval($endTime) : strtotime($endTime . ' 23:59:59'); $query->where('createtime', 'between', [$start, $end]); } if ($status !== null && $status !== '') { $query->where('status', intval($status)); } $rewards = $query->order('id desc')->paginate($perPage); $this->success('获取成功', $rewards); } /** * 可代理等级列表 */ public function levels() { $levels = LevelModel::order('level asc')->select(); $this->success('获取成功', $levels); } }