更新
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\adminapi\logic\order;
|
||||
|
||||
use app\common\model\Order;
|
||||
use app\common\model\OrderDetail;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 订单业务逻辑
|
||||
* Class OrderLogic
|
||||
* @package app\adminapi\logic\order
|
||||
*/
|
||||
class OrderLogic
|
||||
{
|
||||
/**
|
||||
* @notes 生成订单号
|
||||
* @return string
|
||||
*/
|
||||
public static function generateOrderNo(): string
|
||||
{
|
||||
return 'ORD' . date('YmdHis') . mt_rand(100000, 999999);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 创建订单
|
||||
* @param array $params
|
||||
* @return Order|false
|
||||
*/
|
||||
public static function create(array $params)
|
||||
{
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
$order = new Order();
|
||||
$order->order_no = self::generateOrderNo();
|
||||
$order->patient_id = $params['patient_id'];
|
||||
$order->creator_id = $params['creator_id'];
|
||||
$order->order_type = $params['order_type'];
|
||||
$order->amount = $params['amount'];
|
||||
$order->status = 1; // 待支付
|
||||
$order->remark = $params['remark'] ?? '';
|
||||
$order->save();
|
||||
|
||||
// 创建订单详情
|
||||
if (!empty($params['details'])) {
|
||||
foreach ($params['details'] as $detail) {
|
||||
$orderDetail = new OrderDetail();
|
||||
$orderDetail->order_id = $order->id;
|
||||
$orderDetail->related_type = $detail['related_type'];
|
||||
$orderDetail->related_id = $detail['related_id'];
|
||||
$orderDetail->quantity = $detail['quantity'] ?? 1;
|
||||
$orderDetail->unit_price = $detail['unit_price'];
|
||||
$orderDetail->total_price = $detail['total_price'];
|
||||
$orderDetail->save();
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return $order;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 编辑订单
|
||||
* @param int $id
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public static function edit(int $id, array $params): bool
|
||||
{
|
||||
try {
|
||||
$order = Order::find($id);
|
||||
if (!$order) {
|
||||
self::setError('订单不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($params['remark'])) {
|
||||
$order->remark = $params['remark'];
|
||||
}
|
||||
|
||||
$order->save();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 支付订单
|
||||
* @param int $id
|
||||
* @param string $paymentMethod
|
||||
* @return bool
|
||||
*/
|
||||
public static function pay(int $id, string $paymentMethod): bool
|
||||
{
|
||||
try {
|
||||
$order = Order::find($id);
|
||||
if (!$order) {
|
||||
self::setError('订单不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($order->status != 1) {
|
||||
self::setError('订单状态不允许支付');
|
||||
return false;
|
||||
}
|
||||
|
||||
$order->status = 2; // 已支付
|
||||
$order->payment_method = $paymentMethod;
|
||||
$order->payment_time = date('Y-m-d H:i:s');
|
||||
$order->save();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 取消订单
|
||||
* @param int $id
|
||||
* @return bool
|
||||
*/
|
||||
public static function cancel(int $id): bool
|
||||
{
|
||||
try {
|
||||
$order = Order::find($id);
|
||||
if (!$order) {
|
||||
self::setError('订单不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($order->status != 1) {
|
||||
self::setError('只有待支付的订单才能取消');
|
||||
return false;
|
||||
}
|
||||
|
||||
$order->status = 3; // 已取消
|
||||
$order->save();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 退款订单
|
||||
* @param int $id
|
||||
* @return bool
|
||||
*/
|
||||
public static function refund(int $id): bool
|
||||
{
|
||||
try {
|
||||
$order = Order::find($id);
|
||||
if (!$order) {
|
||||
self::setError('订单不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($order->status != 2) {
|
||||
self::setError('只有已支付的订单才能退款');
|
||||
return false;
|
||||
}
|
||||
|
||||
$order->status = 4; // 已退款
|
||||
$order->save();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 删除订单
|
||||
* @param int $id
|
||||
* @return bool
|
||||
*/
|
||||
public static function delete(int $id): bool
|
||||
{
|
||||
try {
|
||||
$order = Order::find($id);
|
||||
if (!$order) {
|
||||
self::setError('订单不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 删除订单详情
|
||||
OrderDetail::where('order_id', $id)->delete();
|
||||
|
||||
// 删除订单
|
||||
$order->delete();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取订单详情
|
||||
* @param int $id
|
||||
* @return array|null
|
||||
*/
|
||||
public static function detail(int $id): ?array
|
||||
{
|
||||
$order = Order::with(['patient', 'creator', 'details'])
|
||||
->find($id);
|
||||
|
||||
if (!$order) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $order->toArray();
|
||||
}
|
||||
|
||||
private static $error = '';
|
||||
|
||||
public static function setError(string $error): void
|
||||
{
|
||||
self::$error = $error;
|
||||
}
|
||||
|
||||
public static function getError(): string
|
||||
{
|
||||
return self::$error;
|
||||
}
|
||||
}
|
||||
@@ -338,7 +338,7 @@ class DiagnosisLogic extends BaseLogic
|
||||
try {
|
||||
// 获取配置
|
||||
$config = self::getTrtcConfig();
|
||||
|
||||
|
||||
if (!$config) {
|
||||
self::setError('请先配置腾讯云TRTC参数');
|
||||
return false;
|
||||
@@ -1197,4 +1197,158 @@ class DiagnosisLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取就诊卡列表(供小程序调用)
|
||||
* @param int $patientId
|
||||
* @return array|false
|
||||
*/
|
||||
public static function getCardList(int $patientId)
|
||||
{
|
||||
try {
|
||||
if (!$patientId) {
|
||||
self::setError('患者ID不能为空');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 查询该患者的所有诊单
|
||||
$cardList = Diagnosis::where('patient_id', $patientId)
|
||||
->where('delete_time', null)
|
||||
->field([
|
||||
'id', 'patient_id', 'patient_name', 'gender', 'age',
|
||||
'diagnosis_date', 'diagnosis_type', 'syndrome_type',
|
||||
'status', 'create_time', 'update_time'
|
||||
])
|
||||
->order('create_time', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 处理数据格式
|
||||
foreach ($cardList as &$card) {
|
||||
// 转换诊断日期为时间戳和格式化文本
|
||||
if ($card['diagnosis_date']) {
|
||||
$card['diagnosis_date_text'] = date('Y-m-d', $card['diagnosis_date']);
|
||||
} else {
|
||||
$card['diagnosis_date_text'] = '-';
|
||||
}
|
||||
|
||||
// 添加性别描述
|
||||
$card['gender_desc'] = $card['gender'] == 1 ? '男' : '女';
|
||||
|
||||
// 添加状态描述
|
||||
$card['status_desc'] = $card['status'] == 1 ? '启用' : '禁用';
|
||||
|
||||
// 格式化创建时间
|
||||
if ($card['create_time']) {
|
||||
$card['create_time'] = date('Y-m-d H:i:s', $card['create_time']);
|
||||
}
|
||||
|
||||
// 格式化更新时间
|
||||
if ($card['update_time']) {
|
||||
$card['update_time'] = date('Y-m-d H:i:s', $card['update_time']);
|
||||
}
|
||||
}
|
||||
|
||||
return $cardList;
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('获取就诊卡列表失败 - patient_id: ' . $patientId . ', error: ' . $e->getMessage());
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更新就诊卡(供小程序调用)
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public static function updateCard(array $params)
|
||||
{
|
||||
try {
|
||||
$diagnosisId = $params['id'] ?? 0;
|
||||
$patientId = $params['patient_id'] ?? 0;
|
||||
|
||||
if (!$diagnosisId) {
|
||||
self::setError('诊单ID不能为空');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$patientId) {
|
||||
self::setError('患者ID不能为空');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取诊单信息
|
||||
$diagnosis = Diagnosis::find($diagnosisId);
|
||||
|
||||
if (!$diagnosis) {
|
||||
self::setError('诊单不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 权限验证:只能修改自己的诊单
|
||||
if ($diagnosis->patient_id != $patientId) {
|
||||
\think\facade\Log::warning('非法修改诊单 - diagnosis_id: ' . $diagnosisId . ', patient_id: ' . $patientId . ', actual_patient_id: ' . $diagnosis->patient_id);
|
||||
self::setError('无权限修改此诊单');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 准备更新数据
|
||||
$updateData = [
|
||||
'patient_name' => $params['patient_name'] ?? '',
|
||||
'gender' => $params['gender'] ?? 1,
|
||||
'age' => $params['age'] ?? 0,
|
||||
'diagnosis_type' => $params['diagnosis_type'] ?? '',
|
||||
'syndrome_type' => $params['syndrome_type'] ?? '',
|
||||
'diabetes_type' => $params['diabetes_type'] ?? '',
|
||||
// 现病史字段
|
||||
'appetite' => $params['appetite'] ?? '',
|
||||
'water_intake' => $params['water_intake'] ?? '',
|
||||
'diet_condition' => $params['diet_condition'] ?? '',
|
||||
'weight_change' => $params['weight_change'] ?? '',
|
||||
'body_feeling' => $params['body_feeling'] ?? '',
|
||||
'sleep_condition' => $params['sleep_condition'] ?? '',
|
||||
'eye_condition' => $params['eye_condition'] ?? '',
|
||||
'head_feeling' => $params['head_feeling'] ?? '',
|
||||
'sweat_condition' => $params['sweat_condition'] ?? '',
|
||||
'skin_condition' => $params['skin_condition'] ?? '',
|
||||
'urine_condition' => $params['urine_condition'] ?? '',
|
||||
'stool_condition' => $params['stool_condition'] ?? '',
|
||||
'kidney_condition' => $params['kidney_condition'] ?? '',
|
||||
'fatty_liver_degree' => $params['fatty_liver_degree'] ?? '',
|
||||
// 既往史字段
|
||||
'past_history' => $params['past_history'] ?? '',
|
||||
'trauma_history' => $params['trauma_history'] ?? 0,
|
||||
'surgery_history' => $params['surgery_history'] ?? 0,
|
||||
'allergy_history' => $params['allergy_history'] ?? 0,
|
||||
'family_history' => $params['family_history'] ?? 0,
|
||||
'pregnancy_history' => $params['pregnancy_history'] ?? 0,
|
||||
// 临床信息
|
||||
'symptoms' => $params['symptoms'] ?? '',
|
||||
'tongue_coating' => $params['tongue_coating'] ?? '',
|
||||
'pulse' => $params['pulse'] ?? '',
|
||||
'treatment_principle' => $params['treatment_principle'] ?? '',
|
||||
'prescription' => $params['prescription'] ?? '',
|
||||
'doctor_advice' => $params['doctor_advice'] ?? '',
|
||||
'remark' => $params['remark'] ?? '',
|
||||
'update_time' => time()
|
||||
];
|
||||
|
||||
// 处理诊断日期
|
||||
if (!empty($params['diagnosis_date'])) {
|
||||
$updateData['diagnosis_date'] = strtotime($params['diagnosis_date']);
|
||||
}
|
||||
|
||||
// 执行更新
|
||||
$diagnosis->save($updateData);
|
||||
|
||||
\think\facade\Log::info('诊单更新成功 - diagnosis_id: ' . $diagnosisId . ', patient_id: ' . $patientId);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('更新诊单失败 - error: ' . $e->getMessage());
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user