优化管理后台
This commit is contained in:
@@ -69,6 +69,19 @@ func (s *GoodsService) Create(ctx context.Context, adminID int64, request *dto.G
|
||||
return nil, errcode.DatabaseError
|
||||
}
|
||||
|
||||
// 虚拟支付道具登记校验:已发布必须有道具 ID;道具 ID 全局唯一(防两个商品指向同一微信道具导致发货金额校验失败)。
|
||||
productID := strings.TrimSpace(request.ProductID)
|
||||
if request.ProductStatus == GoodsProductPublished && productID == "" {
|
||||
return nil, errcode.GoodsProductInvalid.WithDetails("发布状态为「已发布」时必须填写道具 ID")
|
||||
}
|
||||
if productID != "" {
|
||||
if _, err := s.goodsDao.GoodsGetByProductID(ctx, productID); err == nil {
|
||||
return nil, errcode.GoodsProductIDExists
|
||||
} else if !isNotFound(err) {
|
||||
return nil, errcode.DatabaseError
|
||||
}
|
||||
}
|
||||
|
||||
status := request.Status
|
||||
if status == 0 {
|
||||
status = GoodsStatusOffShelf // 新建默认下架,运营确认后再上架。
|
||||
@@ -88,6 +101,8 @@ func (s *GoodsService) Create(ctx context.Context, adminID int64, request *dto.G
|
||||
ValidDays: request.ValidDays,
|
||||
Description: request.Description,
|
||||
Status: status,
|
||||
ProductID: productID,
|
||||
ProductStatus: request.ProductStatus,
|
||||
}
|
||||
goods.CreatedBy = adminID
|
||||
goods.UpdatedBy = adminID
|
||||
@@ -104,13 +119,36 @@ func (s *GoodsService) Update(ctx context.Context, adminID int64, id int64, requ
|
||||
ctx, span := global.Tracer.Start(ctx, "svc.AdminGoodsUpdate")
|
||||
defer span.End()
|
||||
|
||||
if _, err := s.goodsDao.GoodsGetByID(ctx, id); err != nil {
|
||||
existing, err := s.goodsDao.GoodsGetByID(ctx, id)
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return errcode.GoodsNotFound
|
||||
}
|
||||
return errcode.DatabaseError
|
||||
}
|
||||
|
||||
// 虚拟支付道具登记:以「现有值 + 本次改动」的最终态校验,防止只改其中一个字段绕过校验。
|
||||
productID := existing.ProductID
|
||||
if request.ProductID != nil {
|
||||
productID = strings.TrimSpace(*request.ProductID)
|
||||
}
|
||||
productStatus := existing.ProductStatus
|
||||
if request.ProductStatus != nil {
|
||||
productStatus = *request.ProductStatus
|
||||
}
|
||||
if productStatus == GoodsProductPublished && productID == "" {
|
||||
return errcode.GoodsProductInvalid.WithDetails("发布状态为「已发布」时必须填写道具 ID")
|
||||
}
|
||||
if request.ProductID != nil && productID != "" {
|
||||
if other, derr := s.goodsDao.GoodsGetByProductID(ctx, productID); derr == nil {
|
||||
if other.ID != id {
|
||||
return errcode.GoodsProductIDExists
|
||||
}
|
||||
} else if !isNotFound(derr) {
|
||||
return errcode.DatabaseError
|
||||
}
|
||||
}
|
||||
|
||||
updates := map[string]any{"updated_by": adminID}
|
||||
if request.GoodsName != nil {
|
||||
updates["goods_name"] = strings.TrimSpace(*request.GoodsName)
|
||||
@@ -142,6 +180,12 @@ func (s *GoodsService) Update(ctx context.Context, adminID int64, id int64, requ
|
||||
if request.Description != nil {
|
||||
updates["description"] = *request.Description
|
||||
}
|
||||
if request.ProductID != nil {
|
||||
updates["product_id"] = productID
|
||||
}
|
||||
if request.ProductStatus != nil {
|
||||
updates["product_status"] = *request.ProductStatus
|
||||
}
|
||||
if len(updates) == 1 {
|
||||
return nil // 无可更新字段。
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user