v1.1
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
@@ -30,6 +31,7 @@ func NewAdminCreateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
func (l *AdminCreateProductLogic) AdminCreateProduct(req *types.AdminCreateProductReq) (resp *types.AdminCreateProductResp, err error) {
|
||||
// 1. 数据转换
|
||||
data := &model.Product{
|
||||
Id: uuid.NewString(),
|
||||
ProductName: req.ProductName,
|
||||
ProductEn: req.ProductEn,
|
||||
Description: req.Description,
|
||||
@@ -39,23 +41,19 @@ func (l *AdminCreateProductLogic) AdminCreateProduct(req *types.AdminCreateProdu
|
||||
}
|
||||
|
||||
// 2. 数据库操作(使用事务确保产品表和代理产品配置表同步)
|
||||
var productId int64
|
||||
var productId string
|
||||
err = l.svcCtx.ProductModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 2.1 插入产品
|
||||
result, err := l.svcCtx.ProductModel.Insert(ctx, session, data)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"创建产品失败, err: %v, req: %+v", err, req)
|
||||
}
|
||||
|
||||
productId, err = result.LastInsertId()
|
||||
_, err := l.svcCtx.ProductModel.Insert(ctx, session, data)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"获取产品ID失败, err: %v", err)
|
||||
"创建产品失败, err: %v, req: %+v", err, req)
|
||||
}
|
||||
productId = data.Id
|
||||
|
||||
// 2.2 同步创建代理产品配置(使用默认值)
|
||||
agentProductConfig := &model.AgentProductConfig{
|
||||
Id: uuid.NewString(),
|
||||
ProductId: productId,
|
||||
BasePrice: 0.00, // 默认基础底价
|
||||
SystemMaxPrice: 9999.99, // 默认系统价格上限
|
||||
@@ -66,7 +64,7 @@ func (l *AdminCreateProductLogic) AdminCreateProduct(req *types.AdminCreateProdu
|
||||
_, err = l.svcCtx.AgentProductConfigModel.Insert(ctx, session, agentProductConfig)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"创建代理产品配置失败, err: %v, productId: %d", err, productId)
|
||||
"创建代理产品配置失败, err: %v, productId: %s", err, productId)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -39,7 +39,7 @@ func (l *AdminGetProductFeatureListLogic) AdminGetProductFeatureList(req *types.
|
||||
}
|
||||
|
||||
// 3. 获取所有功能ID
|
||||
featureIds := make([]int64, 0, len(list))
|
||||
featureIds := make([]string, 0, len(list))
|
||||
for _, item := range list {
|
||||
featureIds = append(featureIds, item.FeatureId)
|
||||
}
|
||||
@@ -55,13 +55,13 @@ func (l *AdminGetProductFeatureListLogic) AdminGetProductFeatureList(req *types.
|
||||
for i, id := range featureIds {
|
||||
source <- struct {
|
||||
index int
|
||||
id int64
|
||||
id string
|
||||
}{i, id}
|
||||
}
|
||||
}, func(item interface{}, writer mr.Writer[featureResult], cancel func(error)) {
|
||||
data := item.(struct {
|
||||
index int
|
||||
id int64
|
||||
id string
|
||||
})
|
||||
feature, err := l.svcCtx.FeatureModel.FindOne(l.ctx, data.id)
|
||||
writer.Write(featureResult{
|
||||
@@ -71,7 +71,7 @@ func (l *AdminGetProductFeatureListLogic) AdminGetProductFeatureList(req *types.
|
||||
}, func(pipe <-chan featureResult, cancel func(error)) {
|
||||
for result := range pipe {
|
||||
if result.err != nil {
|
||||
l.Logger.Errorf("查询功能详情失败, feature_id: %d, err: %v", result.feature.Id, result.err)
|
||||
l.Logger.Errorf("查询功能详情失败, feature_id: %s, err: %v", result.feature.Id, result.err)
|
||||
continue
|
||||
}
|
||||
results = append(results, result)
|
||||
@@ -84,7 +84,7 @@ func (l *AdminGetProductFeatureListLogic) AdminGetProductFeatureList(req *types.
|
||||
}
|
||||
|
||||
// 5. 构建功能ID到详情的映射
|
||||
featureMap := make(map[int64]*model.Feature)
|
||||
featureMap := make(map[string]*model.Feature)
|
||||
for _, result := range results {
|
||||
if result.feature != nil {
|
||||
featureMap[result.feature.Id] = result.feature
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package admin_product
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
"sync"
|
||||
"context"
|
||||
"sync"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/mr"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/mr"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AdminUpdateProductFeaturesLogic struct {
|
||||
@@ -39,13 +40,13 @@ func (l *AdminUpdateProductFeaturesLogic) AdminUpdateProductFeatures(req *types.
|
||||
}
|
||||
|
||||
// 2. 构建现有关联的映射
|
||||
existingMap := make(map[int64]*model.ProductFeature)
|
||||
existingMap := make(map[string]*model.ProductFeature)
|
||||
for _, item := range existingList {
|
||||
existingMap[item.FeatureId] = item
|
||||
}
|
||||
|
||||
// 3. 构建新关联的映射
|
||||
newMap := make(map[int64]*types.ProductFeatureItem)
|
||||
newMap := make(map[string]*types.ProductFeatureItem)
|
||||
for _, item := range req.Features {
|
||||
newMap[item.FeatureId] = &item
|
||||
}
|
||||
@@ -54,7 +55,7 @@ func (l *AdminUpdateProductFeaturesLogic) AdminUpdateProductFeatures(req *types.
|
||||
err = l.svcCtx.ProductFeatureModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 4.1 处理需要删除的关联
|
||||
var mu sync.Mutex
|
||||
var deleteIds []int64
|
||||
var deleteIds []string
|
||||
err = mr.MapReduceVoid(func(source chan<- interface{}) {
|
||||
for featureId, existing := range existingMap {
|
||||
if _, exists := newMap[featureId]; !exists {
|
||||
@@ -62,7 +63,7 @@ func (l *AdminUpdateProductFeaturesLogic) AdminUpdateProductFeatures(req *types.
|
||||
}
|
||||
}
|
||||
}, func(item interface{}, writer mr.Writer[struct{}], cancel func(error)) {
|
||||
id := item.(int64)
|
||||
id := item.(string)
|
||||
mu.Lock()
|
||||
deleteIds = append(deleteIds, id)
|
||||
mu.Unlock()
|
||||
@@ -90,7 +91,7 @@ func (l *AdminUpdateProductFeaturesLogic) AdminUpdateProductFeatures(req *types.
|
||||
err = mr.MapReduceVoid(func(source chan<- interface{}) {
|
||||
for featureId, newItem := range newMap {
|
||||
source <- struct {
|
||||
featureId int64
|
||||
featureId string
|
||||
newItem *types.ProductFeatureItem
|
||||
existing *model.ProductFeature
|
||||
}{
|
||||
@@ -101,7 +102,7 @@ func (l *AdminUpdateProductFeaturesLogic) AdminUpdateProductFeatures(req *types.
|
||||
}
|
||||
}, func(item interface{}, writer mr.Writer[struct{}], cancel func(error)) {
|
||||
data := item.(struct {
|
||||
featureId int64
|
||||
featureId string
|
||||
newItem *types.ProductFeatureItem
|
||||
existing *model.ProductFeature
|
||||
})
|
||||
@@ -119,14 +120,15 @@ func (l *AdminUpdateProductFeaturesLogic) AdminUpdateProductFeatures(req *types.
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 新增关联
|
||||
newFeature := &model.ProductFeature{
|
||||
ProductId: req.ProductId,
|
||||
FeatureId: data.featureId,
|
||||
Sort: data.newItem.Sort,
|
||||
Enable: data.newItem.Enable,
|
||||
IsImportant: data.newItem.IsImportant,
|
||||
}
|
||||
// 新增关联
|
||||
newFeature := &model.ProductFeature{
|
||||
Id: uuid.NewString(),
|
||||
ProductId: req.ProductId,
|
||||
FeatureId: data.featureId,
|
||||
Sort: data.newItem.Sort,
|
||||
Enable: data.newItem.Enable,
|
||||
IsImportant: data.newItem.IsImportant,
|
||||
}
|
||||
_, err = l.svcCtx.ProductFeatureModel.Insert(ctx, session, newFeature)
|
||||
if err != nil {
|
||||
updateErr = errors.Wrapf(err, "新增产品功能关联失败, product_id: %d, feature_id: %d",
|
||||
|
||||
Reference in New Issue
Block a user