add 业务订单额

This commit is contained in:
2026-04-24 12:39:13 +08:00
parent f7e33e35fa
commit d408ffabe8
2 changed files with 48 additions and 8 deletions
@@ -354,6 +354,7 @@ const summaryCards: MetricCard[] = [
{ key: 'interview_rate', label: '面诊率', type: 'percent' }, { key: 'interview_rate', label: '面诊率', type: 'percent' },
{ key: 'receive_rate', label: '接诊率', type: 'percent' }, { key: 'receive_rate', label: '接诊率', type: 'percent' },
{ key: 'interview_receive_rate', label: '面诊接诊率', type: 'percent' }, { key: 'interview_receive_rate', label: '面诊接诊率', type: 'percent' },
{ key: 'business_order_amount', label: '业务订单额', type: 'money' },
{ key: 'completed_order_amount', label: '诊单金额', type: 'money' }, { key: 'completed_order_amount', label: '诊单金额', type: 'money' },
{ key: 'avg_unit_price', label: '平均单价', type: 'money' }, { key: 'avg_unit_price', label: '平均单价', type: 'money' },
{ key: 'account_cost', label: '账户消耗', type: 'money' }, { key: 'account_cost', label: '账户消耗', type: 'money' },
@@ -306,6 +306,7 @@ class ConversionLogic
'free_appointment_count' => 0, 'free_appointment_count' => 0,
'appointment_total_count' => 0, 'appointment_total_count' => 0,
'interview_count' => 0, 'interview_count' => 0,
'business_order_amount' => 0.0,
'completed_order_amount' => 0.0, 'completed_order_amount' => 0.0,
'completed_order_count' => 0, 'completed_order_count' => 0,
'account_cost' => 0.0, 'account_cost' => 0.0,
@@ -493,25 +494,25 @@ class ConversionLogic
int $endTimestamp, int $endTimestamp,
?array $mediaChannel ?array $mediaChannel
): void { ): void {
$sourceExpr = $dimension === 'doctor' ? 'rx.creator_id' : 'rx.assistant_id'; $completedSourceExpr = $dimension === 'doctor' ? 'rx.creator_id' : 'rx.assistant_id';
$query = Db::name('tcm_prescription_order') $completedQuery = Db::name('tcm_prescription_order')
->alias('po') ->alias('po')
->leftJoin('tcm_prescription rx', 'rx.id = po.prescription_id') ->leftJoin('tcm_prescription rx', 'rx.id = po.prescription_id')
->leftJoin('tcm_diagnosis dg', 'dg.id = po.diagnosis_id') ->leftJoin('tcm_diagnosis dg', 'dg.id = po.diagnosis_id')
->whereNull('po.delete_time') ->whereNull('po.delete_time')
->where('po.payment_slip_audit_status', 1) ->where('po.payment_slip_audit_status', 1)
->where('po.create_time', 'between', [$startTimestamp, $endTimestamp]) ->where('po.create_time', 'between', [$startTimestamp, $endTimestamp])
->fieldRaw("{$sourceExpr} AS source_admin_id, SUM(CASE WHEN po.prescription_audit_status = 1 THEN 1 ELSE 0 END) AS order_count, SUM(CASE WHEN po.prescription_audit_status = 1 THEN po.amount ELSE 0 END) AS total_amount") ->fieldRaw("{$completedSourceExpr} AS source_admin_id, SUM(CASE WHEN po.prescription_audit_status = 1 THEN 1 ELSE 0 END) AS order_count, SUM(CASE WHEN po.prescription_audit_status = 1 THEN po.amount ELSE 0 END) AS total_amount")
->group($sourceExpr); ->group($completedSourceExpr);
if ($mediaChannel !== null) { if ($mediaChannel !== null) {
$query->leftJoin('qywx_external_contact q', 'q.external_userid = dg.external_userid'); $completedQuery->leftJoin('qywx_external_contact q', 'q.external_userid = dg.external_userid');
MediaChannelService::applyFollowUsersChannelFilter($query, 'q.follow_users', $mediaChannel); MediaChannelService::applyFollowUsersChannelFilter($completedQuery, 'q.follow_users', $mediaChannel);
} }
$rows = $query->select()->toArray(); $completedRows = $completedQuery->select()->toArray();
foreach ($rows as $row) { foreach ($completedRows as $row) {
$sourceAdminId = (int)($row['source_admin_id'] ?? 0); $sourceAdminId = (int)($row['source_admin_id'] ?? 0);
$mappedEntityIds = self::mapEntityIds($dimension, $sourceAdminId, $entityIds, $adminToDeptIds); $mappedEntityIds = self::mapEntityIds($dimension, $sourceAdminId, $entityIds, $adminToDeptIds);
@@ -527,6 +528,36 @@ class ConversionLogic
$entities[$entityId]['completed_order_amount'] = round($entities[$entityId]['completed_order_amount'] + $amount, 2); $entities[$entityId]['completed_order_amount'] = round($entities[$entityId]['completed_order_amount'] + $amount, 2);
} }
} }
$businessSourceExpr = $dimension === 'doctor' ? 'po.creator_id' : 'dg.assistant_id';
$businessQuery = Db::name('tcm_prescription_order')
->alias('po')
->leftJoin('tcm_diagnosis dg', 'dg.id = po.diagnosis_id')
->whereNull('po.delete_time')
->where('po.create_time', 'between', [$startTimestamp, $endTimestamp])
->where('po.fulfillment_status', '<>', 4)
->fieldRaw("{$businessSourceExpr} AS source_admin_id, SUM(po.amount) AS total_amount")
->group($businessSourceExpr);
if ($mediaChannel !== null) {
$businessQuery->leftJoin('qywx_external_contact q2', 'q2.external_userid = dg.external_userid');
MediaChannelService::applyFollowUsersChannelFilter($businessQuery, 'q2.follow_users', $mediaChannel);
}
$businessRows = $businessQuery->select()->toArray();
foreach ($businessRows as $row) {
$sourceAdminId = (int)($row['source_admin_id'] ?? 0);
$mappedEntityIds = self::mapEntityIds($dimension, $sourceAdminId, $entityIds, $adminToDeptIds);
if ($mappedEntityIds === []) {
continue;
}
$amount = round((float)($row['total_amount'] ?? 0), 2);
foreach ($mappedEntityIds as $entityId) {
$entities[$entityId]['business_order_amount'] = round($entities[$entityId]['business_order_amount'] + $amount, 2);
}
}
} }
/** /**
@@ -597,6 +628,7 @@ class ConversionLogic
$addFansCount = (int)$entity['add_fans_count']; $addFansCount = (int)$entity['add_fans_count'];
$totalOpenCount = (int)$entity['total_open_count']; $totalOpenCount = (int)$entity['total_open_count'];
$completedOrderCount = (int)$entity['completed_order_count']; $completedOrderCount = (int)$entity['completed_order_count'];
$businessOrderAmount = round((float)$entity['business_order_amount'], 2);
$completedOrderAmount = round((float)$entity['completed_order_amount'], 2); $completedOrderAmount = round((float)$entity['completed_order_amount'], 2);
$accountCost = round((float)$entity['account_cost'], 2); $accountCost = round((float)$entity['account_cost'], 2);
$globalAccountCost = max($globalAccountCost, round((float)($entity['_global_account_cost'] ?? 0), 2)); $globalAccountCost = max($globalAccountCost, round((float)($entity['_global_account_cost'] ?? 0), 2));
@@ -605,6 +637,7 @@ class ConversionLogic
$entity['paid_appointment_count'] = $paidAppointmentCount; $entity['paid_appointment_count'] = $paidAppointmentCount;
$entity['free_appointment_count'] = $freeAppointmentCount; $entity['free_appointment_count'] = $freeAppointmentCount;
$entity['appointment_total_count'] = $appointmentTotalCount; $entity['appointment_total_count'] = $appointmentTotalCount;
$entity['business_order_amount'] = $businessOrderAmount;
$entity['completed_order_amount'] = $completedOrderAmount; $entity['completed_order_amount'] = $completedOrderAmount;
$entity['account_cost'] = $effectiveAccountCost; $entity['account_cost'] = $effectiveAccountCost;
$entity['paid_appointment_rate'] = self::percent($paidAppointmentCount, $addFansCount); $entity['paid_appointment_rate'] = self::percent($paidAppointmentCount, $addFansCount);
@@ -686,6 +719,7 @@ class ConversionLogic
$node['appointment_total_count'] += (int)$childNode['appointment_total_count']; $node['appointment_total_count'] += (int)$childNode['appointment_total_count'];
$node['interview_count'] += (int)$childNode['interview_count']; $node['interview_count'] += (int)$childNode['interview_count'];
$node['completed_order_count'] += (int)$childNode['completed_order_count']; $node['completed_order_count'] += (int)$childNode['completed_order_count'];
$node['business_order_amount'] = round((float)$node['business_order_amount'] + (float)$childNode['business_order_amount'], 2);
$node['completed_order_amount'] = round((float)$node['completed_order_amount'] + (float)$childNode['completed_order_amount'], 2); $node['completed_order_amount'] = round((float)$node['completed_order_amount'] + (float)$childNode['completed_order_amount'], 2);
$node['account_cost'] = round((float)$node['account_cost'] + (float)$childNode['account_cost'], 2); $node['account_cost'] = round((float)$node['account_cost'] + (float)$childNode['account_cost'], 2);
} }
@@ -801,6 +835,7 @@ class ConversionLogic
$addFansCount = (int)$node['add_fans_count']; $addFansCount = (int)$node['add_fans_count'];
$totalOpenCount = (int)$node['total_open_count']; $totalOpenCount = (int)$node['total_open_count'];
$completedOrderCount = (int)$node['completed_order_count']; $completedOrderCount = (int)$node['completed_order_count'];
$businessOrderAmount = round((float)$node['business_order_amount'], 2);
$completedOrderAmount = round((float)$node['completed_order_amount'], 2); $completedOrderAmount = round((float)$node['completed_order_amount'], 2);
$accountCost = round((float)$node['account_cost'], 2); $accountCost = round((float)$node['account_cost'], 2);
$globalAccountCost = round((float)($node['_global_account_cost'] ?? 0), 2); $globalAccountCost = round((float)($node['_global_account_cost'] ?? 0), 2);
@@ -809,6 +844,7 @@ class ConversionLogic
$node['paid_appointment_count'] = $paidAppointmentCount; $node['paid_appointment_count'] = $paidAppointmentCount;
$node['free_appointment_count'] = $freeAppointmentCount; $node['free_appointment_count'] = $freeAppointmentCount;
$node['appointment_total_count'] = $appointmentTotalCount; $node['appointment_total_count'] = $appointmentTotalCount;
$node['business_order_amount'] = $businessOrderAmount;
$node['completed_order_amount'] = $completedOrderAmount; $node['completed_order_amount'] = $completedOrderAmount;
$node['account_cost'] = $effectiveAccountCost; $node['account_cost'] = $effectiveAccountCost;
$node['paid_appointment_rate'] = self::percent($paidAppointmentCount, $addFansCount); $node['paid_appointment_rate'] = self::percent($paidAppointmentCount, $addFansCount);
@@ -842,6 +878,7 @@ class ConversionLogic
'free_appointment_count' => 0, 'free_appointment_count' => 0,
'appointment_total_count' => 0, 'appointment_total_count' => 0,
'interview_count' => 0, 'interview_count' => 0,
'business_order_amount' => 0.0,
'completed_order_amount' => 0.0, 'completed_order_amount' => 0.0,
'completed_order_count' => 0, 'completed_order_count' => 0,
'account_cost' => 0.0, 'account_cost' => 0.0,
@@ -857,6 +894,7 @@ class ConversionLogic
$summary['appointment_total_count'] += (int)$row['appointment_total_count']; $summary['appointment_total_count'] += (int)$row['appointment_total_count'];
$summary['interview_count'] += (int)$row['interview_count']; $summary['interview_count'] += (int)$row['interview_count'];
$summary['completed_order_count'] += (int)$row['completed_order_count']; $summary['completed_order_count'] += (int)$row['completed_order_count'];
$summary['business_order_amount'] = round($summary['business_order_amount'] + (float)($row['business_order_amount'] ?? 0), 2);
$summary['completed_order_amount'] = round($summary['completed_order_amount'] + (float)$row['completed_order_amount'], 2); $summary['completed_order_amount'] = round($summary['completed_order_amount'] + (float)$row['completed_order_amount'], 2);
$globalAccountCost = max($globalAccountCost, round((float)($row['_global_account_cost'] ?? 0), 2)); $globalAccountCost = max($globalAccountCost, round((float)($row['_global_account_cost'] ?? 0), 2));
} }
@@ -887,6 +925,7 @@ class ConversionLogic
|| (int)($row['appointment_total_count'] ?? 0) > 0 || (int)($row['appointment_total_count'] ?? 0) > 0
|| (int)($row['interview_count'] ?? 0) > 0 || (int)($row['interview_count'] ?? 0) > 0
|| (int)($row['completed_order_count'] ?? 0) > 0 || (int)($row['completed_order_count'] ?? 0) > 0
|| (float)($row['business_order_amount'] ?? 0) > 0
|| (float)($row['completed_order_amount'] ?? 0) > 0; || (float)($row['completed_order_amount'] ?? 0) > 0;
} }