This commit is contained in:
Your Name
2026-07-06 16:34:42 +08:00
parent eb0efba739
commit 55e3b5d9e0
10 changed files with 772 additions and 111 deletions
@@ -328,6 +328,20 @@ class PrescriptionOrderController extends BaseAdminController
return $this->success('', $result);
}
/**
* 手工新增操作日志(可选同步调整处方/支付单审核状态)
*/
public function addLog()
{
$params = (new PrescriptionOrderValidate())->post()->goCheck('addLog');
$result = PrescriptionOrderLogic::addLog($params, $this->adminId, $this->adminInfo);
if ($result === false) {
return $this->fail(PrescriptionOrderLogic::getError());
}
return $this->success('日志已添加', $result);
}
/**
* 为「已发货」订单新增一条关联支付单,并重置支付单审核状态为待审核
*/
@@ -820,6 +820,11 @@ class PrescriptionOrderLists extends BaseAdminDataLists implements ListsSearchIn
'export_guahao_channel_source' => '自媒体渠道(挂号渠道来源)',
'export_medication_form' => '药品形态',
'export_prescription_name' => '药方名称',
'export_prescription_herbs' => '处方',
'export_main_usage' => '主方服用方式',
'export_main_usage_days' => '主方天数',
'export_aux_usage' => '辅方服用方式',
'export_aux_usage_days' => '辅方天数',
'export_service_package' => '服务套餐',
'export_medication_days' => '天数',
'export_amount' => '总金额',
@@ -909,6 +914,9 @@ class PrescriptionOrderLists extends BaseAdminDataLists implements ListsSearchIn
$wrapWideKeys = [
'export_linked_pay_records',
'export_prescription_name',
'export_prescription_herbs',
'export_main_usage',
'export_aux_usage',
'export_guahao_channel_source',
'export_assistant_dept',
'export_service_package',
@@ -919,6 +927,8 @@ class PrescriptionOrderLists extends BaseAdminDataLists implements ListsSearchIn
'export_patient_gender' => 6,
'export_patient_age' => 6,
'export_medication_days' => 6,
'export_main_usage_days' => 8,
'export_aux_usage_days' => 8,
'export_amount' => 10,
'export_paid_amount' => 10,
'export_refund_amount' => 10,
@@ -927,6 +937,9 @@ class PrescriptionOrderLists extends BaseAdminDataLists implements ListsSearchIn
'export_supply_mode' => 10,
'export_linked_pay_records' => 52,
'export_prescription_name' => 34,
'export_prescription_herbs' => 36,
'export_main_usage' => 28,
'export_aux_usage' => 28,
'export_guahao_channel_source' => 22,
'export_assistant_dept' => 24,
'export_service_package' => 18,
@@ -2375,6 +2375,123 @@ class PrescriptionOrderLogic
->toArray();
}
/**
* 手工新增操作日志;可选单独调整处方审核 / 支付单审核状态(不触发常规审核流程副作用)
*
* @param array<string,mixed> $params id, summary, prescription_audit_status?, payment_slip_audit_status?, prescription_audit_remark?, payment_slip_audit_remark?
* @return array<string,mixed>|false
*/
public static function addLog(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
$id = (int) ($params['id'] ?? 0);
$summary = mb_substr(trim((string) ($params['summary'] ?? '')), 0, 500);
if ($summary === '') {
self::$error = '请填写日志内容';
return false;
}
$order = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->find();
if (!$order) {
self::$error = '订单不存在';
return false;
}
if (!self::canAccessOrder($order, $adminId, $adminInfo)) {
self::$error = '无权限操作';
return false;
}
$changeParts = [];
$hasRxChange = array_key_exists('prescription_audit_status', $params)
&& $params['prescription_audit_status'] !== ''
&& $params['prescription_audit_status'] !== null;
$hasPayChange = array_key_exists('payment_slip_audit_status', $params)
&& $params['payment_slip_audit_status'] !== ''
&& $params['payment_slip_audit_status'] !== null;
if ($hasRxChange) {
if (!self::canAuditPrescriptionOrder($adminInfo)) {
self::$error = '无处方审核权限,不能调整处方审核状态';
return false;
}
$newRx = (int) $params['prescription_audit_status'];
if (!in_array($newRx, [0, 1, 2], true)) {
self::$error = '处方审核状态无效';
return false;
}
$oldRx = (int) $order->prescription_audit_status;
if ($newRx !== $oldRx) {
$order->prescription_audit_status = $newRx;
$changeParts[] = '处方审核:' . self::auditStatusLabelForLog($oldRx)
. ' → ' . self::auditStatusLabelForLog($newRx);
}
if (array_key_exists('prescription_audit_remark', $params)) {
$order->prescription_audit_remark = mb_substr(trim((string) $params['prescription_audit_remark']), 0, 500);
}
}
if ($hasPayChange) {
if (!self::canAuditPaymentSlipOrder($adminInfo)) {
self::$error = '无支付单审核权限,不能调整支付单审核状态';
return false;
}
$newPay = (int) $params['payment_slip_audit_status'];
if (!in_array($newPay, [0, 1, 2], true)) {
self::$error = '支付单审核状态无效';
return false;
}
$oldPay = (int) $order->payment_slip_audit_status;
if ($newPay !== $oldPay) {
$order->payment_slip_audit_status = $newPay;
$changeParts[] = '支付单审核:' . self::auditStatusLabelForLog($oldPay)
. ' → ' . self::auditStatusLabelForLog($newPay);
}
if (array_key_exists('payment_slip_audit_remark', $params)) {
$order->payment_slip_audit_remark = mb_substr(trim((string) $params['payment_slip_audit_remark']), 0, 500);
}
}
if ($hasRxChange || $hasPayChange) {
self::syncFulfillmentStatus($order);
try {
$order->save();
} catch (\Throwable $e) {
self::$error = $e->getMessage();
return false;
}
}
$logSummary = $summary;
if ($changeParts !== []) {
$logSummary .= '' . implode('', $changeParts) . '';
}
self::writeLog($id, $adminId, $adminInfo, 'manual_log', $logSummary);
$out = $order->toArray();
self::maskInternalCostIfNeeded($out, $adminInfo);
self::maskRemarkExtraIfNeeded($out, $adminInfo);
self::attachLinkedPayOrders($out);
return $out;
}
private static function auditStatusLabelForLog(int $status): string
{
return match ($status) {
1 => '已通过',
2 => '已驳回',
default => '待审核',
};
}
/**
* 为「已发货/已签收」(fulfillment_status=5/6) 的业务订单新增一条关联支付单(zyt_order),
* 创建后将支付单链接到业务订单,并将处方/支付审核状态重置为待审核以启动再次审核流程。
@@ -3107,6 +3224,210 @@ class PrescriptionOrderLogic
return $type;
}
/**
* 导出用:处方药材明细(主方/辅方分行,与处方笺一致)
*
* @param array<string, mixed> $rx
*/
public static function formatPrescriptionHerbsForExport(array $rx): string
{
[$mainHerbs, $auxHerbs] = self::splitPrescriptionHerbsFromRx($rx);
$formatList = static function (array $herbs): string {
$parts = [];
foreach ($herbs as $h) {
if (!\is_array($h)) {
continue;
}
$name = trim((string) ($h['name'] ?? ''));
if ($name === '') {
continue;
}
$parts[] = $name . ' ' . self::formatExportDosageNumber((float) ($h['dosage'] ?? 0)) . 'g';
}
return implode('、', $parts);
};
$sections = [];
$mainText = $formatList($mainHerbs);
if ($mainText !== '') {
$sections[] = '主方:' . $mainText;
}
$auxText = $formatList($auxHerbs);
if ($auxText !== '') {
$sections[] = '辅方:' . $auxText;
}
return implode("\n", $sections);
}
/**
* 导出用:主方/辅方服用方式(与前端 buildUsageSegmentText / 处方笺同口径)
*
* @param array<string, mixed> $usage
*/
public static function formatUsageSegmentForExport(
array $usage,
string $prescriptionType = '浓缩水丸',
string $fallbackWay = '',
string $fallbackTime = ''
): string {
$pt = trim($prescriptionType) !== '' ? trim($prescriptionType) : '浓缩水丸';
$times = (int) ($usage['times_per_day'] ?? 0);
if ($times <= 0) {
$times = 3;
}
$amount = isset($usage['dosage_amount']) && $usage['dosage_amount'] !== '' && $usage['dosage_amount'] !== null
? (float) $usage['dosage_amount']
: 10.0;
$unit = trim((string) ($usage['usage_dosage_unit'] ?? ($usage['dosage_unit'] ?? '')));
if ($unit === '') {
$unit = $pt === '饮片' ? 'ml' : 'g';
}
$usageWay = trim((string) ($usage['usage_way'] ?? ''));
if ($usageWay === '') {
$usageWay = $fallbackWay !== '' ? $fallbackWay : '温水送服';
}
$usageTime = trim((string) ($usage['usage_time'] ?? ''));
if ($usageTime === '') {
$usageTime = $fallbackTime;
}
$seg = ['每天' . $times . '次'];
if ($pt === '浓缩水丸') {
$bags = (int) ($usage['dosage_bag_count'] ?? 0);
if ($bags <= 0) {
$bags = 1;
}
$seg[] = '一次' . $bags . '袋';
$seg[] = '每袋' . self::formatExportDosageNumber($amount) . $unit;
} else {
$seg[] = '一次' . self::formatExportDosageNumber($amount) . $unit;
}
$seg[] = $usageWay;
if ($usageTime !== '') {
$seg[] = $usageTime;
}
return implode(', ', $seg);
}
/**
* 导出用:辅方用法 JSON 规范化(与前端 normalizeSlipAuxUsageForm 默认值一致)
*
* @return array<string, mixed>
*/
private static function normalizeAuxUsageForExport($raw, string $prescriptionType): array
{
$pt = trim($prescriptionType) !== '' ? trim($prescriptionType) : '浓缩水丸';
if ($pt === '饮片') {
$base = [
'dosage_amount' => 50.0,
'dosage_bag_count' => 1,
'times_per_day' => 3,
'usage_days' => 7,
];
} elseif ($pt === '浓缩水丸') {
$base = [
'dosage_amount' => 5.0,
'dosage_bag_count' => 1,
'times_per_day' => 3,
'usage_days' => 7,
];
} else {
$base = [
'dosage_amount' => 1.0,
'dosage_bag_count' => 1,
'times_per_day' => 3,
'usage_days' => 7,
];
}
if (\is_string($raw) && $raw !== '') {
$decoded = json_decode($raw, true);
$raw = \is_array($decoded) ? $decoded : null;
}
if (!\is_array($raw)) {
return $base;
}
return [
'dosage_amount' => isset($raw['dosage_amount']) && $raw['dosage_amount'] !== '' && $raw['dosage_amount'] !== null
? (float) $raw['dosage_amount']
: $base['dosage_amount'],
'dosage_bag_count' => (int) ($raw['dosage_bag_count'] ?? 0) > 0
? (int) $raw['dosage_bag_count']
: $base['dosage_bag_count'],
'times_per_day' => (int) ($raw['times_per_day'] ?? 0) > 0
? (int) $raw['times_per_day']
: $base['times_per_day'],
'usage_days' => (int) ($raw['usage_days'] ?? 0) > 0
? (int) $raw['usage_days']
: $base['usage_days'],
];
}
/**
* @return array{0: array<int, array<string, mixed>>, 1: array<int, array<string, mixed>>}
*/
private static function splitPrescriptionHerbsFromRx(array $rx): array
{
$herbs = $rx['herbs'] ?? null;
if (\is_string($herbs) && $herbs !== '') {
$decoded = json_decode($herbs, true);
$herbs = \is_array($decoded) ? $decoded : [];
}
if (!\is_array($herbs)) {
$herbs = [];
}
$mainHerbs = [];
$auxHerbs = [];
foreach ($herbs as $h) {
if (!\is_array($h)) {
continue;
}
if (((string) ($h['formula_type'] ?? '')) === '辅方') {
$auxHerbs[] = $h;
} else {
$mainHerbs[] = $h;
}
}
return [$mainHerbs, $auxHerbs];
}
private static function formatExportDosageNumber(float $dosage): string
{
if (floor($dosage) === $dosage) {
return (string) (int) $dosage;
}
return rtrim(rtrim(number_format($dosage, 4, '.', ''), '0'), '.');
}
/**
* 导出用:主方/辅方服用天数(优先业务订单 medication_days,缺省回退处方 usage_days / 辅方 aux_usage
*
* @param array<string, mixed> $rx
* @param array<string, mixed>|null $auxUsage
*/
private static function resolveExportUsageDays(array $rx, ?array $auxUsage, $orderMedicationDays, bool $isAux): string
{
$medDays = $orderMedicationDays;
if ($medDays !== null && $medDays !== '' && (int) $medDays > 0) {
return (string) (int) $medDays;
}
if ($isAux) {
$days = (int) ($auxUsage['usage_days'] ?? 0);
return $days > 0 ? (string) $days : '';
}
$days = (int) ($rx['usage_days'] ?? 0);
return $days > 0 ? (string) $days : '';
}
/**
* 导出列:挂号表渠道来源展示(与 AppointmentLists channel_source_desc 同字典口径)
*
@@ -3403,7 +3724,12 @@ class PrescriptionOrderLogic
$rxById = [];
if ($rxIdList !== []) {
$rxRows = Prescription::whereIn('id', $rxIdList)->whereNull('delete_time')
->field(['id', 'prescription_type', 'need_decoction', 'dose_unit', 'assistant_id', 'appointment_id', 'prescription_name', 'aux_usage', 'herbs', 'creator_id'])
->field([
'id', 'prescription_type', 'need_decoction', 'dose_unit', 'assistant_id', 'appointment_id',
'prescription_name', 'aux_usage', 'herbs', 'creator_id',
'dosage_amount', 'dosage_unit', 'dosage_bag_count', 'times_per_day', 'usage_days',
'usage_way', 'usage_time',
])
->select()
->toArray();
foreach ($rxRows as $xr) {
@@ -3637,6 +3963,39 @@ class PrescriptionOrderLogic
}
$item['export_prescription_name'] = implode(' ', $rxNameParts);
$rxArr = \is_array($rx) ? $rx : [];
$rxType = trim((string) ($rxArr['prescription_type'] ?? '')) ?: '浓缩水丸';
$item['export_prescription_herbs'] = self::formatPrescriptionHerbsForExport($rxArr);
$item['export_main_usage'] = $rxArr !== []
? self::formatUsageSegmentForExport($rxArr, $rxType)
: '';
[, $auxHerbs] = self::splitPrescriptionHerbsFromRx($rxArr);
$auxUsageNorm = $auxHerbs !== []
? self::normalizeAuxUsageForExport($rxArr['aux_usage'] ?? null, $rxType)
: null;
if ($auxHerbs !== [] && $auxUsageNorm !== null) {
$item['export_aux_usage'] = self::formatUsageSegmentForExport(
[
'dosage_amount' => $auxUsageNorm['dosage_amount'],
'dosage_bag_count' => $auxUsageNorm['dosage_bag_count'],
'times_per_day' => $auxUsageNorm['times_per_day'],
'usage_dosage_unit' => $rxArr['dosage_unit'] ?? '',
'usage_way' => $rxArr['usage_way'] ?? '',
'usage_time' => $rxArr['usage_time'] ?? '',
],
$rxType,
(string) ($rxArr['usage_way'] ?? ''),
(string) ($rxArr['usage_time'] ?? '')
);
} else {
$item['export_aux_usage'] = '';
}
$orderMedDays = $item['medication_days'] ?? null;
$item['export_main_usage_days'] = self::resolveExportUsageDays($rxArr, $auxUsageNorm, $orderMedDays, false);
$item['export_aux_usage_days'] = $auxHerbs !== []
? self::resolveExportUsageDays($rxArr, $auxUsageNorm, $orderMedDays, true)
: '';
$item['export_service_package'] = self::formatServicePackageForExport(
$item['service_package'] ?? '',
$packageNameByValue
@@ -3807,27 +4166,7 @@ class PrescriptionOrderLogic
$doctorId = (int) ($rx['creator_id'] ?? 0);
$herbs = $rx['herbs'] ?? null;
if (\is_string($herbs) && $herbs !== '') {
$decoded = json_decode($herbs, true);
$herbs = \is_array($decoded) ? $decoded : [];
}
if (!\is_array($herbs)) {
$herbs = [];
}
$mainHerbs = [];
$auxHerbs = [];
foreach ($herbs as $h) {
if (!\is_array($h)) {
continue;
}
if (((string) ($h['formula_type'] ?? '')) === '辅方') {
$auxHerbs[] = $h;
} else {
$mainHerbs[] = $h;
}
}
[$mainHerbs, $auxHerbs] = self::splitPrescriptionHerbsFromRx($rx);
$lookup = static function (string $ft, array $hs) use ($doctorId, $libByDoctor, $libPublic): string {
if ($hs === []) {
@@ -32,6 +32,11 @@ class PrescriptionOrderValidate extends BaseValidate
'remark_assistant' => 'max:500',
'action' => 'require|in:approve,reject',
'remark' => 'max:500',
'summary' => 'require|max:500',
'prescription_audit_status' => 'in:0,1,2',
'payment_slip_audit_status' => 'in:0,1,2',
'prescription_audit_remark' => 'max:500',
'payment_slip_audit_remark' => 'max:500',
'fulfillment_status' => 'require|integer|in:3,7,8,9,11,12',
'reason' => 'require|max:500',
'refund_amount' => 'float|egt:0',
@@ -74,6 +79,7 @@ class PrescriptionOrderValidate extends BaseValidate
'withdraw' => ['id'],
'ship' => ['id', 'tracking_number', 'express_company', 'ship_mode'],
'logs' => ['id'],
'addLog' => ['id', 'summary'],
'paidPayOrders' => ['diagnosis_id'],
'addPayOrder' => ['id', 'order_type', 'pay_amount', 'pay_remark'],
'linkPayOrder' => ['id', 'pay_order_id'],
@@ -0,0 +1,13 @@
-- 处方业务订单:手工新增操作日志(可选调整处方/支付单审核状态)
SET @po_menu_id := (SELECT id FROM zyt_system_menu WHERE perms = 'tcm.prescriptionOrder/lists' LIMIT 1);
INSERT INTO zyt_system_menu (
pid, type, name, icon, sort, perms, paths, component,
selected, params, is_cache, is_show, is_disable, create_time, update_time
)
SELECT
@po_menu_id, 'A', '新增操作日志', '', 10, 'tcm.prescriptionOrder/addLog', '', '',
'', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
FROM DUAL
WHERE @po_menu_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM zyt_system_menu WHERE perms = 'tcm.prescriptionOrder/addLog');