InterviewExport.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Exports\Human;
  3. use App\Repositories\Enums\Human\InterviewInductionStatusEnum;
  4. use App\Repositories\Enums\Human\InterviewStatusEnum;
  5. use App\Repositories\Enums\ModelStatusEnum;
  6. use App\Repositories\Models\Base\Dict;
  7. use App\Repositories\Models\Human\Dispatch;
  8. use App\Repositories\Models\Human\Interview;
  9. use App\Repositories\Models\Recruit\Position;
  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. class InterviewExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  20. {
  21. use Exportable;
  22. const BaseFields = [
  23. [
  24. 'name' => '序号',
  25. 'width' => 8,
  26. ],
  27. [
  28. 'name' => '姓名',
  29. 'width' => 15,
  30. ],
  31. [
  32. 'name' => '性别',
  33. 'width' => 8,
  34. ],
  35. [
  36. 'name' => '身份证号',
  37. 'width' => 30,
  38. ],
  39. [
  40. 'name' => '年龄',
  41. 'width' => 8,
  42. ],
  43. [
  44. 'name' => '供应商',
  45. 'width' => 30,
  46. ],
  47. [
  48. 'name' => '门店名称',
  49. 'width' => 30,
  50. ],
  51. [
  52. 'name' => '派遣单位',
  53. 'width' => 30,
  54. ],
  55. [
  56. 'name' => '派遣工种',
  57. 'width' => 15,
  58. ],
  59. [
  60. 'name' => '接收日期',
  61. 'width' => 20,
  62. ],
  63. [
  64. 'name' => '接收时间',
  65. 'width' => 20,
  66. ],
  67. [
  68. 'name' => '面试日期',
  69. 'width' => 20,
  70. ],
  71. ];
  72. protected $rowNums = 0;
  73. protected $factory_id = 0;
  74. protected $type = 0;
  75. protected $lastC = 'A';
  76. protected $fields = [];
  77. public function __construct($type, $factory_id, $field_ids = [])
  78. {
  79. $this->type = $type;
  80. $this->factory_id = $factory_id;
  81. $fields = [];
  82. if (is_string($field_ids)) {
  83. $fields = self::BaseFields;
  84. } else {
  85. foreach ($field_ids as $field_id) {
  86. $fields[] = self::BaseFields[$field_id];
  87. }
  88. }
  89. $this->fields = $fields;
  90. }
  91. public function query()
  92. {
  93. $type = $this->type;
  94. $factory_id = $this->factory_id;
  95. return Interview::query()
  96. ->with(['human', 'factory', 'supplier', 'staff', 'shop'])
  97. ->where('status', ModelStatusEnum::OK)
  98. ->when($this->factory_id, function ($query) use ($factory_id) {
  99. return $query->where('factory_id', $factory_id);
  100. })
  101. ->when($type == 1, function ($query) {
  102. return $query->where('interview_status', InterviewStatusEnum::WAIT);
  103. })->when($type == 2, function ($query) {
  104. return $query->where('induction_status', InterviewInductionStatusEnum::WAIT);
  105. })
  106. ->orderBy('id');
  107. }
  108. public function headingRow(): int
  109. {
  110. return 2;
  111. }
  112. public function headings(): array
  113. {
  114. if ($this->type == 1) $room_name = '员工面试名单';
  115. if ($this->type == 2) $room_name = '员工待入职名单';
  116. $data[] = [$room_name];
  117. $data[] = array_column($this->fields, 'name');
  118. return $data;
  119. }
  120. public function columnWidths(): array
  121. {
  122. $i = 65;
  123. $data = [];
  124. foreach ($this->fields as $k => $field) {
  125. $s = intval($k / 26);
  126. if (!$s) $key = chr($i + $k);
  127. if ($s > 0) {
  128. $y = intval($k % 26);
  129. $key = chr($s + $i - 1);
  130. $key .= chr($y + $i);
  131. }
  132. $data["{$key}"] = $field['width'];
  133. $this->lastC = $key;
  134. }
  135. return $data;
  136. }
  137. public function styles(Worksheet $sheet)
  138. {
  139. $last = $this->lastC;
  140. $cellRange = "A1:{$last}1";
  141. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  142. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  143. // 文字居中
  144. $lastrow = $sheet->getHighestRow();
  145. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  146. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  147. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  148. $sheet->mergeCells("A1:{$last}1"); //合并
  149. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  150. }
  151. public function map($row): array
  152. {
  153. $row = $row->toArray();
  154. $this->rowNums++;
  155. $data = [];
  156. foreach (array_column($this->fields, 'name') as $k => $field) {
  157. if ($field == '序号') $data[$k] = $this->rowNums;
  158. if ($field == '姓名') {
  159. $data[$k] = $row['human'] ? $row['human']['name'] : '';
  160. }
  161. if ($field == '身份证号') {
  162. $data[$k] = $row['human'] ? $row['human']['card_id'] . ' ' : '';
  163. }
  164. if ($field == '性别') {
  165. if ($row['human']) {
  166. if ($row['human']['sex'] == 1) $data[$k] = '男';
  167. if ($row['human']['sex'] == 2) $data[$k] = '女';
  168. } else {
  169. $data[$k] = '未知';
  170. }
  171. }
  172. if ($field == '年龄') {
  173. $data[$k] = $row['human'] ? $row['human']['age'] : '';
  174. }
  175. // if ($field == '住址') {
  176. // $data[$k] = $row['human'] ? $row['human']['address'] : '';
  177. // }
  178. if ($field == '供应商') {
  179. $data[$k] = $row['supplier'] ? $row['supplier']['name'] : '';
  180. }
  181. if ($field == '派遣单位') {
  182. $data[$k] = $row['factory'] ? $row['factory']['name'] : '';
  183. }
  184. if ($field == '派遣工种') {
  185. $data[$k] = Dict::byCodeAndIdGetDict(Position::JOB_CODE, $row['job_position']);
  186. }
  187. if ($field == '门店名称') {
  188. $data[$k] = $row['shop'] ? $row['shop']['name'] : '';
  189. }
  190. if ($field == '接收日期') $data[$k] = date('Y-m-d', strtotime($row['created_at']));
  191. if ($field == '接收时间') $data[$k] = $row['created_at'];
  192. if ($field == '面试日期') $data[$k] = $row['interview_date'];
  193. // if ($field == '车费类型') {
  194. // if ($row['car_type'] == 1) $data[$k] = '免费';
  195. // if ($row['car_type'] == 2) $data[$k] = '已收';
  196. // if ($row['car_type'] == 3) $data[$k] = '未付';
  197. // }
  198. // if ($field == '车费') {
  199. // $data[$k] = $row['car_money'];
  200. // }
  201. // if ($field == '派遣包') $data[$k] = $row['package'] ? $row['package']['name'] : '';
  202. // if ($field == '单价') $data[$k] = $row['human']['card_id'];
  203. // if ($field == '提成价') $data[$k] = $row['human']['card_id'];
  204. }
  205. return $data;
  206. }
  207. }