request->isGet) { return $this->error('请求方式错误'); } $parent_agent = CloudStockAgent::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'); // 批发商列表 $child_id_arr = UserPartitionRelationship::getChildIdArr($this->user_id); $child_id_arr = array_reverse($child_id_arr); 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(); $uids = array_column($users, 'id'); $child_id_arr = array_intersect($child_id_arr,$uids); } //跟自身平级的代理商下的所有代理都不会找自身补货 $cloud_stock_agent = CloudStockAgent::findOne(['user_id'=>$this->user_id,'status'=>[StatusEnum::DISABLED, StatusEnum::ENABLED],'mall_id'=>$this->mall_id]); if(!empty($child_id_arr)){ $condition = []; $condition[] = ['mall_id'=> $this->mall_id]; $condition[] = ['status'=> [StatusEnum::DISABLED, StatusEnum::ENABLED]]; $condition[] = ['user_id'=> $child_id_arr]; $cloud_stock_agent_list = CloudStockAgent::lists(['where'=>$condition]); $children_ids = []; foreach ($cloud_stock_agent_list as $agent){ if($agent['level']>=$cloud_stock_agent['level']){ $res = UserPartitionRelationship::getChildIdArr($agent['user_id']); $children_ids=array_merge($children_ids,$res); $children_ids[] = $agent['user_id']; } } foreach ($cloud_stock_agent_list as $k=>$agent){ if(in_array($agent['user_id'],$children_ids)){ unset($cloud_stock_agent_list[$k]); } } $child_id_arr = array_column($cloud_stock_agent_list,'user_id'); } $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 = CloudStockAgent::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 = CloudStockAgent::listPage($params); if (empty($child_agent_list['list'])) { if (!is_array($child_agent_list['list'])) { return $this->error(CloudStockAgent::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 = CloudStockAgentGoods::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 = CloudStockLevel::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(); $stat_query_week = clone $stat_query; $stat_query_month = clone $stat_query; $all_child_agent = $stat_query->count(); $week_number = $stat_query_week->andWhere(['>=','created_at',$this_week['start']])->andWhere(['<=','created_at',$this_week['end']])->count(); $month_number = $stat_query_month->andWhere(['>=','created_at',$this_month['start']])->andWhere(['<=','created_at',$this_month['end']])->count(); $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 = CloudStockLevel::getLevelList($this->mall_id); if ($this->stock_agent->level < 2) { $give_quotas = []; } else { $give_quotas = CloudStockAgentQuota::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'); $user_keyword = trim($user_keyword); if (empty($user_keyword)) return $this->error('参数错误'); $userModel = User::find() ->select('id,nickname,avatar_url,mobile') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]); // 判断是否符合国内手机号11位 如果不符合则按用户id查询 $pattern = '/^1\d{10}$/'; if (preg_match($pattern, $user_keyword)) { $userModel->andWhere(['mobile' => $user_keyword]); } else { $userModel->andWhere(['id' => $user_keyword]); } $user = $userModel->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, '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 = CloudStockAgent::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 = CloudStockLevel::getLevelList($this->mall_id, $level); $give_quota = CloudStockLevel::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' => CloudStockEnum::UPGRADE_STATUS_BY_PARENT, ]; $res = CloudStockAgent::setWholesaler($this->mall_id, $params); if (false === $res) return $this->error(CloudStockAgent::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]) ->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 = CloudStockDeductionDetail::find() ->select('stock_log_id') ->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->column(); $increase_log_ids = CloudStockIncreaseDetail::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 = CloudStockGoodsStockLog::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 (CloudStockEnum::STOCK_CHANGE_SUB == $log->changes_dir) $out_stock += $log->num; if (CloudStockEnum::STOCK_CHANGE_ADD == $log->changes_dir) $in_stock += $log->num; } } return $this->success('success', compact('in_income', 'out_income', 'out_stock', 'in_stock')); } /** * 我的批发商列表-最新 * @return mixed|void */ public function actionWholesalerListNew() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $user_keyword = \Yii::$app->request->get('user_keyword'); $level = \Yii::$app->request->get('level'); $get_user_partition_relationship_id = \Yii::$app->request->get('user_partition_relationship_id'); $user_partition_relationship = UserPartitionRelationship::findOne(['user_id'=>$this->user_id]); $cloud_stock_agent = CloudStockAgent::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]); $i = 1; $page_size = 100; $temp_list = []; $stock_levels = CloudStockLevel::find() ->select('id,level,name as level_name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->indexBy('level') ->all(); $user_partition_relationship_id = 0; while (true){ $query = UserPartitionRelationship::find() ->where(['!=','user_id',$this->user_id]) ->andWhere(['like','tree',$user_partition_relationship['tree'].'%',false]); if(!empty($get_user_partition_relationship_id)) $query->andWhere(['>','id',$get_user_partition_relationship_id]); if(!empty($user_keyword)) $query->andWhere(['=','user_id',$user_keyword]); $child_list = $query->select('id,user_id')->offset(($i - 1) * $page_size)->limit($page_size)->orderBy('id asc')->all(); if(empty($child_list)) break; $user_ids = array_column($child_list,'user_id'); $user_id_arr = array_column($child_list,'id','user_id'); $where = [['status'=>StatusEnum::ENABLED], ['user_id'=>$user_ids],]; $where[]= ['<','level',$cloud_stock_agent['level']]; if(!empty($level)){ $where[]= ['level'=>$level]; } $cloud_stock_agent_list = CloudStockAgent::lists(['where'=>$where,'with'=>'user']); foreach ($cloud_stock_agent_list as $item){ $parent_ids = UserPartitionRelationship::getParentIdArr($item['user_id']); $temp_parent_ids = []; $parent_ids = array_reverse($parent_ids); foreach ($parent_ids as $uid){ if($uid==$this->user_id) break; $temp_parent_ids[]=$uid; } if(empty($temp_parent_ids)){ $temp_list[]=$item; if(count($temp_list)>=5){ $user_partition_relationship_id = $user_id_arr[$item['user_id']]; break; } }else{ $temp_cloud_stock_agent_list = CloudStockAgent::lists([ 'where'=>[ ['status'=>StatusEnum::ENABLED], ['user_id'=>$temp_parent_ids], ['>=','level',$cloud_stock_agent['level']] ] ]); if(empty($temp_cloud_stock_agent_list)){ $user_partition_relationship_id = $user_id_arr[$item['user_id']]; $temp_list[]=$item; if(count($temp_list)>=5) { $user_partition_relationship_id = $user_id_arr[$item['user_id']]; break; } } } } if(count($temp_list)>=5) break; $i++; } $user_ids = array_column($temp_list,'user_id'); $child_good_numbers = CloudStockAgentGoods::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(); $list = []; foreach ($temp_list as $item){ $good_number = $child_good_numbers[$item['user_id']] ?? []; $list[]=[ 'avatar_url'=>$item['user']['avatar_url'], 'inviter_at'=>$item['user']['inviter_at'], 'mobiel'=>$item['user']['mobile'], 'nickname'=>$item['user']['nickname'], 'created_at'=>$item['created_at'], 'user_id'=>$item['user_id'], 'level'=>$item['level'], 'level_name'=>$stock_levels[$item['level']]['level_name'] ?? '默认等级', 'id'=>$item['id'], 'mall_id'=>$item['mall_id'], 'stock_good_counts'=>$good_number['stock_good_counts'] ?? 0, ]; } return $this->success('success', ['list'=>$list,'user_partition_relationship_id'=>$user_partition_relationship_id]); } /** * 我的批发商-统计 * @return mixed|void */ public function actionWholesalerTotal() { ini_set('memory_limit','1024M'); set_time_limit(0); if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $user_keyword = \Yii::$app->request->get('user_keyword'); $level = \Yii::$app->request->get('level'); $get_user_partition_relationship_id = \Yii::$app->request->get('user_partition_relationship_id'); $user_partition_relationship = UserPartitionRelationship::findOne(['user_id'=>$this->user_id]); $cloud_stock_agent = CloudStockAgent::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]); $i = 1; $page_size = 100; $all_child_agent = 0; $month_number = 0; $week_number = 0; $this_week = DateHelper::thisWeek(); $this_month = DateHelper::thisMonth(); while (true){ $query = UserPartitionRelationship::find() ->where(['!=','user_id',$this->user_id]) ->andWhere(['like','tree',$user_partition_relationship['tree'].'%',false]); if(!empty($get_user_partition_relationship_id)) $query->andWhere(['>','id',$get_user_partition_relationship_id]); if(!empty($user_keyword)) $query->andWhere(['=','user_id',$user_keyword]); $child_list = $query->select('id,user_id')->offset(($i - 1) * $page_size)->limit($page_size)->orderBy('id asc')->all(); if(empty($child_list)) break; $user_ids = array_column($child_list,'user_id'); $where = [['status'=>StatusEnum::ENABLED], ['user_id'=>$user_ids],]; if(!empty($level)){ $where[] = ['level'=>$level]; }else{ $where[] = ['<','level',$cloud_stock_agent['level']]; } $cloud_stock_agent_list = CloudStockAgent::lists(['where'=>$where,'with'=>'user']); foreach ($cloud_stock_agent_list as $item){ $parent_ids = UserPartitionRelationship::getParentIdArr($item['user_id']); $temp_parent_ids = []; $parent_ids = array_reverse($parent_ids); foreach ($parent_ids as $uid){ if($uid==$this->user_id) break; $temp_parent_ids[]=$uid; } if(empty($temp_parent_ids)){ $all_child_agent++; if($item['created_at']>=$this_week['start']&&$item['created_at']<=$this_week['end']) $week_number++; if($item['created_at']>=$this_month['start']&&$item['created_at']<=$this_month['end']) $month_number++; }else{ $temp_cloud_stock_agent_list = CloudStockAgent::lists([ 'where'=>[ ['status'=>StatusEnum::ENABLED], ['user_id'=>$temp_parent_ids], ['>=','level',$cloud_stock_agent['level']] ] ]); if(empty($temp_cloud_stock_agent_list)){ $all_child_agent++; if($item['created_at']>=$this_week['start']&&$item['created_at']<=$this_week['end']) $week_number++; if($item['created_at']>=$this_month['start']&&$item['created_at']<=$this_month['end']) $month_number++; } } } $i++; } $stock_levels = CloudStockLevel::find() ->select('id,level,name as level_name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->indexBy('level') ->all(); return $this->success('success', ['level_list'=>array_values($stock_levels),'all_child_agent'=>$all_child_agent,'month_number'=>$month_number,'week_number'=>$week_number]); } public function actionWithdraw() { try { $params = Yii::$app->request->post(); $data = (new WithdrawService(['mall_id' => $this->mall_id,'user_id'=>$this->user_id]))->withdraw($params); return $this->success('操作成功', $data); } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } public function actionCashSetting(){ try { $request = Yii::$app->request; if ($request->isGet) { $params = Yii::$app->request->get(); $data = (new WithdrawService(['mall_id' => $this->mall_id,'user_id'=>$this->user_id]))->cashSetting(); return $this->success('操作成功', $data); } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } public function actionTransferBalance() { try { $request = Yii::$app->request; if ($request->isPost) { $params = Yii::$app->request->post(); $data = (new WithdrawService(['mall_id' => $this->mall_id,'user_id'=>$this->user_id]))->transferBalance($params); return $this->success('操作成功', $data); } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * 修改代理配置 * * @return void */ public function actionModifyInfo() { $post = Yii::$app->request->post(); // 验证 $res = Yii::$app->services->validate->validate($post,[ [['is_stock_deducted_by_subordinate'], 'integer'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); if (empty($post)) { $this->success(); return; } $service = new AgentService(['mall_id' => $this->mall_id]); $service->modifyInfo($this->user_id, $post); $this->success(); } }