PackageDispatchExport.php 7.0 KB

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