Skip to content

Commit af8dbc8

Browse files
committed
update: 使用仓库处理逻辑代码
1 parent 527c20f commit af8dbc8

File tree

4 files changed

+371
-11
lines changed

4 files changed

+371
-11
lines changed

laravel/app/Helper/functions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ function cacheUserRolesAndPermissions($user_id, $flash = false)
5252
}
5353
}
5454
}
55+
56+
if (!function_exists('cacheTotalExcel')) {
57+
function cacheTotalExcel($api_excel_id, $file_path, $flash = false)
58+
{
59+
if ($flash) {
60+
Cache::forget('api_excel_total_'.$api_excel_id);
61+
return cacheTotalExcel($api_excel_id, false);
62+
} else {
63+
return Cache::remember('api_excel_total_'.$api_excel_id, 60, function() use ($api_excel_id, $file_path) {
64+
$excel = \App\Http\Repository\MultithreadingRepository::getInstent();
65+
$data = $excel->getExcelData($file_path);
66+
return isset($data['data']) ? count($data['data']) : 0;
67+
});
68+
}
69+
}
70+
}
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
<?php
2+
/**
3+
* Desc: ExcelRepository 仓库类
4+
* User: lisgroup
5+
* Date: 2019-04-04
6+
* Time: 10:15
7+
*/
8+
9+
namespace App\Http\Repository;
10+
11+
12+
use Illuminate\Support\Facades\Log;
13+
use PhpOffice\PhpSpreadsheet\IOFactory;
14+
use PhpOffice\PhpSpreadsheet\Reader\Exception;
15+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
16+
17+
class ExcelRepository
18+
{
19+
/**
20+
* @var array 原始数据
21+
*/
22+
public $dataSet;
23+
/**
24+
* @var array 请求后的数据
25+
*/
26+
public $data = [];
27+
28+
public $api_excel_id = 0;
29+
public $fileName;
30+
public $config;
31+
32+
/**
33+
* @var self 单例
34+
*/
35+
private static $instance;
36+
37+
/**
38+
* 获取单例
39+
*
40+
* @return ExcelRepository
41+
*/
42+
public static function getInstent()
43+
{
44+
if (!isset(self::$instance)) {
45+
self::$instance = new static();
46+
}
47+
return self::$instance;
48+
}
49+
50+
public function setApiExcelId($id)
51+
{
52+
$this->api_excel_id = $id;
53+
}
54+
55+
/**
56+
* 设置参数
57+
*
58+
* @param $fileName
59+
* @param array $config
60+
*/
61+
public function setParam($fileName, $config = [])
62+
{
63+
$this->fileName = $fileName;
64+
$this->config = $config;
65+
// $this->config || $this->config = $config ? $config : config('apiParam');
66+
}
67+
68+
/**
69+
* 加载 Excel 文件返回内容数组
70+
*
71+
* @param $fileName
72+
*
73+
* @return bool
74+
*/
75+
public function loadExcel($fileName)
76+
{
77+
try {
78+
// new PhpOffice\PhpSpreadsheet\IOFactory 读取 Excel 文件
79+
$excel = IOFactory::load($fileName);
80+
// 1. 取出全部数组
81+
$data = $excel->getActiveSheet()->toArray('', true, true, true);
82+
// 2. 数组第一元素为参数名称
83+
$dataSet['param'] = array_shift($data);
84+
85+
// 3. 循环数组每个单元格的数据
86+
$dataSet['data'] = $data;
87+
88+
return $dataSet;
89+
} catch (Exception|\PhpOffice\PhpSpreadsheet\Exception $exception) {
90+
return false;
91+
}
92+
}
93+
94+
95+
/**
96+
* a. 使用 PhpOffice/PhpSpreadsheet/IOFactory 获取 Excel 内容
97+
*
98+
* @return bool
99+
*/
100+
public function newLoadExcel()
101+
{
102+
try {
103+
// new PhpOffice\PhpSpreadsheet\IOFactory 读取 Excel 文件
104+
$excel = IOFactory::load($this->fileName);
105+
// 1. 取出全部数组
106+
$data = $excel->getActiveSheet()->toArray('', true, true, true);
107+
// 2. 数组第一元素为参数名称
108+
$this->dataSet['param'] = array_shift($data);
109+
110+
// 3. 循环数组每个单元格的数据
111+
$this->dataSet['data'] = $data;
112+
113+
return true;
114+
} catch (Exception|\PhpOffice\PhpSpreadsheet\Exception $exception) {
115+
return false;
116+
}
117+
118+
}
119+
120+
121+
/**
122+
* 存储 Excel 数据
123+
*
124+
* @param $param
125+
* @param $result
126+
*
127+
* @return bool|string
128+
*/
129+
public function saveExcel($param, $result)
130+
{
131+
/************************* 2. 写入 Excel 文件 ******************************/
132+
// 首先创建一个新的对象 PHPExcel object
133+
$objPHPExcel = new Spreadsheet();
134+
135+
/** 以下是一些设置 ,什么作者、标题信息 */
136+
$objPHPExcel->getProperties()->setCreator('lisgroup')
137+
->setLastModifiedBy('lisgroup')
138+
->setTitle('EXCEL 导出')
139+
->setSubject('EXCEL 导出')
140+
->setDescription('导出数据')
141+
->setKeywords("excel php")
142+
->setCategory("result file");
143+
/*以下就是对处理Excel里的数据, 横着取数据,主要是这一步,其他基本都不要改*/
144+
145+
// Excel 的第 A 列,uid 是你查出数组的键值,下面以此类推
146+
try {
147+
// ErrorException: Undefined offset: 0 in ApiExcelListener.php:76
148+
$setActive = $objPHPExcel->setActiveSheetIndex(0);
149+
// 1. 第一行应该是 param 参数
150+
$keys = array_keys($result[0]['param']);
151+
$i = 'A';
152+
foreach ($keys as $num => $key) {
153+
$setActive->setCellValue($i.'1', "\t".$key);
154+
$i++;
155+
}
156+
157+
// 1.2 处理配置的字段
158+
if ($param['result'] && $arr = explode(',', $param['result'])) {
159+
foreach ($arr as $item) {
160+
$val = $item ?? '';
161+
if (strpos($item, '.') !== false) {
162+
$kems = explode('.', $item);
163+
$val = end($kems);
164+
}
165+
$setActive->setCellValue($i.'1', $val ?? '');
166+
$i++;
167+
}
168+
}
169+
// 1.3 is_need 字段
170+
if ($param['is_need'] == 1) {
171+
$setActive->setCellValue($i.'1', 'res');
172+
}
173+
174+
// 2. 第二行开始循环数据
175+
foreach ($result as $key => $value) {
176+
// 2.1 第二行位置
177+
$number = $key + 2;
178+
179+
$i = 'A';
180+
foreach ($keys as $num => $key) {
181+
$setActive->setCellValue($i.$number, "\t".$value['param'][$key]);
182+
$i++;
183+
}
184+
185+
// 2.2 处理配置的字段
186+
$array = json_decode($value['result'], true);
187+
188+
// 示例1: $param['result'] = 'res'; -- 为 api_param 表一行数据
189+
// 示例2: $value = ['param' => ['realname' => '**', 'idcard' => '***'], 'result' => '{"reason":"成功","result":{"realname":"**","idcard":"***","res":2},"error_code":0}'];
190+
if ($param['result'] && $arr = explode(',', $param['result'])) {
191+
foreach ($arr as $k => $item) {
192+
// 2019-03-15 输出可能的参数异常等错误信息,需判断最后一列输出
193+
if ((isset($array['error_code']) && $array['error_code'] != 0) && ($k == count($arr) - 1)) {
194+
$val = $array['reason'];
195+
} else {
196+
// 2019-02-27 日新增: 354 接口配置 data.0.status 字段
197+
// 输出需要 $array['result']['data'][0]['status']
198+
$val = $array['result'][$item] ?? '';
199+
if (strpos($item, '.') !== false) {
200+
$kems = explode('.', $item);
201+
$val = $array['result'];
202+
foreach ($kems as $kem) {
203+
$val = $val[$kem] ?? '';
204+
}
205+
206+
}
207+
}
208+
209+
$setActive->setCellValue($i.$number, $val);
210+
$i++;
211+
}
212+
}
213+
214+
// 1.3 is_need 字段
215+
if ($param['is_need'] == 1) {
216+
if (isset($array['error_code']) && $array['error_code'] == 0) {
217+
if (isset($array['result']['res'])) {
218+
$message = $array['result']['res'] == 1 ? '一致' : '不一致';
219+
} else {
220+
$message = '';
221+
}
222+
} else {
223+
$message = $array['reason'] ?? '';
224+
}
225+
$setActive->setCellValue($i.$number, $message);
226+
}
227+
}
228+
229+
//得到当前活动的表,注意下文教程中会经常用到$objActSheet
230+
$objActSheet = $objPHPExcel->getActiveSheet();
231+
// 位置bbb *为下文代码位置提供锚
232+
// 给当前活动的表设置名称
233+
$objActSheet->setTitle('Simple');
234+
// 代码还没有结束,可以复制下面的代码来决定我们将要做什么
235+
236+
// 1,直接生成一个文件
237+
$objWriter = IOFactory::createWriter($objPHPExcel, 'Xlsx');
238+
$path = storage_path('app/public');
239+
// is_dir($path) || mkdir($path, 777, true);
240+
$did = explode('/', $param['website']);
241+
$did = is_numeric(end($did)) ? end($did) : '999';
242+
$fileName = '/out-'.$did.'-'.date('YmdHis').uniqid().'.xlsx';
243+
$objWriter->save($path.$fileName);
244+
245+
return '/storage'.$fileName;
246+
} catch (\PhpOffice\PhpSpreadsheet\Exception|\PhpOffice\PhpSpreadsheet\Writer\Exception $exception) {
247+
// 记录任务失败的错误日志
248+
Log::error('Api_Excel 任务执行失败: ', ['error' => $exception]);
249+
return false;
250+
}
251+
}
252+
253+
/**
254+
* BusRepository constructor.
255+
*/
256+
private function __construct()
257+
{
258+
//$this->ql || $this->ql = QueryList::getInstance();
259+
}
260+
261+
/**
262+
* 不允许 clone
263+
*/
264+
private function __clone()
265+
{
266+
267+
}
268+
}

laravel/app/Http/Repository/MultithreadingRepository.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace App\Http\Repository;
1010

1111

12+
use App\Models\ApiExcel;
1213
use App\Models\ApiExcelLogs;
1314
use GuzzleHttp\Pool;
1415
use GuzzleHttp\Client;
@@ -496,6 +497,60 @@ public function saveExcel($param, $result)
496497
}
497498
}
498499

500+
/**
501+
* 加载 Excel 文件返回内容数组
502+
*
503+
* @param $fileName
504+
*
505+
* @return bool
506+
*/
507+
public function getExcelData($fileName)
508+
{
509+
try {
510+
// new PhpOffice\PhpSpreadsheet\IOFactory 读取 Excel 文件
511+
$excel = IOFactory::load($fileName);
512+
// 1. 取出全部数组
513+
$data = $excel->getActiveSheet()->toArray('', true, true, true);
514+
// 2. 数组第一元素为参数名称
515+
$dataSet['param'] = array_shift($data);
516+
517+
// 3. 循环数组每个单元格的数据
518+
$dataSet['data'] = $data;
519+
520+
return $dataSet;
521+
} catch (Exception|\PhpOffice\PhpSpreadsheet\Exception $exception) {
522+
return false;
523+
}
524+
}
525+
526+
/**
527+
* 计算任务完成百分比
528+
*
529+
* @param $excel_id
530+
*
531+
* @return string
532+
*/
533+
public function completionRate($excel_id)
534+
{
535+
if ($api_excel = ApiExcel::find($excel_id)) {
536+
if ($api_excel['state'] == 1) {
537+
$total_excel = cacheTotalExcel($excel_id, $api_excel['upload_url']);
538+
if ($total_excel > 0) {
539+
// 2. 查询 api_excel_logs 表更新的数据量
540+
$total = ApiExcelLogs::where('api_excel_id', $excel_id)->count();
541+
// 3. 返回完成率
542+
return floor($total / $api_excel['total_excel'] * 100).'%';
543+
} else {
544+
return '100%';
545+
}
546+
} else {
547+
return '';
548+
}
549+
} else {
550+
return '100%';
551+
}
552+
}
553+
499554
/**
500555
* BusRepository constructor.
501556
*/

0 commit comments

Comments
 (0)