MexOrderController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Module\Mex\AdminControllers;
  3. use App\Module\Mex\Repositories\MexOrderRepository;
  4. use App\Module\Mex\Enums\OrderStatus;
  5. use App\Module\Mex\Enums\OrderType;
  6. use App\Module\Mex\AdminControllers\Helper\GridHelper;
  7. use App\Module\Mex\AdminControllers\Helper\FilterHelper;
  8. use App\Module\Mex\AdminControllers\Helper\ShowHelper;
  9. use App\Module\Mex\AdminControllers\Helper\FormHelper;
  10. use App\Module\Mex\Services\MexTransactionService;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. use UCore\DcatAdmin\AdminController;
  13. use Dcat\Admin\Form;
  14. use Dcat\Admin\Grid;
  15. use Dcat\Admin\Show;
  16. /**
  17. * 农贸市场订单管理
  18. *
  19. * 路由:/admin/mex-orders
  20. */
  21. #[Resource('mex-orders', names: 'dcat.admin.mex-orders')]
  22. class MexOrderController extends AdminController
  23. {
  24. /**
  25. * 页面标题
  26. *
  27. * @var string
  28. */
  29. protected $title = '农贸市场订单';
  30. /**
  31. * 列表页面
  32. *
  33. * @return Grid
  34. */
  35. protected function grid()
  36. {
  37. return Grid::make(new MexOrderRepository(), function (Grid $grid) {
  38. $helper = new GridHelper($grid, $this);
  39. // 添加顶部信息显示最后撮合时间
  40. $grid->header(function () {
  41. $matchTimes = MexTransactionService::getLastMatchTimes();
  42. $lastSellTime = $matchTimes['last_sell_match_time']
  43. ? $matchTimes['last_sell_match_time']->format('Y-m-d H:i:s')
  44. : '暂无记录';
  45. $lastBuyTime = $matchTimes['last_buy_match_time']
  46. ? $matchTimes['last_buy_match_time']->format('Y-m-d H:i:s')
  47. : '暂无记录';
  48. return '<div class="alert alert-info mb-3">
  49. <div class="row">
  50. <div class="col-md-6">
  51. <strong>最后卖出撮合时间:</strong> ' . $lastSellTime . '
  52. </div>
  53. <div class="col-md-6">
  54. <strong>最后买入撮合时间:</strong> ' . $lastBuyTime . '
  55. </div>
  56. </div>
  57. </div>';
  58. });
  59. $grid->column('id', 'ID')->sortable();
  60. $helper->columnUserId();
  61. $helper->columnItemId();
  62. $grid->column('item.name', '商品名称')->display(function ($value) {
  63. return $value ?: '未知商品';
  64. });
  65. $grid->column('order_type', '订单类型')->display(function ($value) {
  66. return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
  67. })->label([
  68. 'BUY' => 'primary',
  69. 'SELL' => 'success',
  70. ]);
  71. $helper->columnQuantity();
  72. $helper->columnPrice();
  73. $helper->columnAmount('total_amount', '总金额');
  74. $grid->column('status', '状态')->display(function ($value) {
  75. return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
  76. })->label([
  77. 'PENDING' => 'warning',
  78. 'COMPLETED' => 'success',
  79. 'CANCELLED' => 'default',
  80. 'FAILED' => 'danger',
  81. ]);
  82. $helper->columnQuantity('completed_quantity', '已成交数量');
  83. $helper->columnAmount('completed_amount', '已成交金额');
  84. $helper->columnDatetime('created_at', '创建时间');
  85. $helper->columnDatetime('completed_at', '完成时间');
  86. // 禁用新增和删除
  87. $grid->disableCreateButton();
  88. $grid->disableDeleteButton();
  89. // 筛选器
  90. $grid->filter(function (Grid\Filter $filter) {
  91. $filterHelper = new FilterHelper($filter, $this);
  92. $filterHelper->equalId();
  93. $filterHelper->equalUserId();
  94. $filterHelper->equalItemId();
  95. $filterHelper->equalOrderType();
  96. $filterHelper->equalOrderStatus();
  97. $filterHelper->betweenPrice();
  98. $filterHelper->betweenAmount('total_amount', '总金额范围');
  99. $filterHelper->betweenCreatedAt();
  100. });
  101. // 默认排序
  102. $grid->model()->orderBy('created_at', 'desc');
  103. });
  104. }
  105. /**
  106. * 详情页面
  107. *
  108. * @param mixed $id
  109. * @return Show
  110. */
  111. protected function detail($id)
  112. {
  113. return Show::make($id, new MexOrderRepository(['item']), function (Show $show) {
  114. $show->field('id', 'ID');
  115. $show->field('user_id', '用户ID');
  116. $show->field('item_id', '商品ID');
  117. $show->field('item.name', '商品名称')->as(function ($value) {
  118. return $value ?: '未知商品';
  119. });
  120. $show->field('order_type', '订单类型')->as(function ($value) {
  121. return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
  122. });
  123. $show->field('quantity', '数量');
  124. $show->field('price', '价格');
  125. $show->field('total_amount', '总金额');
  126. $show->field('status', '状态')->as(function ($value) {
  127. return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
  128. });
  129. $show->field('frozen_amount', '冻结金额');
  130. $show->field('completed_quantity', '已成交数量');
  131. $show->field('completed_amount', '已成交金额');
  132. $show->field('failed_reason', '失败原因');
  133. $show->field('created_at', '创建时间');
  134. $show->field('updated_at', '更新时间');
  135. $show->field('completed_at', '完成时间');
  136. // 禁用编辑和删除
  137. $show->disableEditButton();
  138. $show->disableDeleteButton();
  139. });
  140. }
  141. /**
  142. * 表单页面(仅用于查看,不允许编辑)
  143. *
  144. * @return Form
  145. */
  146. protected function form()
  147. {
  148. return Form::make(new MexOrderRepository(['item']), function (Form $form) {
  149. $helper = new FormHelper($form, $this);
  150. $helper->display('id', 'ID');
  151. $helper->display('user_id', '用户ID');
  152. $helper->display('item_id', '商品ID');
  153. $form->display('item.name', '商品名称')->with(function ($value) {
  154. return $value ?: '未知商品';
  155. });
  156. $form->display('order_type', '订单类型')->with(function ($value) {
  157. return OrderType::from($value)->getDescription();
  158. });
  159. $helper->display('quantity', '数量');
  160. $helper->display('price', '单价');
  161. $helper->display('total_amount', '总金额');
  162. $form->display('status', '状态')->with(function ($value) {
  163. return OrderStatus::from($value)->getDescription();
  164. });
  165. $helper->display('frozen_amount', '冻结金额');
  166. $helper->display('completed_quantity', '已成交数量');
  167. $helper->display('completed_amount', '已成交金额');
  168. $helper->display('failed_reason', '失败原因');
  169. $helper->display('created_at', '创建时间');
  170. $helper->display('updated_at', '更新时间');
  171. $helper->display('completed_at', '完成时间');
  172. // 禁用保存按钮
  173. $form->disableSubmitButton();
  174. $form->disableResetButton();
  175. });
  176. }
  177. }