| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- namespace App\Module\Mex\AdminControllers;
- use App\Module\Mex\AdminControllers\Helper\GridHelper;
- use App\Module\Mex\Repositories\MexWarehouseRepository;
- use App\Module\Fund\Models\FundModel;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Widgets\Tab;
- /**
- * 农贸市场仓库管理
- *
- * 路由:/admin/mex-warehouse
- */
- #[Resource('mex-warehouse', names: 'dcat.admin.mex-warehouse')]
- class MexWarehouseController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '农贸市场仓库';
- /**
- * 列表页面
- *
- * @return Tab
- */
- protected function grid()
- {
- $tab = new Tab();
- // 物品仓库标签页
- $tab->add('物品仓库', $this->itemWarehouseGrid());
- // 资金仓库标签页
- $tab->add('资金仓库', $this->fundWarehouseGrid());
- return $tab;
- }
- /**
- * 物品仓库列表
- *
- * @return Grid
- */
- protected function itemWarehouseGrid()
- {
- return Grid::make(new MexWarehouseRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid,$this);
- $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 '-';
- });
- $helper->columnDateTime('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();
- });
- }
- /**
- * 资金仓库列表
- *
- * @return Grid
- */
- protected function fundWarehouseGrid()
- {
- // 获取仓库账户ID
- $warehouseUserId = config('mex.accounts.warehouse_user_id', 15);
- return Grid::make(FundModel::class, function (Grid $grid) use ($warehouseUserId) {
- // 只显示仓库账户的资金
- $grid->model()->where('user_id', $warehouseUserId);
- $grid->column('id', 'ID')->sortable();
- $grid->column('user_id', 'user_id');
- $grid->column('fund_id', '资金类型')->display(function ($value) {
- return self::getFundTypeName($value);
- });
- $grid->column('balance', '当前余额')->display(function ($value) {
- // 根据资金类型获取精度
- $precision = self::getFundPrecision($this->fund_id);
- return number_format($value, $precision);
- });
- $grid->column('currency_name', '币种')->display(function () {
- return self::getCurrencyName($this->fund_id);
- });
- $grid->column('balance_formatted', '格式化余额')->display(function () {
- $precision = self::getFundPrecision($this->fund_id);
- $currencyName = self::getCurrencyName($this->fund_id);
- return number_format($this->balance, $precision) . ' ' . $currencyName;
- });
- $grid->column('update_time', '更新时间')->display(function ($value) {
- return date('Y-m-d H:i:s', $value);
- });
- $grid->column('create_time', '创建时间')->display(function ($value) {
- return date('Y-m-d H:i:s', $value);
- });
- // 禁用新增、编辑和删除
- $grid->disableCreateButton();
- $grid->disableEditButton();
- $grid->disableDeleteButton();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('fund_id', '资金类型')->select([
- 1 => '金币账户',
- 2 => '钻石账户',
- 3 => '钻石冻结账户'
- ]);
- $filter->between('balance', '余额范围');
- $filter->between('update_time', '更新时间范围')->datetime();
- });
- // 默认排序
- $grid->model()->orderBy('fund_id', 'asc');
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<div class="btn-group">
- <button type="button" class="btn btn-sm btn-outline-success" onclick="showFundStats()">
- <i class="fa fa-money"></i> 资金统计
- </button>
- </div>');
- });
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->append('<a href="javascript:void(0)" onclick="showFundDetails('.$actions->getKey().')" class="btn btn-xs btn-outline-info">
- <i class="fa fa-info-circle"></i> 详情
- </a>');
- });
- });
- }
- /**
- * 获取资金类型名称
- *
- * @param mixed $fundId
- * @return string
- */
- private static function getFundTypeName($fundId): string
- {
- // 如果已经是枚举类型,直接使用
- if ($fundId instanceof FUND_TYPE) {
- $fundType = $fundId;
- } else {
- // 否则尝试从int转换
- $fundType = FUND_TYPE::tryFrom((int)$fundId);
- }
- return match($fundType) {
- FUND_TYPE::FUND1 => '金币账户',
- FUND_TYPE::FUND2 => '钻石账户',
- FUND_TYPE::FUND3 => '钻石冻结账户',
- default => "未知类型({$fundId})"
- };
- }
- /**
- * 获取币种名称
- *
- * @param mixed $fundId
- * @return string
- */
- private static function getCurrencyName($fundId): string
- {
- // 如果已经是枚举类型,直接使用
- if ($fundId instanceof FUND_TYPE) {
- $fundType = $fundId;
- } else {
- // 否则尝试从int转换
- $fundType = FUND_TYPE::tryFrom((int)$fundId);
- }
- if (!$fundType) {
- return '未知币种';
- }
- $currency = FUND_TYPE::type2Currency($fundType);
- return $currency ? $currency->getCurrencyName() : '未知币种';
- }
- /**
- * 获取资金精度
- *
- * @param mixed $fundId
- * @return int
- */
- private static function getFundPrecision($fundId): int
- {
- // 如果已经是枚举类型,直接使用
- if ($fundId instanceof FUND_TYPE) {
- $fundType = $fundId;
- } else {
- // 否则尝试从int转换
- $fundType = FUND_TYPE::tryFrom((int)$fundId);
- }
- if (!$fundType) {
- return 2;
- }
- $currency = FUND_TYPE::type2Currency($fundType);
- return $currency ? $currency->getPrecision() : 2;
- }
- }
|