13日1530-Fund模块币种ID概念修复.md 4.9 KB

Fund模块币种ID概念修复

任务时间: 2025年06月13日 15:30:00 CST
任务内容: 明确Fund模块中'币种ID'概念,修复混淆币种ID和数据库表ID的错误

1. 问题背景

Fund模块中存在概念混淆:

  • 币种ID:应该是 FUND_CURRENCY_TYPE 枚举的 value(1=金币, 2=钻石, 3=人民币, 4=美元)
  • 数据库表ID:是 fund_currency 表的自增主键 id

多个地方错误地使用了数据库表ID而不是币种ID,导致逻辑错误。

2. 主要修复

2.1 FUND_TYPE枚举修复

修复前

$map = [
    self::FUND1->value() => FUND_CURRENCY_TYPE::JINBI,
    self::FUND2->value() => FUND_CURRENCY_TYPE::ZUANSHI,
    self::FUND2->value() => FUND_CURRENCY_TYPE::ZUANSHI  // 重复映射错误
];

修复后

$map = [
    self::FUND1->value() => FUND_CURRENCY_TYPE::JINBI,    // 金币账户 -> 金币币种
    self::FUND2->value() => FUND_CURRENCY_TYPE::ZUANSHI,  // 钻石账户 -> 钻石币种
    self::FUND3->value() => FUND_CURRENCY_TYPE::ZUANSHI   // 钻石冻结账户 -> 钻石币种
];

2.2 CurrencyDto增强

新增字段

  • $id:数据库表ID
  • $currencyId:币种ID(枚举值)

fromModel方法更新

$dto->id = $model->id;  // 数据库表ID
$dto->currencyId = $model->type ? $model->type->value : 0;  // 币种ID(枚举值)

2.3 CurrencyService修复

check方法

// 修复前:使用数据库表ID比较
return $currency1->id === $currency2->id;

// 修复后:使用币种ID比较
return $currency1->currencyId === $currency2->currencyId;

getCurrencyByFundId方法

// 修复前:错误使用数据库表ID查找
$currency = FundCurrencyModel::find($currencyEnum->value());

// 修复后:通过币种枚举查找
$currency = FundCurrencyModel::where('type', $currencyEnum)->first();

getCurrencyPrecision方法

// 修复前:通过数据库表ID查找
$currency = FundCurrencyModel::find($currency_id);

// 修复后:直接通过币种ID获取精度
$currencyEnum = FUND_CURRENCY_TYPE::tryFrom($currency_id);
return $currencyEnum->getPrecision();

formatAmount方法

// 修复前:使用数据库表ID查找
$currency = FundCurrencyModel::find($currency_id);

// 修复后:通过币种ID查找
$currencyEnum = FUND_CURRENCY_TYPE::tryFrom($currency_id);
$currency = FundCurrencyModel::where('type', $currencyEnum)->first();

2.4 FundService修复

所有调用CurrencyService的地方都修复为使用币种ID:

// 修复前:使用数据库表ID
CurrencyService::validateAmountPrecision($amount, $currency->id)

// 修复后:使用币种ID
CurrencyService::validateAmountPrecision($amount, $currency->currencyId)

3. 概念明确

3.1 币种ID映射

币种名称 币种ID(枚举值) 数据库表ID 标识
金币 1 1 GOLD
钻石 2 2 OWG
人民币 3 3 CNY
美元 4 4 USD

3.2 账户到币种映射

账户类型 账户ID 币种ID 币种名称
FUND1 1 1 金币
FUND2 2 2 钻石
FUND3 3 2 钻石

3.3 使用原则

  • 对外接口:使用币种ID(枚举值)
  • 内部存储:数据库表ID仅用于关联查询
  • 业务逻辑:统一使用币种ID进行比较和计算

4. 验证结果

创建了专门的验证命令 php artisan fund:verify-currency-id

币种ID概念正确:1=金币, 2=钻石, 3=人民币, 4=美元
账户映射正确:FUND1→金币, FUND2→钻石, FUND3→钻石
币种一致性检查正确:金币≠钻石, 钻石=钻石冻结
精度获取正确:金币0位, 钻石10位, 人民币/美元2位
格式化显示正确:使用正确的币种标识和精度

5. 影响范围

5.1 修复的文件

  • app/Module/Fund/Enums/FUND_TYPE.php
  • app/Module/Fund/Dto/CurrencyDto.php
  • app/Module/Fund/Services/CurrencyService.php
  • app/Module/Fund/Services/FundService.php

5.2 新增的验证

  • app/Console/Commands/VerifyFundCurrencyId.php

5.3 文档更新

  • AiWork/记忆习惯.md

6. 总结

成功修复了Fund模块中币种ID概念混淆的问题:

  1. 明确了概念:币种ID = FUND_CURRENCY_TYPE枚举值,不是数据库表ID
  2. 修复了逻辑错误:所有币种比较和查找都使用正确的币种ID
  3. 增强了数据结构:CurrencyDto同时包含数据库表ID和币种ID
  4. 完善了验证:创建专门的验证命令确保修复正确
  5. 更新了文档:在记忆习惯中明确记录币种ID概念

Fund模块现在拥有了清晰、正确的币种ID概念和实现!