添加订单列表、详情

This commit is contained in:
lewis 2025-07-18 16:24:28 +08:00
parent a5a43f5674
commit 25e3b1adbd
7 changed files with 499 additions and 0 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdminController;
use app\admin\lists\OrderLists;
use app\admin\logic\OrderLogic;
use app\admin\validate\OrderValidate;
/**
* Order控制器
* Class OrderController
* @package app\admin\controller
*/
class OrderController extends BaseAdminController
{
/**
* @notes 获取列表
* @return \think\response\Json
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function lists()
{
return $this->dataLists(new OrderLists());
}
/**
* @notes 添加
* @return \think\response\Json
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function add()
{
$params = (new OrderValidate())->post()->goCheck('add');
$result = OrderLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(OrderLogic::getError());
}
/**
* @notes 编辑
* @return \think\response\Json
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function edit()
{
$params = (new OrderValidate())->post()->goCheck('edit');
$result = OrderLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(OrderLogic::getError());
}
/**
* @notes 删除
* @return \think\response\Json
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function delete()
{
$params = (new OrderValidate())->post()->goCheck('delete');
OrderLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取详情
* @return \think\response\Json
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function detail()
{
$params = (new OrderValidate())->goCheck('detail');
$result = OrderLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\Order;
use app\common\lists\ListsSearchInterface;
/**
* Order列表
* Class OrderLists
* @package app\admin\lists
*/
class OrderLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function setSearch(): array
{
return [
'=' => ['order_sn', 'uid', 'project_id', 'order_type', 'status', 'paid', 'pay_time', 'customer_name', 'phone'],
];
}
/**
* @notes 获取列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function lists(): array
{
return Order::where($this->searchWhere)
->with(['project'])
->field(['id', 'order_sn', 'uid', 'project_id', 'order_type', 'total_amount', 'pay_amount', 'status', 'paid', 'pay_time', 'customer_name', 'phone', 'address', 'delivery_time', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->each(function ($item) {
$item['status_text'] = $item->statusText;
$item['pay_status_text'] = $item->payStatusText;
$item['order_type_text'] = $item->orderTypeText;
})
->toArray();
}
/**
* @notes 获取数量
* @return int
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function count(): int
{
return Order::where($this->searchWhere)->count();
}
}

View File

@ -0,0 +1,130 @@
<?php
namespace app\admin\logic;
use app\common\model\Order;
use app\common\logic\BaseLogic;
use think\facade\Db;
/**
* Order逻辑
* Class OrderLogic
* @package app\admin\logic
*/
class OrderLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2025/07/17 17:16
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
Order::create([
'order_sn' => $params['order_sn'],
'uid' => $params['uid'],
'project_id' => $params['project_id'],
'order_type' => $params['order_type'],
'total_amount' => $params['total_amount'],
'pay_amount' => $params['pay_amount'],
'status' => $params['status'],
'paid' => $params['paid'],
'pay_time' => $params['pay_time'],
'customer_name' => $params['customer_name'],
'phone' => $params['phone'],
'address' => $params['address'],
'delivery_time' => $params['delivery_time'],
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author likeadmin
* @date 2025/07/17 17:16
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
Order::where('id', $params['id'])->update([
'order_sn' => $params['order_sn'],
'uid' => $params['uid'],
'project_id' => $params['project_id'],
'order_type' => $params['order_type'],
'total_amount' => $params['total_amount'],
'pay_amount' => $params['pay_amount'],
'status' => $params['status'],
'paid' => $params['paid'],
'pay_time' => $params['pay_time'],
'customer_name' => $params['customer_name'],
'phone' => $params['phone'],
'address' => $params['address'],
'delivery_time' => $params['delivery_time'],
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2025/07/17 17:16
*/
public static function delete(array $params): bool
{
return Order::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2025/07/17 17:16
*/
public static function detail($params): array
{
$data = Order::with(['orderDishes' => function ($query) {
$query->with('dishes');
}, 'orderProduct' => function ($query) {
$query->with('product');
}])->findOrEmpty($params['id']);
if (empty($data)) {
self::setError('订单不存在');
return [];
}
$data['status_text'] = $data->statusText;
$data['pay_status_text'] = $data->payStatusText;
$data['order_type_text'] = $data->orderTypeText;
return $data->toArray();
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace app\admin\validate;
use app\common\validate\BaseValidate;
/**
* Order验证器
* Class OrderValidate
* @package app\admin\validate
*/
class OrderValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
];
/**
* @notes 添加场景
* @return OrderValidate
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function sceneAdd()
{
return $this->remove('id', true);
}
/**
* @notes 编辑场景
* @return OrderValidate
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function sceneEdit()
{
return $this->only(['id']);
}
/**
* @notes 删除场景
* @return OrderValidate
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return OrderValidate
* @author likeadmin
* @date 2025/07/17 17:16
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace app\common\model;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* Order模型
* Class Order
* @package app\common\model
*/
class Order extends BaseModel
{
use SoftDelete;
const StatusMap = [
0 => '待发货',
1 => '待收货',
2 => '待评价',
3 => '已完成',
9 => '拼团中',
10 => '待付尾款',
11 => '尾款超时未付',
-1 => '已退款',
];
const OrderTypeMap = [
1 => '普通订单',
2 => '自提订单',
];
const PayStatusMap = [
0 => '待支付',
1 => '已支付',
];
protected $name = 'order';
protected $deleteTime = 'delete_time';
public function project()
{
return $this->hasOne(Project::class, 'id', 'project_id')->field('id,name');
}
public function orderDishes()
{
return $this->hasMany(OrderDishes::class, 'order_id', 'id');
}
public function orderProduct()
{
return $this->hasMany(OrderProduct::class, 'order_id', 'id');
}
public function getStatusTextAttr($value)
{
return self::StatusMap[$this->status];
}
public function getOrderTypeTextAttr($value)
{
return self::OrderTypeMap[$this->order_type];
}
public function getPayStatusTextAttr($value)
{
return self::PayStatusMap[$this->paid];
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace app\common\model;
use think\model\concern\SoftDelete;
/**
* OrderDishes模型
* Class OrderDishes
* @package app\common\model
*/
class OrderDishes extends BaseModel
{
use SoftDelete;
protected $name = 'order_dishes';
protected $deleteTime = 'delete_time';
public function dishes()
{
return $this->hasOne(Dishes::class, 'id', 'dishes_id');
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace app\common\model;
use think\model\concern\SoftDelete;
/**
* OrderProduct模型
* Class OrderProduct
* @package app\common\model
*/
class OrderProduct extends BaseModel
{
use SoftDelete;
protected $name = 'order_product';
protected $deleteTime = 'delete_time';
public function product()
{
return $this->hasOne(Product::class, 'id', 'product_id');
}
}