123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package service
-
- import (
- "SSO/models"
- "SSO/utils"
-
- "github.com/jinzhu/gorm"
- )
-
- func GetAllProvince() []*models.District {
- var province []*models.District
- err := readUserDb.Model(&models.District{}).Where("level = 1").Find(&province).Error
- if err != nil {
- utils.ErrorLog("获取所有省份失败: %v", err)
- return nil
- } else {
- return province
- }
- }
-
- func GetCitiesWithProvinceID(provinceID int) []*models.District {
- var cities []*models.District
- err := readUserDb.Model(&models.District{}).Where("level = 2 AND upid = ?", provinceID).Find(&cities).Error
- if err != nil {
- utils.ErrorLog("获取省份 id 为 %v 下的城市失败: %v", provinceID, err)
- return nil
- } else {
- return cities
- }
- }
-
- func GetDistrictsWithCityID(cityID int) []*models.District {
- var districts []*models.District
- err := readUserDb.Model(&models.District{}).Where("level = 3 AND upid = ?", cityID).Find(&districts).Error
- if err != nil {
- utils.ErrorLog("获取城市 id 为 %v 下的区县失败: %v", cityID, err)
- return nil
- } else {
- return districts
- }
- }
-
- func GetProvinceWithName(name string) (*models.District, error) {
- var province models.District
- err := readUserDb.Model(&models.District{}).Where("level = 1 AND name like ?", "%"+name+"%").First(&province).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return &province, nil
- }
-
- func GetCityWithName(provinceID int64, cityName string) (*models.District, error) {
- var city models.District
- err := readUserDb.Model(&models.District{}).Where("level = 2 AND upid = ? AND name like ?", provinceID, "%"+cityName+"%").First(&city).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return &city, nil
- }
-
- func GetDistrictWithName(cityID int64, name string) (*models.District, error) {
- var district models.District
- err := readUserDb.Model(&models.District{}).Where("level = 3 AND upid = ? AND name like ?", cityID, "%"+name+"%").First(&district).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return &district, nil
- }
|