|
|
@@ -0,0 +1,197 @@
|
|
|
+# Mex模块移除精度处理,信任Fund模块数据
|
|
|
+
|
|
|
+**任务时间**: 2025年06月13日 16:22:54 CST
|
|
|
+**任务内容**: 根据用户要求,Mex模块不应自行处理Fund模块的数值精度,应信任Fund模块的数据,数额验证应使用Fund模块的Validator
|
|
|
+
|
|
|
+## 1. 任务背景
|
|
|
+
|
|
|
+用户指出Mex模块不应该自行处理Fund模块的数值精度,应该信任Fund模块的数据处理。原有代码中存在以下问题:
|
|
|
+
|
|
|
+- AddHandler中使用`number_format($price, 5, '.', '')`进行精度处理
|
|
|
+- MexOrderLogic中使用`bcmul($price, $quantity, 5)`进行精度计算
|
|
|
+- MexOrderValidator中使用`bcpow`和`bcmul`进行精度转换
|
|
|
+- 这些精度处理与Fund模块的处理逻辑重复,可能导致不一致
|
|
|
+
|
|
|
+## 2. 修改内容
|
|
|
+
|
|
|
+### 2.1 AddHandler修改
|
|
|
+
|
|
|
+**文件**: `app/Module/AppGame/Handler/Matchexchange/AddHandler.php`
|
|
|
+
|
|
|
+**修改内容**:
|
|
|
+- 移除`processBuyOrder`方法中的`number_format($price, 5, '.', '')`
|
|
|
+- 移除`processSellOrder`方法中的`number_format($price, 5, '.', '')`
|
|
|
+- 直接传递原始价格值给服务层
|
|
|
+
|
|
|
+**修改前**:
|
|
|
+```php
|
|
|
+$result = MexOrderService::createBuyOrder(
|
|
|
+ $userId,
|
|
|
+ $itemId,
|
|
|
+ $quantity,
|
|
|
+ number_format($price, 5, '.', ''),
|
|
|
+ $currencyType
|
|
|
+);
|
|
|
+```
|
|
|
+
|
|
|
+**修改后**:
|
|
|
+```php
|
|
|
+$result = MexOrderService::createBuyOrder(
|
|
|
+ $userId,
|
|
|
+ $itemId,
|
|
|
+ $quantity,
|
|
|
+ $price,
|
|
|
+ $currencyType
|
|
|
+);
|
|
|
+```
|
|
|
+
|
|
|
+### 2.2 MexOrderService修改
|
|
|
+
|
|
|
+**文件**: `app/Module/Mex/Services/MexOrderService.php`
|
|
|
+
|
|
|
+**修改内容**:
|
|
|
+- 将价格参数类型从`string`改为`float`
|
|
|
+- 更新注释说明信任Fund模块的数据处理
|
|
|
+
|
|
|
+**修改前**:
|
|
|
+```php
|
|
|
+public static function createSellOrder(int $userId, int $itemId, int $quantity, string $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
|
|
|
+```
|
|
|
+
|
|
|
+**修改后**:
|
|
|
+```php
|
|
|
+public static function createSellOrder(int $userId, int $itemId, int $quantity, float $price, ?FUND_CURRENCY_TYPE $currencyType = null): array
|
|
|
+```
|
|
|
+
|
|
|
+### 2.3 MexOrderLogic修改
|
|
|
+
|
|
|
+**文件**: `app/Module/Mex/Logic/MexOrderLogic.php`
|
|
|
+
|
|
|
+**修改内容**:
|
|
|
+- 将价格参数类型从`string`改为`float`
|
|
|
+- 移除`bcmul($price, $quantity, 5)`精度计算
|
|
|
+- 改为直接使用`$price * $quantity`计算总金额
|
|
|
+
|
|
|
+**修改前**:
|
|
|
+```php
|
|
|
+$totalAmount = bcmul($price, $quantity, 5);
|
|
|
+```
|
|
|
+
|
|
|
+**修改后**:
|
|
|
+```php
|
|
|
+// 直接计算总金额,信任Fund模块的精度处理
|
|
|
+$totalAmount = $price * $quantity;
|
|
|
+```
|
|
|
+
|
|
|
+### 2.4 MexOrderValidator修改
|
|
|
+
|
|
|
+**文件**: `app/Module/Mex/Validators/MexOrderValidator.php`
|
|
|
+
|
|
|
+**修改内容**:
|
|
|
+- 移除复杂的`bcpow`和`bcmul`精度转换
|
|
|
+- 直接使用Fund模块的FundService进行余额比较
|
|
|
+- 简化验证逻辑,信任Fund模块的数据处理
|
|
|
+
|
|
|
+**修改前**:
|
|
|
+```php
|
|
|
+// 根据币种精度转换所需金额
|
|
|
+$precision = $currencyType->getPrecision();
|
|
|
+$requiredAmountInStorage = (int)bcmul($requiredAmount, bcpow('10', $precision), 0);
|
|
|
+
|
|
|
+if ($balance < $requiredAmountInStorage) {
|
|
|
+ // 转换余额为显示格式
|
|
|
+ $balanceDisplay = bcdiv($balance, bcpow('10', $precision), $precision);
|
|
|
+ $this->addError("资金不足,当前{$currencyType->name}余额 {$balanceDisplay},需要 {$requiredAmount}");
|
|
|
+ return false;
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+**修改后**:
|
|
|
+```php
|
|
|
+// 使用Fund模块的FundService进行资金验证,信任Fund模块的数据处理
|
|
|
+$fundService = new FundService($userId, $availableAccountType->value);
|
|
|
+$balance = $fundService->balance();
|
|
|
+
|
|
|
+// 直接比较,不进行精度转换,信任Fund模块的数据处理
|
|
|
+if ($balance < $requiredAmount) {
|
|
|
+ $this->addError("资金不足,当前{$currencyType->name}余额 {$balance},需要 {$requiredAmount}");
|
|
|
+ return false;
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+## 3. 技术优化
|
|
|
+
|
|
|
+### 3.1 代码简化
|
|
|
+- 移除了复杂的精度转换逻辑
|
|
|
+- 减少了bcmath函数的使用
|
|
|
+- 统一了数据类型(float)
|
|
|
+- 简化了错误信息显示
|
|
|
+
|
|
|
+### 3.2 架构改进
|
|
|
+- 明确了模块职责边界
|
|
|
+- Mex模块专注于业务逻辑,不处理数值精度
|
|
|
+- Fund模块负责所有资金相关的精度处理
|
|
|
+- 避免了重复的精度处理逻辑
|
|
|
+
|
|
|
+### 3.3 维护性提升
|
|
|
+- 减少了代码复杂度
|
|
|
+- 降低了模块间的耦合
|
|
|
+- 提高了代码的可读性
|
|
|
+- 便于后续维护和扩展
|
|
|
+
|
|
|
+## 4. 影响范围
|
|
|
+
|
|
|
+### 4.1 修改文件清单
|
|
|
+- `app/Module/AppGame/Handler/Matchexchange/AddHandler.php`
|
|
|
+- `app/Module/Mex/Services/MexOrderService.php`
|
|
|
+- `app/Module/Mex/Logic/MexOrderLogic.php`
|
|
|
+- `app/Module/Mex/Validators/MexOrderValidator.php`
|
|
|
+- `AiWork/记忆习惯.md`
|
|
|
+
|
|
|
+### 4.2 功能影响
|
|
|
+- 挂单功能:价格传递更直接,减少精度损失
|
|
|
+- 资金验证:使用Fund模块的标准验证方式
|
|
|
+- 数据一致性:避免了多重精度处理导致的不一致
|
|
|
+
|
|
|
+## 5. 测试验证
|
|
|
+
|
|
|
+### 5.1 语法检查
|
|
|
+- ✅ AddHandler.php 语法正确
|
|
|
+- ✅ MexOrderService.php 语法正确
|
|
|
+- ✅ MexOrderLogic.php 语法正确
|
|
|
+- ✅ MexOrderValidator.php 语法正确
|
|
|
+
|
|
|
+### 5.2 代码质量
|
|
|
+- 移除了未使用的导入
|
|
|
+- 简化了方法实现
|
|
|
+- 提高了代码可读性
|
|
|
+- 减少了IDE警告
|
|
|
+
|
|
|
+## 6. 设计原则
|
|
|
+
|
|
|
+### 6.1 信任原则
|
|
|
+- Mex模块信任Fund模块的数据处理
|
|
|
+- 不重复实现已有的精度处理逻辑
|
|
|
+- 直接使用Fund模块提供的服务
|
|
|
+
|
|
|
+### 6.2 职责分离
|
|
|
+- Fund模块:负责资金精度处理和验证
|
|
|
+- Mex模块:负责交易业务逻辑
|
|
|
+- 避免跨模块的重复实现
|
|
|
+
|
|
|
+### 6.3 简化原则
|
|
|
+- 移除不必要的复杂性
|
|
|
+- 使用最直接的数据传递方式
|
|
|
+- 减少中间转换步骤
|
|
|
+
|
|
|
+## 7. 总结
|
|
|
+
|
|
|
+成功完成了Mex模块精度处理的简化工作:
|
|
|
+
|
|
|
+1. **移除重复逻辑**:删除了Mex模块中的精度处理代码
|
|
|
+2. **信任Fund模块**:直接使用Fund模块的数据和服务
|
|
|
+3. **简化数据流**:统一使用float类型传递价格
|
|
|
+4. **提升维护性**:减少了代码复杂度和模块耦合
|
|
|
+5. **保持功能**:确保业务功能不受影响
|
|
|
+
|
|
|
+Mex模块现在更专注于交易业务逻辑,而将数值精度处理完全交给Fund模块处理,符合单一职责原则和模块化设计理念。
|