MexWarehouseController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace App\Module\Mex\AdminControllers;
  3. use App\Module\Mex\AdminControllers\Helper\GridHelper;
  4. use App\Module\Mex\Repositories\MexWarehouseRepository;
  5. use App\Module\Fund\Models\FundModel;
  6. use App\Module\Fund\Enums\FUND_TYPE;
  7. use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
  8. use Spatie\RouteAttributes\Attributes\Resource;
  9. use UCore\DcatAdmin\AdminController;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use Dcat\Admin\Widgets\Tab;
  14. /**
  15. * 农贸市场仓库管理
  16. *
  17. * 路由:/admin/mex-warehouse
  18. */
  19. #[Resource('mex-warehouse', names: 'dcat.admin.mex-warehouse')]
  20. class MexWarehouseController extends AdminController
  21. {
  22. /**
  23. * 页面标题
  24. *
  25. * @var string
  26. */
  27. protected $title = '农贸市场仓库';
  28. /**
  29. * 列表页面
  30. *
  31. * @return Tab
  32. */
  33. protected function grid()
  34. {
  35. $tab = new Tab();
  36. // 物品仓库标签页
  37. $tab->add('物品仓库', $this->itemWarehouseGrid());
  38. // 资金仓库标签页
  39. $tab->add('资金仓库', $this->fundWarehouseGrid());
  40. return $tab;
  41. }
  42. /**
  43. * 物品仓库列表
  44. *
  45. * @return Grid
  46. */
  47. protected function itemWarehouseGrid()
  48. {
  49. return Grid::make(new MexWarehouseRepository(), function (Grid $grid) {
  50. $helper = new GridHelper($grid,$this);
  51. $grid->column('id', 'ID')->sortable();
  52. $grid->column('item_id', '商品ID')->display(function ($value) {
  53. return "<a href='" . admin_url("game-items/{$value}") . "' target='_blank'>{$value}</a>";
  54. });
  55. $grid->column('quantity', '当前库存')->display(function ($value) {
  56. return number_format($value);
  57. });
  58. $grid->column('total_buy_quantity', '累计买入数量')->display(function ($value) {
  59. return number_format($value);
  60. });
  61. $grid->column('total_sell_quantity', '累计卖出数量')->display(function ($value) {
  62. return number_format($value);
  63. });
  64. $grid->column('total_buy_amount', '累计买入金额')->display(function ($value) {
  65. return number_format($value, 5);
  66. });
  67. $grid->column('total_sell_amount', '累计卖出金额')->display(function ($value) {
  68. return number_format($value, 5);
  69. });
  70. $grid->column('average_buy_price', '平均买入价')->display(function () {
  71. if ($this->total_buy_quantity > 0) {
  72. $avgPrice = bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
  73. return number_format($avgPrice, 5);
  74. }
  75. return '-';
  76. });
  77. $grid->column('average_sell_price', '平均卖出价')->display(function () {
  78. if ($this->total_sell_quantity > 0) {
  79. $avgPrice = bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
  80. return number_format($avgPrice, 5);
  81. }
  82. return '-';
  83. });
  84. $helper->columnDateTime('last_transaction_at','最后交易时间');
  85. // 禁用新增、编辑和删除
  86. $grid->disableCreateButton();
  87. $grid->disableEditButton();
  88. $grid->disableDeleteButton();
  89. // 筛选器
  90. $grid->filter(function (Grid\Filter $filter) {
  91. $filter->equal('id', 'ID');
  92. $filter->equal('item_id', '商品ID');
  93. $filter->between('quantity', '库存范围');
  94. $filter->between('total_buy_quantity', '累计买入数量范围');
  95. $filter->between('total_sell_quantity', '累计卖出数量范围');
  96. $filter->between('total_buy_amount', '累计买入金额范围');
  97. $filter->between('total_sell_amount', '累计卖出金额范围');
  98. $filter->between('last_transaction_at', '最后交易时间')->datetime();
  99. });
  100. // 默认排序
  101. $grid->model()->orderBy('quantity', 'desc');
  102. // 工具栏
  103. $grid->tools(function (Grid\Tools $tools) {
  104. $tools->append('<div class="btn-group">
  105. <button type="button" class="btn btn-sm btn-outline-primary" onclick="showWarehouseStats()">
  106. <i class="fa fa-pie-chart"></i> 仓库统计
  107. </button>
  108. </div>');
  109. });
  110. // 行操作
  111. $grid->actions(function (Grid\Displayers\Actions $actions) {
  112. // 可以添加查看详细统计的操作
  113. $actions->append('<a href="javascript:void(0)" onclick="showItemStats('.$actions->getKey().')" class="btn btn-xs btn-outline-info">
  114. <i class="fa fa-line-chart"></i> 统计
  115. </a>');
  116. });
  117. });
  118. }
  119. /**
  120. * 详情页面
  121. *
  122. * @param mixed $id
  123. * @return Show
  124. */
  125. protected function detail($id)
  126. {
  127. return Show::make($id, new MexWarehouseRepository(), function (Show $show) {
  128. $show->field('id', 'ID');
  129. $show->field('item_id', '商品ID');
  130. $show->divider('库存信息');
  131. $show->field('quantity', '当前库存');
  132. $show->field('total_buy_quantity', '累计买入数量');
  133. $show->field('total_sell_quantity', '累计卖出数量');
  134. $show->field('net_quantity', '净买入数量')->as(function () {
  135. return $this->total_buy_quantity - $this->total_sell_quantity;
  136. });
  137. $show->divider('金额信息');
  138. $show->field('total_buy_amount', '累计买入金额');
  139. $show->field('total_sell_amount', '累计卖出金额');
  140. $show->field('net_amount', '净买入金额')->as(function () {
  141. return bcsub($this->total_buy_amount, $this->total_sell_amount, 5);
  142. });
  143. $show->divider('价格信息');
  144. $show->field('average_buy_price', '平均买入价')->as(function () {
  145. if ($this->total_buy_quantity > 0) {
  146. return bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
  147. }
  148. return '0.00000';
  149. });
  150. $show->field('average_sell_price', '平均卖出价')->as(function () {
  151. if ($this->total_sell_quantity > 0) {
  152. return bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
  153. }
  154. return '0.00000';
  155. });
  156. $show->field('last_transaction_at', '最后交易时间');
  157. // 禁用编辑和删除
  158. $show->disableEditButton();
  159. $show->disableDeleteButton();
  160. });
  161. }
  162. /**
  163. * 表单页面(仅用于查看,不允许编辑)
  164. *
  165. * @return Form
  166. */
  167. protected function form()
  168. {
  169. return Form::make(new MexWarehouseRepository(), function (Form $form) {
  170. $form->display('id', 'ID');
  171. $form->display('item_id', '商品ID');
  172. $form->display('quantity', '当前库存');
  173. $form->display('total_buy_quantity', '累计买入数量');
  174. $form->display('total_sell_quantity', '累计卖出数量');
  175. $form->display('total_buy_amount', '累计买入金额');
  176. $form->display('total_sell_amount', '累计卖出金额');
  177. $form->display('last_transaction_at', '最后交易时间');
  178. // 禁用保存按钮
  179. $form->disableSubmitButton();
  180. $form->disableResetButton();
  181. });
  182. }
  183. /**
  184. * 资金仓库列表
  185. *
  186. * @return Grid
  187. */
  188. protected function fundWarehouseGrid()
  189. {
  190. // 获取仓库账户ID
  191. $warehouseUserId = config('mex.accounts.warehouse_user_id', 15);
  192. return Grid::make(FundModel::class, function (Grid $grid) use ($warehouseUserId) {
  193. // 只显示仓库账户的资金
  194. $grid->model()->where('user_id', $warehouseUserId);
  195. $grid->column('id', 'ID')->sortable();
  196. $grid->column('user_id', 'user_id');
  197. $grid->column('fund_id', '资金类型')->display(function ($value) {
  198. return self::getFundTypeName($value);
  199. });
  200. $grid->column('balance', '当前余额')->display(function ($value) {
  201. // 根据资金类型获取精度
  202. $precision = self::getFundPrecision($this->fund_id);
  203. return number_format($value, $precision);
  204. });
  205. $grid->column('currency_name', '币种')->display(function () {
  206. return self::getCurrencyName($this->fund_id);
  207. });
  208. $grid->column('balance_formatted', '格式化余额')->display(function () {
  209. $precision = self::getFundPrecision($this->fund_id);
  210. $currencyName = self::getCurrencyName($this->fund_id);
  211. return number_format($this->balance, $precision) . ' ' . $currencyName;
  212. });
  213. $grid->column('update_time', '更新时间')->display(function ($value) {
  214. return date('Y-m-d H:i:s', $value);
  215. });
  216. $grid->column('create_time', '创建时间')->display(function ($value) {
  217. return date('Y-m-d H:i:s', $value);
  218. });
  219. // 禁用新增、编辑和删除
  220. $grid->disableCreateButton();
  221. $grid->disableEditButton();
  222. $grid->disableDeleteButton();
  223. // 筛选器
  224. $grid->filter(function (Grid\Filter $filter) {
  225. $filter->equal('fund_id', '资金类型')->select([
  226. 1 => '金币账户',
  227. 2 => '钻石账户',
  228. 3 => '钻石冻结账户'
  229. ]);
  230. $filter->between('balance', '余额范围');
  231. $filter->between('update_time', '更新时间范围')->datetime();
  232. });
  233. // 默认排序
  234. $grid->model()->orderBy('fund_id', 'asc');
  235. // 工具栏
  236. $grid->tools(function (Grid\Tools $tools) {
  237. $tools->append('<div class="btn-group">
  238. <button type="button" class="btn btn-sm btn-outline-success" onclick="showFundStats()">
  239. <i class="fa fa-money"></i> 资金统计
  240. </button>
  241. </div>');
  242. });
  243. // 行操作
  244. $grid->actions(function (Grid\Displayers\Actions $actions) {
  245. $actions->append('<a href="javascript:void(0)" onclick="showFundDetails('.$actions->getKey().')" class="btn btn-xs btn-outline-info">
  246. <i class="fa fa-info-circle"></i> 详情
  247. </a>');
  248. });
  249. });
  250. }
  251. /**
  252. * 获取资金类型名称
  253. *
  254. * @param mixed $fundId
  255. * @return string
  256. */
  257. private static function getFundTypeName($fundId): string
  258. {
  259. // 如果已经是枚举类型,直接使用
  260. if ($fundId instanceof FUND_TYPE) {
  261. $fundType = $fundId;
  262. } else {
  263. // 否则尝试从int转换
  264. $fundType = FUND_TYPE::tryFrom((int)$fundId);
  265. }
  266. return match($fundType) {
  267. FUND_TYPE::FUND1 => '金币账户',
  268. FUND_TYPE::FUND2 => '钻石账户',
  269. FUND_TYPE::FUND3 => '钻石冻结账户',
  270. default => "未知类型({$fundId})"
  271. };
  272. }
  273. /**
  274. * 获取币种名称
  275. *
  276. * @param mixed $fundId
  277. * @return string
  278. */
  279. private static function getCurrencyName($fundId): string
  280. {
  281. // 如果已经是枚举类型,直接使用
  282. if ($fundId instanceof FUND_TYPE) {
  283. $fundType = $fundId;
  284. } else {
  285. // 否则尝试从int转换
  286. $fundType = FUND_TYPE::tryFrom((int)$fundId);
  287. }
  288. if (!$fundType) {
  289. return '未知币种';
  290. }
  291. $currency = FUND_TYPE::type2Currency($fundType);
  292. return $currency ? $currency->getCurrencyName() : '未知币种';
  293. }
  294. /**
  295. * 获取资金精度
  296. *
  297. * @param mixed $fundId
  298. * @return int
  299. */
  300. private static function getFundPrecision($fundId): int
  301. {
  302. // 如果已经是枚举类型,直接使用
  303. if ($fundId instanceof FUND_TYPE) {
  304. $fundType = $fundId;
  305. } else {
  306. // 否则尝试从int转换
  307. $fundType = FUND_TYPE::tryFrom((int)$fundId);
  308. }
  309. if (!$fundType) {
  310. return 2;
  311. }
  312. $currency = FUND_TYPE::type2Currency($fundType);
  313. return $currency ? $currency->getPrecision() : 2;
  314. }
  315. }