12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package geo
- import (
- "reflect"
- "zhiyuan/models/geo"
- "zhiyuan/pkg/db"
- )
- type GeoType struct {
- Typ reflect.Type
- Code string
- Count int
- }
- var GEO_LIST = []GeoType{
- {
- Typ: db.Type(geo.Province{}),
- Code: "provinceCode",
- Count: 2,
- },
- {
- Typ: db.Type(geo.City{}),
- Code: "cityCode",
- Count: 4,
- },
- {
- Typ: db.Type(geo.Area{}),
- Code: "areaCode",
- Count: 6,
- },
- {
- Typ: db.Type(geo.Street{}),
- Code: "streetCode",
- Count: 9,
- },
- {
- Typ: db.Type(geo.Village{}),
- Code: "villageCode",
- Count: 12,
- },
- }
- func codeN(code string) int {
- for i, v := range GEO_LIST {
- if len(code) == v.Count {
- return i
- }
- }
- return -1
- }
- func Lower(code string) ([]map[string]interface{}, error) {
- n := codeN(code)
- var typ reflect.Type
- var search map[string]interface{}
- if n < 0 {
- typ = GEO_LIST[0].Typ
- } else if n >= 4 {
- return []map[string]interface{}{}, nil
- } else {
- typ = GEO_LIST[n+1].Typ
- search = map[string]interface{}{
- GEO_LIST[n].Code: code,
- }
- }
- s := db.ModelQuery(typ, search, false)
- query, params := s.Query()
- list, err := db.QueryMap(query, params, reflect.New(typ).Interface().(db.Model).DB())
- if err != nil {
- return []map[string]interface{}{}, err
- }
- if list == nil {
- list = make([]map[string]interface{}, 0)
- }
- return list, nil
- }
- func Code(code string) []map[string]interface{} {
- n := codeN(code)
- list := make([]map[string]interface{}, 0)
- if n < 0 {
- return list
- }
- for ; n >= 0; n-- {
- one, err := db.GetOneModelMap(GEO_LIST[n].Typ, map[string]interface{}{
- "code": code,
- }, nil)
- if err != nil || one == nil {
- return list
- }
- list = append([]map[string]interface{}{one}, list...)
- if n != 0 {
- code = db.ToString(one[GEO_LIST[n-1].Code])
- }
- }
- return list
- }
|