From 28c65b73c370b2eaca3e8d84541220a665f3abe8 Mon Sep 17 00:00:00 2001 From: Zhaolong Date: Thu, 28 Apr 2022 00:19:30 +0800 Subject: [PATCH] fix: 'province' & AreaInfo add parent --- handler/statistics.go | 2 +- models/statistic.go | 1 + router/statistics.go | 2 +- service/statistics/cache.go | 63 ++++++++++++++++---------------- service/statistics/statistics.go | 10 ++--- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/handler/statistics.go b/handler/statistics.go index 9807b0b..951057e 100644 --- a/handler/statistics.go +++ b/handler/statistics.go @@ -17,7 +17,7 @@ import ( // @Param sort path string false "data sorted by" Enums(today, total, now, default) func ProvinceDataHandler(c *gin.Context) { sort := c.Param("sort") - data := service.GetAllProvienceData(sort) + data := service.GetAllProvinceData(sort) utils.Succ(c, data) } diff --git a/models/statistic.go b/models/statistic.go index 0a58282..47a624d 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -2,6 +2,7 @@ package models type AreaInfo struct { Name string `json:"name"` + Parent string `json:"parent"` Today AreaToday `json:"today"` Total AreaTotal `json:"total"` Children []AreaInfo `json:"children"` diff --git a/router/statistics.go b/router/statistics.go index 263ab83..58a9f4c 100644 --- a/router/statistics.go +++ b/router/statistics.go @@ -8,7 +8,7 @@ import ( func statisticRouter(router *gin.RouterGroup) { statisticsRouter := router.Group("/statistics") { - statisticsRouter.GET("/provience/:sort", handler.ProvinceDataHandler) + statisticsRouter.GET("/province/:sort", handler.ProvinceDataHandler) statisticsRouter.GET("/city/:sort", handler.CityDataHandler) statisticsRouter.GET("/country/child", handler.CountryDataHandler) statisticsRouter.GET("/country", handler.CountryDataHandler) diff --git a/service/statistics/cache.go b/service/statistics/cache.go index c5322c4..2f002ee 100644 --- a/service/statistics/cache.go +++ b/service/statistics/cache.go @@ -12,21 +12,21 @@ import ( ) const ( - rds_NCOV_STATISTIC_KEY = "nCovStatistic" - rds_CHINA_TOTAL_KEY = "chinaTotal" - rds_CHINA_ADD_KEY = "chinaAdd" - rds_COUNTRY_LEVEL_KEY = "countryLevel" - rds_COUNTRY_LEVEL_CHILD_KEY = "countryLevelChild" - rds_PROVIENCE_LEVEL_CHILD_KEY = "provienceLevelChild" - rds_PROVIENCE_LEVEL_NOW_CONFIRM_KEY = "provienceLevelNowConfirm" - rds_PROVIENCE_LEVEL_TODAY_CONFIRM_KEY = "provienceLevelTodayConfirm" - rds_PROVIENCE_LEVEL_TOTAL_CONFIRM_KEY = "provienceLevelTotalConfirm" - rds_CITY_LEVEL_CHILD_KEY = "cityLevelChild" - rds_CITY_LEVEL_NOW_CONFIRM_KEY = "cityLevelNowConfirm" - rds_CITY_LEVEL_TODAY_CONFIRM_KEY = "cityLevelTodayConfirm" - rds_CITY_LEVEL_TOTAL_CONFIRM_KEY = "cityLevelTotalConfirm" - rds_LAST_UPDATE_TIME = "statisticsLastUpdateTime" - rds_LAST_CACHE_TIME = "statisticsLastCacheTime" + rds_NCOV_STATISTIC_KEY = "nCovStatistic" + rds_CHINA_TOTAL_KEY = "chinaTotal" + rds_CHINA_ADD_KEY = "chinaAdd" + rds_COUNTRY_LEVEL_KEY = "countryLevel" + rds_COUNTRY_LEVEL_CHILD_KEY = "countryLevelChild" + rds_PROVINCE_LEVEL_CHILD_KEY = "provinceLevelChild" + rds_PROVINCE_LEVEL_NOW_CONFIRM_KEY = "provinceLevelNowConfirm" + rds_PROVINCE_LEVEL_TODAY_CONFIRM_KEY = "provinceLevelTodayConfirm" + rds_PROVINCE_LEVEL_TOTAL_CONFIRM_KEY = "provinceLevelTotalConfirm" + rds_CITY_LEVEL_CHILD_KEY = "cityLevelChild" + rds_CITY_LEVEL_NOW_CONFIRM_KEY = "cityLevelNowConfirm" + rds_CITY_LEVEL_TODAY_CONFIRM_KEY = "cityLevelTodayConfirm" + rds_CITY_LEVEL_TOTAL_CONFIRM_KEY = "cityLevelTotalConfirm" + rds_LAST_UPDATE_TIME = "statisticsLastUpdateTime" + rds_LAST_CACHE_TIME = "statisticsLastCacheTime" SORT_TODAY_CONFIRM = "today" SORT_TOTAL_CONFIRM = "total" @@ -80,8 +80,8 @@ func cacheLevelInfo(data map[string]interface{}) { // Get Every Level's Info var countryLevels []models.AreaInfo json.Unmarshal(areaTree, &countryLevels) - provienceLevels := children(countryLevels) - cityLevels := children(provienceLevels) + provinceLevels := children(countryLevels) + cityLevels := children(provinceLevels) // Country Level Area Info With Child cacheList(rds_COUNTRY_LEVEL_CHILD_KEY, areaInfoToJson(countryLevels)...) @@ -89,34 +89,34 @@ func cacheLevelInfo(data map[string]interface{}) { areaInfoChildNil(&countryLevels) cacheList(rds_COUNTRY_LEVEL_KEY, areaInfoToJson(countryLevels)...) - // Provience Level Area Info With Child - cacheList(rds_PROVIENCE_LEVEL_CHILD_KEY, areaInfoToJson(provienceLevels)...) - areaInfoChildNil(&provienceLevels) + // Province Level Area Info With Child + cacheList(rds_PROVINCE_LEVEL_CHILD_KEY, areaInfoToJson(provinceLevels)...) + areaInfoChildNil(&provinceLevels) // City Level Area Info With Child cacheList(rds_CITY_LEVEL_CHILD_KEY, areaInfoToJson(cityLevels)...) - areaInfoChildNil(&provienceLevels) + areaInfoChildNil(&provinceLevels) - // Provience Level Area Info Sorted by Now Confirm - provienceLevelsSlice := AreaSlice(provienceLevels) - sort.Sort(provienceLevelsSlice) - cacheList(rds_PROVIENCE_LEVEL_NOW_CONFIRM_KEY, areaInfoToJson(provienceLevelsSlice)...) + // Province Level Area Info Sorted by Now Confirm + provinceLevelsSlice := AreaSlice(provinceLevels) + sort.Sort(provinceLevelsSlice) + cacheList(rds_PROVINCE_LEVEL_NOW_CONFIRM_KEY, areaInfoToJson(provinceLevelsSlice)...) // City Level Area Info Sorted By Now Confirm cityLevelsSlice := AreaSlice(cityLevels) sort.Sort(cityLevelsSlice) cacheList(rds_CITY_LEVEL_NOW_CONFIRM_KEY, areaInfoToJson(cityLevelsSlice)...) sortBy = SORT_TODAY_CONFIRM - // Provience Level Area Info Sorted by Today Confirm - sort.Sort(provienceLevelsSlice) - cacheList(rds_PROVIENCE_LEVEL_TODAY_CONFIRM_KEY, areaInfoToJson(provienceLevelsSlice)...) + // Province Level Area Info Sorted by Today Confirm + sort.Sort(provinceLevelsSlice) + cacheList(rds_PROVINCE_LEVEL_TODAY_CONFIRM_KEY, areaInfoToJson(provinceLevelsSlice)...) // City Level Area Info Sorted by Today Confirm sort.Sort(cityLevelsSlice) cacheList(rds_CITY_LEVEL_TODAY_CONFIRM_KEY, areaInfoToJson(cityLevelsSlice)...) sortBy = SORT_TOTAL_CONFIRM - // Provience Level Area Info Sorted by Total Confirm - sort.Sort(provienceLevelsSlice) - cacheList(rds_PROVIENCE_LEVEL_TOTAL_CONFIRM_KEY, areaInfoToJson(provienceLevelsSlice)...) + // Province Level Area Info Sorted by Total Confirm + sort.Sort(provinceLevelsSlice) + cacheList(rds_PROVINCE_LEVEL_TOTAL_CONFIRM_KEY, areaInfoToJson(provinceLevelsSlice)...) // City Level Area Info Sorted by Total Confirm sort.Sort(cityLevelsSlice) cacheList(rds_CITY_LEVEL_TOTAL_CONFIRM_KEY, areaInfoToJson(cityLevelsSlice)...) @@ -136,6 +136,7 @@ func children(parents []models.AreaInfo) []models.AreaInfo { } for _, item := range parent.Children { name := item.Name + item.Parent = parent.Name if !strings.Contains(name, json_FOREIGN_CITY) && !strings.Contains(name, json_FOREIGN_COUNTRY) && !strings.Contains(name, json_TO_BE_CONFIRM) { areaInfos = append(areaInfos, item) } diff --git a/service/statistics/statistics.go b/service/statistics/statistics.go index c77a99a..501a57c 100644 --- a/service/statistics/statistics.go +++ b/service/statistics/statistics.go @@ -7,18 +7,18 @@ import ( "strings" ) -func GetAllProvienceData(sort string) []interface{} { +func GetAllProvinceData(sort string) []interface{} { checkCache() if sort == SORT_TODAY_CONFIRM { - return getEntireRedisList(rds_PROVIENCE_LEVEL_TODAY_CONFIRM_KEY) + return getEntireRedisList(rds_PROVINCE_LEVEL_TODAY_CONFIRM_KEY) } if sort == SORT_TOTAL_CONFIRM { - return getEntireRedisList(rds_PROVIENCE_LEVEL_TOTAL_CONFIRM_KEY) + return getEntireRedisList(rds_PROVINCE_LEVEL_TOTAL_CONFIRM_KEY) } if sort == SORT_NOW_CONFIRM { - return getEntireRedisList(rds_PROVIENCE_LEVEL_NOW_CONFIRM_KEY) + return getEntireRedisList(rds_PROVINCE_LEVEL_NOW_CONFIRM_KEY) } - return getEntireRedisList(rds_PROVIENCE_LEVEL_CHILD_KEY) + return getEntireRedisList(rds_PROVINCE_LEVEL_CHILD_KEY) } func GetAllCityData(sort string) []interface{} {