更新
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace app\adminapi\logic\doctor;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\enum\AppointmentTypeEnum;
|
||||
use app\common\model\doctor\Appointment;
|
||||
use app\common\model\doctor\Roster;
|
||||
use app\common\service\doctor\RosterSegmentService;
|
||||
@@ -46,7 +47,7 @@ class AppointmentLogic extends BaseLogic
|
||||
/**
|
||||
* @param array<int|string, mixed> $cols Db::name('doctor_appointment')->getTableFields()
|
||||
*/
|
||||
private static function assertAppointmentChannelWritable(array $cols, string $chSrc): ?string
|
||||
private static function assertAppointmentChannelWritable(array $cols, string $chSrc): ?string
|
||||
{
|
||||
$hasChannelSource = in_array('channel_source', $cols, true);
|
||||
$hasChannels = in_array('channels', $cols, true);
|
||||
@@ -86,9 +87,12 @@ class AppointmentLogic extends BaseLogic
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function filterAppointmentRowByExistingColumns(array $row, array $cols): array
|
||||
{
|
||||
$out = [];
|
||||
private static function filterAppointmentRowByExistingColumns(array $row, array $cols): array
|
||||
{
|
||||
if (array_key_exists('appointment_type', $row) && !in_array('appointment_type', $cols, true)) {
|
||||
throw new \RuntimeException('挂号表缺少 appointment_type 字段,无法保存问诊类型');
|
||||
}
|
||||
$out = [];
|
||||
foreach ($row as $k => $v) {
|
||||
if (in_array((string) $k, $cols, true)) {
|
||||
$out[$k] = $v;
|
||||
@@ -273,9 +277,15 @@ class AppointmentLogic extends BaseLogic
|
||||
* @param array $params
|
||||
* @return array|bool
|
||||
*/
|
||||
public static function create(array $params, int $operatorAdminId = 0, array $operatorAdminInfo = [])
|
||||
{
|
||||
try {
|
||||
public static function create(array $params, int $operatorAdminId = 0, array $operatorAdminInfo = [])
|
||||
{
|
||||
$params = AppointmentTypeEnum::withDefault($params);
|
||||
if (!AppointmentTypeEnum::isWritable($params['appointment_type'])) {
|
||||
self::setError('问诊类型仅支持图文问诊或视频问诊');
|
||||
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
// 同一诊单患者在「所选预约日」仅允许一条「已预约」或「已过号」记录(与 appointment_date 一致,不能误用服务器当天拦其它日期)
|
||||
@@ -345,7 +355,7 @@ class AppointmentLogic extends BaseLogic
|
||||
'doctor_id' => (int) $params['doctor_id'],
|
||||
'appointment_date' => $params['appointment_date'],
|
||||
'appointment_time' => $appointmentTime,
|
||||
'appointment_type' => $params['appointment_type'] ?? 'video',
|
||||
'appointment_type' => $params['appointment_type'],
|
||||
'remark' => $params['remark'] ?? '',
|
||||
'status' => 1,
|
||||
'create_time' => time(),
|
||||
@@ -491,12 +501,8 @@ class AppointmentLogic extends BaseLogic
|
||||
];
|
||||
$appointment['status_desc'] = $statusMap[$appointment['status']] ?? '未知';
|
||||
|
||||
$typeMap = [
|
||||
'video' => '视频问诊',
|
||||
'text' => '图文问诊',
|
||||
'phone' => '电话问诊',
|
||||
];
|
||||
$appointment['appointment_type_desc'] = $typeMap[$appointment['appointment_type']] ?? '未知';
|
||||
$appointment['appointment_type'] = AppointmentTypeEnum::normalizeStored($appointment['appointment_type'] ?? null);
|
||||
$appointment['appointment_type_desc'] = AppointmentTypeEnum::description($appointment['appointment_type']);
|
||||
|
||||
// 格式化时间戳为日期时间
|
||||
if (isset($appointment['create_time']) && is_numeric($appointment['create_time'])) {
|
||||
@@ -969,9 +975,14 @@ class AppointmentLogic extends BaseLogic
|
||||
*
|
||||
* @param array<string, mixed> $params
|
||||
*/
|
||||
public static function adminEdit(array $params, int $adminId, array $adminInfo): bool
|
||||
{
|
||||
try {
|
||||
public static function adminEdit(array $params, int $adminId, array $adminInfo): bool
|
||||
{
|
||||
if (!AppointmentTypeEnum::isWritable($params['appointment_type'] ?? null)) {
|
||||
self::setError('问诊类型仅支持图文问诊或视频问诊');
|
||||
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$id = (int) ($params['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
self::setError('参数错误');
|
||||
@@ -1023,12 +1034,7 @@ class AppointmentLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
$appointmentType = trim((string) ($params['appointment_type'] ?? ''));
|
||||
if (!in_array($appointmentType, ['video', 'text', 'phone'], true)) {
|
||||
self::setError('预约类型无效');
|
||||
|
||||
return false;
|
||||
}
|
||||
$appointmentType = $params['appointment_type'];
|
||||
|
||||
$remark = isset($params['remark']) ? trim((string) $params['remark']) : '';
|
||||
if (mb_strlen($remark) > 500) {
|
||||
|
||||
@@ -330,7 +330,8 @@ class WecomPromotionLogic
|
||||
$createdRemote = true;
|
||||
try {
|
||||
$remote = self::normaliseRemoteLink($api->getLink($remoteLinkId), $remoteLinkId);
|
||||
if (!QywxPromotionMemberRange::same($remote['range_userids'], $eligibleUserIds)) {
|
||||
if (!QywxPromotionMemberRange::same($remote['range_userids'], $eligibleUserIds)
|
||||
|| $remote['range_department_ids'] !== []) {
|
||||
throw new RuntimeException('企业微信返回的多人路由成员范围与方案可用医助不一致');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
@@ -488,9 +489,7 @@ class WecomPromotionLogic
|
||||
'range_userids' => $eligibleUserIds,
|
||||
// 前端据此确认标签、欢迎语等扩展配置已和方案一并提交并完成回读校验。
|
||||
'automation_saved' => $automation !== null,
|
||||
'sync_error' => $syncError,
|
||||
'sync_queued' => $needsQueuedSync && !$syncImmediately,
|
||||
];
|
||||
] + self::memberSyncResult($id, $syncError);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -679,13 +678,13 @@ class WecomPromotionLogic
|
||||
$memberMatched += $poolMemberResult['matched'];
|
||||
$memberUpdated += $poolMemberResult['updated'];
|
||||
}
|
||||
$syncError = trim((string) ($saved['sync_error'] ?? ''));
|
||||
$syncResult = self::memberSyncResult($poolId, (string) ($saved['sync_error'] ?? ''));
|
||||
$syncError = $syncResult['sync_error'];
|
||||
$updated++;
|
||||
if ($syncError !== '') {
|
||||
$syncErrorCount++;
|
||||
}
|
||||
$syncQueued = !empty($saved['sync_queued'])
|
||||
|| !empty($poolMemberResult['dispatch']['queued']);
|
||||
$syncQueued = $syncResult['sync_queued'];
|
||||
if ($syncQueued) {
|
||||
$syncQueuedCount++;
|
||||
}
|
||||
@@ -693,6 +692,7 @@ class WecomPromotionLogic
|
||||
'id' => $poolId,
|
||||
'name' => (string) ($pool['name'] ?? ''),
|
||||
'success' => true,
|
||||
'sync_status' => $syncResult['sync_status'],
|
||||
'sync_error' => $syncError,
|
||||
'sync_queued' => $syncQueued,
|
||||
'member_matched' => $poolMemberResult['matched'],
|
||||
@@ -754,7 +754,8 @@ class WecomPromotionLogic
|
||||
]);
|
||||
}
|
||||
|
||||
$dispatch = $updateIds !== []
|
||||
// 状态相同也需要重算:上次保存可能只入队,远端仍保留已下线成员。
|
||||
$dispatch = $matchedIds !== []
|
||||
? QywxPromotionMemberSchedulerService::reconcilePool($poolId)
|
||||
: null;
|
||||
// 下线必须能够安全收缩远端范围;上线即使尚未到生效时段,也应先保存规则。
|
||||
@@ -1235,7 +1236,94 @@ class WecomPromotionLogic
|
||||
}
|
||||
}
|
||||
|
||||
return ['id' => $id, 'pool_id' => $poolId, 'dispatch' => $planned, 'sync_error' => $syncError];
|
||||
return ['id' => $id, 'pool_id' => $poolId, 'dispatch' => $planned]
|
||||
+ self::memberSyncResult($poolId, $syncError);
|
||||
}
|
||||
|
||||
/** 显式重新推送当前范围;一次请求仅处理一个方案,供批量前端逐方案调用。 */
|
||||
public static function syncMemberRange(
|
||||
int $poolId,
|
||||
int $adminId,
|
||||
array $adminInfo,
|
||||
?QywxPromotionRangeSyncService $syncService = null
|
||||
): array {
|
||||
self::assertMemberDispatchSchema();
|
||||
if (!QywxPromotionOperatorAccess::hasPagePermission($adminId, $adminInfo)) {
|
||||
throw new RuntimeException('权限不足');
|
||||
}
|
||||
self::assertScopedRow('qywx_promotion_pool', $poolId, $adminId, $adminInfo);
|
||||
$ready = Db::transaction(static function () use ($poolId): bool {
|
||||
// 与成员保存、方案删除使用相同锁顺序,不重启删除中的同步任务。
|
||||
$pool = Db::name('qywx_promotion_pool')->where('id', $poolId)->whereNull('delete_time')->lock(true)->find();
|
||||
if (!$pool) {
|
||||
throw new RuntimeException('分流方案不存在或已删除');
|
||||
}
|
||||
$sync = Db::name('qywx_promotion_range_sync')->where('pool_id', $poolId)->lock(true)->find();
|
||||
if ((int) ($sync['status'] ?? 0) === 5
|
||||
|| ((int) ($sync['status'] ?? 0) === 4
|
||||
&& str_starts_with((string) ($sync['last_error'] ?? ''), '企业微信官方获客链接删除失败'))) {
|
||||
return false;
|
||||
}
|
||||
$plan = QywxPromotionMemberSchedulerService::reconcilePool($poolId);
|
||||
if ($plan['blocked']) {
|
||||
return false;
|
||||
}
|
||||
$linkId = (int) (Db::name('qywx_promotion_range_sync')->where('pool_id', $poolId)->value('promotion_link_id') ?? 0);
|
||||
// 即便快照看起来一致也重新 update/get,修复未被本地快照发现的企微端变化。
|
||||
QywxPromotionMemberSchedulerService::requestPoolSync($poolId, $linkId);
|
||||
|
||||
return true;
|
||||
});
|
||||
$error = '';
|
||||
if ($ready) {
|
||||
try {
|
||||
($syncService ?? new QywxPromotionRangeSyncService())->syncPool($poolId);
|
||||
} catch (\Throwable $e) {
|
||||
$error = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return self::memberSyncResult($poolId, $error);
|
||||
}
|
||||
|
||||
/** 返回已确认的远端范围及任务状态,不能把无异常的 noop 当成同步成功。 */
|
||||
private static function memberSyncResult(int $poolId, string $error = ''): array
|
||||
{
|
||||
$sync = Db::name('qywx_promotion_range_sync')->where('pool_id', $poolId)->find() ?: [];
|
||||
$link = Db::name('qywx_promotion_link')->where('id', (int) ($sync['promotion_link_id'] ?? 0))
|
||||
->whereNull('delete_time')->find() ?: [];
|
||||
$members = Db::name('qywx_promotion_pool_member')->where('pool_id', $poolId)
|
||||
->whereNull('delete_time')->select()->toArray();
|
||||
$range = QywxPromotionMemberRange::evaluate($members, date('Y-m-d'), time(), QywxPromotionConfig::forPool($poolId));
|
||||
$remoteUsers = self::decodeStringList($link['range_user_json'] ?? null);
|
||||
$remoteDepartments = self::decodeStringList($link['range_department_json'] ?? null);
|
||||
$status = (int) ($sync['status'] ?? -1);
|
||||
$syncError = trim($error ?: (string) ($sync['last_error'] ?? $link['sync_error'] ?? ''));
|
||||
if ($link === [] || trim((string) ($link['remote_link_id'] ?? '')) === ''
|
||||
|| (int) ($link['remote_status'] ?? 0) === 2 || $range['userids'] === [] || in_array($status, [4, 5], true)) {
|
||||
$resultStatus = 'blocked';
|
||||
$syncError = $syncError ?: '当前没有可同步的官方链接或可用成员,请检查方案和成员规则';
|
||||
} elseif ($error !== '' || $status === 3) {
|
||||
$resultStatus = 'failed';
|
||||
$syncError = $syncError ?: '企业微信成员范围同步失败,请重试';
|
||||
} elseif ($status === 0 && (int) ($sync['desired_version'] ?? 0) > 0
|
||||
&& (int) ($sync['desired_version'] ?? 0) === (int) ($sync['applied_version'] ?? -1)
|
||||
&& (int) ($link['last_sync_time'] ?? 0) > 0 && $remoteDepartments === []
|
||||
&& QywxPromotionMemberRange::same($range['userids'], $remoteUsers)) {
|
||||
$resultStatus = 'synced';
|
||||
$syncError = '';
|
||||
} else {
|
||||
$resultStatus = 'pending';
|
||||
}
|
||||
|
||||
return [
|
||||
'pool_id' => $poolId,
|
||||
'sync_status' => $resultStatus,
|
||||
'sync_error' => $syncError,
|
||||
'sync_queued' => in_array($resultStatus, ['pending', 'failed'], true),
|
||||
'range_userids' => $remoteUsers,
|
||||
'range_department_ids' => $remoteDepartments,
|
||||
];
|
||||
}
|
||||
|
||||
public static function toggleMember(int $id, int $status, int $adminId, array $adminInfo): array
|
||||
|
||||
@@ -748,16 +748,16 @@ class PrescriptionLogic
|
||||
$bizPo = PrescriptionOrder::where('prescription_id', $id)
|
||||
->where('prescription_audit_status', 2)
|
||||
->whereNull('delete_time')
|
||||
->where('fulfillment_status', '<>', 4)
|
||||
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
$arr['business_prescription_audit_rejected'] = $bizPo ? 1 : 0;
|
||||
$arr['business_prescription_audit_remark'] = $bizPo ? (string) ($bizPo->prescription_audit_remark ?? '') : '';
|
||||
|
||||
// 与 PrescriptionLists「业务订单」角标一致:未删除且非已取消(4) 即视为存在有效业务订单
|
||||
// 与列表和创建校验一致:已取消 / 已退款的订单保留历史关联,但不再占用处方。
|
||||
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
|
||||
->whereNull('delete_time')
|
||||
->where('fulfillment_status', '<>', 4)
|
||||
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
|
||||
->count() > 0;
|
||||
$arr['has_prescription_order'] = $hasBizOrder ? 1 : 0;
|
||||
|
||||
@@ -1216,7 +1216,7 @@ class PrescriptionLogic
|
||||
$rxId = (int) ($row->id ?? 0);
|
||||
$bizCount = (int) PrescriptionOrder::where('prescription_id', $rxId)
|
||||
->whereNull('delete_time')
|
||||
->where('fulfillment_status', '<>', 4)
|
||||
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
|
||||
->count();
|
||||
if ($bizCount > 0) {
|
||||
self::setError('该处方已存在业务订单,无法作废');
|
||||
|
||||
@@ -963,6 +963,31 @@ class PrescriptionOrderLogic
|
||||
* @return array<string,mixed>|false
|
||||
*/
|
||||
public static function create(array $params, int $adminId, array $adminInfo)
|
||||
{
|
||||
self::$error = '';
|
||||
try {
|
||||
return Db::transaction(static function () use ($params, $adminId, $adminInfo) {
|
||||
// 先锁始终存在的处方行;空订单集合也能串行化两次创建。
|
||||
// 首个一致性读发生在获得该锁后,等待者可见前一次创建提交的订单。
|
||||
$rx = Prescription::where('id', (int) ($params['prescription_id'] ?? 0))
|
||||
->whereNull('delete_time')->lock(true)->find();
|
||||
if (!$rx) {
|
||||
throw new \DomainException('处方不存在');
|
||||
}
|
||||
$result = self::createLocked($params, $adminId, $adminInfo);
|
||||
if ($result === false) {
|
||||
throw new \DomainException(self::$error ?: '创建业务订单失败');
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static function createLocked(array $params, int $adminId, array $adminInfo)
|
||||
{
|
||||
self::$error = '';
|
||||
$rxId = (int) $params['prescription_id'];
|
||||
@@ -989,8 +1014,9 @@ class PrescriptionOrderLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
if (PrescriptionOrder::where('prescription_id', $rxId)->whereNull('delete_time')->where('fulfillment_status', '<>', 4)->count() > 0) {
|
||||
self::$error = '该处方已存在有效业务订单(未撤回前不可重复创建)';
|
||||
if (PrescriptionOrder::where('prescription_id', $rxId)->whereNull('delete_time')
|
||||
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)->count() > 0) {
|
||||
self::$error = '该处方已存在有效业务订单(撤回或全额退款后可重新创建)';
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1087,7 +1113,7 @@ class PrescriptionOrderLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
self::writeLog((int) $order->id, $adminId, $adminInfo, 'create', '创建业务订单');
|
||||
self::writeLog((int) $order->id, $adminId, $adminInfo, 'create', '创建业务订单', true);
|
||||
|
||||
if ($payOrderIds !== []) {
|
||||
self::replacePayOrderLinks((int) $order->id, $payOrderIds);
|
||||
@@ -1103,6 +1129,52 @@ class PrescriptionOrderLogic
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** 只修正业务订单创建时间,不改变关联支付、处方、药房快照或履约状态。 */
|
||||
public static function editTime(array $params, int $adminId, array $adminInfo)
|
||||
{
|
||||
self::$error = '';
|
||||
// 菜单尚未部署时,中间件可能放行未知权限,因此必须显式校验。
|
||||
if ((int) ($adminInfo['root'] ?? 0) !== 1
|
||||
&& !in_array('tcm.prescriptionOrder/editTime', AuthLogic::getAuthByAdminId($adminId), true)) {
|
||||
self::$error = '无权限修改业务订单创建时间';
|
||||
return false;
|
||||
}
|
||||
$validator = new \app\adminapi\validate\tcm\PrescriptionOrderValidate();
|
||||
if (!$validator->scene('editTime')->check($params)) {
|
||||
self::$error = (string) $validator->getError();
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return Db::transaction(static function () use ($params, $adminId, $adminInfo) {
|
||||
$order = PrescriptionOrder::where('id', (int) $params['id'])
|
||||
->whereNull('delete_time')->lock(true)->find();
|
||||
if (!$order) {
|
||||
throw new \DomainException('订单不存在');
|
||||
}
|
||||
if (!self::canAccessOrder($order, $adminId, $adminInfo)) {
|
||||
throw new \DomainException('无权限操作此订单');
|
||||
}
|
||||
$oldValue = $order->getData('create_time');
|
||||
$dateTime = (string) $params['create_time'];
|
||||
$isTimestamp = is_int($oldValue) || (is_string($oldValue) && ctype_digit($oldValue));
|
||||
$newValue = $isTimestamp ? (int) strtotime($dateTime) : $dateTime;
|
||||
if ((string) $oldValue !== (string) $newValue) {
|
||||
$order->create_time = $newValue;
|
||||
$order->save();
|
||||
$oldLabel = $isTimestamp ? date('Y-m-d H:i:s', (int) $oldValue) : (string) $oldValue;
|
||||
self::writeLog((int) $order->id, $adminId, $adminInfo, 'edit_time',
|
||||
'修改业务订单创建时间:' . $oldLabel . ' → ' . $dateTime, true);
|
||||
}
|
||||
|
||||
return ['id' => (int) $order->id, 'create_time' => $newValue];
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function detail(int $id, int $adminId, array $adminInfo): ?array
|
||||
{
|
||||
self::$error = '';
|
||||
|
||||
Reference in New Issue
Block a user