StaffExport.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\Human\StaffEntryStatusEnum;
  6. use App\Repositories\Enums\Human\StaffStepEnum;
  7. use App\Repositories\Enums\ModelStatusEnum;
  8. use App\Repositories\Models\Base\Dict;
  9. use App\Repositories\Models\Human\Dispatch;
  10. use App\Repositories\Models\Human\Interview;
  11. use App\Repositories\Models\Human\Staff;
  12. use App\Repositories\Models\Recruit\Position;
  13. use Maatwebsite\Excel\Concerns\Exportable;
  14. use Maatwebsite\Excel\Concerns\FromQuery;
  15. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  16. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  17. use Maatwebsite\Excel\Concerns\WithHeadings;
  18. use Maatwebsite\Excel\Concerns\WithMapping;
  19. use Maatwebsite\Excel\Concerns\WithStyles;
  20. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  21. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  22. class StaffExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  23. {
  24. use Exportable;
  25. const BaseFields = [
  26. [
  27. 'name' => '序号',
  28. 'width' => 8,
  29. ],
  30. [
  31. 'name' => '姓名',
  32. 'width' => 15,
  33. ],
  34. [
  35. 'name' => '性别',
  36. 'width' => 8,
  37. ],
  38. [
  39. 'name' => '身份证号',
  40. 'width' => 30,
  41. ],
  42. [
  43. 'name' => '手机号',
  44. 'width' => 20,
  45. ],
  46. [
  47. 'name' => '年龄',
  48. 'width' => 8,
  49. ],
  50. [
  51. 'name' => '供应商',
  52. 'width' => 30,
  53. ],
  54. [
  55. 'name' => '派遣单位',
  56. 'width' => 30,
  57. ],
  58. [
  59. 'name' => '派遣工种',
  60. 'width' => 15,
  61. ],
  62. [
  63. 'name' => '入职日期',
  64. 'width' => 15,
  65. ],
  66. [
  67. 'name' => '是否完善注册信息',
  68. 'width' => 20,
  69. ],
  70. [
  71. 'name' => '银行卡号',
  72. 'width' => 30,
  73. ],
  74. [
  75. 'name' => '银行名称',
  76. 'width' => 20,
  77. ],
  78. ];
  79. protected $rowNums = 0;
  80. protected $factory_id = 0;
  81. protected $supplier_id = 0;
  82. protected $where = false;
  83. protected $day = 0;
  84. protected $lastC = 'A';
  85. protected $fields = [];
  86. public function __construct($day, $factory_id, $supplier_id, $field_ids = [], $where = false)
  87. {
  88. $this->day = $day;
  89. $this->factory_id = $factory_id;
  90. $this->supplier_id = $supplier_id;
  91. $this->where = $where;
  92. $fields = [];
  93. if (is_string($field_ids)) {
  94. $fields = self::BaseFields;
  95. } else {
  96. foreach ($field_ids as $field_id) {
  97. $fields[] = self::BaseFields[$field_id];
  98. }
  99. }
  100. $this->fields = $fields;
  101. }
  102. public function query()
  103. {
  104. $day = $this->day;
  105. $factory_id = $this->factory_id;
  106. $supplier_id = $this->supplier_id;
  107. $where = $this->where;
  108. return Staff::query()
  109. ->with(['human' => function ($q) {
  110. return $q->select('*');
  111. }, 'factory', 'supplier'])
  112. ->where('status', ModelStatusEnum::OK)
  113. ->where('step', StaffStepEnum::WORK)
  114. ->whereIn('entry_status', [StaffEntryStatusEnum::WAIT_SIGN, StaffEntryStatusEnum::WORKING])
  115. ->when($this->factory_id, function ($query) use ($factory_id) {
  116. return $query->where('factory_id', $factory_id);
  117. })
  118. ->when($day, function ($query) use ($day) {
  119. return $query->whereDate('induction_date', $day);
  120. })->when($supplier_id, function ($query) use ($supplier_id) {
  121. return $query->where('supplier_id', $supplier_id);
  122. })
  123. ->when($where, function ($query) use ($where) {
  124. return $query->where($where);
  125. })
  126. ->orderBy('id');
  127. }
  128. public function headingRow(): int
  129. {
  130. return 2;
  131. }
  132. public function headings(): array
  133. {
  134. $room_name = '在职员工名单';
  135. $data[] = [$room_name];
  136. $data[] = array_column($this->fields, 'name');
  137. return $data;
  138. }
  139. public function columnWidths(): array
  140. {
  141. $i = 65;
  142. $data = [];
  143. foreach ($this->fields as $k => $field) {
  144. $s = intval($k / 26);
  145. if (!$s) $key = chr($i + $k);
  146. if ($s > 0) {
  147. $y = intval($k % 26);
  148. $key = chr($s + $i - 1);
  149. $key .= chr($y + $i);
  150. }
  151. $data["{$key}"] = $field['width'];
  152. $this->lastC = $key;
  153. }
  154. return $data;
  155. }
  156. public function styles(Worksheet $sheet)
  157. {
  158. $last = $this->lastC;
  159. $cellRange = "A1:{$last}1";
  160. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  161. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  162. // 文字居中
  163. $lastrow = $sheet->getHighestRow();
  164. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  165. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  166. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  167. $sheet->mergeCells("A1:{$last}1"); //合并
  168. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  169. }
  170. public function map($row): array
  171. {
  172. $row = $row->toArray();
  173. $this->rowNums++;
  174. $data = [];
  175. foreach (array_column($this->fields, 'name') as $k => $field) {
  176. if ($field == '序号') $data[$k] = $this->rowNums;
  177. if ($field == '姓名') {
  178. $data[$k] = $row['human'] ? $row['human']['name'] : '';
  179. }
  180. if ($field == '身份证号') {
  181. $data[$k] = $row['human'] ? $row['human']['card_id'] . ' ' : '';
  182. }
  183. if ($field == '性别') {
  184. if ($row['human']) {
  185. if ($row['human']['sex'] == 1) $data[$k] = '男';
  186. if ($row['human']['sex'] == 2) $data[$k] = '女';
  187. } else {
  188. $data[$k] = '未知';
  189. }
  190. }
  191. if ($field == '年龄') {
  192. $data[$k] = $row['human'] ? $row['human']['age'] : '';
  193. }
  194. if ($field == '手机号') {
  195. $data[$k] = $row['human'] ? $row['human']['mobile'] : '';
  196. }
  197. if ($field == '供应商') {
  198. $data[$k] = $row['supplier'] ? $row['supplier']['name'] : '';
  199. }
  200. if ($field == '派遣单位') {
  201. $data[$k] = $row['factory'] ? $row['factory']['name'] : '';
  202. }
  203. if ($field == '派遣工种') {
  204. $data[$k] = Dict::byCodeAndIdGetDict(Position::JOB_CODE, $row['job_position']);
  205. }
  206. if ($field == '入职日期') $data[$k] = $row['induction_date'];
  207. if ($field == '是否完善注册信息') {
  208. if ($row['human']) {
  209. $data[$k] = $row['human']['is_register_info'] ? '已完善' : '未完善';
  210. } else {
  211. $data[$k] = $row['human'] ? $row['human']['is_register_info'] : '未完善';
  212. }
  213. }
  214. if ($field == '银行卡号') {
  215. $data[$k] = $row['human'] ? $row['human']['bank_no'] . ' ' : '';
  216. }
  217. if ($field == '银行名称') {
  218. $data[$k] = $row['human'] ? $row['human']['bank_name'] : '';
  219. }
  220. }
  221. return $data;
  222. }
  223. }