city_service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "gopkg.in/guregu/null.v3"
  4. "xiaoniaokuaiyan.com/xiaoniao/dal"
  5. "xiaoniaokuaiyan.com/xiaoniao/entity"
  6. "xiaoniaokuaiyan.com/xiaoniao/util"
  7. )
  8. type CityService struct {
  9. dal.ICity
  10. }
  11. func (csrv *CityService) GetOpenCityList() (interface{}, error) {
  12. return csrv.ICity.GetOpenList()
  13. }
  14. func (csrv *CityService) GetCityListById(ids []int) (interface{}, error) {
  15. return csrv.ICity.GetCityListById(ids)
  16. }
  17. func (csrv *CityService) GetCountyByCityId(cityId int) (interface{}, error) {
  18. return csrv.ICity.GetCountyByCityId(cityId)
  19. }
  20. func (csrv *CityService) GetCityAllList() (interface{}, error) {
  21. cityAll, err := csrv.ICity.GetCityAllList()
  22. var provinceList = []entity.Province{}
  23. //var cityList = []entity.City{}
  24. //var countryList = []entity.County{}
  25. if err != nil {
  26. return cityAll, err
  27. } else {
  28. for _, v := range cityAll {
  29. if !_checkRepeatProvince(v.ProvinceId, provinceList) {
  30. provinceList = append(provinceList, entity.Province{Id: v.ProvinceId, Name: v.Province, Cities: []entity.City2{}})
  31. }
  32. }
  33. for _, v := range cityAll {
  34. for k1, v1 := range provinceList {
  35. if v.ProvinceId == provinceList[k1].Id {
  36. if !_checkRepeatCity(v.CityId, v1.Cities) {
  37. provinceList[k1].Cities = append(provinceList[k1].Cities, entity.City2{Id: v.CityId, Name: v.City, Counties: []entity.County2{}})
  38. }
  39. }
  40. }
  41. }
  42. for _, v := range cityAll {
  43. for k1, _ := range provinceList {
  44. if v.ProvinceId == provinceList[k1].Id {
  45. for k2, _ := range provinceList[k1].Cities {
  46. if v.CityId == provinceList[k1].Cities[k2].Id {
  47. provinceList[k1].Cities[k2].Counties = append(provinceList[k1].Cities[k2].Counties, entity.County2{Id: v.CountryId, Name: v.Country})
  48. }
  49. }
  50. }
  51. }
  52. }
  53. return provinceList, err
  54. }
  55. }
  56. func _checkRepeatProvince(id int, list []entity.Province) bool {
  57. if len(list) == 0 {
  58. return false
  59. }
  60. for _, v := range list {
  61. if v.Id == id {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. func _checkRepeatCity(id int, list []entity.City2) bool {
  68. if len(list) == 0 {
  69. return false
  70. }
  71. for _, v := range list {
  72. if v.Id == id {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. func (csrv *CityService) GetCityByShortName(shortName string) (entity.City, error) {
  79. db := util.GetSqlDB()
  80. strSql := "select * from t_city where shortname =?"
  81. var city = entity.City{}
  82. err := db.Get(&city, strSql, shortName)
  83. return city, err
  84. }
  85. // 获取 开通城市以及区县
  86. func (csrv *CityService) GetCityOpenCounty() (interface{}, error) {
  87. //citys, err := csrv.ICity.GetOpenList()
  88. //if err != nil {
  89. // return nil, err
  90. //}
  91. db := util.GetSqlDB()
  92. 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"
  93. var citys = []entity.CityNode{}
  94. err := db.Select(&citys, strSql)
  95. if err != nil {
  96. return nil, err
  97. }
  98. for k, v := range citys {
  99. countys, err := csrv.ICity.GetCountyByCityId(v.Id)
  100. countyResult := []entity.CountyNode{}
  101. for i := range countys {
  102. item := entity.CountyNode{
  103. Id: countys[i].Id,
  104. Name: countys[i].Name,
  105. PName: null.StringFrom(""),
  106. }
  107. countyResult = append(countyResult, item)
  108. }
  109. if err != nil {
  110. return nil, err
  111. }
  112. //item := entity.CityWithProvince{
  113. // Id: citys[k].Id,
  114. // Name: citys[k].Name,
  115. // Counties: countyResult,
  116. //}
  117. //result = append(result, item)
  118. citys[k].Counties = countyResult
  119. }
  120. //return result, err
  121. return citys, nil
  122. }