'ID', 'user_id' => '用户ID', 'mobile' => '用户手机号', 'nickname' => '用户昵称', 'level' => '会员等级', 'p_id' => '推荐人ID', 'p_nickname' => '推荐人昵称', 'p_mobile' => '推荐人号码', 'pay_money' => '成交金额', 'pay_num' => '成交订单数', ]; /** * 导出 * * @return array */ public static function getExportList() { return array_map(function ($k, $v) { return [ 'value' => $k, 'title' => $v ]; }, array_keys(static::EXPORT), array_values(static::EXPORT)); } /** * 导出 * * @param array $data * @return void */ public static function exportHandle(array $data) { ini_set('memory_limit', '512M'); $download_id = $data['download_id'] ?? 0; /** * @var Download */ $download = Download::findOne(['id' => $download_id]); try { if ($download) { $data = json_decode($download->params, true); $params = $data['params']; $fields = $data['fields']; // 3是我的团队 仅导出我的团队 if ($params['status'] == 3) { $user_id = $params['user_id']; $keyword = $params['keyword']; $search_type = $params['search_type']; $child_ids = MallUserPartitionRelationship::getChildIds($user_id, $keyword, $search_type); $where[] = ['in', 'u.id', $child_ids]; $select = ['u.id', 'u.nickname', 'u.mobile', 'u.level', 'u.avatar_url', 'ust.user_id', 'ust.pay_money', 'ust.pay_num']; $join = [['LEFT JOIN', UserStatisticsTotal::tableName() . ' as ust', 'u.id=ust.user_id']]; array_push($select, 'p.id p_id', 'p.avatar_url p_avatar_url', 'p.nickname p_nickname', 'p.mobile p_mobile'); array_push($join, ['LEFT JOIN', MallUser::tableName() . ' as p', 'p.id=u.parent_id']); $params = [ 'alias' => 'u', 'where' => $where, 'select' => $select, 'join' => $join ]; $model = User::find(); User::paramPack($params, $model); $csv = new CsvTool(); $csv->filename = $download->name; $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($download->type); $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC; $fields_arr = static::EXPORT; $have_select_field_arr = []; foreach ($fields as $value) { $have_select_field_arr[] = $fields_arr[$value]; } $csv->header = $have_select_field_arr; // 获取会用等级 $levels = UserLevel::getListIndexByLevelColumn($download['mall_id'], [StatusEnum::ENABLED,StatusEnum::DISABLED]); //遍历组装数据 foreach ($model->asArray()->batch() as $list) { //查询上级昵称数组 $csv->data = array_map(function ($item) use ($fields, $levels) { $row = []; foreach ($fields as $key) { $row[$key] = $item[$key]; if ($key == 'user_id') {// 当用户id为空时,取id $user_id = $item['user_id'] ?? $item['id']; $row['user_id'] = $user_id; } if ($key == 'level') { $row['level'] = $levels[$item['level']] ?? '普通用户'; } } return $row; }, $list); $csv->export(); } $csv->updateDown($download, $download->mall_id); } return true; } } catch (Exception $e) { $download && $download->status = 3; $download && $download->save(); return false; } } }