123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package dao
- import (
- "Cold_Logistic/internal/server/infra/models"
- "context"
- )
- // RegionStore 省市区表
- type RegionStore interface {
- DbBaseStore
- FindAll(ctx context.Context) ([]models.Region, error)
- FindNameMapByIds(ctx context.Context, ids []int) (map[int]string, error)
- }
- var _ RegionStore = ®ion{}
- type region struct {
- dbBase
- }
- func newRegion(ds *DataStore) *region {
- return ®ion{dbBase: dbBase{
- store: ds,
- baseEntity: &models.Region{},
- }}
- }
- func (ds *DataStore) Region() RegionStore {
- return newRegion(ds)
- }
- func (a *region) FindAll(ctx context.Context) ([]models.Region, error) {
- db := a.store.optionDB(ctx)
- stmt := db.Model(&models.Region{}).Where("deleted = ?", models.DeleteNo)
- var ret []models.Region
- stmt.Order("level ASC")
- if err := stmt.Find(&ret).Error; err != nil {
- return nil, err
- }
- return ret, nil
- }
- func (a *region) FindNameMapByIds(ctx context.Context, ids []int) (map[int]string, error) {
- db := a.store.optionDB(ctx)
- stmt := db.Model(&models.Region{}).Where("deleted = ?", models.DeleteNo)
- stmt.Select("id, name")
- var ents []models.Region
- stmt.Order("level ASC")
- if err := stmt.Find(&ents).Error; err != nil {
- return nil, err
- }
- ret := make(map[int]string, len(ids))
- for _, v := range ents {
- ret[v.Id] = v.Name
- }
- return ret, nil
- }
|