This commit is contained in:
Your Name
2026-04-30 17:36:47 +08:00
parent 4c21ee5938
commit 7e557b1ea2
33 changed files with 8718 additions and 94 deletions
+180 -2
View File
@@ -450,6 +450,182 @@ class OrderLogic
}
}
/**
* 拆分订单:待支付(1)、付呗待审核(5)、已支付(2);已关联业务订单的自动迁关联至各子单
*
* @param array<int, float|int|string> $amounts 子单金额(元),2~20 笔,分合计须与原单一致
* @param array<int, int|string> $orderTypes 与子单一一对应的订单类型 1~7
* @return array{new_order_ids: int[], orders: array<int, array<string, mixed>>}|false
*/
public static function split(int $orderId, array $amounts, array $orderTypes): array|false
{
$order = Order::whereNull('delete_time')->find($orderId);
if (! $order) {
self::setError('订单不存在');
return false;
}
$st = (int) $order->status;
if ($st === 5 && (string) ($order->payment_method ?? '') !== 'fubei') {
self::setError('待审核订单仅支持付呗渠道拆分');
return false;
}
if (! in_array($st, [1, 5, 2], true)) {
self::setError('仅「待支付」「待审核(付呗)」或「已支付」的订单可拆分');
return false;
}
$rawList = array_values($amounts);
$count = count($rawList);
if ($count < 2 || $count > 20) {
self::setError('拆分管数须在 220 之间');
return false;
}
$centsList = [];
foreach ($rawList as $a) {
$v = round((float) $a, 2);
if ($v <= 0) {
self::setError('每笔子单金额须大于 0');
return false;
}
$centsList[] = (int) round($v * 100);
}
$origCents = (int) round(round((float) $order->amount, 2) * 100);
if (array_sum($centsList) !== $origCents) {
self::setError(
'子单金额之和必须等于原订单金额(¥' . number_format($origCents / 100, 2, '.', '') . ''
);
return false;
}
$normAmounts = array_map(static fn (int $c) => round($c / 100, 2), $centsList);
$typeList = array_values($orderTypes);
if (count($typeList) !== $count) {
self::setError('订单类型笔数须与拆分数一致');
return false;
}
$allowedOt = [1, 2, 3, 4, 5, 6, 7, 8];
foreach ($typeList as $ot) {
if (! in_array((int) $ot, $allowedOt, true)) {
self::setError('订单类型无效,须为 18');
return false;
}
}
Db::startTrans();
try {
$origRemark = trim((string) ($order->remark ?? ''));
$splitTag = '[拆自原单#' . $orderId . '-' . (string) ($order->order_no ?? '') . ']';
$newOrders = [];
foreach ($normAmounts as $i => $amt) {
$n = new Order();
$n->order_no = self::generateOrderNo();
$n->patient_id = $order->patient_id;
$n->creator_id = $order->creator_id;
$n->order_type = (int) $typeList[$i];
$n->amount = $amt;
if ($st === 5) {
$n->payment_method = 'fubei';
$n->status = 5;
} elseif ($st === 2) {
$n->status = 2;
$n->payment_method = (string) ($order->payment_method ?? 'manual');
if ((string) ($order->payment_time ?? '') !== '') {
$n->payment_time = (string) $order->payment_time;
}
if ((string) ($order->trade_no ?? '') !== '') {
$n->trade_no = (string) $order->trade_no;
}
} else {
$n->status = 1;
}
$combined = $origRemark !== '' ? $origRemark . ' ' . $splitTag : $splitTag;
$n->remark = mb_substr($combined, 0, 500);
if ($order->payee_userid !== null && (string) $order->payee_userid !== '') {
$n->payee_userid = (string) $order->payee_userid;
}
if ($order->payer_external_userid !== null && (string) $order->payer_external_userid !== '') {
$n->payer_external_userid = (string) $order->payer_external_userid;
}
$n->save();
$newOrders[] = $n;
}
$newIds = [];
foreach ($newOrders as $o) {
$newIds[] = (int) $o->id;
}
// 处方业务订单 ↔ 支付单:原单关联迁移到全部子单(各业务订单对每笔子单各一条关联,合计金额不变)
$linkRows = PrescriptionOrderPayOrder::where('pay_order_id', $orderId)->select();
foreach ($linkRows as $link) {
$poId = (int) $link->prescription_order_id;
if ($poId <= 0) {
continue;
}
PrescriptionOrderPayOrder::where('prescription_order_id', $poId)
->where('pay_order_id', $orderId)
->delete();
foreach ($newIds as $nid) {
$dup = PrescriptionOrderPayOrder::where('prescription_order_id', $poId)
->where('pay_order_id', $nid)
->count();
if ($dup > 0) {
continue;
}
$nl = new PrescriptionOrderPayOrder();
$nl->prescription_order_id = $poId;
$nl->pay_order_id = $nid;
$nl->create_time = time();
$nl->save();
}
}
$order->remark = mb_substr(
($origRemark !== '' ? $origRemark . ' | ' : '') . '已拆分,子单ID:' . implode(',', $newIds),
0,
500
);
$order->save();
$order->delete();
Db::commit();
$brief = [];
foreach ($newOrders as $o) {
$brief[] = [
'id' => (int) $o->id,
'order_no' => (string) $o->order_no,
'amount' => (float) $o->amount,
'order_type' => (int) $o->order_type,
];
}
return [
'new_order_ids' => $newIds,
'orders' => $brief,
];
} catch (\Throwable $e) {
Db::rollback();
Log::error('Order split failed: ' . $e->getMessage(), ['order_id' => $orderId]);
self::setError('拆分失败:' . $e->getMessage());
return false;
}
}
/**
* @notes 今日收益(按角色权限:超管/主管看全部,普通员工看自己)
* @param int $adminId 当前管理员ID
@@ -723,16 +899,18 @@ class OrderLogic
4 => '首付费用',
5 => '尾款费用',
6 => '其他费用',
7 => '全部费用',
8 => '驼奶费用',
];
/**
* @notes 订单统计(按订单类型或退款)
* @param array $params order_type(-1全部已支付类型合计,0退款,1挂号费,2问诊费,3药品费用,4首付,5尾款,6其他), days(0今天,7,30)
* @param array $params order_type(-1全部已支付类型合计,0退款,18 各费用类型含驼奶费用), days(0今天,7,30)
* @return array
*/
public static function orderStats(array $params = [], int $adminId = 0, ?array $adminInfo = null)
{
$allPaidOrderTypes = [1, 2, 3, 4, 5, 6];
$allPaidOrderTypes = [1, 2, 3, 4, 5, 6, 7, 8];
$orderType = array_key_exists('order_type', $params) ? (int) $params['order_type'] : 1;
if ($orderType !== -1 && $orderType !== 0 && !array_key_exists($orderType, self::$orderTypeNames)) {
$orderType = 1;