city_dal.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dal
  2. import (
  3. "xiaoniaokuaiyan.com/xiaoniao/entity"
  4. "xiaoniaokuaiyan.com/xiaoniao/util"
  5. )
  6. type City struct{}
  7. func (c *City) GetOpenList() ([]entity.City, error) {
  8. db := util.GetSqlDB()
  9. strSql := "select * from t_city where is_open = 1"
  10. var cityList = []entity.City{}
  11. err := db.Select(&cityList, strSql)
  12. if err != nil {
  13. return nil, err
  14. }
  15. return cityList, nil
  16. }
  17. func (c *City) GetCityListById(ids []int) ([]entity.City, error) {
  18. strIds := util.IntJoin(ids, ",")
  19. db := util.GetSqlDB()
  20. strSql := "select * from t_city where id in(" + strIds + ");"
  21. var cityList = []entity.City{}
  22. err := db.Select(&cityList, strSql)
  23. return cityList, err
  24. }
  25. func (c *City) GetCountyByCityId(cityId int) ([]entity.County, error) {
  26. db := util.GetSqlDB()
  27. strSql := "select * from t_county where city_id = ? and flag = 'Y';"
  28. var countyList = []entity.County{}
  29. err := db.Select(&countyList, strSql, cityId)
  30. return countyList, err
  31. }
  32. func (c *City) GetCityAllList() ([]entity.CityAll, error) {
  33. db := util.GetSqlDB()
  34. strSql := `SELECT
  35. t1.id AS province_id,
  36. t1.name AS province,
  37. t2.id AS city_id,
  38. t2.NAME AS city,
  39. t3.id AS country_id,
  40. t3.name AS country
  41. FROM
  42. t_province t1
  43. LEFT JOIN t_city t2 ON t1.id = t2.province_id
  44. LEFT JOIN t_county t3 ON t3.city_id = t2.id
  45. where t3.flag = 'Y'`
  46. var cityAll = []entity.CityAll{}
  47. err := db.Select(&cityAll, strSql)
  48. return cityAll, err
  49. }
  50. var DefaultCityDal = &City{}