| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace App\Module\Mex\AdminControllers;
- use App\Module\Mex\Repositories\MexOrderRepository;
- use App\Module\Mex\Enums\OrderStatus;
- use App\Module\Mex\Enums\OrderType;
- use App\Module\Mex\AdminControllers\Helper\GridHelper;
- use App\Module\Mex\AdminControllers\Helper\FilterHelper;
- use App\Module\Mex\AdminControllers\Helper\ShowHelper;
- use App\Module\Mex\AdminControllers\Helper\FormHelper;
- use App\Module\Mex\Services\MexTransactionService;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- /**
- * 农贸市场订单管理
- *
- * 路由:/admin/mex-orders
- */
- #[Resource('mex-orders', names: 'dcat.admin.mex-orders')]
- class MexOrderController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '农贸市场订单';
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new MexOrderRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- // 添加顶部信息显示最后撮合时间
- $grid->header(function () {
- $matchTimes = MexTransactionService::getLastMatchTimes();
- $lastSellTime = $matchTimes['last_sell_match_time']
- ? $matchTimes['last_sell_match_time']->format('Y-m-d H:i:s')
- : '暂无记录';
- $lastBuyTime = $matchTimes['last_buy_match_time']
- ? $matchTimes['last_buy_match_time']->format('Y-m-d H:i:s')
- : '暂无记录';
- return '<div class="alert alert-info mb-3">
- <div class="row">
- <div class="col-md-6">
- <strong>最后卖出撮合时间:</strong> ' . $lastSellTime . '
- </div>
- <div class="col-md-6">
- <strong>最后买入撮合时间:</strong> ' . $lastBuyTime . '
- </div>
- </div>
- </div>';
- });
- $grid->column('id', 'ID')->sortable();
- $helper->columnUserId();
- $helper->columnItemId();
- $grid->column('item.name', '商品名称')->display(function ($value) {
- return $value ?: '未知商品';
- });
- $grid->column('order_type', '订单类型')->display(function ($value) {
- return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
- })->label([
- 'BUY' => 'primary',
- 'SELL' => 'success',
- ]);
- $helper->columnQuantity();
- $helper->columnPrice();
- $helper->columnAmount('total_amount', '总金额');
- $grid->column('status', '状态')->display(function ($value) {
- return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
- })->label([
- 'PENDING' => 'warning',
- 'COMPLETED' => 'success',
- 'CANCELLED' => 'default',
- 'FAILED' => 'danger',
- ]);
- $helper->columnQuantity('completed_quantity', '已成交数量');
- $helper->columnAmount('completed_amount', '已成交金额');
- $helper->columnDatetime('created_at', '创建时间');
- $helper->columnDatetime('completed_at', '完成时间');
- // 禁用新增和删除
- $grid->disableCreateButton();
- $grid->disableDeleteButton();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filterHelper = new FilterHelper($filter, $this);
- $filterHelper->equalId();
- $filterHelper->equalUserId();
- $filterHelper->equalItemId();
- $filterHelper->equalOrderType();
- $filterHelper->equalOrderStatus();
- $filterHelper->betweenPrice();
- $filterHelper->betweenAmount('total_amount', '总金额范围');
- $filterHelper->betweenCreatedAt();
- });
- // 默认排序
- $grid->model()->orderBy('created_at', 'desc');
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new MexOrderRepository(['item']), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('user_id', '用户ID');
- $show->field('item_id', '商品ID');
- $show->field('item.name', '商品名称')->as(function ($value) {
- return $value ?: '未知商品';
- });
- $show->field('order_type', '订单类型')->as(function ($value) {
- return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
- });
- $show->field('quantity', '数量');
- $show->field('price', '价格');
- $show->field('total_amount', '总金额');
- $show->field('status', '状态')->as(function ($value) {
- return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
- });
- $show->field('frozen_amount', '冻结金额');
- $show->field('completed_quantity', '已成交数量');
- $show->field('completed_amount', '已成交金额');
- $show->field('failed_reason', '失败原因');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- $show->field('completed_at', '完成时间');
- // 禁用编辑和删除
- $show->disableEditButton();
- $show->disableDeleteButton();
- });
- }
- /**
- * 表单页面(仅用于查看,不允许编辑)
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new MexOrderRepository(['item']), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $helper->display('id', 'ID');
- $helper->display('user_id', '用户ID');
- $helper->display('item_id', '商品ID');
- $form->display('item.name', '商品名称')->with(function ($value) {
- return $value ?: '未知商品';
- });
- $form->display('order_type', '订单类型')->with(function ($value) {
- return OrderType::from($value)->getDescription();
- });
- $helper->display('quantity', '数量');
- $helper->display('price', '单价');
- $helper->display('total_amount', '总金额');
- $form->display('status', '状态')->with(function ($value) {
- return OrderStatus::from($value)->getDescription();
- });
- $helper->display('frozen_amount', '冻结金额');
- $helper->display('completed_quantity', '已成交数量');
- $helper->display('completed_amount', '已成交金额');
- $helper->display('failed_reason', '失败原因');
- $helper->display('created_at', '创建时间');
- $helper->display('updated_at', '更新时间');
- $helper->display('completed_at', '完成时间');
- // 禁用保存按钮
- $form->disableSubmitButton();
- $form->disableResetButton();
- });
- }
- }
|