FormRecordSumService.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\ExcelData\Services;
  3. use App\Exceptions\ApiException;
  4. use App\Common\Facades\OssFacade;
  5. use App\Form\Facades\FormRecordFacade;
  6. use Maatwebsite\Excel\Concerns\Exportable;
  7. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  8. class FormRecordSumService implements WithMultipleSheets
  9. {
  10. use Exportable;
  11. protected $params = [];
  12. /**
  13. * 设置参数
  14. * @param $params 详见:\App\Forms\Services\FormInfoService::getList
  15. * @return $this
  16. */
  17. public function setParams($params)
  18. {
  19. $this->params = $params;
  20. return $this;
  21. }
  22. /**
  23. * 设置sheet
  24. * @return array
  25. */
  26. public function sheets(): array
  27. {
  28. $sheets = [];
  29. //通过查询条件获取sheets基本信息
  30. $params = $this->params;
  31. $params['format_list'] = false;
  32. $data = FormRecordFacade::getFormRecordList($params);
  33. empty($data) && $data = [];
  34. if(empty($data)){
  35. throw new ApiException(40013);
  36. }
  37. $forms = mapByKey($data, 'form_id');
  38. /*$formIds = array_column($data, 'form_id');
  39. !empty($formIds) && $formIds = array_unique($formIds);*/
  40. $params = $this->params;
  41. foreach ($forms as $id => $form) {
  42. $params['form_id'] = $id;
  43. $sheets[] = (new FormService())->setParams($params);
  44. }
  45. return $sheets;
  46. }
  47. public function toOss()
  48. {
  49. $filename = 'form'.date('ymdHis').rand(0,99).'.xlsx';
  50. $filePath = storage_path('app/').$filename;
  51. $this->store($filename);
  52. $etag = md5_file($filePath);
  53. $url = OssFacade::uploadToOss($filePath, $filename, $etag);
  54. return $url;
  55. }
  56. }