feat(region):增加获取全部级别行政区划的功能。
This commit is contained in:
parent
f4f8b97ad3
commit
6e9779bd93
|
@ -20,6 +20,7 @@ func InitializeRegionController(router *gin.Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
RegionController.Router.GET("/region/:rid", RegionController.FetchRegions)
|
RegionController.Router.GET("/region/:rid", RegionController.FetchRegions)
|
||||||
|
RegionController.Router.GET("/regions/:rid", RegionController.FetchAllLeveledRegions)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_RegionController) FetchRegions(c *gin.Context) {
|
func (_RegionController) FetchRegions(c *gin.Context) {
|
||||||
|
@ -36,3 +37,18 @@ func (_RegionController) FetchRegions(c *gin.Context) {
|
||||||
}
|
}
|
||||||
result.Json(http.StatusOK, "已经获取到相关的行政区划。", gin.H{"regions": regions})
|
result.Json(http.StatusOK, "已经获取到相关的行政区划。", gin.H{"regions": regions})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_RegionController) FetchAllLeveledRegions(c *gin.Context) {
|
||||||
|
result := response.NewResult(c)
|
||||||
|
requestRegionCode := c.Param("rid")
|
||||||
|
regions, err := service.RegionService.FetchAllParentRegions(requestRegionCode)
|
||||||
|
if err != nil {
|
||||||
|
result.Error(http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(regions) == 0 {
|
||||||
|
result.Json(http.StatusNotFound, "未能获取到相关的行政区划。", gin.H{"regions": make([]string, 0)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result.Json(http.StatusOK, "以及获取到相关的行政区划。", gin.H{"regions": regions})
|
||||||
|
}
|
||||||
|
|
24
repository/region.go
Normal file
24
repository/region.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"electricity_bill_calc/cache"
|
||||||
|
"electricity_bill_calc/global"
|
||||||
|
"electricity_bill_calc/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
type _RegionRepository struct{}
|
||||||
|
|
||||||
|
var RegionRepository _RegionRepository
|
||||||
|
|
||||||
|
func (_RegionRepository) FetchRegion(code string) (*model.Region, error) {
|
||||||
|
cachedRegion, _ := cache.RetreiveData[model.Region]("region", code)
|
||||||
|
if cachedRegion != nil {
|
||||||
|
return cachedRegion, nil
|
||||||
|
}
|
||||||
|
region := new(model.Region)
|
||||||
|
has, err := global.DBConn.ID(code).NoAutoCondition().Get(region)
|
||||||
|
if has {
|
||||||
|
cache.CacheData(region, "region", code)
|
||||||
|
}
|
||||||
|
return _postProcessSingle(region, has, err)
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package service
|
||||||
import (
|
import (
|
||||||
"electricity_bill_calc/global"
|
"electricity_bill_calc/global"
|
||||||
"electricity_bill_calc/model"
|
"electricity_bill_calc/model"
|
||||||
|
"electricity_bill_calc/repository"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
@ -19,3 +20,20 @@ func (_RegionService) FetchSubRegions(parent string) ([]model.Region, error) {
|
||||||
}
|
}
|
||||||
return regions, err
|
return regions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_RegionService) FetchAllParentRegions(code string) ([]model.Region, error) {
|
||||||
|
regions := make([]model.Region, 0)
|
||||||
|
region, err := repository.RegionRepository.FetchRegion(code)
|
||||||
|
if err != nil {
|
||||||
|
return regions, err
|
||||||
|
}
|
||||||
|
regions = append(regions, *region)
|
||||||
|
for region.Level > 1 {
|
||||||
|
region, err = repository.RegionRepository.FetchRegion(region.Parent)
|
||||||
|
if err != nil {
|
||||||
|
return make([]model.Region, 0), nil
|
||||||
|
}
|
||||||
|
regions = append(regions, *region)
|
||||||
|
}
|
||||||
|
return regions, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user