hi-server/.trae/documents/排查并修复订单价格与折扣未生效问题.md
shanshanzhong 9987bd43fa
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m53s
fix(订单): 修复折扣计算问题并添加四舍五入处理
统一处理百分比和系数两种折扣输入方式,增加边界保护
在金额计算中使用math.Round进行四舍五入处理
添加相关单元测试确保计算准确性
2025-12-02 02:22:09 -08:00

31 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 问题确认
- 前端以“百分比”形式配置折扣(如 95、95.19),当前后端 `getDiscount` 函数期望“系数01导致折扣未生效。
- 受影响位置:
- 登录下单折扣:`internal/logic/public/order/getDiscount.go`
- 门户预下单/下单折扣:`internal/logic/public/portal/tool.go:getDiscount`
## 改造目标
- 后端折扣计算统一兼容两种输入:
- 系数01直接使用
- 百分比(>1 且 <=100自动转换为 `值/100` 再使用
- 对非法值进行边界保护(<0 0>100 → 忽略或按 1 处理),避免异常。
## 实施步骤
1. 修改折扣计算函数:
- `internal/logic/public/order/getDiscount.go`
-`discount.Discount > 1 && discount.Discount <= 100`,转换为 `discount.Discount/100`
- 保持“取满足阈值的最小折扣”策略,默认 `finalDiscount=1.0`
- `internal/logic/public/portal/tool.go:getDiscount`
- 同上逻辑,移除对 `*100` 的中间整数化处理,统一用浮点小数系数比较。
2. 单元测试补充:
- 既测系数(如 0.95),也测百分比输入(如 95、95.19以及边界0、100、>100
3. 验证流程:
- 用你当前 7 天的配置(前端百分比)进行“预下单→下单→订单查询”,确认 `Discount``Amount` 按预期生效。
4. 文档与界面提示:
- 在后台/前端表单处增加说明:支持百分比与系数;百分比将自动转换;推荐使用百分比,避免歧义。
## 交付与保障
- 代码改动仅限折扣计算函数与测试,风险低;保留原有行为的向后兼容。
- 提供测试报告与一次联调记录(数据截图:价格、折扣、总计)。
请确认是否按该兼容方案执行,我将据此修改并验证。