f
This commit is contained in:
51
app/main/api/internal/logic/productcost/sync.go
Normal file
51
app/main/api/internal/logic/productcost/sync.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package productcost
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// SyncProductCostFromModules 将产品 cost_price 更新为当前关联模块的 feature.cost_price 之和
|
||||
func SyncProductCostFromModules(ctx context.Context, c *svc.ServiceContext, productId string) error {
|
||||
sum, err := c.ProductFeatureModel.SumFeatureCostPriceByProductId(ctx, productId)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"汇总产品模块成本失败, err: %v, productId: %s", err, productId)
|
||||
}
|
||||
record, err := c.ProductModel.FindOne(ctx, productId)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查找产品失败, err: %v, productId: %s", err, productId)
|
||||
}
|
||||
record.CostPrice = sum
|
||||
if _, err = c.ProductModel.Update(ctx, nil, record); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"同步产品成本失败, err: %v, productId: %s", err, productId)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncAllProductsUsingFeature 在模块成本等变更后,刷新所有关联了该模块的产品的 cost_price
|
||||
func SyncAllProductsUsingFeature(ctx context.Context, c *svc.ServiceContext, featureId string) error {
|
||||
builder := c.ProductFeatureModel.SelectBuilder().Where("feature_id = ?", featureId)
|
||||
list, err := c.ProductFeatureModel.FindAll(ctx, builder, "")
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询产品模块关联失败, err: %v, featureId: %s", err, featureId)
|
||||
}
|
||||
seen := make(map[string]struct{})
|
||||
for _, pf := range list {
|
||||
if _, ok := seen[pf.ProductId]; ok {
|
||||
continue
|
||||
}
|
||||
seen[pf.ProductId] = struct{}{}
|
||||
if err := SyncProductCostFromModules(ctx, c, pf.ProductId); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user