request->isGet) { return $this->error('请求方式错误'); } $parent_agent = CloudStockTwoAgent::getUserParentAgent($this->user_id, $this->stock_agent->level); if (!empty($parent_agent)) { $parent_user = User::find() ->select('id,nickname,mobile,avatar_url,inviter_at') ->where(['mall_id' => $this->mall_id, 'id' => $parent_agent->user_id, 'status' => StatusEnum::ENABLED]) ->one(); $parent_agent_info = [ 'id' => $parent_agent->id, 'user_id' => $parent_agent->user_id, 'level' => $parent_agent->level, 'level_name' => $parent_agent->stockLevel->name, 'nickname' => $parent_user->nickname ?? '平台', 'avatar_url' => $parent_user->avatar_url ?? \Yii::$app->request->hostInfo . '/resources/img/default_avatar_url.png', 'mobile' => $parent_user->mobile ?? '', ]; } else { $parent_agent_info = [ 'id' => 0, 'user_id' => 0, 'level' => 0, 'level_name' => '默认等级', 'nickname' => '平台', 'avatar_url' => \Yii::$app->request->hostInfo . '/resources/img/default_avatar_url.png', 'mobile' => '', ]; } return $this->success('success', $parent_agent_info); } /** * @name: * @msg: 我的批发商列表 * @param {*} * @return {*} */ public function actionWholesalerList() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $page = \Yii::$app->request->get('page', 1); $limit = \Yii::$app->request->get('limit', $this->pageSize); $user_keyword = \Yii::$app->request->get('user_keyword'); $level = \Yii::$app->request->get('level'); // 批发商列表 if (!empty($user_keyword)) { $users = User::find() ->select('id,nickname,mobile,avatar_url,inviter_at') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->andWhere(['or', ['like', 'id', $user_keyword], ['like', 'nickname', $user_keyword]]) ->asArray() ->indexBy('id') ->all(); $child_id_arr = array_column($users, 'id'); } else { $child_id_arr = UserPartitionRelationship::getChildIdArr($this->user_id); $child_id_arr = array_reverse($child_id_arr); } $wheres[] = [ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $child_id_arr, ]; // 批发商等级比自己小的下级 $wheres[] = ['<', 'level', $this->stock_agent->level]; if (!empty($level)) { $wheres[] = ['level' => $level]; } // 统计 query 对象 $stat_query = CloudStockTwoAgent::find(); foreach ($wheres as $where) { $stat_query->andWhere($where); } $params = [ 'select' => 'id,mall_id,user_id,level,created_at', 'where' => $wheres, 'order' => 'created_at desc', 'page' => $page, 'limit' => $limit, ]; $child_agent_list = CloudStockTwoAgent::listPage($params); if (empty($child_agent_list['list'])) { if (!is_array($child_agent_list['list'])) { return $this->error(CloudStockTwoAgent::getStaticError()); } } if (!empty($users)) { $child_users = $users; $user_ids = array_column($child_users, 'user_id'); } else { $user_ids = array_column($child_agent_list['list'], 'user_id'); $child_users = User::find() ->select('id,nickname,mobile,avatar_url,inviter_at') ->where(['mall_id' => $this->mall_id, 'id' => $user_ids, 'status' => StatusEnum::ENABLED]) ->asArray() ->indexBy('id') ->all(); } $child_good_numbers = CloudStockTwoAgentGoods::find() ->select('user_id,sum(num) as stock_good_counts') ->where(['mall_id' => $this->mall_id, 'user_id' => $user_ids, 'status' => StatusEnum::ENABLED]) ->groupBy('user_id') ->asArray() ->indexBy('user_id') ->all(); $stock_levels = CloudStockTwoLevel::find() ->select('id,level,name as level_name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->indexBy('level') ->all(); foreach ($child_agent_list['list'] as &$list) { $user = $child_users[$list['user_id']] ?? []; $good_number = $child_good_numbers[$list['user_id']] ?? []; $list['level_name'] = $stock_levels[$list['level']]['level_name'] ?? '默认等级'; $list['nickname'] = $user['nickname'] ?? ''; $list['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png'; $list['mobiel'] = $user['mobile'] ?? ''; $list['inviter_at'] = $user['inviter_at'] ?? 0; $list['stock_good_counts'] = $good_number['stock_good_counts'] ?? 0; } $this_month = DateHelper::thisMonth(); $this_week = DateHelper::thisWeek(); $all_child_agent = $month_number = $week_number = 0; foreach ($stat_query->each(100) as $agent) { if ($agent['created_at'] >= $this_week['start'] && $agent['created_at'] <= $this_week['end']) { $week_number += 1; } if ($agent['created_at'] >= $this_month['start'] && $agent['created_at'] <= $this_month['end']) { $month_number += 1; } $all_child_agent += 1; } $child_agent_list['all_child_agent'] = $all_child_agent; $child_agent_list['week_number'] = $week_number; $child_agent_list['month_number'] = $month_number; $child_agent_list['level_list'] = array_values($stock_levels); return $this->success('success', $child_agent_list); } /** * @name: * @msg: 设置代理商,获取等级和设置名额数据 * @param {*} * @return {*} */ public function actionSetWholesalerInfo() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $levels = CloudStockTwoLevel::getLevelList($this->mall_id); if ($this->stock_agent->level < 2) { $give_quotas = []; } else { $give_quotas = CloudStockTwoAgentQuota::find() ->select('id,mall_id,user_id,level,num,status as is_use') ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); if (!empty($give_quotas)) { foreach ($give_quotas as &$give_quota) { $give_quota['name'] = $levels[$give_quota['level']]['level_name'] ?? '默认等级'; } } } return $this->success('success', ['level_list' => array_values($levels), 'give_quota' => $give_quotas]); } /** * @name: * @msg: 根据 id 获取会员基本信息 * @param {*} * @return {*} */ public function actionUserInfo() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $user_keyword = \Yii::$app->request->get('user_keyword'); if (empty($user_keyword)) return $this->error('参数错误'); $user = User::find() ->select('id,nickname,avatar_url,mobile') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_keyword]) ->asArray() ->one(); return $this->success('success', $user); } /** * @name: * @msg: 设定代理商 * @param {integer} $user_id * @param {integer} $level * @return {*} */ public function actionSetWholesaler() { if (!\Yii::$app->request->isPost) { return $this->error('请求方式错误'); } $user_id = \Yii::$app->request->post('user_id'); $level = \Yii::$app->request->post('level'); if (empty($user_id) || empty($level)) return $this->error('参数错误'); if ($this->stock_agent->level < 2) return $this->error('您当前等级不能设置代理商'); $is_give_quota = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic.is_give_quota'); if ($is_give_quota != 1) return $this->error('系统未开启设置代理商功能'); $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]); if (empty($user)) return $this->error('目标会员不存在'); $agent = CloudStockTwoAgent::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id]); if (!empty($agent)) return $this->error('目标会员已经是代理商'); $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id); // dd($parent_id_arr); if (!in_array($this->user_id, $parent_id_arr)) return $this->error('目标会员不是您的下级会员'); $level_info = CloudStockTwoLevel::getLevelList($this->mall_id, $level); $give_quota = CloudStockTwoLevel::getGiveQuota($this->mall_id, $level, $this->user_id); if ($give_quota <= 0) { return $this->error('设定代理[等级:' . $level_info['level_name'] . ']的名额已经用完'); } $params = [ 'user_id' => $this->user_id, 'agent_id' => $this->stock_agent->id, 'child_id' => $user_id, 'set_level' => $level, 'upgrade_status' => CloudStockTwoEnum::UPGRADE_STATUS_BY_PARENT, ]; $res = CloudStockTwoAgent::setWholesaler($this->mall_id, $params); if (false === $res) return $this->error(CloudStockTwoAgent::getStaticError()); return $this->success('操作成功'); } /** * @name: * @msg: 我的报表数据 * @param {*} * @return {*} */ public function actionReportForm() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $date = date('Y-m'); $start_time = strtotime($date . '-01 00:00:00'); $end_time = strtotime($date . '-' . date('t', $start_time) . ' 23:59:59'); // 本月收入/支出数据 $in_income = $out_income = 0; $asset_logs = CommissionLog::find() ->select('id,mall_id,user_id,change_type,change_num,current_num,desc,from_type,capital_type,created_at') ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id,'from_type'=>AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO]) ->andWhere(['and', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]]) ->orderBy('created_at desc') ->all(); if (!empty($asset_logs)) { foreach ($asset_logs as $log) { if ('sub' == $log->change_type) $out_income += $log->change_num; if ('add' == $log->change_type) $in_income += $log->change_num; } } // 本月出库/入库数据 $out_stock = $in_stock = 0; $dedution_log_ids = CloudStockTwoDeductionDetail::find() ->select('stock_log_id') ->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->column(); $increase_log_ids = CloudStockTwoIncreaseDetail::find() ->select('stock_log_id') ->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->column(); $log_ids = array_merge($dedution_log_ids, $increase_log_ids); $stock_logs = CloudStockTwoGoodsStockLog::find() ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $log_ids]) ->andWhere(['and', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]]) ->orderBy('created_at desc') ->all(); if (!empty($stock_logs)) { foreach ($stock_logs as $log) { if (CloudStockTwoEnum::STOCK_CHANGE_SUB == $log->changes_dir) $out_stock += $log->num; if (CloudStockTwoEnum::STOCK_CHANGE_ADD == $log->changes_dir) $in_stock += $log->num; } } return $this->success('success', compact('in_income', 'out_income', 'out_stock', 'in_stock')); } }