sso

district_service.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package service
  2. import (
  3. "SSO/models"
  4. "SSO/utils"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func GetAllProvince() []*models.District {
  8. var province []*models.District
  9. err := readUserDb.Model(&models.District{}).Where("level = 1").Find(&province).Error
  10. if err != nil {
  11. utils.ErrorLog("获取所有省份失败: %v", err)
  12. return nil
  13. } else {
  14. return province
  15. }
  16. }
  17. func GetCitiesWithProvinceID(provinceID int) []*models.District {
  18. var cities []*models.District
  19. err := readUserDb.Model(&models.District{}).Where("level = 2 AND upid = ?", provinceID).Find(&cities).Error
  20. if err != nil {
  21. utils.ErrorLog("获取省份 id 为 %v 下的城市失败: %v", provinceID, err)
  22. return nil
  23. } else {
  24. return cities
  25. }
  26. }
  27. func GetDistrictsWithCityID(cityID int) []*models.District {
  28. var districts []*models.District
  29. err := readUserDb.Model(&models.District{}).Where("level = 3 AND upid = ?", cityID).Find(&districts).Error
  30. if err != nil {
  31. utils.ErrorLog("获取城市 id 为 %v 下的区县失败: %v", cityID, err)
  32. return nil
  33. } else {
  34. return districts
  35. }
  36. }
  37. func GetProvinceWithName(name string) (*models.District, error) {
  38. var province models.District
  39. err := readUserDb.Model(&models.District{}).Where("level = 1 AND name like ?", "%"+name+"%").First(&province).Error
  40. if err != nil {
  41. if err == gorm.ErrRecordNotFound {
  42. return nil, nil
  43. } else {
  44. return nil, err
  45. }
  46. }
  47. return &province, nil
  48. }
  49. func GetCityWithName(provinceID int64, cityName string) (*models.District, error) {
  50. var city models.District
  51. err := readUserDb.Model(&models.District{}).Where("level = 2 AND upid = ? AND name like ?", provinceID, "%"+cityName+"%").First(&city).Error
  52. if err != nil {
  53. if err == gorm.ErrRecordNotFound {
  54. return nil, nil
  55. } else {
  56. return nil, err
  57. }
  58. }
  59. return &city, nil
  60. }
  61. func GetDistrictWithName(cityID int64, name string) (*models.District, error) {
  62. var district models.District
  63. err := readUserDb.Model(&models.District{}).Where("level = 3 AND upid = ? AND name like ?", cityID, "%"+name+"%").First(&district).Error
  64. if err != nil {
  65. if err == gorm.ErrRecordNotFound {
  66. return nil, nil
  67. } else {
  68. return nil, err
  69. }
  70. }
  71. return &district, nil
  72. }