This commit is contained in:
Your Name
2026-03-11 09:49:47 +08:00
parent 02ae537b4c
commit 38ad60f4bb
290 changed files with 36917 additions and 123 deletions
@@ -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;
}
}