1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- 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
- {
- private $model;
- private $query_data;
- const ready = 1;
- const running = 2;
- const complete = 3;
- const failure = 4;
- public function __construct(Model $model, ExportQueryData $query_data = null)
- {
- $this->model = $model;
- $this->query_data = $query_data;
- }
- abstract public function getTaskData(): array;
- /**
- * 创建导出任务
- */
- public function addTask(array $query_params)
- {
- if ($this->query_data) {
- $params = array_merge($this->getTaskData(), [
- 'name' => $this->query_data->memu_name,
- 'query_params' => json_encode($query_params, JSON_UNESCAPED_UNICODE),
- 'query_condition' => $this->query_data->getQueryParamsContent($query_params),
- 'status' => self::ready,
- ]);
- $this->setModel($this->model::create($params));
- }
- }
- public function setModel(Model $model)
- {
- $this->model = $model;
- }
- public function getModel(): Model
- {
- return $this->model;
- }
- /**
- * 设置状态
- */
- public function setStatus(int $status)
- {
- $this->model->status = $status;
- $this->model->save();
- }
- /**
- * 保存文件
- */
- public function saveExportTaskFile(string $local_path, string $file_name)
- {
- $service = new AliOSSHelper;
- $oss_path = $service->uploadFile("reports/" . $file_name, $local_path . '/' . $file_name);
- if ($oss_path) {
- $this->model->file_path = "reports/" . $file_name;
- $this->setStatus(self::complete);
- } else {
- $this->task->setStatus(self::failure);
- }
- }
- /**
- * 删除文件
- */
- public function delExportTaskFile()
- {
- $service = new AliOSSHelper;
- $service->delFile($this->model->file_path);
- $this->model->is_file_deleted = 1;
- $this->model->save();
- }
- }
|