| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Module\Mex\AdminControllers;
- use App\Module\Mex\Repositories\MexWarehouseRepository;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- /**
- * 农贸市场仓库管理
- *
- * 路由:/admin/mex-warehouse
- */
- #[Resource('mex-warehouse', names: 'dcat.admin.mex-warehouse')]
- class MexWarehouseController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '农贸市场仓库';
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new MexWarehouseRepository(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('item_id', '商品ID')->display(function ($value) {
- return "<a href='" . admin_url("game-items/{$value}") . "' target='_blank'>{$value}</a>";
- });
- $grid->column('quantity', '当前库存')->display(function ($value) {
- return number_format($value);
- });
- $grid->column('total_buy_quantity', '累计买入数量')->display(function ($value) {
- return number_format($value);
- });
- $grid->column('total_sell_quantity', '累计卖出数量')->display(function ($value) {
- return number_format($value);
- });
- $grid->column('total_buy_amount', '累计买入金额')->display(function ($value) {
- return number_format($value, 5);
- });
- $grid->column('total_sell_amount', '累计卖出金额')->display(function ($value) {
- return number_format($value, 5);
- });
- $grid->column('average_buy_price', '平均买入价')->display(function () {
- if ($this->total_buy_quantity > 0) {
- $avgPrice = bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
- return number_format($avgPrice, 5);
- }
- return '-';
- });
- $grid->column('average_sell_price', '平均卖出价')->display(function () {
- if ($this->total_sell_quantity > 0) {
- $avgPrice = bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
- return number_format($avgPrice, 5);
- }
- return '-';
- });
- $grid->column('last_transaction_at', '最后交易时间');
- // 禁用新增、编辑和删除
- $grid->disableCreateButton();
- $grid->disableEditButton();
- $grid->disableDeleteButton();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->equal('item_id', '商品ID');
- $filter->between('quantity', '库存范围');
- $filter->between('total_buy_quantity', '累计买入数量范围');
- $filter->between('total_sell_quantity', '累计卖出数量范围');
- $filter->between('total_buy_amount', '累计买入金额范围');
- $filter->between('total_sell_amount', '累计卖出金额范围');
- $filter->between('last_transaction_at', '最后交易时间')->datetime();
- });
- // 默认排序
- $grid->model()->orderBy('quantity', 'desc');
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<div class="btn-group">
- <button type="button" class="btn btn-sm btn-outline-primary" onclick="showWarehouseStats()">
- <i class="fa fa-pie-chart"></i> 仓库统计
- </button>
- </div>');
- });
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 可以添加查看详细统计的操作
- $actions->append('<a href="javascript:void(0)" onclick="showItemStats('.$actions->getKey().')" class="btn btn-xs btn-outline-info">
- <i class="fa fa-line-chart"></i> 统计
- </a>');
- });
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new MexWarehouseRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('item_id', '商品ID');
-
- $show->divider('库存信息');
- $show->field('quantity', '当前库存');
- $show->field('total_buy_quantity', '累计买入数量');
- $show->field('total_sell_quantity', '累计卖出数量');
- $show->field('net_quantity', '净买入数量')->as(function () {
- return $this->total_buy_quantity - $this->total_sell_quantity;
- });
- $show->divider('金额信息');
- $show->field('total_buy_amount', '累计买入金额');
- $show->field('total_sell_amount', '累计卖出金额');
- $show->field('net_amount', '净买入金额')->as(function () {
- return bcsub($this->total_buy_amount, $this->total_sell_amount, 5);
- });
- $show->divider('价格信息');
- $show->field('average_buy_price', '平均买入价')->as(function () {
- if ($this->total_buy_quantity > 0) {
- return bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
- }
- return '0.00000';
- });
- $show->field('average_sell_price', '平均卖出价')->as(function () {
- if ($this->total_sell_quantity > 0) {
- return bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
- }
- return '0.00000';
- });
-
- $show->field('last_transaction_at', '最后交易时间');
- // 禁用编辑和删除
- $show->disableEditButton();
- $show->disableDeleteButton();
- });
- }
- /**
- * 表单页面(仅用于查看,不允许编辑)
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new MexWarehouseRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->display('item_id', '商品ID');
- $form->display('quantity', '当前库存');
- $form->display('total_buy_quantity', '累计买入数量');
- $form->display('total_sell_quantity', '累计卖出数量');
- $form->display('total_buy_amount', '累计买入金额');
- $form->display('total_sell_amount', '累计卖出金额');
- $form->display('last_transaction_at', '最后交易时间');
- // 禁用保存按钮
- $form->disableSubmitButton();
- $form->disableResetButton();
- });
- }
- }
|