FormSheetsService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 FormSheetsService 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::getList($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 = resource_path('upload/') . $filename;;
  51. return $this->download($filename);
  52. // // $etag = md5_file($filePath);
  53. // // $url = OssFacade::uploadToOss($filePath, $filename, $etag);
  54. // $url='/resources/upload/'.$filename;
  55. // return $url;
  56. }
  57. }