ProjectKanbanExport.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Exports;
  3. use App\Models\Project;
  4. use App\Services\Project\ProjectKanbanService;
  5. use Illuminate\Support\Collection;
  6. use Maatwebsite\Excel\Concerns\Exportable;
  7. use Maatwebsite\Excel\Concerns\FromCollection;
  8. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  9. use Maatwebsite\Excel\Concerns\WithHeadings;
  10. use Maatwebsite\Excel\Concerns\WithMapping;
  11. class ProjectKanbanExport implements FromCollection, WithMapping, WithHeadings, ShouldAutoSize
  12. {
  13. use Exportable;
  14. private static $index = 1;
  15. public function collection()
  16. {
  17. $service = new ProjectKanbanService();
  18. $project = Project::allowed(request()->project_id)->findOrFail(request()->project_id);
  19. $kanban = $service->kanban($project, request()->get("group", "requirement_asc"));
  20. $TaskType = $kanban['status_items'];
  21. $item = $kanban['group_data'];
  22. $result=[];
  23. $j = 0;
  24. for ($i = 0; $i < count($item) ; $i++) {
  25. foreach ($TaskType as $type){ //遍历每一种任务状态,转为扁平数据行
  26. $arrItem = (array)$item[$i];
  27. if ($arrItem[$type]) {
  28. foreach ($arrItem[$type] as $value){
  29. $result[$j]['title'] = $arrItem['title']??'';
  30. $result[$j]['priority'] = $arrItem['priority']??'';
  31. $result[$j]['status'] = $arrItem['status']??'';
  32. $result[$j]['type'] = $type;
  33. $result[$j]['typeName'] = $value->name;
  34. $result[$j]['typeEnd'] = $value->end;
  35. $result[$j]['typeAssign'] = $value->assignTo->username??'';
  36. $j++;
  37. }
  38. }
  39. }
  40. }
  41. return new Collection($result);
  42. }
  43. public function map($project): array
  44. {
  45. $displayId = self::$index++;
  46. return [
  47. $displayId,
  48. $project['title'],
  49. $project['priority'],
  50. $project['status'],
  51. $project['typeName'],
  52. $project['type'],
  53. $project['typeEnd'],
  54. $project['typeAssign'],
  55. ];
  56. }
  57. public function headings(): array
  58. {
  59. return [
  60. 'ID',
  61. 'Title',
  62. 'Priority',
  63. 'Status',
  64. 'TaskName',
  65. 'TaskStatus',
  66. 'TaskEnd',
  67. 'TaskAssign',
  68. ];
  69. }
  70. }