| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace App\Service;
- use App\Exports\ExportOrder;
- use App\Exports\MultiSheetExport;
- use App\Model\Employee;
- use App\Model\Quantization;
- use App\Model\RevenueCost;
- use Maatwebsite\Excel\Facades\Excel;
- class ExportFileService extends Service
- {
- public static $filename = "";
- //导出的方式 0 选择导出的数据 1 查询后 导出指定的数据(最多每次一千条)
- public static $export_type = [
- 0,
- 1
- ];
- public function exportAll($data,$user){
- if(empty($data['menu_id'])) return [false, '菜单ID不能为空'];
- list($function, $name) = EmployeeService::fillMenu2($data['menu_id'], $user);
- if (empty($function) || ! method_exists(self::class, $function)) return [false, "导出方法不存在,请联系开发"];
- self::$filename = $name;
- $export_type = $data['export_type'] ?? 0;
- if(! isset(self::$export_type[$export_type])) return [false,'导出文件方式错误或者不存在'];
- if(empty($export_type)){
- if(empty($data['id'])) return [false,'请选择导出数据'];
- $search = $data;
- }else{
- if(empty($data['order_search'])) return [false,'搜索条件不能为空'];
- $search = $data['order_search'];
- if(empty($search['page_index'])) return [false,'请选择导出数据的开始页码'];
- if(empty($search['page_size'])) return [false,'请选择导出数据的条数'];
- if($search['page_size'] > 5000) return [false,'请选择导出数据的条数每次最多5000条'];
- $data['order_search']['menu_id'] = $data['menu_id'];
- $search = $data['order_search'];
- }
- list($status, $return) = $this->$function($search,$user);
- if(! $status) return [false, $return];
- return [true, $return];
- }
- private function fillData($data, $column, &$return)
- {
- // 预先创建包含默认值的键数组
- $default = array_fill_keys($column, '');
- foreach ($data as $value) {
- // 提取交集,并用默认值补充缺失的键
- $return[] = array_merge($default, array_intersect_key($value, $default));
- }
- }
- private function fillTotalData($data, $column, &$return){
- $tmp = [];
- foreach ($column as $value){
- $key = $value['key'];
- if(! empty($value['sum']) && isset($data[$value['key']])){
- $decimals = $col['decimals'] ?? 2;
- // $tmp[$value['key']] = $data[$value['key']];
- // 用 number_format 格式化输出(保持字符串形式,避免科学计数)
- $tmp[$key] = number_format((float)$data[$key], $decimals, '.', '');
- }else{
- $tmp[$value['key']] = "";
- }
- }
- $return[] = $tmp;
- }
- public function one($ergs, $user){
- $service = new QuantizationService();
- $ergs['type'] = Quantization::type_one;
- list($status, $model) = $service->quantizationCreateCommon($ergs, $user);
- if(! $status) return [false, $model];
- // 导出数据
- $header_default = $user['e_header_default'];
- $column = array_column($header_default,'key');
- $header = array_column($header_default,'value');
- $detailKeys = ['a', 'b', 'c', 'd', 'e', 'f'];
- $detailLabels = ['维度名称', '维度总分', '总得分', '维度详细名称', '分数', '打分比例'];
- $fullColumn = array_merge($column, $detailKeys);
- $fullHeader = array_merge($header, $detailLabels);
- // 2. 导出逻辑(建议直接返回文件流或路径)
- $exportData = [];
- $employeeMap = Employee::pluck('emp_name', 'id')
- ->toArray();
- $model->with('details')->chunk(200, function ($items) use (&$exportData, $fullColumn, $employeeMap) {
- foreach ($items as $item) {
- $mainRow = $item->toArray();
- $mainRow['crt_name'] = $employeeMap[$mainRow['crt_id']] ?? "";
- $mainRow['crt_time'] = $mainRow['crt_time'] ? date('Y-m-d H:i:s', $mainRow['crt_time']) : '';
- // 如果该主行没有明细,也要保底导出一行
- if ($item->details->isEmpty()) {
- $exportData[] = $this->mapRowToColumn($mainRow, $fullColumn);
- continue;
- }
- // 1. 先取出所有顶级父项 (parent_id == 0)
- $parentDetails = $item->details->where('parent_id', 0);
- foreach ($parentDetails as $parent) {
- // --- A. 写入父级行 ---
- $parentRow = $mainRow;
- $parentRow['a'] = $parent->title;
- $parentRow['b'] = $parent->quantization_score;
- $parentRow['c'] = $parent->score;
- $parentRow['d'] = '';
- $parentRow['e'] = '';
- $parentRow['f'] = '';
- $exportData[] = $this->mapRowToColumn($parentRow, $fullColumn);
- // --- B. 紧接着查找并写入该父级下的所有子级 ---
- $children = $item->details->where('parent_id', $parent->id);
- foreach ($children as $child) {
- $childRow = $mainRow;
- // 子级行:a, b, c 字段可以保留父级信息(方便对账),或者留空
- $childRow['a'] = $parent->title;
- $childRow['b'] = $parent->quantization_score;
- $childRow['c'] = $parent->score;
- // 填充子级特有信息到 d, e, f 列
- $childRow['d'] = $child->title;
- $childRow['e'] = $child->score;
- $childRow['f'] = $child->rate;
- $exportData[] = $this->mapRowToColumn($childRow, $fullColumn);
- }
- }
- }
- });
- return [true, $this->saveExportData($exportData, $fullHeader)];
- }
- /**
- * 辅助方法:确保每行数据字段顺序与表头完全一致
- */
- private function mapRowToColumn($data, $columns) {
- $res = [];
- foreach ($columns as $col) {
- $res[$col] = $data[$col] ?? '';
- }
- return $res;
- }
- public function two($ergs, $user){
- $service = new StatisticsService();
- $model = $service->valueStatisticsCommon($ergs, $user);
- // 导出数据
- $return = [];
- $header_default = $user['e_header_default'];
- $column = array_column($header_default,'key');
- $header = array_column($header_default,'value');
- $model->chunk(500,function ($data) use(&$return,$column, $service, $user){
- $data = $data->toArray();
- $list['data'] = $data;
- $list = $service->valueStatisticsFillData($list);
- //返回数据
- $this->fillData($list['data'], $column, $return);
- });
- return [true, $this->saveExportData($return,$header)];
- }
- public function saveExportData($data, $headers, $type = 'default',$file_name = ''){
- if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
- $filename = $file_name . '.' . 'xlsx';
- $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []);
- return $filename;
- }
- public function saveExportData2($data,$file_name = ''){
- if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
- $filename = $file_name . '.' . 'xlsx';
- \Maatwebsite\Excel\Facades\Excel::store(new MultiSheetExport($data),"/public/export/{$filename}", null, 'Xlsx', []);
- return $filename;
- }
- }
|