You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
6.4 KiB
165 lines
6.4 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Helpers\ResponseCode;
|
|
use App\Models\VipCustomer;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Rap2hpoutre\FastExcel\FastExcel;
|
|
use Illuminate\Http\Request;
|
|
|
|
class VipCustomerController extends CommonController
|
|
{
|
|
public function index()
|
|
{
|
|
$all = request()->all();
|
|
$list = VipCustomer::where(function ($query) use ($all) {
|
|
if (!empty($all['keyword'])) {
|
|
$keyword = trim($all['keyword']);
|
|
$query->where(function ($q) use ($keyword) {
|
|
$q->where('name', 'like', '%' . $keyword . '%')
|
|
->orWhere('mobile', 'like', '%' . $keyword . '%')
|
|
->orWhere('company_name', 'like', '%' . $keyword . '%')
|
|
->orWhere('idcard', 'like', '%' . $keyword . '%')
|
|
->orWhere('plate_no', 'like', '%' . $keyword . '%');
|
|
});
|
|
}
|
|
if (isset($all['status']) && $all['status'] !== '') {
|
|
$query->where('status', $all['status']);
|
|
}
|
|
if (isset($all['credent']) && $all['credent'] !== '') {
|
|
$query->where('credent', $all['credent']);
|
|
}
|
|
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc');
|
|
if (isset($all['is_export']) && !empty($all['is_export'])) {
|
|
return (new FastExcel($list->limit(5000)->get()->toArray()))->download('VIP客户列表' . date('YmdHis') . '.xlsx', function ($info) {
|
|
return [
|
|
'姓名' => $info['name'] ?? '',
|
|
'手机号' => $info['mobile'] ?? '',
|
|
'证件类型' => ($info['credent'] ?? 1) == 1 ? '身份证' : '护照',
|
|
'证件号码' => $info['idcard'] ?? '',
|
|
'车牌号' => $info['plate_no'] ?? '',
|
|
'单位名称' => $info['company_name'] ?? '',
|
|
'职位' => $info['position'] ?? '',
|
|
'状态' => ($info['status'] ?? 1) == 1 ? '启用' : '禁用',
|
|
'备注' => $info['remark'] ?? '',
|
|
'创建时间' => $info['created_at'] ?? '',
|
|
];
|
|
});
|
|
}
|
|
$list = $list->paginate($all['page_size'] ?? 20);
|
|
return $this->success($list);
|
|
}
|
|
|
|
public function show()
|
|
{
|
|
$all = request()->all();
|
|
$validator = Validator::make($all, [
|
|
'id' => 'required',
|
|
], [
|
|
'id.required' => 'Id必填',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
}
|
|
$detail = VipCustomer::find($all['id']);
|
|
return $this->success($detail);
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$all = request()->all();
|
|
$validator = Validator::make($all, [
|
|
'name' => 'required',
|
|
'mobile' => 'required',
|
|
], [
|
|
'name.required' => '姓名必填',
|
|
'mobile.required' => '手机号必填',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
if (isset($all['id']) && $all['id']) {
|
|
$model = VipCustomer::find($all['id']);
|
|
} else {
|
|
$model = new VipCustomer();
|
|
$all['admin_id'] = $this->getUserId();
|
|
$all['department_id'] = $this->getUser()->department_id;
|
|
}
|
|
$model->fill($all);
|
|
$model->save();
|
|
DB::commit();
|
|
return $this->success('更新成功');
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function destroy()
|
|
{
|
|
$all = request()->all();
|
|
$validator = Validator::make($all, [
|
|
'id' => 'required',
|
|
], [
|
|
'id.required' => 'Id必填',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
}
|
|
VipCustomer::where('id', $all['id'])->delete();
|
|
return $this->success('删除成功');
|
|
}
|
|
|
|
public function import(Request $request)
|
|
{
|
|
$file = $request->file('file');
|
|
if (!($request->hasFile('file') && $file->isValid())) {
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']);
|
|
}
|
|
$ext = $file->getClientOriginalExtension();
|
|
if (!in_array($ext, ['xlsx'])) {
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xlsx格式']);
|
|
}
|
|
$tempFile = $file->getRealPath();
|
|
$dataArray = (new FastExcel)->import($tempFile)->toArray();
|
|
if (empty($dataArray)) {
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '导入文件为空']);
|
|
}
|
|
$keyList = array_keys($dataArray[0]);
|
|
if (!in_array('姓名', $keyList) || !in_array('手机号', $keyList)) {
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '模板字段缺失,至少包含:姓名、手机号']);
|
|
}
|
|
foreach ($dataArray as $value) {
|
|
$name = trim($value['姓名'] ?? '');
|
|
$mobile = trim($value['手机号'] ?? '');
|
|
if (empty($name) || empty($mobile)) {
|
|
continue;
|
|
}
|
|
$credentText = trim($value['证件类型'] ?? '身份证');
|
|
$credent = $credentText === '护照' ? 2 : 1;
|
|
VipCustomer::updateOrCreate(
|
|
['mobile' => $mobile],
|
|
[
|
|
'name' => $name,
|
|
'credent' => $credent,
|
|
'idcard' => trim($value['证件号码'] ?? ''),
|
|
'plate_no' => trim($value['车牌号'] ?? ''),
|
|
'company_name' => trim($value['单位名称'] ?? ''),
|
|
'position' => trim($value['职位'] ?? ''),
|
|
'remark' => trim($value['备注'] ?? ''),
|
|
'status' => trim($value['状态'] ?? '') === '禁用' ? 2 : 1,
|
|
'admin_id' => $this->getUserId(),
|
|
'department_id' => $this->getUser()->department_id,
|
|
]
|
|
);
|
|
}
|
|
return $this->success('导入成功');
|
|
}
|
|
}
|
|
|