This commit is contained in:
2025-03-18 11:20:49 +08:00
parent 87aa221719
commit 930ed06139
4 changed files with 81 additions and 1 deletions

View File

@ -14,6 +14,7 @@ use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;
use think\exception\HttpResponseException;
use \PhpOffice\PhpSpreadsheet\IOFactory;
/**
* 工单管理
@ -363,4 +364,56 @@ class Ticket extends Controller
->append(['imgs_arr', 'source_type_name', 'status_text', 'type_name', 'last_reply'])->with('approval');
});
}
/**
* @return void
* @auth true
*/
public function import() {
$file = $this->app->request->post('file');
if (!$file) {
// 创建一个临时Excel模板用于下载
$this->redirect('/static/excel/ticket_import_template.xlsx');
};
$file = '.' . str_replace($this->app->request->domain(), '', $file);
//表格字段对应
$fields = [
'A' => 'title',
'B' => 'type_id',
'C' => 'content',
'D' => 'contact_name',
'E' => 'contact_phone',
'F' => 'contact_address',
'G' => 'ticket_address',
];
//加载文件
$spreadsheet = IOFactory::load($file);
$sheet = $spreadsheet->getActiveSheet(); // 获取表格
$highestRow = $sheet->getHighestRow(); // 取得总行数
$sheetData = [];
for ($row = 2; $row <= $highestRow; $row++) { // $row表示从第几行开始读取
foreach ($fields as $cell => $field) {
$value = $sheet->getCell($cell . $row)->getValue();
$value = trim($value);
$sheetData[$row][$field] = $value;
}
}
$sheetData = array_values($sheetData);
foreach ($sheetData as $key => $value) {
if (!empty($value['type_id'])) {
$type = TicketType::query()->where(['id' => $value['type_id']])->find();
if (empty($type)) {
$this->error('工单类型【'.$value['type_id'].'】不存在');
} else {
$sheetData[$key]['type_id'] = $type['id'];
}
}
$sheetData[$key]['create_at'] = date('Y-m-d H:i:s');
$sheetData[$key]['user_id'] = $this->request->session('user')['id'];
$sheetData[$key]['status'] = 0;
}
TicketTicket::mk()->saveAll($sheetData);
$this->success("成功");
}
}