Explorar o código

feat(fund): 实现账户种类配置模块

- 新增 FundConfigController 控制器,实现账户种类配置的 CRUD 功能
- 更新 GenerateFundCurrencyConfigJson 命令,使用数据库中的 currency_id- 修改 FundConfigModel 和 FundCurrencyModel,增加关联关系
- 新增 FundConfigRepository 仓库类,提供账户种类配置数据的访问和操作功能
- 更新 CurrencyService服务类,使用 FundConfigModel 中的 currency_id
- 更新币种与账户关系 文档,说明币种与账户种类的关系实现方式
Your Name hai 8 meses
pai
achega
52ec8acffb

+ 136 - 0
app/Module/Fund/AdminControllers/FundConfigController.php

@@ -0,0 +1,136 @@
+<?php
+
+namespace App\Module\Fund\AdminControllers;
+
+use App\Module\Fund\AdminControllers\Helper\FormHelper;
+use App\Module\Fund\AdminControllers\Helper\GridHelper;
+use App\Module\Fund\AdminControllers\Helper\ShowHelper;
+use App\Module\Fund\AdminControllers\Tools\SyncFundCurrencyJsonTool;
+use App\Module\Fund\Repositorys\FundConfigRepository;
+use App\Module\Fund\Repositorys\FundCurrencyRepository;
+use Dcat\Admin\Form;
+use Dcat\Admin\Grid;
+use Dcat\Admin\Show;
+use UCore\DcatAdmin\AdminController;
+use Spatie\RouteAttributes\Attributes\Resource;
+
+/**
+ * 账户种类配置控制器
+ */
+#[Resource('fund-configs', names: 'dcat.admin.fund-configs')]
+class FundConfigController extends AdminController
+{
+    /**
+     * 页面标题
+     *
+     * @var string
+     */
+    protected $title = '账户种类配置';
+
+    /**
+     * 列表页
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+        return Grid::make(new FundConfigRepository(), function (Grid $grid) {
+            $helper = new GridHelper($grid, $this);
+            
+            // 添加工具按钮
+            $grid->tools([
+                new SyncFundCurrencyJsonTool()
+            ]);
+            
+            $helper->columnId();
+            $grid->column('name', '账户种类名称')->sortable();
+            
+            $grid->column('currency.name', '关联币种')->sortable();
+            
+            $grid->column('create_time', '创建时间')->display(function ($value) {
+                return date('Y-m-d H:i:s', $value);
+            })->sortable();
+            
+            $grid->column('update_time', '更新时间')->display(function ($value) {
+                return date('Y-m-d H:i:s', $value);
+            })->sortable();
+
+            // 筛选器
+            $grid->filter(function (Grid\Filter $filter) {
+                $filter->equal('id', 'ID');
+                $filter->like('name', '账户种类名称');
+                
+                // 获取所有币种作为筛选选项
+                $currencyRepository = new FundCurrencyRepository();
+                $currencies = $currencyRepository->all()->pluck('name', 'id')->toArray();
+                $filter->equal('currency_id', '关联币种')->select($currencies);
+            });
+        });
+    }
+
+    /**
+     * 详情页
+     *
+     * @param mixed $id
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        return Show::make($id, new FundConfigRepository(), function (Show $show) {
+            $helper = new ShowHelper($show, $this);
+            
+            $show->field('id', 'ID');
+            $show->field('name', '账户种类名称');
+            $show->field('currency.name', '关联币种');
+            $show->field('create_time', '创建时间')->as(function ($time) {
+                return date('Y-m-d H:i:s', $time);
+            });
+            $show->field('update_time', '更新时间')->as(function ($time) {
+                return date('Y-m-d H:i:s', $time);
+            });
+        });
+    }
+
+    /**
+     * 表单
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        return Form::make(new FundConfigRepository(), function (Form $form) {
+            $helper = new FormHelper($form, $this);
+            
+            $form->display('id', 'ID');
+            
+            $form->text('name', '账户种类名称')
+                ->required()
+                ->maxLength(30)
+                ->help('账户种类的名称,如"可用美元账户"、"冻结美元账户"等');
+            
+            // 获取所有币种作为选项
+            $currencyRepository = new FundCurrencyRepository();
+            $currencies = $currencyRepository->all()->pluck('name', 'id')->toArray();
+            
+            $form->select('currency_id', '关联币种')
+                ->options($currencies)
+                ->required()
+                ->help('该账户种类关联的币种');
+            
+            // 保存前处理
+            $form->saving(function (Form $form) {
+                // 设置时间戳
+                if ($form->isCreating()) {
+                    $form->create_time = time();
+                }
+                $form->update_time = time();
+            });
+            
+            // 保存后处理
+            $form->saved(function (Form $form) {
+                // 提示用户更新JSON配置
+                admin_toastr('账户种类配置已保存,请点击"生成JSON"按钮更新配置文件', 'info');
+            });
+        });
+    }
+}

+ 2 - 13
app/Module/Fund/Commands/GenerateFundCurrencyConfigJson.php

@@ -73,22 +73,11 @@ class GenerateFundCurrencyConfigJson extends Command
 
             // 处理账户种类数据
             foreach ($fundConfigs as $fundConfig) {
-                // 从账户种类名称中提取币种信息
-                // 假设账户种类名称格式为"可用美元账户"、"冻结美元账户"等
-                // 这里我们通过名称匹配来关联币种和账户种类
-                $currencyId = null;
-                foreach ($currencies as $currency) {
-                    // 检查账户种类名称中是否包含币种名称
-                    if (strpos($fundConfig->name, $currency->name) !== false) {
-                        $currencyId = $currency->id;
-                        break;
-                    }
-                }
-
+                // 直接使用数据库中存储的currency_id字段
                 $fundConfigData = [
                     'id' => $fundConfig->id,
                     'name' => $fundConfig->name,
-                    'currency_id' => $currencyId, // 关联的币种ID
+                    'currency_id' => $fundConfig->currency_id, // 关联的币种ID
                 ];
 
                 $jsonData['fund_configs'][] = $fundConfigData;

+ 21 - 8
app/Module/Fund/Docs/币种与账户关系.md

@@ -50,16 +50,20 @@
 
 ### 3.1 币种与账户种类的关系
 
-一个币种可以对应多个账户种类,例如:
+一个币种可以对应多个账户种类,这是通过 `fund_config` 表中的 `currency_id` 字段实现的。例如:
 
-- 美元(币种)可以有:
-  - 可用美元账户(账户种类)
-  - 冻结美元账户(账户种类)
+- 美元(币种,ID=1)可以有:
+  - 可用美元账户(账户种类,currency_id=1
+  - 冻结美元账户(账户种类,currency_id=1
 
-- 比特币(币种)可以有:
-  - 可用比特币账户(账户种类)
-  - 冻结比特币账户(账户种类)
-  - 质押比特币账户(账户种类)
+- 比特币(币种,ID=2)可以有:
+  - 可用比特币账户(账户种类,currency_id=2)
+  - 冻结比特币账户(账户种类,currency_id=2)
+  - 质押比特币账户(账户种类,currency_id=2)
+
+在代码中,这种关系通过模型关联方法实现:
+- `FundCurrencyModel::fundConfigs()` - 获取币种的所有账户种类
+- `FundConfigModel::currency()` - 获取账户种类关联的币种
 
 ### 3.2 账户种类与用户账户的关系
 
@@ -90,6 +94,7 @@
 |------|------|------|
 | id | int | 主键 |
 | name | varchar(30) | 账户种类名称 |
+| currency_id | int | 关联的币种ID,外键关联fund_currency表 |
 | create_time | int | 创建时间 |
 | update_time | int | 更新时间 |
 
@@ -141,6 +146,14 @@ $fundConfigs = FundConfigModel::all();
 
 // 获取账户种类名称列表
 $fundConfigNames = AccountService::getFundsDesc();
+
+// 获取特定币种的账户种类
+$currencyId = 1; // 币种ID
+$fundConfigs = FundConfigModel::where('currency_id', $currencyId)->get();
+
+// 通过币种关联获取账户种类
+$currency = FundCurrencyModel::find($currencyId);
+$fundConfigs = $currency->fundConfigs;
 ```
 
 ### 6.3 获取用户账户

+ 14 - 3
app/Module/Fund/Models/FundConfigModel.php

@@ -10,11 +10,11 @@ use UCore\ModelCore;
  * 用于存储系统支持的各种账户种类,如可用账户、冻结账户等。
  * 同一个币种(FundCurrencyModel)可以有多个不同的账户种类。
  *
- * field start 
+ * field start
  * @property   int  $id  自增
  * @property   string  $name  资金名字
  * @property   int  $currency_id  关联的币种ID,外键关联kku_fund_currency表
- * @property   int  $create_time  
+ * @property   int  $create_time
  * @property   int  $update_time  更新时间
  * field end
  */
@@ -22,7 +22,7 @@ class FundConfigModel extends ModelCore
 {
     protected $table = 'fund_config';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'name',
@@ -33,4 +33,15 @@ class FundConfigModel extends ModelCore
     // attrlist end
     public $timestamps = false;
 
+    /**
+     * 获取关联的币种
+     *
+     * 一个账户种类属于一个币种,这是一对多关系中的"多"一方
+     *
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     */
+    public function currency()
+    {
+        return $this->belongsTo(FundCurrencyModel::class, 'currency_id');
+    }
 }

+ 14 - 3
app/Module/Fund/Models/FundCurrencyModel.php

@@ -10,13 +10,13 @@ use UCore\ModelCore;
  * 用于存储系统支持的各种币种信息,如美元、人民币、比特币等。
  * 一个币种可以对应多个账户种类(FundConfigModel)。
  *
- * field start 
+ * field start
  * @property   int  $id  自增
  * @property   string  $identification  资金标识
  * @property   string  $icon  资金标识
  * @property   string  $name  资金名字
  * @property   string  $data1  数据
- * @property   int  $create_time  
+ * @property   int  $create_time
  * @property   int  $update_time  更新时间
  * field end
  */
@@ -24,7 +24,7 @@ class FundCurrencyModel extends ModelCore
 {
     protected $table = 'fund_currency';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'identification',
@@ -37,4 +37,15 @@ class FundCurrencyModel extends ModelCore
     // attrlist end
     public $timestamps = false;
 
+    /**
+     * 获取该币种的所有账户种类
+     *
+     * 一个币种可以有多个账户种类,这是一对多关系中的"一"一方
+     *
+     * @return \Illuminate\Database\Eloquent\Relations\HasMany
+     */
+    public function fundConfigs()
+    {
+        return $this->hasMany(FundConfigModel::class, 'currency_id');
+    }
 }

+ 60 - 0
app/Module/Fund/Repositorys/FundConfigRepository.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace App\Module\Fund\Repositorys;
+
+use App\Module\Fund\Models\FundConfigModel;
+use Dcat\Admin\Repositories\EloquentRepository;
+
+/**
+ * 账户种类配置仓库
+ * 
+ * 提供账户种类配置数据的访问和操作功能。
+ * 该类是账户种类配置模块与后台管理系统的桥梁,用于处理账户种类配置数据的CRUD操作。
+ */
+class FundConfigRepository extends EloquentRepository
+{
+    /**
+     * 关联的模型类
+     *
+     * @var string
+     */
+    protected $eloquentClass = FundConfigModel::class;
+
+    /**
+     * 根据币种ID获取账户种类列表
+     *
+     * @param int $currencyId
+     * @return \Illuminate\Database\Eloquent\Collection
+     */
+    public function findByCurrencyId(int $currencyId)
+    {
+        return FundConfigModel::where('currency_id', $currencyId)->get();
+    }
+
+    /**
+     * 获取所有账户种类,按币种分组
+     *
+     * @return array
+     */
+    public function getGroupedByFundCurrency()
+    {
+        $result = [];
+        $configs = FundConfigModel::with('currency')->get();
+        
+        foreach ($configs as $config) {
+            $currencyId = $config->currency_id ?? 0;
+            $currencyName = $config->currency ? $config->currency->name : '未分类';
+            
+            if (!isset($result[$currencyId])) {
+                $result[$currencyId] = [
+                    'currency_name' => $currencyName,
+                    'configs' => []
+                ];
+            }
+            
+            $result[$currencyId]['configs'][] = $config;
+        }
+        
+        return $result;
+    }
+}

+ 0 - 2
app/Module/Fund/Services/CurrencyService.php

@@ -58,8 +58,6 @@ class CurrencyService
         }
 
         // 从账户种类信息中获取币种ID
-        // 注意:这里假设FundConfigModel中有currency_id字段
-        // 如果实际结构不同,需要根据实际情况调整
         $currencyId = $fundConfig->currency_id ?? null;
         if (!$currencyId) {
             return null;