|
9 | 9 | namespace App\Http\Repository;
|
10 | 10 |
|
11 | 11 |
|
| 12 | +use App\Models\ApiExcel; |
| 13 | +use App\Models\ApiExcelLogs; |
12 | 14 | use Illuminate\Support\Facades\Log;
|
13 | 15 | use PhpOffice\PhpSpreadsheet\IOFactory;
|
14 | 16 | use PhpOffice\PhpSpreadsheet\Reader\Exception;
|
@@ -123,10 +125,11 @@ public function newLoadExcel()
|
123 | 125 | *
|
124 | 126 | * @param $param
|
125 | 127 | * @param $result
|
| 128 | + * @param $file |
126 | 129 | *
|
127 |
| - * @return bool|string |
| 130 | + * @return string |
128 | 131 | */
|
129 |
| - public function saveExcel($param, $result) |
| 132 | + public function saveExcel($param, $result, $file = '') |
130 | 133 | {
|
131 | 134 | /************************* 2. 写入 Excel 文件 ******************************/
|
132 | 135 | // 首先创建一个新的对象 PHPExcel object
|
@@ -239,17 +242,55 @@ public function saveExcel($param, $result)
|
239 | 242 | // is_dir($path) || mkdir($path, 777, true);
|
240 | 243 | $did = explode('/', $param['website']);
|
241 | 244 | $did = is_numeric(end($did)) ? end($did) : '999';
|
242 |
| - $fileName = '/out-'.$did.'-'.date('YmdHis').uniqid().'.xlsx'; |
| 245 | + $fileName = '/out-'.$file.$did.'-'.date('YmdHis').uniqid().'.xlsx'; |
243 | 246 | $objWriter->save($path.$fileName);
|
244 | 247 |
|
245 | 248 | return '/storage'.$fileName;
|
246 | 249 | } catch (\PhpOffice\PhpSpreadsheet\Exception|\PhpOffice\PhpSpreadsheet\Writer\Exception $exception) {
|
247 | 250 | // 记录任务失败的错误日志
|
248 | 251 | Log::error('Api_Excel 任务执行失败: ', ['error' => $exception]);
|
249 |
| - return false; |
| 252 | + return ''; |
250 | 253 | }
|
251 | 254 | }
|
252 | 255 |
|
| 256 | + |
| 257 | + /** |
| 258 | + * @param $api_excel_id |
| 259 | + * @param $user_id |
| 260 | + * |
| 261 | + * @return bool|string |
| 262 | + */ |
| 263 | + public function exportExcelLogs($api_excel_id, $user_id) |
| 264 | + { |
| 265 | + $api_excel = ApiExcel::find($api_excel_id); |
| 266 | + if ($api_excel->finish_url) { |
| 267 | + return $api_excel->finish_url; |
| 268 | + } |
| 269 | + |
| 270 | + if ($api_excel && ($user_id == 1 || $user_id == $api_excel['uid'])) { |
| 271 | + // 有权限查询 |
| 272 | + $param = $api_excel->apiParam; |
| 273 | + |
| 274 | + $excel_logs = ApiExcelLogs::where('api_excel_id', $api_excel_id)->orderBy('sort_index')->get(); |
| 275 | + |
| 276 | + $result = []; |
| 277 | + foreach ($excel_logs as $key => $excel_log) { |
| 278 | + $reqParam = explode('|', $excel_log->param); |
| 279 | + $result[] = ['param' => $reqParam, 'result' => $excel_log->result]; |
| 280 | + } |
| 281 | + if (!$result) { |
| 282 | + return ''; |
| 283 | + } |
| 284 | + $failed_done_file = $this->saveExcel($param, $result, 'failed-done-'); |
| 285 | + $api_excel->finish_url = $failed_done_file; |
| 286 | + $api_excel->save(); |
| 287 | + |
| 288 | + return $failed_done_file; |
| 289 | + } |
| 290 | + |
| 291 | + return false; |
| 292 | + } |
| 293 | + |
253 | 294 | /**
|
254 | 295 | * BusRepository constructor.
|
255 | 296 | */
|
|
0 commit comments