MexWarehouseController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Module\Mex\AdminControllers;
  3. use App\Module\Mex\Repositories\MexWarehouseRepository;
  4. use Spatie\RouteAttributes\Attributes\Resource;
  5. use UCore\DcatAdmin\AdminController;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. /**
  10. * 农贸市场仓库管理
  11. *
  12. * 路由:/admin/mex-warehouse
  13. */
  14. #[Resource('mex-warehouse', names: 'dcat.admin.mex-warehouse')]
  15. class MexWarehouseController extends AdminController
  16. {
  17. /**
  18. * 页面标题
  19. *
  20. * @var string
  21. */
  22. protected $title = '农贸市场仓库';
  23. /**
  24. * 列表页面
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(new MexWarehouseRepository(), function (Grid $grid) {
  31. $grid->column('id', 'ID')->sortable();
  32. $grid->column('item_id', '商品ID')->display(function ($value) {
  33. return "<a href='" . admin_url("game-items/{$value}") . "' target='_blank'>{$value}</a>";
  34. });
  35. $grid->column('quantity', '当前库存')->display(function ($value) {
  36. return number_format($value);
  37. });
  38. $grid->column('total_buy_quantity', '累计买入数量')->display(function ($value) {
  39. return number_format($value);
  40. });
  41. $grid->column('total_sell_quantity', '累计卖出数量')->display(function ($value) {
  42. return number_format($value);
  43. });
  44. $grid->column('total_buy_amount', '累计买入金额')->display(function ($value) {
  45. return number_format($value, 5);
  46. });
  47. $grid->column('total_sell_amount', '累计卖出金额')->display(function ($value) {
  48. return number_format($value, 5);
  49. });
  50. $grid->column('average_buy_price', '平均买入价')->display(function () {
  51. if ($this->total_buy_quantity > 0) {
  52. $avgPrice = bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
  53. return number_format($avgPrice, 5);
  54. }
  55. return '-';
  56. });
  57. $grid->column('average_sell_price', '平均卖出价')->display(function () {
  58. if ($this->total_sell_quantity > 0) {
  59. $avgPrice = bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
  60. return number_format($avgPrice, 5);
  61. }
  62. return '-';
  63. });
  64. $grid->column('last_transaction_at', '最后交易时间');
  65. // 禁用新增、编辑和删除
  66. $grid->disableCreateButton();
  67. $grid->disableEditButton();
  68. $grid->disableDeleteButton();
  69. // 筛选器
  70. $grid->filter(function (Grid\Filter $filter) {
  71. $filter->equal('id', 'ID');
  72. $filter->equal('item_id', '商品ID');
  73. $filter->between('quantity', '库存范围');
  74. $filter->between('total_buy_quantity', '累计买入数量范围');
  75. $filter->between('total_sell_quantity', '累计卖出数量范围');
  76. $filter->between('total_buy_amount', '累计买入金额范围');
  77. $filter->between('total_sell_amount', '累计卖出金额范围');
  78. $filter->between('last_transaction_at', '最后交易时间')->datetime();
  79. });
  80. // 默认排序
  81. $grid->model()->orderBy('quantity', 'desc');
  82. // 工具栏
  83. $grid->tools(function (Grid\Tools $tools) {
  84. $tools->append('<div class="btn-group">
  85. <button type="button" class="btn btn-sm btn-outline-primary" onclick="showWarehouseStats()">
  86. <i class="fa fa-pie-chart"></i> 仓库统计
  87. </button>
  88. </div>');
  89. });
  90. // 行操作
  91. $grid->actions(function (Grid\Displayers\Actions $actions) {
  92. // 可以添加查看详细统计的操作
  93. $actions->append('<a href="javascript:void(0)" onclick="showItemStats('.$actions->getKey().')" class="btn btn-xs btn-outline-info">
  94. <i class="fa fa-line-chart"></i> 统计
  95. </a>');
  96. });
  97. });
  98. }
  99. /**
  100. * 详情页面
  101. *
  102. * @param mixed $id
  103. * @return Show
  104. */
  105. protected function detail($id)
  106. {
  107. return Show::make($id, new MexWarehouseRepository(), function (Show $show) {
  108. $show->field('id', 'ID');
  109. $show->field('item_id', '商品ID');
  110. $show->divider('库存信息');
  111. $show->field('quantity', '当前库存');
  112. $show->field('total_buy_quantity', '累计买入数量');
  113. $show->field('total_sell_quantity', '累计卖出数量');
  114. $show->field('net_quantity', '净买入数量')->as(function () {
  115. return $this->total_buy_quantity - $this->total_sell_quantity;
  116. });
  117. $show->divider('金额信息');
  118. $show->field('total_buy_amount', '累计买入金额');
  119. $show->field('total_sell_amount', '累计卖出金额');
  120. $show->field('net_amount', '净买入金额')->as(function () {
  121. return bcsub($this->total_buy_amount, $this->total_sell_amount, 5);
  122. });
  123. $show->divider('价格信息');
  124. $show->field('average_buy_price', '平均买入价')->as(function () {
  125. if ($this->total_buy_quantity > 0) {
  126. return bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
  127. }
  128. return '0.00000';
  129. });
  130. $show->field('average_sell_price', '平均卖出价')->as(function () {
  131. if ($this->total_sell_quantity > 0) {
  132. return bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
  133. }
  134. return '0.00000';
  135. });
  136. $show->field('last_transaction_at', '最后交易时间');
  137. // 禁用编辑和删除
  138. $show->disableEditButton();
  139. $show->disableDeleteButton();
  140. });
  141. }
  142. /**
  143. * 表单页面(仅用于查看,不允许编辑)
  144. *
  145. * @return Form
  146. */
  147. protected function form()
  148. {
  149. return Form::make(new MexWarehouseRepository(), function (Form $form) {
  150. $form->display('id', 'ID');
  151. $form->display('item_id', '商品ID');
  152. $form->display('quantity', '当前库存');
  153. $form->display('total_buy_quantity', '累计买入数量');
  154. $form->display('total_sell_quantity', '累计卖出数量');
  155. $form->display('total_buy_amount', '累计买入金额');
  156. $form->display('total_sell_amount', '累计卖出金额');
  157. $form->display('last_transaction_at', '最后交易时间');
  158. // 禁用保存按钮
  159. $form->disableSubmitButton();
  160. $form->disableResetButton();
  161. });
  162. }
  163. }