FormService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\ExcelData\Services;
  3. use App\Form\Facades\FormInfoFacade;
  4. use App\Form\Facades\FormItemFacade;
  5. use App\Form\Facades\FormRecordFacade;
  6. use Maatwebsite\Excel\Concerns\Exportable;
  7. use Maatwebsite\Excel\Concerns\FromArray;
  8. use Maatwebsite\Excel\Concerns\FromQuery;
  9. use Maatwebsite\Excel\Concerns\WithHeadings;
  10. use Maatwebsite\Excel\Concerns\WithMapping;
  11. use Maatwebsite\Excel\Concerns\WithTitle;
  12. class FormService implements FromArray, WithTitle, WithHeadings, WithMapping
  13. {
  14. use Exportable;
  15. const DEFAULT_TITLE = 'sheet';
  16. protected $params;
  17. protected $title = '';
  18. protected $items = [];
  19. protected $heads = [];
  20. protected $website = [];
  21. protected $websiteLanguage = '';
  22. protected $reverseHead = [];
  23. protected $timeKey = '提交时间';
  24. protected $languageKey = '网站语言';
  25. /**
  26. * 设置参数
  27. * @param $params 详见: FormInfoFacade::getList
  28. * @return $this
  29. */
  30. public function setParams($params)
  31. {
  32. $this->params = $params;
  33. $title = '';
  34. $items = [];
  35. $heads = [];
  36. if (!empty($this->params['form_id'])) {
  37. $form = FormInfoFacade::findOneBy(['id' => $this->params['form_id']], 'id,name');
  38. if (!empty($form)) {
  39. $title = $form['name'] ?: self::DEFAULT_TITLE.$this->params['form_id'];
  40. //获取items
  41. $items = FormItemFacade::getData($this->params['form_id']);
  42. empty($items) && $items = [];
  43. $heads = array_column($items, 'name');
  44. $heads = array_unique($heads);
  45. !empty($heads) && array_unshift($heads, $this->timeKey, $this->languageKey);
  46. //获取语言信息
  47. $this->websiteLanguage ='自定义表单';
  48. }
  49. }
  50. //超出31表单导不出
  51. if(strlen($title) > 31){
  52. $title = substr($title, -17).uniqid();
  53. }
  54. $this->title = $title;
  55. $this->items = mapByKey($items, 'id');
  56. $this->heads = $heads;
  57. !empty($heads) && $this->reverseHead = array_flip($heads);
  58. return $this;
  59. }
  60. /**
  61. * 查询query
  62. * @return \Illuminate\Database\Query\Builder
  63. */
  64. public function array():array
  65. {
  66. $items = FormRecordFacade::getList($this->params);
  67. FormRecordFacade::formatList($items);
  68. return $items;
  69. }
  70. /**
  71. * 标题
  72. * @return string
  73. */
  74. public function title(): string
  75. {
  76. return $this->title;
  77. }
  78. /**
  79. * 字段名
  80. * @return array
  81. */
  82. public function headings(): array
  83. {
  84. return $this->heads;
  85. }
  86. /**
  87. * 遍历数据
  88. * @param mixed $row
  89. * @return array
  90. */
  91. public function map($row): array
  92. {
  93. $defaultValue = '';
  94. $defaultGlue = ',';
  95. //根绝head排序数据,若为空,不进行处理
  96. $items = $row['items'];
  97. $data = [];
  98. $tmp = [];
  99. //设置提交时间
  100. $index = $this->reverseHead[$this->timeKey];
  101. $data[$index] = $row['create_time']?:'';
  102. //设置语言
  103. $index = $this->reverseHead[$this->languageKey];
  104. $data[$index] = $this->websiteLanguage;
  105. foreach ($items as &$item) {
  106. $id = $item['form_item_id'];
  107. is_array($item['content']) && $item['content'] = implode($defaultGlue, $item['content']);
  108. if (isset($this->items[$id])) {
  109. //获取在heads中的位置
  110. $name = $this->items[$id]['name'];
  111. $index = $this->reverseHead[$name];
  112. if (!empty($data[$index])) {
  113. $data[$index] .= $defaultGlue.$item['content'];
  114. } else {
  115. $data[$index] = $item['content'];
  116. }
  117. }
  118. $tmp[] = $item['content'];
  119. }
  120. if (empty($this->heads)) {
  121. return $tmp;
  122. }
  123. //默认值处理
  124. $returnData = [];
  125. foreach ($this->heads as $key => $v) {
  126. $returnData[] = $data[$key] ?? $defaultValue;
  127. }
  128. return $returnData;
  129. }
  130. }