district_service.go 2.3KB

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