2026-08-09 10:43:45 +08:00
|
|
|
package library
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-09-22 21:15:34 +08:00
|
|
|
"errors"
|
2026-08-09 10:43:45 +08:00
|
|
|
"time"
|
|
|
|
|
|
2026-08-10 11:35:48 +08:00
|
|
|
"bsm/full/module/ec/address/internal/impl"
|
2026-08-09 12:41:08 +08:00
|
|
|
"bsm/full/module/ec/address/internal/models"
|
|
|
|
|
pb "bsm/full/module/ec/address/pb"
|
2026-08-10 11:35:48 +08:00
|
|
|
|
2026-08-09 10:43:45 +08:00
|
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/printer"
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/service"
|
2026-09-22 21:15:34 +08:00
|
|
|
"gorm.io/gorm"
|
2026-08-09 10:43:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 修改地址
|
|
|
|
|
func Modify(ctx context.Context, in *pb.AddressItem) (reply *pb.StatusReply, err error) {
|
|
|
|
|
// parse authorization meta.
|
|
|
|
|
auth, err := service.ParseMetaCtx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
address := &models.AddressLibrary{
|
|
|
|
|
Country: in.Country,
|
|
|
|
|
Phone: in.Phone,
|
|
|
|
|
Province: in.Province,
|
|
|
|
|
City: in.City,
|
|
|
|
|
Area: in.Area,
|
|
|
|
|
Detail: in.Detail,
|
|
|
|
|
Contact: in.Contact,
|
|
|
|
|
}
|
|
|
|
|
address.Status = int8(in.Status)
|
|
|
|
|
|
2026-09-22 21:15:34 +08:00
|
|
|
// 归属校验与默认地址重置放入同一事务:先按 id + owner 更新目标行,再取消该用户其它默认地址
|
|
|
|
|
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
res := tx.Model(&models.AddressLibrary{}).Where("id=? and owner_identity=?", in.Id, auth.Identity).Updates(&address)
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
return res.Error
|
|
|
|
|
}
|
|
|
|
|
if res.RowsAffected == 0 {
|
|
|
|
|
return models.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
if address.Status == 2 {
|
|
|
|
|
return tx.Model(&models.AddressLibrary{}).Where("owner_id = ? and status = 2 and id <> ?", auth.ID, in.Id).UpdateColumn("status", 1).Error
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2026-08-09 10:43:45 +08:00
|
|
|
if err != nil {
|
2026-09-22 21:15:34 +08:00
|
|
|
if errors.Is(err, models.ErrNotFound) {
|
|
|
|
|
return nil, errcode.ErrRecordNotFound
|
|
|
|
|
}
|
2026-08-09 10:43:45 +08:00
|
|
|
printer.Error(err.Error())
|
|
|
|
|
return nil, errcode.ErrDB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.StatusReply{
|
|
|
|
|
Code: 0,
|
|
|
|
|
Message: "OK",
|
|
|
|
|
Timeseq: time.Now().UnixNano(),
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
|
|
}
|