|
@@ -2,10 +2,12 @@
|
|
|
|
|
|
namespace General\Services\Export;
|
|
|
|
|
|
+use General\Helpers\AliOSSHelper;
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
|
* 导出任务
|
|
|
+ * $model 对象必须有name,query_condition,status,file_path,is_file_deleted 逻辑不能变
|
|
|
*/
|
|
|
abstract class AbstractExportTask
|
|
|
{
|
|
@@ -17,7 +19,7 @@ abstract class AbstractExportTask
|
|
|
const complete = 3;
|
|
|
const failure = 4;
|
|
|
|
|
|
- public function __construct(Model $model, ExportQueryData $query_data)
|
|
|
+ public function __construct(Model $model, ExportQueryData $query_data = null)
|
|
|
{
|
|
|
$this->model = $model;
|
|
|
$this->query_data = $query_data;
|
|
@@ -30,20 +32,14 @@ abstract class AbstractExportTask
|
|
|
*/
|
|
|
public function addTask(array $query_params)
|
|
|
{
|
|
|
- $params = array_merge($this->getTaskData(), [
|
|
|
- 'name' => $this->query_data->memu_name,
|
|
|
- 'query_condition' => $this->query_data->getQueryParamsContent($query_params),
|
|
|
- 'status' => self::ready,
|
|
|
- ]);
|
|
|
- $this->setModel($this->model::create($params));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 更新任务状态
|
|
|
- */
|
|
|
- public function updateTask(int $id, int $status)
|
|
|
- {
|
|
|
- return $this->model::where('id', $id)->update(['status' => $status]);
|
|
|
+ if ($this->query_data) {
|
|
|
+ $params = array_merge($this->getTaskData(), [
|
|
|
+ 'name' => $this->query_data->memu_name,
|
|
|
+ 'query_condition' => $this->query_data->getQueryParamsContent($query_params),
|
|
|
+ 'status' => self::ready,
|
|
|
+ ]);
|
|
|
+ $this->setModel($this->model::create($params));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public function setModel(Model $model)
|
|
@@ -56,9 +52,38 @@ abstract class AbstractExportTask
|
|
|
return $this->model;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * 设置状态
|
|
|
+ */
|
|
|
public function setStatus(int $status)
|
|
|
{
|
|
|
$this->model->status = $status;
|
|
|
$this->model->save();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ * 保存文件
|
|
|
+ */
|
|
|
+ public function saveExportTaskFilePath(string $local_path)
|
|
|
+ {
|
|
|
+ $service = new AliOSSHelper;
|
|
|
+ $oss_path = $service->uploadFile("reports", $local_path);
|
|
|
+ if ($oss_path) {
|
|
|
+ $this->model->file_path = $oss_path;
|
|
|
+ $this->setStatus(self::complete);
|
|
|
+ } else {
|
|
|
+ $this->task->setStatus(self::failure);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 删除文件
|
|
|
+ */
|
|
|
+ public function delOssFile()
|
|
|
+ {
|
|
|
+ $service = new AliOSSHelper;
|
|
|
+ $service->delFile($this->model->file_path);
|
|
|
+ $this->model->is_file_deleted = 1;
|
|
|
+ $this->model->save();
|
|
|
+ }
|
|
|
}
|