address_book.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  4. "Cold_Logistic/internal/server/infra/models"
  5. "context"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/sliceutil"
  9. "time"
  10. )
  11. // AddressBookStore 发件人表
  12. type AddressBookStore interface {
  13. DbBaseStore
  14. BatchSave(ctx context.Context, values []*models.AddressBook, omit ...string) error
  15. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.AddressBook, error)
  16. Page(ctx context.Context, dto AddressBookPageDTO) ([]AddressBookPageVO, int64, error)
  17. UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error
  18. FindByIds(ctx context.Context, ids []int) ([]models.AddressBook, error)
  19. }
  20. var _ AddressBookStore = &addressBook{}
  21. type addressBook struct {
  22. dbBase
  23. }
  24. func newAddressBook(ds *DataStore) *addressBook {
  25. return &addressBook{dbBase: dbBase{
  26. store: ds,
  27. baseEntity: &models.AddressBook{},
  28. }}
  29. }
  30. func (ds *DataStore) AddressBook() AddressBookStore {
  31. return newAddressBook(ds)
  32. }
  33. func (a *addressBook) BatchSave(ctx context.Context, values []*models.AddressBook, omit ...string) error {
  34. if len(values) == 0 {
  35. return nil
  36. }
  37. db := a.store.optionDB(ctx)
  38. tokenInfo := global.GetTokenInfoFromContext(ctx)
  39. for i := range values {
  40. if values[i].Id == 0 {
  41. values[i].CreatedBy = tokenInfo.AccountId
  42. values[i].CreatedTime.Time = time.Now()
  43. values[i].Deleted = models.DeleteNo
  44. } else {
  45. values[i].UpdatedBy = tokenInfo.AccountId
  46. values[i].UpdatedTime.Time = time.Now()
  47. }
  48. }
  49. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  50. }
  51. func (a *addressBook) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.AddressBook, error) {
  52. res := make(map[int]models.AddressBook, len(ids))
  53. if len(ids) == 0 {
  54. return res, nil
  55. }
  56. var list []models.AddressBook
  57. db := a.store.optionDB(ctx)
  58. err := db.Table(a.baseEntity.TableName()).
  59. Where("deleted = ?", models.DeleteNo).
  60. Where("id IN (?)", ids).
  61. Omit(omit...).
  62. Find(&list).Error
  63. if err != nil {
  64. return res, err
  65. }
  66. for i := range list {
  67. res[list[i].Id] = list[i]
  68. }
  69. return res, nil
  70. }
  71. type AddressBookPageDTO struct {
  72. Page core.Page
  73. AddressType string // 地址类型
  74. }
  75. type AddressBookPageVO struct {
  76. AddressId int `gorm:"column:id" json:"addressId"` //
  77. AddressType string `gorm:"column:address_type" json:"addressType"` // 地址类型:sender-发货人 consignee-收货人
  78. Name string `gorm:"column:name" json:"name"` // 姓名
  79. Phone string `gorm:"column:phone" json:"phone"` // 联系电话
  80. ProvinceId int `gorm:"column:province_id" json:"provinceId"` // 省Id
  81. Province string `gorm:"-" json:"province"` // 省名称
  82. CityId int `gorm:"column:city_id" json:"cityId"` // 市Id
  83. City string `gorm:"-" json:"city"` // 市名称
  84. RegionId int `gorm:"column:region_id" json:"regionId"` // 区Id
  85. Region string `gorm:"-" json:"region"` // 地区名称
  86. Address string `gorm:"column:address" json:"address"` // 详细地址
  87. IsDefault int `gorm:"column:is_default" json:"isDefault"` // 是否默认:1-是 2-否
  88. }
  89. func (a *addressBook) Page(ctx context.Context, dto AddressBookPageDTO) ([]AddressBookPageVO, int64, error) {
  90. db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by"))
  91. stmt := db.Model(&models.AddressBook{}).
  92. Where("deleted = ?", models.DeleteNo).
  93. Where("address_type = ?", dto.AddressType)
  94. stmt.Select("id, address_type, name, phone, province_id, city_id, region_id, address, is_default")
  95. var total int64
  96. var ret []AddressBookPageVO
  97. if err := stmt.Count(&total).Error; err != nil {
  98. return nil, 0, errors.WithStackOnce(err)
  99. }
  100. if total == 0 {
  101. return ret, total, nil
  102. }
  103. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  104. stmt.Order("is_default ASC, id DESC")
  105. if err := stmt.Find(&ret).Error; err != nil {
  106. return nil, 0, errors.WithStackOnce(err)
  107. }
  108. regionIds := make([]int, 0, total/3)
  109. for _, v := range ret {
  110. if v.ProvinceId > 0 && !sliceutil.ContainInt(regionIds, v.ProvinceId) {
  111. regionIds = append(regionIds, v.ProvinceId)
  112. }
  113. if v.CityId > 0 && !sliceutil.ContainInt(regionIds, v.CityId) {
  114. regionIds = append(regionIds, v.CityId)
  115. }
  116. if v.RegionId > 0 && !sliceutil.ContainInt(regionIds, v.RegionId) {
  117. regionIds = append(regionIds, v.RegionId)
  118. }
  119. }
  120. regionName, err := a.store.Region().FindNameMapByIds(ctx, regionIds)
  121. if err != nil {
  122. return nil, 0, errors.WithStackOnce(err)
  123. }
  124. for i := range ret {
  125. ret[i].Province = regionName[ret[i].ProvinceId]
  126. ret[i].City = regionName[ret[i].CityId]
  127. ret[i].Region = regionName[ret[i].RegionId]
  128. }
  129. return ret, total, nil
  130. }
  131. // UpdateDefaultByCreateBy 修改我的默认选中
  132. func (a *addressBook) UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error {
  133. accId := global.GetTokenInfoFromContext(ctx).AccountId
  134. ent := models.AddressBook{
  135. UpdatedTime: models.NewMyTime(time.Now()),
  136. UpdatedBy: accId,
  137. IsDefault: isDefault,
  138. }
  139. stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
  140. Where("deleted = ?", models.DeleteNo).
  141. Where("created_by = ?", accId)
  142. if len(excludeId) > 0 {
  143. stmt.Where("id NOT IN(?)", excludeId)
  144. }
  145. err := stmt.Updates(&ent).Error
  146. return errors.WithStackOnce(err)
  147. }
  148. // FindByIds id批量查
  149. func (a *addressBook) FindByIds(ctx context.Context, ids []int) ([]models.AddressBook, error) {
  150. ret := make([]models.AddressBook, 0, len(ids))
  151. err := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
  152. Where("deleted = ?", models.DeleteNo).
  153. Where("id IN(?)", ids).Find(&ret).Error
  154. return ret, errors.WithStackOnce(err)
  155. }