|
|
|
|
@ -3,8 +3,12 @@
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Exports\BaseExport;
|
|
|
|
|
use App\Helpers\ResponseCode;
|
|
|
|
|
use App\Models\CustomForm;
|
|
|
|
|
use App\Models\CustomFormField;
|
|
|
|
|
use App\Models\Teacher;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
|
|
|
|
|
|
class TeacherController extends BaseController
|
|
|
|
|
@ -44,7 +48,7 @@ class TeacherController extends BaseController
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$all = request()->all();
|
|
|
|
|
$list = $this->model->with('courseContents.course','courseContents.directionDetail')->where(function ($query) use ($all) {
|
|
|
|
|
$list = $this->model->with('courseContents.course', 'courseContents.directionDetail')->where(function ($query) use ($all) {
|
|
|
|
|
if (isset($all['filter']) && !empty($all['filter'])) {
|
|
|
|
|
foreach ($all['filter'] as $condition) {
|
|
|
|
|
$key = $condition['key'] ?? null;
|
|
|
|
|
@ -224,7 +228,46 @@ class TeacherController extends BaseController
|
|
|
|
|
*/
|
|
|
|
|
public function import()
|
|
|
|
|
{
|
|
|
|
|
return parent::import();
|
|
|
|
|
$all = \request()->all();
|
|
|
|
|
$messages = [
|
|
|
|
|
'data.required' => '数据必填',
|
|
|
|
|
];
|
|
|
|
|
$validator = Validator::make($all, [
|
|
|
|
|
'data' => 'required',
|
|
|
|
|
], $messages);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
|
|
}
|
|
|
|
|
$records = $all['data'];
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
// 获取数据表的所有字段
|
|
|
|
|
$tableName = $this->model->getTable();
|
|
|
|
|
$existingColumns = (new CustomFormField)->getRowTableFieldsByComment($tableName);
|
|
|
|
|
// 过滤掉不存在的字段
|
|
|
|
|
$filteredRecords = array_map(function ($record) use ($existingColumns) {
|
|
|
|
|
return array_intersect_key($record, $existingColumns);
|
|
|
|
|
}, $records);
|
|
|
|
|
// 去除空数据
|
|
|
|
|
$filteredRecords = array_filter($filteredRecords);
|
|
|
|
|
// 分段导入
|
|
|
|
|
foreach ($filteredRecords as $item) {
|
|
|
|
|
$where = ['name'=>$item['name']];
|
|
|
|
|
$data = [
|
|
|
|
|
'name' => $item['name'],
|
|
|
|
|
'sex' => $item['sex'],
|
|
|
|
|
'remark' => $item['remark'],
|
|
|
|
|
'introduce' => $item['introduce'],
|
|
|
|
|
'mobile'=>$item['mobile'],
|
|
|
|
|
];
|
|
|
|
|
$this->model->firstOrCreate($where, $data);
|
|
|
|
|
}
|
|
|
|
|
DB::commit();
|
|
|
|
|
return $this->success(['total' => count($records), 'filter_total' => count($filteredRecords)]);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|