Files
oil/server-tp6/app/common/service/QytDriver.php
T
2026-09-18 20:55:21 +08:00

97 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | 企油通 · 司机档案
// +----------------------------------------------------------------------
// | 按身份证号唯一。上游 7.12 / 7.13 / 7.17 都以「手机号 + 身份证号」定位司机,
// | 7.7 下发前会做实名二要素核验,所以本地提前把会导致核验失败的数据标出来:
// | · 同一身份证号出现不同姓名
// | · 同一手机号对应多个身份证号
// | · 同一身份证号换了手机号
// | 累计发放 / 退款 / 订单数不存冗余,列表查询时从订单实时汇总,避免对不上账。
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\common\service;
use think\facade\Db;
class QytDriver
{
/**
* 下单成功后更新司机档案。任何异常都吞掉 —— 档案是辅助数据,不能影响建单
*/
public static function touch(array $order): void
{
$idCard = strtoupper(trim((string) ($order['id_card'] ?? '')));
if ($idCard === '') {
return;
}
$name = trim((string) ($order['driver_name'] ?? ''));
$mobile = trim((string) ($order['mobile'] ?? ''));
$storeId = (int) ($order['store_id'] ?? 0);
$now = time();
try {
$row = Db::name('qyt_driver')->where('store_id', $storeId)->where('id_card', $idCard)->find();
if (!$row) {
try {
Db::name('qyt_driver')->insert([
'id_card' => $idCard, 'name' => $name, 'mobile' => $mobile,
'last_order_time' => $now, 'store_id' => $storeId, 'create_time' => $now, 'update_time' => $now,
]);
} catch (\Throwable $e) {
if (strpos($e->getMessage(), 'Duplicate') === false) {
throw $e;
}
}
$row = Db::name('qyt_driver')->where('store_id', $storeId)->where('id_card', $idCard)->find();
}
$notes = self::detectConflicts($row, $name, $mobile);
// 同一手机号挂在别的身份证号上:两边都标记
if ($mobile !== '') {
$others = Db::name('qyt_driver')
->where('store_id', $storeId)->where('mobile', $mobile)->where('id_card', '<>', $idCard)
->column('driver_id');
if ($others) {
$notes[] = '同一手机号对应多个身份证号';
Db::name('qyt_driver')->whereIn('driver_id', $others)
->update(['conflict' => 1, 'conflict_text' => '同一手机号对应多个身份证号', 'update_time' => $now]);
}
}
$update = [
'name' => $name !== '' ? $name : $row['name'],
'mobile' => $mobile !== '' ? $mobile : $row['mobile'],
'last_order_time' => $now,
'update_time' => $now,
];
if ($notes) {
$update['conflict'] = 1;
$update['conflict_text'] = mb_substr(implode(';', array_unique($notes)), 0, 250);
}
Db::name('qyt_driver')->where('driver_id', $row['driver_id'])->update($update);
} catch (\Throwable $e) {
(new QytOil())->saveLog('司机档案更新失败 order=' . ($order['order_id'] ?? '') . ':' . $e->getMessage());
}
}
/**
* 纯函数:对比已有档案与本次下单的姓名 / 手机号
*/
public static function detectConflicts(array $row, string $name, string $mobile): array
{
$notes = [];
$oldName = (string) ($row['name'] ?? '');
$oldMobile = (string) ($row['mobile'] ?? '');
if ($oldName !== '' && $name !== '' && $oldName !== $name) {
$notes[] = "同一身份证号出现不同姓名:{$oldName} / {$name}";
}
if ($oldMobile !== '' && $mobile !== '' && $oldMobile !== $mobile) {
$notes[] = '手机号变更:' . QytOil::maskMobile($oldMobile) . ' → ' . QytOil::maskMobile($mobile);
}
return $notes;
}
}