MexOrderController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $grid->column('last_match_failure_reason', '最后无法成交原因')->display(function ($value) {
  85. return $value ? \Illuminate\Support\Str::limit($value, 50) : '-';
  86. })->help('显示撮合过程中无法成交的具体原因');
  87. $helper->columnDatetime('created_at', '创建时间');
  88. $helper->columnDatetime('completed_at', '完成时间');
  89. // 禁用新增和删除
  90. $grid->disableCreateButton();
  91. $grid->disableDeleteButton();
  92. // 筛选器
  93. $grid->filter(function (Grid\Filter $filter) {
  94. $filterHelper = new FilterHelper($filter, $this);
  95. $filterHelper->equalId();
  96. $filterHelper->equalUserId();
  97. $filterHelper->equalItemId();
  98. $filterHelper->equalOrderType();
  99. $filterHelper->equalOrderStatus();
  100. $filterHelper->betweenPrice();
  101. $filterHelper->betweenAmount('total_amount', '总金额范围');
  102. $filterHelper->betweenCreatedAt();
  103. });
  104. // 默认排序
  105. $grid->model()->orderBy('created_at', 'desc');
  106. });
  107. }
  108. /**
  109. * 详情页面
  110. *
  111. * @param mixed $id
  112. * @return Show
  113. */
  114. protected function detail($id)
  115. {
  116. return Show::make($id, new MexOrderRepository(['item']), function (Show $show) {
  117. $show->field('id', 'ID');
  118. $show->field('user_id', '用户ID');
  119. $show->field('item_id', '商品ID');
  120. $show->field('item.name', '商品名称')->as(function ($value) {
  121. return $value ?: '未知商品';
  122. });
  123. $show->field('order_type', '订单类型')->as(function ($value) {
  124. return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
  125. });
  126. $show->field('quantity', '数量');
  127. $show->field('price', '价格');
  128. $show->field('total_amount', '总金额');
  129. $show->field('status', '状态')->as(function ($value) {
  130. return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
  131. });
  132. $show->field('frozen_amount', '冻结金额');
  133. $show->field('completed_quantity', '已成交数量');
  134. $show->field('completed_amount', '已成交金额');
  135. $show->field('failed_reason', '失败原因');
  136. $show->field('last_match_failure_reason', '最后无法成交原因')->as(function ($value) {
  137. return $value ?: '无';
  138. });
  139. $show->field('created_at', '创建时间');
  140. $show->field('updated_at', '更新时间');
  141. $show->field('completed_at', '完成时间');
  142. // 禁用编辑和删除
  143. $show->disableEditButton();
  144. $show->disableDeleteButton();
  145. });
  146. }
  147. /**
  148. * 表单页面(仅用于查看,不允许编辑)
  149. *
  150. * @return Form
  151. */
  152. protected function form()
  153. {
  154. return Form::make(new MexOrderRepository(['item']), function (Form $form) {
  155. $helper = new FormHelper($form, $this);
  156. $helper->display('id', 'ID');
  157. $helper->display('user_id', '用户ID');
  158. $helper->display('item_id', '商品ID');
  159. $form->display('item.name', '商品名称')->with(function ($value) {
  160. return $value ?: '未知商品';
  161. });
  162. $form->display('order_type', '订单类型')->with(function ($value) {
  163. return OrderType::from($value)->getDescription();
  164. });
  165. $helper->display('quantity', '数量');
  166. $helper->display('price', '单价');
  167. $helper->display('total_amount', '总金额');
  168. $form->display('status', '状态')->with(function ($value) {
  169. return OrderStatus::from($value)->getDescription();
  170. });
  171. $helper->display('frozen_amount', '冻结金额');
  172. $helper->display('completed_quantity', '已成交数量');
  173. $helper->display('completed_amount', '已成交金额');
  174. $helper->display('failed_reason', '失败原因');
  175. $helper->display('last_match_failure_reason', '最后无法成交原因');
  176. $helper->display('created_at', '创建时间');
  177. $helper->display('updated_at', '更新时间');
  178. $helper->display('completed_at', '完成时间');
  179. // 禁用保存按钮
  180. $form->disableSubmitButton();
  181. $form->disableResetButton();
  182. });
  183. }
  184. }