ProjectKanbanExport.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $data =json_encode($kanban['group_data']);
  21. $TaskType = $kanban['status_items'];
  22. $result=[];
  23. $item = json_decode($data);
  24. for ($i = 0; $i <count($item) ; $i++) {
  25. $result[$i]['title'] = $item[$i]->title??'';
  26. $result[$i]['priority'] = $item[$i]->priority??'';
  27. $result[$i]['status'] = $item[$i]->status??'';
  28. foreach ($TaskType as $type){ //遍历每一种任务状态,转为扁平数据行
  29. foreach ($item[$i]->{$type} as $value){
  30. $result[$i]['type'] = $type;
  31. $result[$i]['typeName'] = $value->name;
  32. $result[$i]['typeEnd'] = $value->end;
  33. $result[$i]['typeAssign'] = $value->assign->name??'';
  34. }
  35. }
  36. }
  37. return new Collection($result);
  38. }
  39. public function map($project): array
  40. {
  41. $displayId = self::$index++;
  42. return [
  43. $displayId,
  44. $project['title'],
  45. $project['priority'],
  46. $project['status'],
  47. $project['typeName'],
  48. $project['type'],
  49. $project['typeEnd'],
  50. $project['typeAssign'],
  51. ];
  52. }
  53. public function headings(): array
  54. {
  55. return [
  56. 'ID',
  57. 'Title',
  58. 'Priority',
  59. 'Status',
  60. 'TaskName',
  61. 'TaskStatus',
  62. 'TaskEnd',
  63. 'TaskAssign',
  64. ];
  65. }
  66. }