ExportFileService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\ExportOrder;
  4. use App\Exports\MultiSheetExport;
  5. use App\Model\Employee;
  6. use App\Model\Quantization;
  7. use App\Model\RevenueCost;
  8. use Maatwebsite\Excel\Facades\Excel;
  9. class ExportFileService extends Service
  10. {
  11. public static $filename = "";
  12. //导出的方式 0 选择导出的数据 1 查询后 导出指定的数据(最多每次一千条)
  13. public static $export_type = [
  14. 0,
  15. 1
  16. ];
  17. public function exportAll($data,$user){
  18. if(empty($data['menu_id'])) return [false, '菜单ID不能为空'];
  19. list($function, $name) = EmployeeService::fillMenu2($data['menu_id'], $user);
  20. if (empty($function) || ! method_exists(self::class, $function)) return [false, "导出方法不存在,请联系开发"];
  21. self::$filename = $name;
  22. $export_type = $data['export_type'] ?? 0;
  23. if(! isset(self::$export_type[$export_type])) return [false,'导出文件方式错误或者不存在'];
  24. if(empty($export_type)){
  25. if(empty($data['id'])) return [false,'请选择导出数据'];
  26. $search = $data;
  27. }else{
  28. if(empty($data['order_search'])) return [false,'搜索条件不能为空'];
  29. $search = $data['order_search'];
  30. if(empty($search['page_index'])) return [false,'请选择导出数据的开始页码'];
  31. if(empty($search['page_size'])) return [false,'请选择导出数据的条数'];
  32. if($search['page_size'] > 5000) return [false,'请选择导出数据的条数每次最多5000条'];
  33. $data['order_search']['menu_id'] = $data['menu_id'];
  34. $search = $data['order_search'];
  35. }
  36. list($status, $return) = $this->$function($search,$user);
  37. if(! $status) return [false, $return];
  38. return [true, $return];
  39. }
  40. private function fillData($data, $column, &$return)
  41. {
  42. // 预先创建包含默认值的键数组
  43. $default = array_fill_keys($column, '');
  44. foreach ($data as $value) {
  45. // 提取交集,并用默认值补充缺失的键
  46. $return[] = array_merge($default, array_intersect_key($value, $default));
  47. }
  48. }
  49. private function fillTotalData($data, $column, &$return){
  50. $tmp = [];
  51. foreach ($column as $value){
  52. $key = $value['key'];
  53. if(! empty($value['sum']) && isset($data[$value['key']])){
  54. $decimals = $col['decimals'] ?? 2;
  55. // $tmp[$value['key']] = $data[$value['key']];
  56. // 用 number_format 格式化输出(保持字符串形式,避免科学计数)
  57. $tmp[$key] = number_format((float)$data[$key], $decimals, '.', '');
  58. }else{
  59. $tmp[$value['key']] = "";
  60. }
  61. }
  62. $return[] = $tmp;
  63. }
  64. public function one($ergs, $user){
  65. $service = new QuantizationService();
  66. $ergs['type'] = Quantization::type_one;
  67. list($status, $model) = $service->quantizationCreateCommon($ergs, $user);
  68. if(! $status) return [false, $model];
  69. // 导出数据
  70. $header_default = $user['e_header_default'];
  71. $column = array_column($header_default,'key');
  72. $header = array_column($header_default,'value');
  73. $detailKeys = ['a', 'b', 'c', 'd', 'e', 'f'];
  74. $detailLabels = ['维度名称', '维度总分', '总得分', '维度详细名称', '分数', '打分比例'];
  75. $fullColumn = array_merge($column, $detailKeys);
  76. $fullHeader = array_merge($header, $detailLabels);
  77. // 2. 导出逻辑(建议直接返回文件流或路径)
  78. $exportData = [];
  79. $employeeMap = Employee::pluck('emp_name', 'id')
  80. ->toArray();
  81. $model->with('details')->chunk(200, function ($items) use (&$exportData, $fullColumn, $employeeMap) {
  82. foreach ($items as $item) {
  83. $mainRow = $item->toArray();
  84. $mainRow['crt_name'] = $employeeMap[$mainRow['crt_id']] ?? "";
  85. $mainRow['crt_time'] = $mainRow['crt_time'] ? date('Y-m-d H:i:s', $mainRow['crt_time']) : '';
  86. // 如果该主行没有明细,也要保底导出一行
  87. if ($item->details->isEmpty()) {
  88. $exportData[] = $this->mapRowToColumn($mainRow, $fullColumn);
  89. continue;
  90. }
  91. // 1. 先取出所有顶级父项 (parent_id == 0)
  92. $parentDetails = $item->details->where('parent_id', 0);
  93. foreach ($parentDetails as $parent) {
  94. // --- A. 写入父级行 ---
  95. $parentRow = $mainRow;
  96. $parentRow['a'] = $parent->title;
  97. $parentRow['b'] = $parent->quantization_score;
  98. $parentRow['c'] = $parent->score;
  99. $parentRow['d'] = '';
  100. $parentRow['e'] = '';
  101. $parentRow['f'] = '';
  102. $exportData[] = $this->mapRowToColumn($parentRow, $fullColumn);
  103. // --- B. 紧接着查找并写入该父级下的所有子级 ---
  104. $children = $item->details->where('parent_id', $parent->id);
  105. foreach ($children as $child) {
  106. $childRow = $mainRow;
  107. // 子级行:a, b, c 字段可以保留父级信息(方便对账),或者留空
  108. $childRow['a'] = $parent->title;
  109. $childRow['b'] = $parent->quantization_score;
  110. $childRow['c'] = $parent->score;
  111. // 填充子级特有信息到 d, e, f 列
  112. $childRow['d'] = $child->title;
  113. $childRow['e'] = $child->score;
  114. $childRow['f'] = $child->rate;
  115. $exportData[] = $this->mapRowToColumn($childRow, $fullColumn);
  116. }
  117. }
  118. }
  119. });
  120. return [true, $this->saveExportData($exportData, $fullHeader)];
  121. }
  122. /**
  123. * 辅助方法:确保每行数据字段顺序与表头完全一致
  124. */
  125. private function mapRowToColumn($data, $columns) {
  126. $res = [];
  127. foreach ($columns as $col) {
  128. $res[$col] = $data[$col] ?? '';
  129. }
  130. return $res;
  131. }
  132. public function two($ergs, $user){
  133. $service = new StatisticsService();
  134. $model = $service->valueStatisticsCommon($ergs, $user);
  135. // 导出数据
  136. $return = [];
  137. $header_default = $user['e_header_default'];
  138. $column = array_column($header_default,'key');
  139. $header = array_column($header_default,'value');
  140. $model->chunk(500,function ($data) use(&$return,$column, $service, $user){
  141. $data = $data->toArray();
  142. $list['data'] = $data;
  143. $list = $service->valueStatisticsFillData($list);
  144. //返回数据
  145. $this->fillData($list['data'], $column, $return);
  146. });
  147. return [true, $this->saveExportData($return,$header)];
  148. }
  149. public function saveExportData($data, $headers, $type = 'default',$file_name = ''){
  150. if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
  151. $filename = $file_name . '.' . 'xlsx';
  152. $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []);
  153. return $filename;
  154. }
  155. public function saveExportData2($data,$file_name = ''){
  156. if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
  157. $filename = $file_name . '.' . 'xlsx';
  158. \Maatwebsite\Excel\Facades\Excel::store(new MultiSheetExport($data),"/public/export/{$filename}", null, 'Xlsx', []);
  159. return $filename;
  160. }
  161. }