package myselfsrv import ( "Cold_Logistic/internal/pkg/common/codex" "Cold_Logistic/internal/pkg/common/constant" "Cold_Logistic/internal/server/domain/domainservice" "Cold_Logistic/internal/server/infra/dao" "Cold_Logistic/internal/server/infra/models" "context" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" ) // AddressPage 地址列表 func (srv *MyselfService) AddressPage(ctx context.Context, req AddressPageReqVO) (res core.PageListResponse, err error) { dto := dao.AddressBookPageDTO{ Page: req.Page, AddressType: req.AddressType, } list, total, err := srv.store.AddressBook().Page(ctx, dto) if err != nil { return res, errors.WithStackOnce(err) } res.Total = total res.TotalPage = total / int64(req.Page.Size) res.Count = len(list) res.List = list return res, nil } // AddAddress 添加地址 func (srv *MyselfService) AddAddress(ctx context.Context, req AddAddressReqVO) (res AddressIdRespVO, err error) { address := models.AddressBook{ AddressType: req.AddressType, Name: req.Name, Phone: req.Phone, ProvinceId: req.ProvinceId, CityId: req.CityId, RegionId: req.RegionId, Address: req.Address, IsDefault: req.IsDefault, } err = srv.store.InTx(ctx, func(ctx context.Context) error { err = domainservice.AddMyselfAddressTx(ctx, &address) if err != nil { return errors.WithStackOnce(err) } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.AddressId = address.Id return res, nil } // UpdateAddress 修改地址 func (srv *MyselfService) UpdateAddress(ctx context.Context, req UpdateAddressReqVO) (res AddressIdRespVO, err error) { address := models.AddressBook{} // todo 去重 ? err = srv.store.InTx(ctx, func(ctx context.Context) error { if err = srv.store.AddressBook().FirstById(ctx, &address, req.AddressId); err != nil { return errors.WithCodeOnce(err, codex.ErrParamValidate, "未找到数据") } address.Name = req.Name address.Phone = req.Phone address.ProvinceId = req.ProvinceId address.CityId = req.CityId address.RegionId = req.RegionId address.Address = req.Address address.IsDefault = req.IsDefault if err = srv.store.AddressBook().Save(ctx, &address); err != nil { return errors.WithStackOnce(err) } // 只能有一个默认 if address.IsDefault == constant.YES { err = srv.store.AddressBook().UpdateDefaultByCreateBy(ctx, constant.NO, address.Id) if err != nil { return errors.WithStackOnce(err) } } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.AddressId = address.Id return res, nil } // DeleteAddress 删除地址 func (srv *MyselfService) DeleteAddress(ctx context.Context, req DeleteAddressReqVO) error { if err := srv.store.AddressBook().DeleteByIds(ctx, req.AddressId); err != nil { return errors.WithStackOnce(err) } return nil }