ApproveRecordExport.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Exports\School;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Enums\School\LessonScheduleApproveStatusEnum;
  5. use App\Repositories\Enums\School\ScheduleRecordTypeEnum;
  6. use App\Repositories\Models\School\LessonSchedule;
  7. use App\Repositories\Models\School\Room;
  8. use App\Repositories\Models\School\ScheduleApproveLog;
  9. use App\Repositories\Models\School\ScheduleApproveRecord;
  10. use Maatwebsite\Excel\Concerns\Exportable;
  11. use Maatwebsite\Excel\Concerns\FromQuery;
  12. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  13. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  14. use Maatwebsite\Excel\Concerns\WithHeadings;
  15. use Maatwebsite\Excel\Concerns\WithMapping;
  16. use Maatwebsite\Excel\Concerns\WithStyles;
  17. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  18. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  19. use function Symfony\Component\String\b;
  20. class ApproveRecordExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  21. {
  22. use Exportable;
  23. protected $rowNums = 0;
  24. protected $room_id = 0;
  25. protected $term_id = 0;
  26. protected $status = 0;
  27. protected $type = 0;
  28. protected $fields = [];
  29. public function __construct($term_id, $room_id, $status, $type)
  30. {
  31. $this->room_id = $room_id;
  32. $this->term_id = $term_id;
  33. $this->status = $status;
  34. $this->type = $type;
  35. }
  36. public function query()
  37. {
  38. $room_id = $this->room_id;
  39. $status = $this->status;
  40. $type = $this->type;
  41. return ScheduleApproveRecord::query()
  42. ->with(['room'])
  43. ->where('term_id', $this->term_id)
  44. ->when($room_id, function ($query) use ($room_id) {
  45. return $query->where('room_id', $room_id);
  46. })->when($status, function ($query) use ($status) {
  47. return $query->where('status', $status);
  48. })
  49. ->when($type, function ($query) use ($type) {
  50. return $query->where('type', $type);
  51. })
  52. ->orderBy('id');
  53. // ->orderBy('time_id');
  54. }
  55. public function headingRow(): int
  56. {
  57. return 2;
  58. }
  59. public function headings(): array
  60. {
  61. $room_name = '停调补课记录';
  62. if ($this->room_id) {
  63. $room_name = Room::query()->where('id', $this->room_id)->value('name') . '-停调补课记录';
  64. }
  65. $data[] = [$room_name];
  66. $bases = [
  67. '序号',
  68. '类型',
  69. '课程名称',
  70. '任课教师',
  71. '班级',
  72. '分组',
  73. '人数',
  74. '原上课时间',
  75. '现上课时间',
  76. '申请时间',
  77. '实验中心审批状态',
  78. '院系审批意见',
  79. '状态',
  80. ];
  81. // $data[1] = '序号';
  82. // foreach ($this->fields as $field) {
  83. // $data[1] = $bases[$field];
  84. // }
  85. $data[] = $bases;
  86. return $data;
  87. }
  88. public function columnWidths(): array
  89. {
  90. $bases = [
  91. 'A' => 8,
  92. 'B' => 8,
  93. 'C' => 30,
  94. 'D' => 12,
  95. 'E' => 30,
  96. 'F' => 8,
  97. 'G' => 8,
  98. 'H' => 50,
  99. 'I' => 50,
  100. 'J' => 20,
  101. 'K' => 35,
  102. 'L' => 35,
  103. 'M' => 15,
  104. ];
  105. // $data['A'] = 8;
  106. // $values = array_values($bases);
  107. // $keys = array_keys($bases);
  108. // foreach ($this->fields as $field) {
  109. // $key = $keys[$field];
  110. // $data["{$key}"] = $values[$field];
  111. // }
  112. $data = $bases;
  113. return $data;
  114. }
  115. public function styles(Worksheet $sheet)
  116. {
  117. $cellRange = 'A1:M1';
  118. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  119. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  120. // 文字居中
  121. $lastrow = $sheet->getHighestRow();
  122. $sheet->getStyle('A1:M' . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  123. $sheet->getStyle('A1:M' . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  124. $sheet->getStyle('A1:M' . $lastrow)->getAlignment()->setWrapText(true);
  125. $sheet->mergeCells('A1:M1'); //合并
  126. $sheet->getStyle('A1:M1')->getFont()->setSize(16);
  127. }
  128. public function map($row): array
  129. {
  130. $this->rowNums++;
  131. $data = [];
  132. $teach_lesson = LessonSchedule::query()->where('id', $row['lesson_schedule_id'])->with(['lesson', 'teacher', 'grade', 'time', 'room'])->first();
  133. if ($teach_lesson) {
  134. $old_times = [
  135. $teach_lesson['room']['name'],
  136. '第' . $teach_lesson['week'] . '周',
  137. '星期' . LessonSchedule::weekInt2name($teach_lesson['week_index']) . "(" . $teach_lesson['day'] . ")",
  138. $teach_lesson['time']['name']
  139. ];
  140. $old_week_name = arr2str($old_times, ' | ');
  141. $group_name = LessonSchedule::groupInt2Name($teach_lesson['group']);
  142. $new_week_name = '';
  143. if (in_array($row['type'], [ScheduleRecordTypeEnum::BK, ScheduleRecordTypeEnum::TK]) && is_array($row['migrate_data'])) {
  144. $new_times = [
  145. isset($row['migrate_data']['room_name']) ? $row['migrate_data']['room_name'] : '',
  146. '第' . $row['migrate_data']['week'] . '周',
  147. '星期' . LessonSchedule::weekInt2name($row['migrate_data']['week_index']) . "(" . $row['migrate_data']['day'] . ")",
  148. $row['migrate_data']['time']['name']
  149. ];
  150. $new_week_name = arr2str($new_times, ' | ');
  151. }
  152. $yxyj = $syzxyj = '';
  153. $yx = ScheduleApproveLog::query()->where('status', ModelStatusEnum::OK)->where('schedule_approve_id', $row['id'])->where('type', ScheduleApproveLog::TYPE_YX)->with(['check_admin'])->select(['id', 'check_admin_id', 'check_time', 'check_status', 'check_result'])->orderByDesc('id')->first();
  154. if ($yx) {
  155. $yxyj = arr2str([
  156. $yx['check_status'] == 1 ? '审核通过' : '审核未通过',
  157. $yx['check_admin']['name'],
  158. $yx['check_time'],
  159. ], '|');
  160. }
  161. $syzx = ScheduleApproveLog::query()->where('status', ModelStatusEnum::OK)->where('schedule_approve_id', $row['id'])->where('type', ScheduleApproveLog::TYPE_SYZX)->with(['check_admin'])->select(['id', 'check_admin_id', 'check_time', 'check_status', 'check_result'])->orderByDesc('id')->first();
  162. if ($syzx) {
  163. $syzxyj = arr2str([
  164. $syzx['check_status'] == 1 ? '审核通过' : '审核未通过',
  165. $syzx['check_admin']['name'],
  166. $syzx['check_time'],
  167. ], '|');
  168. }
  169. $type_name = '';
  170. switch ($row['type']) {
  171. case ScheduleRecordTypeEnum::SK:
  172. $type_name = '停课';
  173. break;
  174. case ScheduleRecordTypeEnum::TK:
  175. $type_name = '调课';
  176. break;
  177. case ScheduleRecordTypeEnum::BK:
  178. $type_name = '补课';
  179. break;
  180. }
  181. $bases = [
  182. $this->rowNums,
  183. $type_name,
  184. $teach_lesson['lesson']['name'],
  185. $teach_lesson['teacher']['name'],
  186. $teach_lesson['grade']['name'],
  187. $group_name,
  188. $teach_lesson['student_nums'],
  189. $old_week_name,
  190. $new_week_name,
  191. $row['created_at'],
  192. $syzxyj,
  193. $yxyj,
  194. LessonScheduleApproveStatusEnum::getDescription($row['status'])
  195. ];
  196. $data = $bases;
  197. }
  198. // foreach ($this->fields as $field) {
  199. // $data[] = $bases[$field];
  200. // }
  201. return $data;
  202. }
  203. }