master
cody 3 months ago
parent e11528637d
commit d903e9aeef

@ -863,18 +863,17 @@ class UserController extends BaseController
];
}
// 构建返回数据(使用手机号作为唯一标识)
// 构建返回数据(使用ID作为唯一标识)
$list[$key] = [
'name' => $name,
'company_name' => $companyName,
'company_position' => $companyPosition,
'matched' => $matchedUser ? true : false,
'mobile' => $matchedUser ? $matchedUser->mobile : null, // 使用手机号作为唯一标识
'id' => $matchedUser ? $matchedUser->id : null, // 使用ID作为唯一标识
'existing_data' => $matchedUser ? [
'id' => $matchedUser->id,
'username' => $matchedUser->username,
'name' => $matchedUser->name,
'mobile' => $matchedUser->mobile,
'company_name' => $matchedUser->company_name,
'company_position' => $matchedUser->company_position,
] : null,
@ -947,109 +946,80 @@ class UserController extends BaseController
$filteredRecords = $all['data'];
$suc = 0;
$updateCount = 0;
$unmatchedUsers = []; // 记录未匹配到的用户
$failedRecords = []; // 记录导入失败的记录
DB::beginTransaction();
try {
foreach ($filteredRecords as $index => $record) {
// 获取匹配的手机号(如果预览时已匹配到)
$mobile = $record['mobile'] ?? null;
// 如果没有mobile尝试通过姓名、公司、职位再次匹配
if (!$mobile) {
$name = $record['name'] ?? '';
$companyName = $record['company_name'] ?? '';
$companyPosition = $record['company_position'] ?? '';
if (!empty($name) && !empty($companyName) && !empty($companyPosition)) {
$matchedUser = $this->model->where('username', $name)
->where('company_name', $companyName)
->where('company_position', $companyPosition)
->first();
if ($matchedUser) {
$mobile = $matchedUser->mobile;
}
}
}
// 获取匹配的用户ID如果预览时已匹配到
$userId = $record['id'] ?? null;
// 如果仍未匹配到手机号,记录并跳过
if (!$mobile) {
$unmatchedUsers[] = [
// 如果没有ID记录失败并跳过
if (!$userId) {
$failedRecords[] = [
'row' => $index + 1,
'name' => $record['name'] ?? '',
'company_name' => $record['company_name'] ?? '',
'company_position' => $record['company_position'] ?? '',
'reason' => '缺少用户ID'
];
continue;
}
// 去除匹配相关的字段,避免更新到数据库
unset($record['matched'], $record['existing_data']);
unset($record['matched'], $record['existing_data'], $record['id']);
// 去除空值
$record = array_filter($record, function ($value) {
return $value != '';
});
// 通过手机号查找并更新用户
$user = $this->model->where('mobile', $mobile)->first();
if ($user) {
// 通过ID查找并更新用户
$user = $this->model->find($userId);
if (!$user) {
// 用户不存在,记录失败并跳过
$failedRecords[] = [
'row' => $index + 1,
'name' => $record['name'] ?? '',
'company_name' => $record['company_name'] ?? '',
'company_position' => $record['company_position'] ?? '',
'reason' => '用户不存在ID: ' . $userId . ''
];
continue;
}
// 设置username如果name字段存在
if (isset($record['name'])) {
$record['username'] = $record['name'];
}
// 所有数据通过追加的形式更新参考importStudy的逻辑
foreach ($record as $k => &$v) {
if (!in_array($k, User::$coverFields)) {
// 追加更新
$tempArray = explode(',', ($user->$k ?? '') . ',' . $v);
$tempArray = array_unique(array_filter($tempArray));
$v = implode(',', $tempArray);
}
}
// 直接覆盖更新
$user->fill($record);
$user->save();
$updateCount++;
$suc++;
// 写入报名表(如果有课程信息)
if (isset($record['course_id']) && !empty($record['course_id'])) {
$whereSign = ['course_id' => $record['course_id'], 'user_id' => $user->id];
$dataSign = [
'course_id' => $record['course_id'],
'user_id' => $user->id,
'is_import' => 1,
'status' => $record['status'] ?? 1,
'fee_status' => $record['fee_status'] ?? 0
];
$courseSign = CourseSign::updateOrCreate($whereSign, $dataSign);
// 加导入次数,加预约次数
if ($courseSign->status == 1) {
CourseAppointmentTotal::add($courseSign->user_id, $courseSign->id);
}
}
}
}
// 如果有未匹配到的用户,返回错误
if (count($unmatchedUsers) > 0) {
DB::rollBack();
$unmatchedNames = array_map(function ($user) {
return "第{$user['row']}行:{$user['name']}{$user['company_name']} - {$user['company_position']}";
}, $unmatchedUsers);
return $this->fail([
ResponseCode::ERROR_BUSINESS,
'以下用户未匹配到,无法更新:' . implode('', $unmatchedNames)
]);
}
DB::commit();
return $this->success([
// 构建返回结果
$result = [
'total' => count($filteredRecords),
'suc' => $suc,
'update_count' => $updateCount
]);
'update_count' => $updateCount,
'failed_count' => count($failedRecords),
'failed_records' => $failedRecords
];
// 如果有失败的记录,添加提示信息
if (count($failedRecords) > 0) {
$failedNames = array_map(function ($record) {
return "第{$record['row']}行:{$record['name']}{$record['company_name']} - {$record['company_position']}- {$record['reason']}";
}, $failedRecords);
$result['message'] = '以下记录导入失败:' . implode('', $failedNames);
}
return $this->success($result);
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);

@ -83,7 +83,6 @@ class User extends Authenticatable implements Auditable
public static $coverFields = [
'name',
'sex',
'mobile',
'company_has_share',
'company_name',
'school',

Loading…
Cancel
Save