mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add region indexing and rules validation for freight templates
- Implemented RegionIndex to manage region codes and their relationships from embedded region.json. - Added ValidateRulesJSON function to validate freight rules against specified criteria, including region code existence and overlap checks. - Created unit tests for rules validation covering various scenarios including valid rules, overlap detection, and error handling for invalid inputs. - Updated freight_template_server to integrate rules validation during freight template creation and updates, ensuring compliance with the new rules structure.
This commit is contained in:
parent
4e010a6476
commit
eeb3efa996
441
docs/admin-freight-template.md
Normal file
441
docs/admin-freight-template.md
Normal file
@ -0,0 +1,441 @@
|
|||||||
|
# 管理端运费模板(快递发货)
|
||||||
|
|
||||||
|
**Status:** Backend rules validation **Implemented**;admin-web / 小程序 UI **未做**(另排期)。
|
||||||
|
**Scope(已落地后端):** 本服务对 `pricing_mode=2` 的 `rules_json` 做 schema + 区域存在性 + 父子 overlap 校验;区域索引来自嵌入的 [`internal/freight/region.json`](../internal/freight/region.json)。
|
||||||
|
**Scope(文档约定、UI 未实现):** `ids-admin-web` 列表 / 创建 / 编辑 / 删除;小程序结算。
|
||||||
|
**数据落点:** 表 `product.freight_templates` + Admin API(`/admin/v1/freight-templates`)→ Product gRPC。
|
||||||
|
**Depends on:** [admin-product-create-edit-phase1.md](../../ids-admin-api/docs/admin-product-create-edit-phase1.md) §2 运费模板 HTTP。
|
||||||
|
|
||||||
|
**归属判断:** 运费模板是商品销售配置主数据,且商品只持有 `freight_template_id` 引用;因此模板 CRUD、店铺作用域、启停、删除引用检查、`rules_json` schema 校验归 Product 是合适的。Fulfillment / Quote / Checkout 后续只消费已校验规则并执行计价,不作为模板主数据源。
|
||||||
|
|
||||||
|
**非目标(本期):**
|
||||||
|
|
||||||
|
- **上门自提**(页签见 §2.3:展示且 disabled,不做配置与存贮)
|
||||||
|
- 下单 / Quote 按模板实时计价(BFF 现网仍可能返回 `FREIGHT_TEMPLATE_UNSUPPORTED`;计价公式见 §3.3,供后续实现)
|
||||||
|
- admin-web / 小程序页面
|
||||||
|
- 微信物流助手、电子面单
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 0. 结论(先读)
|
||||||
|
|
||||||
|
| # | 结论 |
|
||||||
|
| --- | --- |
|
||||||
|
| 1 | **后端 CRUD 已就绪**;缺口在 admin-web:无列表页、无创建/编辑表单、商品侧只能手输模板 ID。 |
|
||||||
|
| 2 | **UI 对齐附图「快递发货」:** 模板名称、计费方式(按件 / 按重)、配送区域表(首件/首重、运费、续件/续重、续费)、「指定可配送区域和运费」;空表初始态见表头 +「没有更多数据了」。 |
|
||||||
|
| 3 | **创建/更新一律走 `pricing_mode = 2`**,规则写入 `rules_json`(见 §3);`pricing_mode = 1`(统一运费)本期管理端不提供新建入口。 |
|
||||||
|
| 4 | **区域数据:** 源文件 [`internal/freight/region.json`](../internal/freight/region.json)(`go:embed` 校验用);admin-web 开发/构建时复制到 `src/assets/region.json`;不做区域后台接口。 |
|
||||||
|
| 5 | **上门自提本期不做**;为对齐附图,页签**显示且 disabled**,标注「即将支持」。 |
|
||||||
|
| 6 | **本服务已校验 `rules_json`(create/update)**:version/delivery/charge_type、至少一行规则、`region_codes` 字符串且落在 region.json、行内/行间父子 overlap、按件整数/按重≤3 位小数、`region_paths` 必填等长。Admin API 透传 `InvalidArgument`。 |
|
||||||
|
|
||||||
|
```text
|
||||||
|
列表 → 新建/编辑页(快递)→ 选区(region.json) + 填首续费
|
||||||
|
→ 前端完整校验(含 overlap)→ POST/PATCH { pricing_mode:2, rules_json }
|
||||||
|
商品编辑 → remote 搜索模板 + 已绑 ID 详情补回显
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 问题与目标
|
||||||
|
|
||||||
|
### 1.1 现状
|
||||||
|
|
||||||
|
| 层 | 状态 |
|
||||||
|
| --- | --- |
|
||||||
|
| Product gRPC + DB `product.freight_templates` | Implemented |
|
||||||
|
| Admin API 五接口(list/create/get/patch/delete) | Implemented;透传 Product 对 `rules_json` 的结构 / 区域 / overlap 校验错误 |
|
||||||
|
| admin-web 模板管理页 | **缺失** |
|
||||||
|
| admin-web 商品绑定 | 手输 / `allow-create` ID;注释仍写「暂无列表接口」 |
|
||||||
|
| `region.json` | [`internal/freight/region.json`](../internal/freight/region.json)(结构见 §4.2) |
|
||||||
|
| 结算按模板算运费 | 非本期;公式预约定于 §3.3 |
|
||||||
|
|
||||||
|
### 1.2 目标
|
||||||
|
|
||||||
|
1. 管理端可完整维护快递运费模板(CRUD)。
|
||||||
|
2. 创建/编辑交互对齐附图(快递可用;自提 tab disabled)。
|
||||||
|
3. 配送区域从 `region.json` 多选;**exact code 与父子层级均不可重叠**。
|
||||||
|
4. 商品编辑在「运费模板」模式下 remote 搜索本店启用模板,并可靠回显已绑 ID。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 管理端信息架构
|
||||||
|
|
||||||
|
### 2.1 路由与菜单
|
||||||
|
|
||||||
|
建议挂在商品相关导航下(与供应商/品牌同级或「物流设置」分组):
|
||||||
|
|
||||||
|
| 路由 | 页面 |
|
||||||
|
| --- | --- |
|
||||||
|
| `/product/freight-template` | 列表 |
|
||||||
|
| `/product/freight-template/create` | 创建 |
|
||||||
|
| `/product/freight-template/edit/:id` | 编辑 |
|
||||||
|
|
||||||
|
(实现时可改为 dialog 模式;**推荐独立页**以便区域选择器占宽。)
|
||||||
|
|
||||||
|
### 2.2 列表页
|
||||||
|
|
||||||
|
参考 [`supplier.vue`](../../ids-admin-web/src/views/catalog/supplier.vue) 主数据页:
|
||||||
|
|
||||||
|
| 列 | 来源 |
|
||||||
|
| --- | --- |
|
||||||
|
| 模板名称 | `name` |
|
||||||
|
| 计费方式 | 见下表「计费方式展示」 |
|
||||||
|
| 配送区域数 | `pricing_mode=2` 且规则合法时:`rules_json.rules.length`;否则 `—` |
|
||||||
|
| 状态 | `status`:1 启用 / 0 停用 |
|
||||||
|
| 更新时间 | `updated_at` |
|
||||||
|
| 操作 | 编辑、启用/停用、删除 |
|
||||||
|
|
||||||
|
**计费方式展示(必须区分历史数据):**
|
||||||
|
|
||||||
|
| 条件 | 展示文案 |
|
||||||
|
| --- | --- |
|
||||||
|
| `pricing_mode === 1` | 统一运费 |
|
||||||
|
| `pricing_mode === 2` 且 `rules_json.charge_type === 'piece'` | 按件数 |
|
||||||
|
| `pricing_mode === 2` 且 `rules_json.charge_type === 'weight'` | 按重量 |
|
||||||
|
| `pricing_mode === 2` 但 JSON 无法解析 / 缺 `charge_type` | 规则异常 |
|
||||||
|
|
||||||
|
筛选:`keyword`、`status`;分页:`page` / `page_size`。
|
||||||
|
|
||||||
|
删除:带 `expected_version`;若商品仍引用 → 展示后端 `409` 文案。
|
||||||
|
|
||||||
|
### 2.3 创建 / 编辑页(快递)
|
||||||
|
|
||||||
|
对齐附图,字段如下:
|
||||||
|
|
||||||
|
| UI | 说明 |
|
||||||
|
| --- | --- |
|
||||||
|
| 页签「快递发货」 | 本期唯一可用(active) |
|
||||||
|
| 页签「上门自提」 | **显示且 `disabled`**,旁注「即将支持」;无表单、无提交字段 |
|
||||||
|
| 模板名称 * | 必填,≤64 字(与服务端一致) |
|
||||||
|
| 计费方式 | 单选:按件数 / 按重量;切换时表格列标题联动(首件↔首重、续件↔续重),单位:个 / kg |
|
||||||
|
| 配送区域表 | 初始:**表头已展示、无数据行**,空态文案 **「没有更多数据了」**(与附图一致)。有数据后:可配送区域、首(件/重)、运费(元)、续(件/重)、续费(元);支持行内编辑与删除行 |
|
||||||
|
| 「指定可配送区域和运费」 | 打开区域选择弹层 → 确认后追加一行(默认首单元=1、续单元=1;运费/续费默认 0,保存前须用户确认) |
|
||||||
|
| 保存 / 取消 | 见 §2.5 |
|
||||||
|
|
||||||
|
历史 `pricing_mode=1` 打开编辑:允许改名称/状态;规则区提示「当前为统一运费,保存区域规则将切换为 `pricing_mode=2`」;用户补齐至少一行规则后保存即转 mode=2。
|
||||||
|
|
||||||
|
### 2.4 提交校验(后端已落地;前端仍应预校验)
|
||||||
|
|
||||||
|
**本服务(权威):** `CreateFreightTemplate` / `UpdateFreightTemplate` 在 `pricing_mode=2` 或写入 `rules_json` 时调用 `freight.ValidateRulesJSON`;不合法 → gRPC `InvalidArgument` → Admin API 4xx。
|
||||||
|
**Update:** 若将 `pricing_mode` 设为 `2`,同请求必须带合法 `rules_json`。仅改名称/状态且不碰 mode/rules 时不重验旧数据。
|
||||||
|
**前端(UI 落地后):** 提交前仍做同等预校验以改善体验;不能替代后端。
|
||||||
|
|
||||||
|
后端校验清单:
|
||||||
|
|
||||||
|
- 至少一条配送规则;`version=1`,`delivery=express`,`charge_type` ∈ {piece,weight};
|
||||||
|
- 每行 `region_codes` 非空字符串,且均存在于嵌入的 `region.json`;与 `region_names` / `region_paths` 等长(**新写入 paths 必填**);
|
||||||
|
- `first_fee` / `additional_fee` ≥ 0(分);
|
||||||
|
- `charge_type=piece`:`first_unit` / `additional_unit` 为正整数;
|
||||||
|
- `charge_type=weight`:单位 > 0 且最多 3 位小数(见 §3.3);
|
||||||
|
- **区域重叠:** 同行与跨行,相同 code 或祖先–后代关系均拒绝(见 §4.4)。
|
||||||
|
|
||||||
|
### 2.5 保存交互
|
||||||
|
|
||||||
|
- 保存按钮:`loading` 期间禁用;防重复提交(单飞)。
|
||||||
|
- 成功:提示后回列表(或停留编辑并刷新 `version`)。
|
||||||
|
- `409` / 版本冲突:提示「数据已被他人更新」,**重新 `GET` 详情**后保留用户可对比;不静默覆盖。
|
||||||
|
- 删除在用 `409`:展示服务端文案,不移除行。
|
||||||
|
- 取消:有未保存变更时二次确认。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. `rules_json` 契约(前端写入 / 后端存取)
|
||||||
|
|
||||||
|
服务端对 `rules_json` **校验结构后存贮、暂不解析计价**;写入必须符合本节契约,供后续 Quote 使用。计价语义见 §3.3。
|
||||||
|
|
||||||
|
### 3.1 Schema
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "piece",
|
||||||
|
"rules": [
|
||||||
|
{
|
||||||
|
"region_codes": ["440000", "310000"],
|
||||||
|
"region_names": ["广东省", "上海市"],
|
||||||
|
"region_paths": ["广东省", "上海市"],
|
||||||
|
"first_unit": 1,
|
||||||
|
"first_fee": 800,
|
||||||
|
"additional_unit": 1,
|
||||||
|
"additional_fee": 200
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `version` | int | 固定 `1` |
|
||||||
|
| `delivery` | string | 固定 `"express"`(自提将来另约定,本期不写) |
|
||||||
|
| `charge_type` | string | `"piece"` \| `"weight"` |
|
||||||
|
| `rules[].region_codes` | string[] | 归一化后的区域 code;**必须**为字符串(源文件 `regionId` 为 number,写入时 `String(regionId)`,如 `"210000"`)。禁止在 `rules_json` 中存 number,以免 overlap / 回填出现 `"210000" !== 210000` |
|
||||||
|
| `rules[].region_names` | string[] | 节点 `name`,与 codes 等长(短展示) |
|
||||||
|
| `rules[].region_paths` | string[] | **新写入必须写**;仅历史数据可缺失,编辑时按 code 回查 `region.json` 补全。路径为省/市/区名称以 `/` 连接(不含 `part` 分组名),如 `"辽宁省/沈阳市/和平区"`;与 codes 等长。表格优先展示 path |
|
||||||
|
| `rules[].first_unit` | number | 首件(整数)或首重(kg,见 §3.3) |
|
||||||
|
| `rules[].first_fee` | int64 | 分 |
|
||||||
|
| `rules[].additional_unit` | number | 续件(整数)或续重(kg,见 §3.3) |
|
||||||
|
| `rules[].additional_fee` | int64 | 分 |
|
||||||
|
|
||||||
|
创建/更新请求示例:
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /admin/v1/freight-templates
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "华东快递",
|
||||||
|
"pricing_mode": 2,
|
||||||
|
"rules_json": { "...见上..." },
|
||||||
|
"status": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
编辑还需 `expected_version`。`default_fee` 在 mode=2 时可不传。
|
||||||
|
|
||||||
|
若历史数据无 `region_paths`:编辑页用 `region_codes` 反查 `region.json` 补全 path;补全失败则回退 `region_names`。
|
||||||
|
|
||||||
|
### 3.2 与现网 `pricing_mode` 映射
|
||||||
|
|
||||||
|
| 管理端场景 | `pricing_mode` | `default_fee` | `rules_json` |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| 本期创建/编辑(附图) | `2` | 省略 | 必填,§3.1 |
|
||||||
|
| 历史 mode=1 统一运费 | 保持 1 直至用户保存区域规则 | 列表可展示 `default_fee` | 编辑见 §2.3 |
|
||||||
|
|
||||||
|
### 3.3 计费语义(按件 / 按重,供 UI 与未来 Quote)
|
||||||
|
|
||||||
|
**按件数(`charge_type=piece`)**
|
||||||
|
|
||||||
|
- `first_unit` / `additional_unit`:正整数;UI 步长 1。
|
||||||
|
- 件数 `n`(正整数)运费:
|
||||||
|
|
||||||
|
```text
|
||||||
|
if n <= first_unit:
|
||||||
|
fee = first_fee
|
||||||
|
else:
|
||||||
|
extra = ceil( (n - first_unit) / additional_unit )
|
||||||
|
fee = first_fee + extra * additional_fee
|
||||||
|
```
|
||||||
|
|
||||||
|
**按重量(`charge_type=weight`)**
|
||||||
|
|
||||||
|
- 单位:**kg**。
|
||||||
|
- `first_unit` / `additional_unit`:存 number,**最多 3 位小数**;UI `precision=3`,步长 `0.001`;须 `> 0`。
|
||||||
|
- 商品合计重量 `w`(kg,计算侧同样按 3 位小数处理)运费:
|
||||||
|
|
||||||
|
```text
|
||||||
|
if w <= first_unit:
|
||||||
|
fee = first_fee
|
||||||
|
else:
|
||||||
|
extra = ceil( (w - first_unit) / additional_unit ) // 向上取整续重段数
|
||||||
|
fee = first_fee + extra * additional_fee
|
||||||
|
```
|
||||||
|
|
||||||
|
- **边界:** `w == first_unit` 只收首重费;超出部分按续重段 `ceil` 计,不足一段按一段计。
|
||||||
|
- 管理端输入超 3 位小数:提交前四舍五入到 3 位(或直接限制输入)。
|
||||||
|
|
||||||
|
本期 Quote **不实现**上述公式,但 `rules_json` 必须按此语义可解释,避免日后无法计价。
|
||||||
|
|
||||||
|
### 3.4 金额元 ↔ 分
|
||||||
|
|
||||||
|
- UI 展示/输入:元,`precision=2`。
|
||||||
|
- 提交:`fee_fen = Math.round(yuan * 100)`;回填:`yuan = fen / 100`。
|
||||||
|
- 拒绝 NaN / 负数。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. `region.json`
|
||||||
|
|
||||||
|
### 4.1 落盘位置
|
||||||
|
|
||||||
|
**Source of truth:**
|
||||||
|
|
||||||
|
```text
|
||||||
|
ids-svc-product/internal/freight/region.json
|
||||||
|
```
|
||||||
|
|
||||||
|
(与 `go:embed` 校验同源;改此文件即可。)
|
||||||
|
|
||||||
|
admin-web 读取方式(约定优先 A):
|
||||||
|
|
||||||
|
| 方式 | 说明 |
|
||||||
|
| --- | --- |
|
||||||
|
| A(推荐) | 开发/构建时从上述源文件 **复制**到 `ids-admin-web/src/assets/region.json`,再 `import` |
|
||||||
|
| B | Vite alias 指向 `../../ids-svc-product/internal/freight/region.json`;若需包外访问,配置 `server.fs.allow` |
|
||||||
|
|
||||||
|
选择器**只读该静态文件**,不请求后端。admin-web 副本勿手改;改源文件后重新同步。
|
||||||
|
|
||||||
|
### 4.2 源文件结构(实际)
|
||||||
|
|
||||||
|
顶层为接口包装,不是树数组:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"msg": "",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"part": "东北",
|
||||||
|
"provinces": [
|
||||||
|
{
|
||||||
|
"regionId": 210000,
|
||||||
|
"name": "辽宁省",
|
||||||
|
"cities": [
|
||||||
|
{
|
||||||
|
"regionId": 210100,
|
||||||
|
"name": "沈阳市",
|
||||||
|
"counties": [
|
||||||
|
{ "regionId": 210102, "name": "和平区" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
| 源字段 | 说明 |
|
||||||
|
| --- | --- |
|
||||||
|
| `code` / `msg` | 包装字段;`code !== 0` 时前端应视为加载失败 |
|
||||||
|
| `data[]` | 按拼音/地区的**分组**列表 |
|
||||||
|
| `data[].part` | 分组名(如「东北」);**仅 UI 分组展示,不写入** `region_codes` / `region_paths` |
|
||||||
|
| `data[].index` | 分组索引;类型以源文件为准,可选用于侧栏锚点 |
|
||||||
|
| `provinces[]` | 省;字段 `regionId`(number)、`name`、`cities` |
|
||||||
|
| `cities[]` | 市;字段 `regionId`、`name`、`counties` |
|
||||||
|
| `counties[]` | 区/县;字段 `regionId`、`name` |
|
||||||
|
|
||||||
|
### 4.3 归一化为选择器树(实现必做)
|
||||||
|
|
||||||
|
内部统一树节点:`{ code: string, name: string, children?: Node[] }`。
|
||||||
|
|
||||||
|
转换规则:
|
||||||
|
|
||||||
|
```text
|
||||||
|
load region.json
|
||||||
|
assert wrapper.code === 0
|
||||||
|
for each group in data:
|
||||||
|
// part 可作为 el-tree / 侧栏分组标签,不是可选区节点
|
||||||
|
for each province in group.provinces:
|
||||||
|
node = {
|
||||||
|
code: String(province.regionId), // number → string,必须
|
||||||
|
name: province.name,
|
||||||
|
children: (province.cities || []).map(city => ({
|
||||||
|
code: String(city.regionId),
|
||||||
|
name: city.name,
|
||||||
|
children: (city.counties || []).map(county => ({
|
||||||
|
code: String(county.regionId),
|
||||||
|
name: county.name
|
||||||
|
}))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
| 源 | 目标 |
|
||||||
|
| --- | --- |
|
||||||
|
| `regionId` (number) | `code` = `String(regionId)` |
|
||||||
|
| `name` | `name` |
|
||||||
|
| `provinces` / `cities` / `counties` | 层层变为 `children` |
|
||||||
|
| `part` / `index` | 保留为分组元数据;**不是**树节点,不参与 overlap |
|
||||||
|
|
||||||
|
`region_paths` 生成:自省至当前节点的 `name` 用 `/` 连接(**不含** `part`),例如选中和平区 → `"辽宁省/沈阳市/和平区"`;仅选省 → `"辽宁省"`。
|
||||||
|
|
||||||
|
### 4.4 选择器与区域重叠规则
|
||||||
|
|
||||||
|
- 弹层多选树;可选省 / 市 / 区(按产品默认勾选省级,允许下钻)。
|
||||||
|
- **冲突定义(禁用 + 保存校验同一套):**
|
||||||
|
对已占用集合 `Occupied`(其它规则行已选 code,编辑当前行时不含本行旧值或按「确认后整行替换」处理):
|
||||||
|
候选节点 `C` 与任一 `O ∈ Occupied` 冲突,当且仅当:
|
||||||
|
- `C.code === O.code`,或
|
||||||
|
- `C` 是 `O` 的祖先,或
|
||||||
|
- `C` 是 `O` 的后代。
|
||||||
|
例:已选「广东省」则不可再选「深圳市」;已选「深圳市」则不可再选「广东省」。
|
||||||
|
- 同行内多选节点之间同样做 overlap 检查(禁止同时勾选父子)。
|
||||||
|
- 确认后写入当前行(或新建行)的 `region_codes` / `region_names` / `region_paths`。
|
||||||
|
- 表格「可配送区域」列:优先 `region_paths` 顿号拼接;过长省略 + tooltip。
|
||||||
|
|
||||||
|
实现提示:加载 `region.json` 时建 `code → parentCode` 与 `code → path` 索引,overlap 用祖先链集合相交判断。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Admin API 用法(已实现,前端对接)
|
||||||
|
|
||||||
|
| 方法 | 路径 | 用途 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `GET` | `/admin/v1/freight-templates` | 列表:`page` `page_size` `keyword` `status` |
|
||||||
|
| `POST` | `/admin/v1/freight-templates` | 创建 → `201 { template }` |
|
||||||
|
| `GET` | `/admin/v1/freight-templates/{id}` | 编辑回填;商品侧补回显 |
|
||||||
|
| `PATCH` | `/admin/v1/freight-templates/{id}` | 更新(含 `expected_version`) |
|
||||||
|
| `DELETE` | `/admin/v1/freight-templates/{id}?expected_version=` | 软删;在用 → `409` |
|
||||||
|
|
||||||
|
前端模块建议:`ids-admin-web/src/api/freightTemplate.js`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 商品编辑联动
|
||||||
|
|
||||||
|
[`product/edit.vue`](../../ids-admin-web/src/views/product/edit.vue) 在 `freight_charge_type === 2` 时:
|
||||||
|
|
||||||
|
1. **`el-select` remote 搜索**:`GET /admin/v1/freight-templates?status=1&keyword=&page=1&page_size=100`;默认打开时加载前 100 条;输入关键词走 `keyword`,**不**一次性拉全站。
|
||||||
|
2. 选项展示 `name`,值为 `template_id`;去掉「暂无列表接口」注释与 `allow-create` 手输依赖。
|
||||||
|
3. **已绑定回显:** 若当前 `freight_template_id` 不在本次 options 中,则 `GET /admin/v1/freight-templates/{id}` 补一条 option;停用/软删则 disabled + 提示更换。
|
||||||
|
4. 搜索无结果且无已绑 ID 时,显示空态,不回落手输任意 ID(避免绑到他店/无效 ID)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 实现分期
|
||||||
|
|
||||||
|
| 阶段 | 内容 |
|
||||||
|
| --- | --- |
|
||||||
|
| **Doc**(本文) | 契约与 UI 范围 |
|
||||||
|
| **Backend**(已做) | `internal/freight`:嵌入 region.json + `ValidateRulesJSON`;create/update 接入 |
|
||||||
|
| **P0 UI**(未做) | admin-web:region 同步 + 列表 CRUD + 创建/编辑页 + 区域选择器 |
|
||||||
|
| **P1 UI**(未做) | 商品编辑 remote 搜索绑定 |
|
||||||
|
| **P2**(另文) | Quote/Checkout 按 §3.3 计价;上门自提 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 验收
|
||||||
|
|
||||||
|
| # | 场景 | 期望 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| A | 新建按件模板,选两个省,填首续费并保存 | 列表可见;GET 回填 `rules_json` 正确(金额为分);空表初始态为表头 +「没有更多数据了」 |
|
||||||
|
| B | 编辑改名、改区域、改计费方式为按重 | PATCH 成功,version +1 |
|
||||||
|
| C | 删除未被引用模板 | `204`,列表消失 |
|
||||||
|
| D | 删除仍被商品引用的模板 | `409`,前端提示 |
|
||||||
|
| E | 商品 remote 搜索选模板;已绑不在首页时详情补回显 | `freight_template_id` 正确 |
|
||||||
|
| F | 上门自提 tab | 可见、disabled、「即将支持」;无配置 |
|
||||||
|
| G | 一行选「广东省」,另一行选「深圳市」 | 选择器禁用或保存拦截,提示区域重叠 |
|
||||||
|
| H | 运费输入 `1.005` 元 | 提交为 `101` 分(`round`);回填展示一致 |
|
||||||
|
| I | 历史 `pricing_mode=1` | 列表显示「统一运费」;不误显示「规则异常/按件」 |
|
||||||
|
| J | 按重:首重 `1`、续重 `0.5`,模拟重量 `1.1` | 规则可按 §3.3 解释为 `ceil(0.1/0.5)=1` 段续费(本期可不跑 Quote,单测/文档样例即可) |
|
||||||
|
| K | 版本冲突保存 | `409` 提示并重新拉取详情 |
|
||||||
|
| L | 选辽宁省等区域并保存 | `region_codes` 为字符串(如 `"210000"`)且含 `region_paths`;GET 回填与树勾选匹配 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 风险
|
||||||
|
|
||||||
|
| 风险 | 处理 |
|
||||||
|
| --- | --- |
|
||||||
|
| `region.json` 体积大 | 异步 chunk;选择器虚拟滚动(若卡顿再做) |
|
||||||
|
| 仅 exact code 去重漏父子重叠 | §4.4 强制祖先/后代冲突 |
|
||||||
|
| admin-web 区域副本过期 | 改 `internal/freight/region.json` 后同步复制到 admin-web assets |
|
||||||
|
| 元↔分 / kg 小数 | §3.3 / §3.4 定死 |
|
||||||
|
| 历史 mode=1 | 列表文案区分;编辑引导转 mode=2 |
|
||||||
|
| 结算仍不认模板 | 产品可知晓;不在本期验收 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 索引
|
||||||
|
|
||||||
|
| 项 | 路径 |
|
||||||
|
| --- | --- |
|
||||||
|
| Admin HTTP 文档 | `ids-admin-api/docs/admin-product-create-edit-phase1.md` §2 |
|
||||||
|
| Handler | `ids-admin-api/internal/handler/freight_templates.go` |
|
||||||
|
| Product 服务 | `ids-svc-product/internal/server/freight_template_server.go` |
|
||||||
|
| 商品编辑(待改) | `ids-admin-web/src/views/product/edit.vue` |
|
||||||
|
| 区域数据(源 / embed) | `ids-svc-product/internal/freight/region.json` |
|
||||||
|
| 校验实现 | `ids-svc-product/internal/freight/rules_json.go` |
|
||||||
15864
internal/freight/region.json
Normal file
15864
internal/freight/region.json
Normal file
File diff suppressed because it is too large
Load Diff
120
internal/freight/region_index.go
Normal file
120
internal/freight/region_index.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
package freight
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed region.json
|
||||||
|
var regionJSON []byte
|
||||||
|
|
||||||
|
// RegionIndex indexes adcodes from region.json for existence + ancestor overlap checks.
|
||||||
|
type RegionIndex struct {
|
||||||
|
parent map[string]string
|
||||||
|
known map[string]struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type regionFile struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data []struct {
|
||||||
|
Provinces []struct {
|
||||||
|
RegionID int64 `json:"regionId"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Cities []struct {
|
||||||
|
RegionID int64 `json:"regionId"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Counties []struct {
|
||||||
|
RegionID int64 `json:"regionId"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"counties"`
|
||||||
|
} `json:"cities"`
|
||||||
|
} `json:"provinces"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
regionOnce sync.Once
|
||||||
|
regionIdx *RegionIndex
|
||||||
|
regionErr error
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultRegionIndex returns the process-wide index built from embedded region.json.
|
||||||
|
func DefaultRegionIndex() (*RegionIndex, error) {
|
||||||
|
regionOnce.Do(func() {
|
||||||
|
regionIdx, regionErr = buildRegionIndex(regionJSON)
|
||||||
|
})
|
||||||
|
return regionIdx, regionErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildRegionIndex(raw []byte) (*RegionIndex, error) {
|
||||||
|
var file regionFile
|
||||||
|
if err := json.Unmarshal(raw, &file); err != nil {
|
||||||
|
return nil, fmt.Errorf("parse region.json: %w", err)
|
||||||
|
}
|
||||||
|
if file.Code != 0 {
|
||||||
|
return nil, fmt.Errorf("region.json code is %d, want 0", file.Code)
|
||||||
|
}
|
||||||
|
idx := &RegionIndex{
|
||||||
|
parent: make(map[string]string),
|
||||||
|
known: make(map[string]struct{}),
|
||||||
|
}
|
||||||
|
for _, group := range file.Data {
|
||||||
|
for _, province := range group.Provinces {
|
||||||
|
pCode := fmt.Sprint(province.RegionID)
|
||||||
|
idx.known[pCode] = struct{}{}
|
||||||
|
for _, city := range province.Cities {
|
||||||
|
cCode := fmt.Sprint(city.RegionID)
|
||||||
|
idx.known[cCode] = struct{}{}
|
||||||
|
idx.parent[cCode] = pCode
|
||||||
|
for _, county := range city.Counties {
|
||||||
|
dCode := fmt.Sprint(county.RegionID)
|
||||||
|
idx.known[dCode] = struct{}{}
|
||||||
|
idx.parent[dCode] = cCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(idx.known) == 0 {
|
||||||
|
return nil, fmt.Errorf("region.json has no regions")
|
||||||
|
}
|
||||||
|
return idx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Known reports whether code exists in region.json.
|
||||||
|
func (idx *RegionIndex) Known(code string) bool {
|
||||||
|
if idx == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok := idx.known[code]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overlap reports same code or ancestor–descendant relationship.
|
||||||
|
func (idx *RegionIndex) Overlap(a, b string) bool {
|
||||||
|
if a == b {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if idx == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if idx.isAncestor(a, b) || idx.isAncestor(b, a) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (idx *RegionIndex) isAncestor(ancestor, descendant string) bool {
|
||||||
|
cur := descendant
|
||||||
|
for {
|
||||||
|
parent, ok := idx.parent[cur]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if parent == ancestor {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
cur = parent
|
||||||
|
}
|
||||||
|
}
|
||||||
156
internal/freight/rules_json.go
Normal file
156
internal/freight/rules_json.go
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
package freight
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RulesDocument is the version-1 express freight rules_json contract.
|
||||||
|
type RulesDocument struct {
|
||||||
|
Version int `json:"version"`
|
||||||
|
Delivery string `json:"delivery"`
|
||||||
|
ChargeType string `json:"charge_type"`
|
||||||
|
Rules []RuleRow `json:"rules"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RuleRow is one deliverable-area pricing row.
|
||||||
|
type RuleRow struct {
|
||||||
|
RegionCodes []string `json:"region_codes"`
|
||||||
|
RegionNames []string `json:"region_names"`
|
||||||
|
RegionPaths []string `json:"region_paths"`
|
||||||
|
FirstUnit float64 `json:"first_unit"`
|
||||||
|
FirstFee int64 `json:"first_fee"`
|
||||||
|
AdditionalUnit float64 `json:"additional_unit"`
|
||||||
|
AdditionalFee int64 `json:"additional_fee"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateRulesJSON validates mode=2 rules_json bytes against docs/admin-freight-template.md.
|
||||||
|
// region_paths is required (new writes). Codes must be strings present in region.json; overlap includes parent/child.
|
||||||
|
func ValidateRulesJSON(raw []byte) error {
|
||||||
|
idx, err := DefaultRegionIndex()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("region index unavailable: %w", err)
|
||||||
|
}
|
||||||
|
return ValidateRulesJSONWithIndex(raw, idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateRulesJSONWithIndex is the testable core of ValidateRulesJSON.
|
||||||
|
func ValidateRulesJSONWithIndex(raw []byte, idx *RegionIndex) error {
|
||||||
|
if len(strings.TrimSpace(string(raw))) == 0 {
|
||||||
|
return fmt.Errorf("rules_json is required")
|
||||||
|
}
|
||||||
|
var doc RulesDocument
|
||||||
|
dec := json.NewDecoder(strings.NewReader(string(raw)))
|
||||||
|
dec.DisallowUnknownFields()
|
||||||
|
if err := dec.Decode(&doc); err != nil {
|
||||||
|
return fmt.Errorf("rules_json invalid: %w", err)
|
||||||
|
}
|
||||||
|
if doc.Version != 1 {
|
||||||
|
return fmt.Errorf("rules_json.version must be 1")
|
||||||
|
}
|
||||||
|
if doc.Delivery != "express" {
|
||||||
|
return fmt.Errorf("rules_json.delivery must be \"express\"")
|
||||||
|
}
|
||||||
|
if doc.ChargeType != "piece" && doc.ChargeType != "weight" {
|
||||||
|
return fmt.Errorf("rules_json.charge_type must be \"piece\" or \"weight\"")
|
||||||
|
}
|
||||||
|
if len(doc.Rules) == 0 {
|
||||||
|
return fmt.Errorf("rules_json.rules must contain at least one rule")
|
||||||
|
}
|
||||||
|
|
||||||
|
occupied := make([]string, 0)
|
||||||
|
for i, rule := range doc.Rules {
|
||||||
|
prefix := fmt.Sprintf("rules_json.rules[%d]", i)
|
||||||
|
if err := validateRuleRow(prefix, doc.ChargeType, rule, idx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, code := range rule.RegionCodes {
|
||||||
|
for _, prev := range occupied {
|
||||||
|
if idx.Overlap(code, prev) {
|
||||||
|
return fmt.Errorf("%s: region %s overlaps occupied region %s", prefix, code, prev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
occupied = append(occupied, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateRuleRow(prefix, chargeType string, rule RuleRow, idx *RegionIndex) error {
|
||||||
|
if len(rule.RegionCodes) == 0 {
|
||||||
|
return fmt.Errorf("%s.region_codes must not be empty", prefix)
|
||||||
|
}
|
||||||
|
if len(rule.RegionNames) != len(rule.RegionCodes) {
|
||||||
|
return fmt.Errorf("%s.region_names length must match region_codes", prefix)
|
||||||
|
}
|
||||||
|
if len(rule.RegionPaths) != len(rule.RegionCodes) {
|
||||||
|
return fmt.Errorf("%s.region_paths length must match region_codes (required for new writes)", prefix)
|
||||||
|
}
|
||||||
|
seen := make(map[string]struct{}, len(rule.RegionCodes))
|
||||||
|
for j, code := range rule.RegionCodes {
|
||||||
|
code = strings.TrimSpace(code)
|
||||||
|
if code == "" {
|
||||||
|
return fmt.Errorf("%s.region_codes[%d] is empty", prefix, j)
|
||||||
|
}
|
||||||
|
if _, dup := seen[code]; dup {
|
||||||
|
return fmt.Errorf("%s.region_codes contains duplicate %s", prefix, code)
|
||||||
|
}
|
||||||
|
seen[code] = struct{}{}
|
||||||
|
if !idx.Known(code) {
|
||||||
|
return fmt.Errorf("%s.region_codes[%d]=%s is not a known region", prefix, j, code)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(rule.RegionNames[j]) == "" {
|
||||||
|
return fmt.Errorf("%s.region_names[%d] is empty", prefix, j)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(rule.RegionPaths[j]) == "" {
|
||||||
|
return fmt.Errorf("%s.region_paths[%d] is empty", prefix, j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for a := range seen {
|
||||||
|
for b := range seen {
|
||||||
|
if a >= b {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if idx.Overlap(a, b) {
|
||||||
|
return fmt.Errorf("%s: region_codes %s and %s overlap", prefix, a, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rule.FirstFee < 0 || rule.AdditionalFee < 0 {
|
||||||
|
return fmt.Errorf("%s fees must be >= 0 (fen)", prefix)
|
||||||
|
}
|
||||||
|
switch chargeType {
|
||||||
|
case "piece":
|
||||||
|
if !isPositiveInt(rule.FirstUnit) || !isPositiveInt(rule.AdditionalUnit) {
|
||||||
|
return fmt.Errorf("%s first_unit/additional_unit must be positive integers for piece", prefix)
|
||||||
|
}
|
||||||
|
case "weight":
|
||||||
|
if err := validateWeightUnit(prefix+".first_unit", rule.FirstUnit); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := validateWeightUnit(prefix+".additional_unit", rule.AdditionalUnit); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPositiveInt(v float64) bool {
|
||||||
|
if v <= 0 || math.IsNaN(v) || math.IsInf(v, 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return v == math.Trunc(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateWeightUnit(field string, v float64) error {
|
||||||
|
if v <= 0 || math.IsNaN(v) || math.IsInf(v, 0) {
|
||||||
|
return fmt.Errorf("%s must be > 0", field)
|
||||||
|
}
|
||||||
|
scaled := v * 1000
|
||||||
|
if math.Abs(scaled-math.Round(scaled)) > 1e-6 {
|
||||||
|
return fmt.Errorf("%s allows at most 3 decimal places", field)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
158
internal/freight/rules_json_test.go
Normal file
158
internal/freight/rules_json_test.go
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
package freight
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_PieceOK(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "piece",
|
||||||
|
"rules": [{
|
||||||
|
"region_codes": ["210000", "310000"],
|
||||||
|
"region_names": ["辽宁省", "上海市"],
|
||||||
|
"region_paths": ["辽宁省", "上海市"],
|
||||||
|
"first_unit": 1,
|
||||||
|
"first_fee": 800,
|
||||||
|
"additional_unit": 1,
|
||||||
|
"additional_fee": 200
|
||||||
|
}]
|
||||||
|
}`)
|
||||||
|
if err := ValidateRulesJSON(raw); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_WeightOK(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "weight",
|
||||||
|
"rules": [{
|
||||||
|
"region_codes": ["210000"],
|
||||||
|
"region_names": ["辽宁省"],
|
||||||
|
"region_paths": ["辽宁省"],
|
||||||
|
"first_unit": 1,
|
||||||
|
"first_fee": 800,
|
||||||
|
"additional_unit": 0.5,
|
||||||
|
"additional_fee": 200
|
||||||
|
}]
|
||||||
|
}`)
|
||||||
|
if err := ValidateRulesJSON(raw); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_ParentChildOverlap(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "piece",
|
||||||
|
"rules": [
|
||||||
|
{
|
||||||
|
"region_codes": ["210000"],
|
||||||
|
"region_names": ["辽宁省"],
|
||||||
|
"region_paths": ["辽宁省"],
|
||||||
|
"first_unit": 1, "first_fee": 0, "additional_unit": 1, "additional_fee": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"region_codes": ["210100"],
|
||||||
|
"region_names": ["沈阳市"],
|
||||||
|
"region_paths": ["辽宁省/沈阳市"],
|
||||||
|
"first_unit": 1, "first_fee": 0, "additional_unit": 1, "additional_fee": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
err := ValidateRulesJSON(raw)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "overlap") {
|
||||||
|
t.Fatalf("want overlap error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_SameRowOverlap(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "piece",
|
||||||
|
"rules": [{
|
||||||
|
"region_codes": ["210000", "210100"],
|
||||||
|
"region_names": ["辽宁省", "沈阳市"],
|
||||||
|
"region_paths": ["辽宁省", "辽宁省/沈阳市"],
|
||||||
|
"first_unit": 1, "first_fee": 0, "additional_unit": 1, "additional_fee": 0
|
||||||
|
}]
|
||||||
|
}`)
|
||||||
|
err := ValidateRulesJSON(raw)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "overlap") {
|
||||||
|
t.Fatalf("want overlap error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_RejectNumericCodes(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "piece",
|
||||||
|
"rules": [{
|
||||||
|
"region_codes": [210000],
|
||||||
|
"region_names": ["辽宁省"],
|
||||||
|
"region_paths": ["辽宁省"],
|
||||||
|
"first_unit": 1, "first_fee": 0, "additional_unit": 1, "additional_fee": 0
|
||||||
|
}]
|
||||||
|
}`)
|
||||||
|
if err := ValidateRulesJSON(raw); err == nil {
|
||||||
|
t.Fatal("expected error for numeric region_codes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_RequirePaths(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "piece",
|
||||||
|
"rules": [{
|
||||||
|
"region_codes": ["210000"],
|
||||||
|
"region_names": ["辽宁省"],
|
||||||
|
"first_unit": 1, "first_fee": 0, "additional_unit": 1, "additional_fee": 0
|
||||||
|
}]
|
||||||
|
}`)
|
||||||
|
err := ValidateRulesJSON(raw)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "region_paths") {
|
||||||
|
t.Fatalf("want region_paths error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRulesJSON_WeightPrecision(t *testing.T) {
|
||||||
|
raw := []byte(`{
|
||||||
|
"version": 1,
|
||||||
|
"delivery": "express",
|
||||||
|
"charge_type": "weight",
|
||||||
|
"rules": [{
|
||||||
|
"region_codes": ["210000"],
|
||||||
|
"region_names": ["辽宁省"],
|
||||||
|
"region_paths": ["辽宁省"],
|
||||||
|
"first_unit": 1.0001,
|
||||||
|
"first_fee": 0,
|
||||||
|
"additional_unit": 1,
|
||||||
|
"additional_fee": 0
|
||||||
|
}]
|
||||||
|
}`)
|
||||||
|
err := ValidateRulesJSON(raw)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "3 decimal") {
|
||||||
|
t.Fatalf("want precision error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegionIndex_Known(t *testing.T) {
|
||||||
|
idx, err := DefaultRegionIndex()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !idx.Known("210000") || !idx.Known("210102") {
|
||||||
|
t.Fatal("expected Liaoning / Heping to be known")
|
||||||
|
}
|
||||||
|
if idx.Known("999999") {
|
||||||
|
t.Fatal("unknown code should not be known")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ import (
|
|||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||||
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/freight"
|
||||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -161,9 +162,15 @@ func freightTemplateFromCreate(req *productv1.CreateFreightTemplateRequest) (rep
|
|||||||
if mode == 1 && fee == nil {
|
if mode == 1 && fee == nil {
|
||||||
return repository.FreightTemplateRecord{}, errors.New("default_fee is required when pricing_mode is 1")
|
return repository.FreightTemplateRecord{}, errors.New("default_fee is required when pricing_mode is 1")
|
||||||
}
|
}
|
||||||
if mode == 2 && len(req.GetRulesJson()) == 0 {
|
rules := append([]byte(nil), req.GetRulesJson()...)
|
||||||
|
if mode == 2 {
|
||||||
|
if len(rules) == 0 {
|
||||||
return repository.FreightTemplateRecord{}, errors.New("rules_json is required when pricing_mode is 2")
|
return repository.FreightTemplateRecord{}, errors.New("rules_json is required when pricing_mode is 2")
|
||||||
}
|
}
|
||||||
|
if err := freight.ValidateRulesJSON(rules); err != nil {
|
||||||
|
return repository.FreightTemplateRecord{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
st := int32(1)
|
st := int32(1)
|
||||||
if req.Status != nil {
|
if req.Status != nil {
|
||||||
if *req.Status != 0 && *req.Status != 1 {
|
if *req.Status != 0 && *req.Status != 1 {
|
||||||
@ -172,7 +179,7 @@ func freightTemplateFromCreate(req *productv1.CreateFreightTemplateRequest) (rep
|
|||||||
st = *req.Status
|
st = *req.Status
|
||||||
}
|
}
|
||||||
return repository.FreightTemplateRecord{
|
return repository.FreightTemplateRecord{
|
||||||
Name: name, PricingMode: mode, DefaultFee: fee, RulesJSON: append([]byte(nil), req.GetRulesJson()...), Status: st,
|
Name: name, PricingMode: mode, DefaultFee: fee, RulesJSON: rules, Status: st,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,6 +225,9 @@ func freightTemplatePatch(req *productv1.UpdateFreightTemplateRequest) (reposito
|
|||||||
}
|
}
|
||||||
if has("rules_json") && req.RulesJson != nil {
|
if has("rules_json") && req.RulesJson != nil {
|
||||||
b := append([]byte(nil), req.GetRulesJson()...)
|
b := append([]byte(nil), req.GetRulesJson()...)
|
||||||
|
if err := freight.ValidateRulesJSON(b); err != nil {
|
||||||
|
return patch, err
|
||||||
|
}
|
||||||
patch.RulesJSON = &b
|
patch.RulesJSON = &b
|
||||||
}
|
}
|
||||||
if has("status") && req.Status != nil {
|
if has("status") && req.Status != nil {
|
||||||
@ -226,6 +236,9 @@ func freightTemplatePatch(req *productv1.UpdateFreightTemplateRequest) (reposito
|
|||||||
}
|
}
|
||||||
patch.Status = req.Status
|
patch.Status = req.Status
|
||||||
}
|
}
|
||||||
|
if patch.PricingMode != nil && *patch.PricingMode == 2 && patch.RulesJSON == nil {
|
||||||
|
return patch, errors.New("rules_json is required when pricing_mode is 2")
|
||||||
|
}
|
||||||
return patch, nil
|
return patch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user