district_service.go 2.5KB

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