package service import ( "gopkg.in/guregu/null.v3" "xiaoniaokuaiyan.com/xiaoniao/dal" "xiaoniaokuaiyan.com/xiaoniao/entity" "xiaoniaokuaiyan.com/xiaoniao/util" ) type CityService struct { dal.ICity } func (csrv *CityService) GetOpenCityList() (interface{}, error) { return csrv.ICity.GetOpenList() } func (csrv *CityService) GetCityListById(ids []int) (interface{}, error) { return csrv.ICity.GetCityListById(ids) } func (csrv *CityService) GetCountyByCityId(cityId int) (interface{}, error) { return csrv.ICity.GetCountyByCityId(cityId) } func (csrv *CityService) GetCityAllList() (interface{}, error) { cityAll, err := csrv.ICity.GetCityAllList() var provinceList = []entity.Province{} //var cityList = []entity.City{} //var countryList = []entity.County{} if err != nil { return cityAll, err } else { for _, v := range cityAll { if !_checkRepeatProvince(v.ProvinceId, provinceList) { provinceList = append(provinceList, entity.Province{Id: v.ProvinceId, Name: v.Province, Cities: []entity.City2{}}) } } for _, v := range cityAll { for k1, v1 := range provinceList { if v.ProvinceId == provinceList[k1].Id { if !_checkRepeatCity(v.CityId, v1.Cities) { provinceList[k1].Cities = append(provinceList[k1].Cities, entity.City2{Id: v.CityId, Name: v.City, Counties: []entity.County2{}}) } } } } for _, v := range cityAll { for k1, _ := range provinceList { if v.ProvinceId == provinceList[k1].Id { for k2, _ := range provinceList[k1].Cities { if v.CityId == provinceList[k1].Cities[k2].Id { provinceList[k1].Cities[k2].Counties = append(provinceList[k1].Cities[k2].Counties, entity.County2{Id: v.CountryId, Name: v.Country}) } } } } } return provinceList, err } } func _checkRepeatProvince(id int, list []entity.Province) bool { if len(list) == 0 { return false } for _, v := range list { if v.Id == id { return true } } return false } func _checkRepeatCity(id int, list []entity.City2) bool { if len(list) == 0 { return false } for _, v := range list { if v.Id == id { return true } } return false } func (csrv *CityService) GetCityByShortName(shortName string) (entity.City, error) { db := util.GetSqlDB() strSql := "select * from t_city where shortname =?" var city = entity.City{} err := db.Get(&city, strSql, shortName) return city, err } // 获取 开通城市以及区县 func (csrv *CityService) GetCityOpenCounty() (interface{}, error) { //citys, err := csrv.ICity.GetOpenList() //if err != nil { // return nil, err //} db := util.GetSqlDB() strSql := "select t1.*,t2.name as province from t_city t1 left join t_province t2 on t1.province_id = t2.id where is_open = 1" var citys = []entity.CityNode{} err := db.Select(&citys, strSql) if err != nil { return nil, err } for k, v := range citys { countys, err := csrv.ICity.GetCountyByCityId(v.Id) countyResult := []entity.CountyNode{} for i := range countys { item := entity.CountyNode{ Id: countys[i].Id, Name: countys[i].Name, PName: null.StringFrom(""), } countyResult = append(countyResult, item) } if err != nil { return nil, err } //item := entity.CityWithProvince{ // Id: citys[k].Id, // Name: citys[k].Name, // Counties: countyResult, //} //result = append(result, item) citys[k].Counties = countyResult } //return result, err return citys, nil }