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.

121 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\API\Admin\YeWu;
use App\Http\Controllers\Controller;
use App\Lib\Tools;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
class QuestionLogController extends Controller
{
public function Export()
{
$q_type = request('q_type');
$dateRange = request('dateRange');
if(!isset($dateRange) or count($dateRange)<>2) return \Yz::echoError1("请选择日期");
$dateRange=[$dateRange[0].' 00:00:00',$dateRange[1].' 23:59:59'];
$q_list=DB::table('questions')->where(['q_type'=>2,'is_del'=>0,'status'=>1])->orderBy('order','asc')->get();
$log_list = DB::table('questions_log')->whereBetween('created_at',$dateRange)->where(['q_type' => $q_type])->get();
if(count($log_list)==0) return \Yz::echoError1("暂无数据可导出");
$cols_SN = $this->generateSequence(400);
$template_path = Storage::path('public/excel/manyidudiaocha.xlsx');
$spreadsheet = IOFactory::load($template_path);
$worksheet = $spreadsheet->getActiveSheet();
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => 'FF000000'],
],
],
];
$row=3;
foreach ($log_list as $key=> $item) {
$personinfo=DB::table('web_user_person')->where(['id'=>$item->personid])->first();
$sex='未知';
if($personinfo->sex==1) $sex='男';
if($personinfo->sex==2) $sex='女';
$item->content=json_decode($item->content,true);
$worksheet->setCellValueExplicit($cols_SN[0] . $row, $key+1, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[1] . $row, $personinfo->name, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[2] . $row, $item->created_at, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[3] . $row, $personinfo->name, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[4] . $row, $personinfo->phone, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[5] . $row, $sex, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[6] . $row, $personinfo->birthday, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
$worksheet->setCellValueExplicit($cols_SN[7] . $row, Tools::GetAge($personinfo->birthday), \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
foreach ( $item->content as $q_key=>$q_item){
foreach ($q_item['more'] as $morekey=>$moreitem){
if($moreitem<>null){
$q_item['answer'][0]=$q_item['answer'][0].','.$moreitem;
}
}
$worksheet->setCellValueExplicit($cols_SN[8+$q_key] . $row, $q_item['answer'][0], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}
$row++;
}
$worksheet->getStyle('A3:Z' . ($row - 1))->applyFromArray($styleArray);
$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 . "/满意度调查.xlsx";
$writer = new Xlsx($spreadsheet);
$writer->save(Storage::path($excel_path));
$url = Storage::url($excel_path);
return \Yz::Return(true,"获取成功",['url' => env('APP_URL').$url]);
}
function generateSequence($L) {
$sequence = [];
$current = '';
// Helper function to increment the sequence
function increment(&$str) {
$len = strlen($str);
$str = strrev($str); // Reverse string for easier manipulation
for ($i = 0; $i < $len; $i++) {
if ($str[$i] == 'Z') {
$str[$i] = 'A';
} else {
$str[$i] = chr(ord($str[$i]) + 1);
break;
}
}
// If we've gone through all characters and they were all 'Z'
if ($i == $len && $str[$len-1] == 'A') {
$str .= 'A'; // Append an 'A' at the end, equivalent to carrying over
}
$str = strrev($str); // Reverse back to original order
}
// Initialize the first value of the sequence
if ($L > 0) {
$current = 'A';
$sequence[] = $current;
}
// Generate the sequence up to length L
for ($i = 1; $i < $L; $i++) {
increment($current);
$sequence[] = $current;
}
return $sequence;
}
}