You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
14 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services;
use App\Http\Controllers\API\PEISApiController;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class FenZhenNewService
{
public function export($dates,$table_num=0){
ini_set('memory_limit', '512M');
$table_list=[];
$clinic_list=[];
$table_list3=[];
$client = new Client();
$url = env('FENZHEN_URL');
$header=[
'Authorization' => 'Bearer BD30333C-CBD4-4BA3-A27C-76727FF4D8B1',
'Content-Type' => 'application/json; charset=utf-8',
];
$api_dates=[];
$promises=[];
foreach ($dates as $date) {
//遍历缓存,如果没有则请求接口,否则直接读取缓存文件
$fileFound = $this->findFilesByDate($date);
if ($fileFound !== null) {
$fileContent = Storage::get($fileFound);
$dataArray = json_decode($fileContent, true);
$table_list = array_merge($table_list, $dataArray['data']['table1'] ?? []);
$clinic_list = array_merge($clinic_list, $dataArray['data']['table2'] ?? []);
$table_list3 = array_merge($table_list3, $dataArray['data']['table3'] ?? []);
}else{
$api_dates[]=$date;
$promises[]=$client->postAsync($url.'/api/Open/TiJian/export',['headers' => $header, 'json' => ['date'=>$date]]);
}
}
$all_request_status=true;//所有接口请求状态
// 等待所有请求完成
$results = Promise\Utils::settle($promises)->wait();
foreach ($results as $key => $result) {
if ($result['state'] === 'fulfilled') {
Log::info('请求接口'.$api_dates[$key]);
$response = $result['value'];
$responseBody=(string) $response->getBody();
//把接口请求成功的缓存到本地txt
$txtName=$api_dates[$key].'created_at'.date('Y-m-d H:m:s');
$TxtfilePath = 'fenzhen_tongji_temp/'.$txtName.'.txt';
if($api_dates[$key]<date('Y-m-d')){
Storage::put($TxtfilePath, $responseBody);
}
$res=json_decode($responseBody,true);
// echo "Response from $key: " . $response->getBody() . "\n";
$table_list = array_merge($table_list, $res['data']['table1'] ?? []);
$clinic_list = array_merge($clinic_list, $res['data']['table2'] ?? []);
$table_list3 = array_merge($table_list3, $res['data']['table3'] ?? []);
}else{
$all_request_status=false;
Log::info("Request failed with key: $key, reason: " . $result['reason']->getMessage());
}
}
if($all_request_status===false){
return ['status'=>false,'msg'=>'部分请求失败,请重新尝试','url'=>''];
}
foreach ($table_list as $key=> $table_item) { //匹配$table_list3获取体检号
foreach ($table_list3 as $table_item3) {
if($table_item['name']==$table_item3['name'] and $table_item['phone']==$table_item3['phone']){
$table_list[$key]['tj_number']=$table_item3['uuid'];
break;
}else{
$table_list[$key]['tj_number']="";
}
}
}
// foreach ($dates as $date) {
// $res = $fzc->fz('export', [
// 'date' => $date
// ]);
//
// // 使用 array_merge 逐个追加每日期的数据到总列表中
// $table_list = array_merge($table_list, $res['data']['table1'] ?? []);
// $clinic_list = array_merge($clinic_list, $res['data']['table2'] ?? []);
// $table_list3 = array_merge($table_list3, $res['data']['table3'] ?? []);
// }
$template_path = Storage::path('public/excel/check_time.xlsx');
if($table_num==1){
$template_path = Storage::path('public/excel/check_time_table1.xlsx');
}
if($table_num==2){
$template_path = Storage::path('public/excel/check_time_table2.xlsx');
}
if($table_num==3){
$template_path = Storage::path('public/excel/check_time_table3.xlsx');
}
if($table_num==4){
$template_path = Storage::path('public/excel/check_time_table4.xlsx');
}
$spreadsheet = IOFactory::load($template_path);
$worksheet = $spreadsheet->getActiveSheet();
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => 'FF000000'],
],
],
];
$row = 3;
$col = [
'A' => 'date',
'B' => 'name',
'C' => 'number',
'D' => 'tj_number', // 新增列
'E' => 'phone', // 原 phone 列往后移动
'F' => 'group_name',
'G' => 'clinic',
'H' => 'doctor',
'I' => 'queue',
'J' => 'calling',
'K' => 'done',
'L' => 'clinic_check_time',
'M' => 'true_check_time',
'N' => 'timeout_check_time',
'O' => 'clinic_wait_time',
'P' => 'true_wait_time',
'Q' => 'timeout_wait_time',
'R' => 'clinic_all_time',
'S' => 'true_all_time',
'T' => 'timeout_all_time',
];
if($table_num==1){
foreach ($table_list as $table_item) {
foreach ($col as $index => $key) {
$worksheet->setCellValueExplicit($index . $row, $table_item[$key], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}
$row++;
}
$worksheet->getStyle('A3:T' . ($row - 1))->applyFromArray($styleArray);
}
$row = 3;
$col = [
'A' => 'group_name',
'B' => 'clinic',
'C' => 'doctor',
'D' => 'check',
'E' => 'check_out',
'F' => 'wait',
'G' => 'wait_out',
];
if($table_num==2){
foreach ($clinic_list as $clinic_item) {
foreach ($col as $index => $key) {
$worksheet->setCellValueExplicit($index . $row, $clinic_item[$key], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}
$row++;
}
$worksheet->getStyle('A3:G' . ($row - 1))->applyFromArray($styleArray);
}
$row = 3;
$col = [
'A' => 'date',
'B' => 'name',
'C' => 'number',
'D' => 'uuid',
'E' => 'phone',
'F' => 'combo',
'G' => 'clinic_time',
'H' => 'used_time',
'I' => 'timeout',
];
//汇总信息
$usercount=count($table_list3);
$biaozhun_timecount=0;
$shiji_timecount=0;
$chaoshi_timecount=0;
$weichaoshi_count=0;
$tj_numbers=[];
$peis = new PEISApiController();
if($table_num==3){
foreach ($table_list3 as $table_list3_i) {
$tj_numbers[]=$table_list3_i['uuid'];
$biaozhun_timecount+=$table_list3_i['clinic_time'];
$shiji_timecount+=$table_list3_i['used_time'];
$chaoshi_timecount+=$table_list3_i['timeout'];
if($table_list3_i['timeout']<=0){
$weichaoshi_count++;
}
$data = [
'电话号码' => "",
'证件号码' => "",
'体检号' => $table_list3_i['uuid'],
'包含内部信息' => true
];
$reports = $peis::Post('体检报告查询', 1, $data);
if(isset($reports['data'][0]['套餐名称'])){
$table_list3_i['combo']=$reports['data'][0]['套餐名称'];
}else{
$table_list3_i['combo']='';
}
foreach ($col as $index => $key) {
$worksheet->setCellValueExplicit($index . $row, $table_list3_i[$key], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}
$row++;
}
$zhushilv=0;
if($usercount>0){
$zhushilv=$weichaoshi_count/$usercount;
}
$worksheet->setCellValueExplicit('A' . $row, '汇总', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('B' . $row, '客户总数', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('C' . $row, $usercount, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('E' . $row, '总用时:', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('G' . $row, $biaozhun_timecount, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('H' . $row, $shiji_timecount, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('I' . $row, $chaoshi_timecount, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('B' . ($row+1), '未超时总数', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('C' . ($row+1), $weichaoshi_count, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('B' . ($row+2), "体检准时率", \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('C' . ($row+2),$zhushilv , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->getStyle('A3:I' . ($row +2))->applyFromArray($styleArray);
}
//---------------------------vvvvvvvvvvvvvvvvvvvvvvv第四个表格先隐藏
/**
//准时到检率
$jianhou_sign_count=0;
$zhunshi_count=0;
$tj_numbers_group= array_chunk($tj_numbers, 100);
$waitGroup=[];
foreach ($tj_numbers_group as $index => $tj_number) {
$tj_number=implode(",", $tj_number);
$wait=$peis::Get($peis::Api('报告时间计算',$tj_number),'460107000001',"报告时间计算");
if(isset($wait['Data'])){
$jianhou_sign_count=$jianhou_sign_count+count($wait['Data']);
$waitGroup=array_merge($waitGroup,$wait['Data']);
}
}
//dd($wait);
foreach ($table_list3 as $key=> $fz_value) {
foreach ($waitGroup as $index => $sx_value) {
if($fz_value['uuid']==$sx_value['体检号']){
if($fz_value['date'].' '.$fz_value['end_time']>$sx_value['检后签到时间']){
$zhunshi_count++;
}
}
}
}
$daojianlv=0;
if($jianhou_sign_count>0){
$daojianlv=$zhunshi_count/$jianhou_sign_count;
}
$datesRange=$dates[0].'~'.$dates[count($dates) - 1];
$worksheet->setCellValueExplicit('AN' . 3,$datesRange, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AO' . 3,$zhunshi_count , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AP' . 3,$jianhou_sign_count , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AQ' . 3,round($daojianlv, 2) , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->getStyle('AN3:AQ' . 3)->applyFromArray($styleArray);
//预约率
$yyl_yuyue_count=0;//预约人数
$yyl_tj_count=0;//体检总人数
$yuyuelv=0;
$yuyueList=DB::table('orders')->whereIn('appointment_date',$dates)->whereIn('status',[2,4])->whereNotNull(['appointment_number'])->get();
$yyl_yuyue_count=count($yuyueList);
$yyl_tj_count=$usercount;
if($yyl_tj_count>0){
$yuyuelv=$yyl_yuyue_count/$yyl_tj_count;
}
$worksheet->setCellValueExplicit('AN' . 7,$datesRange , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AO' . 7,$yyl_yuyue_count , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AP' . 7,$yyl_tj_count , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AQ' . 7,round($yuyuelv, 2) , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->getStyle('AN7:AQ' . 7)->applyFromArray($styleArray);
//预约准时率
$yyzsl_ren_count=0;//预约准时到达人数
$yyzsl_zong_count=0;//预约总人数
$yyzsl_zong_count=$yyl_yuyue_count;
foreach ($yuyueList as $key=> $yy_value) {
if($yy_value->appointment_date.' '.$yy_value->appointment_time > $yy_value->check_time){
$yyzsl_ren_count++;
}
}
$zhunshilv=0;
if($yyzsl_zong_count>0){
$zhunshilv=$yyzsl_ren_count/$yyzsl_zong_count;
}
$worksheet->setCellValueExplicit('AN' . 11,$datesRange , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AO' . 11,$yyzsl_ren_count , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AP' . 11,$yyzsl_zong_count , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit('AQ' . 11,round($zhunshilv, 2) , \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->getStyle('AN11:AQ' . 11)->applyFromArray($styleArray);
*/
//-----------------------------------^^^^^^^^
//新建Sheet2
// $worksheet2 = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Sheet2');
// $spreadsheet->addSheet($worksheet2);
// $worksheet2->setCellValue('A1', 'Foo');
// $worksheet2->setCellValue('B1', 'Bar');
$file_name = Str::orderedUuid();
$dir_path = "public/excel/" . date('Ym') . '/' . $file_name;
Storage::makeDirectory($dir_path);
$name_date = date('n.j', strtotime($date . ' 00:00:00'));
$excel_path = $dir_path . "/体检报表-时间监控$name_date.xlsx";
$writer = new Xlsx($spreadsheet);
$writer->save(Storage::path($excel_path));
$url = Storage::url($excel_path);
return ['status'=>true,'msg'=>'查询完成','url'=> env('APP_URL').$url];
}
function findFilesByDate($datePart, $basePath = 'fenzhen_tongji_temp')
{
// 获取今天的日期格式与文件中的一致
$todayFormatted = Carbon::today()->format('Y-m-d');
// 遍历指定路径下的所有文件
$files = Storage::files($basePath);
foreach ($files as $file) {
// 使用 basename 获取文件名
$fileName = basename($file);
// 检查文件名是否以前缀 $datePart 开头
if (strpos($fileName, $datePart) === 0) {
return $file; // 返回找到的文件路径
}
}
// 如果没有找到任何符合条件的文件,返回 null 或其他信息
return null;
}
}