Skip to content

Commit 6cedda9

Browse files
committed
fix(repo): 进度条完成后台功能
1 parent 8426110 commit 6cedda9

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

laravel/app/Http/Repository/ApiRepository.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
use App\Models\ApiExcel;
13+
use App\Models\ApiExcelLogs;
1314

1415
class ApiRepository
1516
{
@@ -75,7 +76,6 @@ public function autoFailed()
7576
// 开启任务后 10 分钟未查询出结果=》失败
7677
if ($excel['auto_delete'] > 0 && strtotime($excel['updated_at']) + 600 < time()) {
7778
ApiExcel::where('id', $excel['id'])->update(['state' => 5]);
78-
7979
}
8080

8181
}
@@ -111,9 +111,47 @@ public function randomTime()
111111

112112
/**
113113
* 获取下载进度条
114+
*
115+
* @param $lists
116+
*
117+
* @return mixed
114118
*/
115-
public function workProgress($list)
119+
public function workProgress($lists)
116120
{
117-
return $list;
121+
foreach ($lists as $key => $list) {
122+
$rate = 100;
123+
switch ($list['state']) {
124+
case '1': // 正在处理的任务
125+
case '5': // 失败任务
126+
$rate = $this->progressRate($list['id'], $list['total_excel']);
127+
break;
128+
}
129+
130+
$lists[$key]['rate'] = $rate;
131+
}
132+
return $lists;
118133
}
134+
135+
136+
/**
137+
* 计算任务完成百分比
138+
*
139+
* @param int $excel_id api_excel 主键 id
140+
* @param int $total_excel 总数
141+
*
142+
* @return int|bool
143+
*/
144+
private function progressRate($excel_id, $total_excel)
145+
{
146+
if ($total_excel > 0) {
147+
// 2. 查询 api_excel_logs 表更新的数据量
148+
$total = ApiExcelLogs::where('api_excel_id', $excel_id)->count();
149+
// 3. 返回完成率
150+
$rate = floor($total / $total_excel * 100);
151+
return $rate > 100 ? 100 : $rate;
152+
}
153+
154+
return 100;
155+
}
156+
119157
}

0 commit comments

Comments
 (0)