address_book.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package domainservice
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/server/infra/dao"
  6. "Cold_Logistic/internal/server/infra/models"
  7. "context"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  9. )
  10. // AddMyselfAddressTx 添加地址
  11. func AddMyselfAddressTx(ctx context.Context, address *models.AddressBook) error {
  12. store := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB)
  13. if address.ProvinceName == "" || address.CityName == "" || address.RegionName == "" {
  14. nameMap, err := store.Region().FindNameMapByIds(ctx, []int{address.ProvinceId, address.CityId, address.RegionId})
  15. if err != nil {
  16. return errors.WithStackOnce(err)
  17. }
  18. address.ProvinceName = nameMap[address.ProvinceId]
  19. address.CityName = nameMap[address.CityId]
  20. address.RegionName = nameMap[address.RegionId]
  21. }
  22. if err := store.AddressBook().Create(ctx, address); err != nil {
  23. return errors.WithStackOnce(err)
  24. }
  25. // 只能有一个默认
  26. if address.IsDefault == constant.YES {
  27. err := store.AddressBook().UpdateDefaultByCreateBy(ctx, constant.NO, address.Id)
  28. if err != nil {
  29. return errors.WithStackOnce(err)
  30. }
  31. }
  32. return nil
  33. }