-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(product): upsert specific and options
- Loading branch information
Showing
15 changed files
with
355 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
internal/handler/admin/product/productspecific/configproductspecifichandler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package productspecific | ||
|
||
import ( | ||
"net/http" | ||
|
||
"PowerX/internal/logic/admin/product/productspecific" | ||
"PowerX/internal/svc" | ||
"PowerX/internal/types" | ||
"github.com/zeromicro/go-zero/rest/httpx" | ||
) | ||
|
||
func ConfigProductSpecificHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var req types.ConfigProductSpecificRequest | ||
if err := httpx.Parse(r, &req); err != nil { | ||
httpx.ErrorCtx(r.Context(), w, err) | ||
return | ||
} | ||
|
||
l := productspecific.NewConfigProductSpecificLogic(r.Context(), svcCtx) | ||
resp, err := l.ConfigProductSpecific(&req) | ||
if err != nil { | ||
httpx.ErrorCtx(r.Context(), w, err) | ||
} else { | ||
httpx.OkJsonCtx(r.Context(), w, resp) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
internal/logic/admin/product/productspecific/configproductspecificlogic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package productspecific | ||
|
||
import ( | ||
product2 "PowerX/internal/model/product" | ||
"PowerX/internal/types/errorx" | ||
"context" | ||
|
||
"PowerX/internal/svc" | ||
"PowerX/internal/types" | ||
|
||
"github.com/zeromicro/go-zero/core/logx" | ||
) | ||
|
||
type ConfigProductSpecificLogic struct { | ||
logx.Logger | ||
ctx context.Context | ||
svcCtx *svc.ServiceContext | ||
} | ||
|
||
func NewConfigProductSpecificLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ConfigProductSpecificLogic { | ||
return &ConfigProductSpecificLogic{ | ||
Logger: logx.WithContext(ctx), | ||
ctx: ctx, | ||
svcCtx: svcCtx, | ||
} | ||
} | ||
|
||
func (l *ConfigProductSpecificLogic) ConfigProductSpecific(req *types.ConfigProductSpecificRequest) (resp *types.ConfigProductSpecificReply, err error) { | ||
|
||
specifics := TransformProductSpecificsRequestToProductSpecifics(req.ProductSpecifics) | ||
options := GetOptionsFromProductSpecificsRequest(specifics) | ||
//fmt.Dump(specifics) | ||
err = l.svcCtx.PowerX.ProductSpecific.ConfigProductSpecific(l.ctx, specifics, options) | ||
//specifics, err = l.svcCtx.PowerX.ProductSpecific.UpsertProductSpecifics(l.ctx, specifics) | ||
if err != nil { | ||
return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) | ||
} | ||
|
||
// ReFact product skus | ||
product, err := l.svcCtx.PowerX.Product.GetProduct(l.ctx, specifics[0].ProductId) | ||
if err != nil { | ||
return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) | ||
} | ||
err = l.svcCtx.PowerX.Product.ReFactSKUs(l.ctx, product) | ||
if err != nil { | ||
return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) | ||
} | ||
|
||
return &types.ConfigProductSpecificReply{ | ||
Result: true, | ||
}, nil | ||
} | ||
|
||
func TransformProductSpecificsRequestToProductSpecifics(specificsRequest []types.ProductSpecific) []*product2.ProductSpecific { | ||
|
||
specifics := []*product2.ProductSpecific{} | ||
for _, specificRequest := range specificsRequest { | ||
specifics = append(specifics, TransformProductSpecificRequestToProductSpecific(specificRequest)) | ||
} | ||
|
||
return specifics | ||
} | ||
|
||
func GetOptionsFromProductSpecificsRequest(specifics []*product2.ProductSpecific) []*product2.SpecificOption { | ||
options := []*product2.SpecificOption{} | ||
for _, specific := range specifics { | ||
if len(specific.Options) > 0 { | ||
for _, option := range specific.Options { | ||
options = append(options, option) | ||
} | ||
} | ||
} | ||
return options | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package powermodel | ||
|
||
import ( | ||
fmt "PowerX/pkg/printx" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetModelFields(t *testing.T) { | ||
type SpecificOption struct { | ||
ProductSpecificId int64 `gorm:"comment: 产品规格Id;index;not null" json:"productSpecificId"` | ||
Name string `gorm:"comment: 规格项名称;not null" json:"name"` | ||
IsActivated bool `gorm:"comment: 是否被激活;" json:"isActivated"` | ||
} | ||
|
||
expectedFields := []string{"product_specific_id", "name", "is_activated"} | ||
|
||
model := SpecificOption{} | ||
fields := GetModelFields(model) | ||
fmt.Dump(fields) | ||
|
||
if !reflect.DeepEqual(fields, expectedFields) { | ||
t.Errorf("Expected fields %v, but got %v", expectedFields, fields) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package product | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSKU_GetComposedUniqueID(t *testing.T) { | ||
sku := SKU{ | ||
ProductId: 40, | ||
// 注意这里的json不能有空格 | ||
OptionIds: []byte(`[235,240]`), | ||
} | ||
|
||
expectedUniqueID := "42f065a6a7bdf58e09ad25e9cf5b2031" // 假设使用MD5进行哈希计算 | ||
|
||
composedUniqueID := sku.GetComposedUniqueID() | ||
assert.True(t, composedUniqueID.Valid) | ||
assert.Equal(t, expectedUniqueID, composedUniqueID.String) | ||
} | ||
|
||
func TestSKU_GetComposedUniqueID_Invalid(t *testing.T) { | ||
sku := SKU{ | ||
ProductId: 123, | ||
OptionIds: []byte{}, | ||
} | ||
|
||
composedUniqueID := sku.GetComposedUniqueID() | ||
assert.False(t, composedUniqueID.Valid) | ||
assert.Empty(t, composedUniqueID.String) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.