region.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/server/infra/models"
  4. "context"
  5. )
  6. // RegionStore 省市区表
  7. type RegionStore interface {
  8. DbBaseStore
  9. FindAll(ctx context.Context) ([]models.Region, error)
  10. FindNameMapByIds(ctx context.Context, ids []int) (map[int]string, error)
  11. }
  12. var _ RegionStore = &region{}
  13. type region struct {
  14. dbBase
  15. }
  16. func newRegion(ds *DataStore) *region {
  17. return &region{dbBase: dbBase{
  18. store: ds,
  19. baseEntity: &models.Region{},
  20. }}
  21. }
  22. func (ds *DataStore) Region() RegionStore {
  23. return newRegion(ds)
  24. }
  25. func (a *region) FindAll(ctx context.Context) ([]models.Region, error) {
  26. db := a.store.optionDB(ctx)
  27. stmt := db.Model(&models.Region{}).Where("deleted = ?", models.DeleteNo)
  28. var ret []models.Region
  29. stmt.Order("level ASC")
  30. if err := stmt.Find(&ret).Error; err != nil {
  31. return nil, err
  32. }
  33. return ret, nil
  34. }
  35. func (a *region) FindNameMapByIds(ctx context.Context, ids []int) (map[int]string, error) {
  36. db := a.store.optionDB(ctx)
  37. stmt := db.Model(&models.Region{}).Where("deleted = ?", models.DeleteNo)
  38. stmt.Select("id, name")
  39. var ents []models.Region
  40. stmt.Order("level ASC")
  41. if err := stmt.Find(&ents).Error; err != nil {
  42. return nil, err
  43. }
  44. ret := make(map[int]string, len(ids))
  45. for _, v := range ents {
  46. ret[v.Id] = v.Name
  47. }
  48. return ret, nil
  49. }