mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
- 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.
121 lines
2.6 KiB
Go
121 lines
2.6 KiB
Go
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
|
||
}
|
||
}
|