This commit is contained in:
Your Name
2026-04-23 15:56:01 +08:00
parent aabf2f3a38
commit f9c8b2a632
258 changed files with 489 additions and 367 deletions
@@ -8,6 +8,7 @@ use app\common\model\ExpressTracking;
use app\common\model\ExpressTrace;
use app\common\model\ExpressStateLog;
use app\common\model\ExpressQueryLog;
use app\common\model\tcm\PrescriptionOrder;
use think\facade\Log;
/**
@@ -86,13 +87,15 @@ class ExpressTrackingService
return null;
}
self::backfillRecipientPhoneFromPrescriptionOrder($tracking);
$startTime = microtime(true);
// 调用快递100查询
// 调用快递100查询(顺丰等单号需带收件人手机号后四位,见 ExpressTrackService
$result = ExpressTrackService::query(
$tracking->express_company,
$tracking->tracking_number,
$tracking->recipient_phone
(string) $tracking->recipient_phone
);
$responseTime = (int) ((microtime(true) - $startTime) * 1000);
@@ -167,6 +170,30 @@ class ExpressTrackingService
return $result;
}
/**
* 物流表未存收件人手机时,从关联业务订单回填(顺丰/京东等查件必填后四位)
*/
private static function backfillRecipientPhoneFromPrescriptionOrder(ExpressTracking $tracking): void
{
$current = trim((string) $tracking->recipient_phone);
if ($current !== '') {
return;
}
if ((string) $tracking->order_type !== 'prescription' || (int) $tracking->order_id <= 0) {
return;
}
$po = PrescriptionOrder::where('id', (int) $tracking->order_id)
->whereNull('delete_time')
->find();
if ($po === null) {
return;
}
$p = trim((string) ($po->recipient_phone ?? ''));
if ($p !== '') {
$tracking->recipient_phone = $p;
}
}
/**
* 保存轨迹明细
*/
@@ -298,6 +325,7 @@ class ExpressTrackingService
public static function autoUpdateBatch(int $limit = 50): array
{
$now = time();
$poTable = (new PrescriptionOrder())->getTable();
// 查询需要更新的记录
// 条件:
@@ -305,15 +333,23 @@ class ExpressTrackingService
// 2. next_update_time <= now(到达更新时间)
// 3. is_signed = 0(未签收)
// 4. current_state 不是终态(排除已签收、退签、拒签)
$list = ExpressTracking::where('auto_update', 1)
->where('next_update_time', '<=', $now)
->where('is_signed', 0)
->whereNotIn('current_state', [
// 5. 关联业务订单 tcm_prescription_order:已存在甘草单号 gancao_reciperl_order_no 的不再走本任务(由甘草侧物流拉取)
$list = ExpressTracking::alias('et')
->leftJoin("{$poTable} po", "et.order_id = po.id AND et.order_type = 'prescription'")
->where('et.auto_update', 1)
->where('et.next_update_time', '<=', $now)
->where('et.is_signed', 0)
->whereNotIn('et.current_state', [
ExpressTracking::STATE_SIGNED,
ExpressTracking::STATE_RETURN_SIGNED,
ExpressTracking::STATE_REJECTED,
])
->whereNull('delete_time')
->whereNull('et.delete_time')
->whereRaw(
"(et.order_type <> ? OR TRIM(COALESCE(po.gancao_reciperl_order_no, '')) = '')",
['prescription']
)
->field('et.*')
->limit($limit)
->select();