address_service.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package myselfsrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/constant"
  5. "Cold_Logistic/internal/server/domain/domainservice"
  6. "Cold_Logistic/internal/server/infra/dao"
  7. "Cold_Logistic/internal/server/infra/models"
  8. "context"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  11. )
  12. // AddressPage 地址列表
  13. func (srv *MyselfService) AddressPage(ctx context.Context, req AddressPageReqVO) (res core.PageListResponse, err error) {
  14. dto := dao.AddressBookPageDTO{
  15. Page: req.Page,
  16. AddressType: req.AddressType,
  17. }
  18. list, total, err := srv.store.AddressBook().Page(ctx, dto)
  19. if err != nil {
  20. return res, errors.WithStackOnce(err)
  21. }
  22. res.Total = total
  23. res.TotalPage = total / int64(req.Page.Size)
  24. res.Count = len(list)
  25. res.List = list
  26. return res, nil
  27. }
  28. // AddAddress 添加地址
  29. func (srv *MyselfService) AddAddress(ctx context.Context, req AddAddressReqVO) (res AddressIdRespVO, err error) {
  30. address := models.AddressBook{
  31. AddressType: req.AddressType,
  32. Name: req.Name,
  33. Phone: req.Phone,
  34. ProvinceId: req.ProvinceId,
  35. CityId: req.CityId,
  36. RegionId: req.RegionId,
  37. Address: req.Address,
  38. IsDefault: req.IsDefault,
  39. }
  40. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  41. err = domainservice.AddMyselfAddressTx(ctx, &address)
  42. if err != nil {
  43. return errors.WithStackOnce(err)
  44. }
  45. return nil
  46. })
  47. if err != nil {
  48. return res, errors.WithStackOnce(err)
  49. }
  50. res.AddressId = address.Id
  51. return res, nil
  52. }
  53. // UpdateAddress 修改地址
  54. func (srv *MyselfService) UpdateAddress(ctx context.Context, req UpdateAddressReqVO) (res AddressIdRespVO, err error) {
  55. address := models.AddressBook{}
  56. // todo 去重 ?
  57. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  58. if err = srv.store.AddressBook().FirstById(ctx, &address, req.AddressId); err != nil {
  59. return errors.WithCodeOnce(err, codex.ErrParamValidate, "未找到数据")
  60. }
  61. address.Name = req.Name
  62. address.Phone = req.Phone
  63. address.ProvinceId = req.ProvinceId
  64. address.CityId = req.CityId
  65. address.RegionId = req.RegionId
  66. address.Address = req.Address
  67. address.IsDefault = req.IsDefault
  68. if err = srv.store.AddressBook().Save(ctx, &address); err != nil {
  69. return errors.WithStackOnce(err)
  70. }
  71. // 只能有一个默认
  72. if address.IsDefault == constant.YES {
  73. err = srv.store.AddressBook().UpdateDefaultByCreateBy(ctx, constant.NO, address.Id)
  74. if err != nil {
  75. return errors.WithStackOnce(err)
  76. }
  77. }
  78. return nil
  79. })
  80. if err != nil {
  81. return res, errors.WithStackOnce(err)
  82. }
  83. res.AddressId = address.Id
  84. return res, nil
  85. }
  86. // DeleteAddress 删除地址
  87. func (srv *MyselfService) DeleteAddress(ctx context.Context, req DeleteAddressReqVO) error {
  88. if err := srv.store.AddressBook().DeleteByIds(ctx, req.AddressId); err != nil {
  89. return errors.WithStackOnce(err)
  90. }
  91. return nil
  92. }