+ UpdateFoodRecipe
This commit is contained in:
@@ -19,7 +19,65 @@ type FoodRecipeStepParam struct {
|
|||||||
Img string `orm:"size(48)" json:"img"`
|
Img string `orm:"size(48)" json:"img"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateFoodRecipeItemAndStep(ctx *jxcontext.Context, db *dao.DaoDB, recipeID int, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
|
||||||
|
for k, v := range itemList {
|
||||||
|
if len(v.SkuIDs) == 0 {
|
||||||
|
return fmt.Errorf("SkuIDs必须要有值")
|
||||||
|
}
|
||||||
|
skuIDs, err2 := dao.GetSkus(db, v.SkuIDs, nil, nil, nil)
|
||||||
|
if err = err2; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(v.SkuIDs) != len(skuIDs) {
|
||||||
|
return fmt.Errorf("某些SkuIDs不存在")
|
||||||
|
}
|
||||||
|
|
||||||
|
item := &model.FoodRecipeItem{
|
||||||
|
RecipeID: recipeID,
|
||||||
|
Index: int8(k + 1),
|
||||||
|
Name: v.Name,
|
||||||
|
}
|
||||||
|
dao.WrapAddIDCULEntity(item, ctx.GetUserName())
|
||||||
|
if err = dao.CreateEntity(db, item); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for k2, v2 := range v.SkuIDs {
|
||||||
|
choice := &model.FoodRecipeItemChoice{
|
||||||
|
RecipeID: recipeID,
|
||||||
|
Index: item.Index,
|
||||||
|
SkuID: v2,
|
||||||
|
|
||||||
|
ChoiceIndex: int8(k2 + 1),
|
||||||
|
}
|
||||||
|
dao.WrapAddIDCULEntity(choice, ctx.GetUserName())
|
||||||
|
if err = dao.CreateEntity(db, choice); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range stepList {
|
||||||
|
step := &model.FoodRecipeStep{
|
||||||
|
RecipeID: recipeID,
|
||||||
|
Index: int8(k + 1),
|
||||||
|
Name: v.Name,
|
||||||
|
}
|
||||||
|
dao.WrapAddIDCULEntity(step, ctx.GetUserName())
|
||||||
|
if err = dao.CreateEntity(db, step); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func CreateFoodRecipe(ctx *jxcontext.Context, foodRecipe *model.FoodRecipe, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
|
func CreateFoodRecipe(ctx *jxcontext.Context, foodRecipe *model.FoodRecipe, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
|
||||||
|
if len(itemList) == 0 {
|
||||||
|
return fmt.Errorf("必须要有配料")
|
||||||
|
}
|
||||||
|
if len(stepList) == 0 {
|
||||||
|
return fmt.Errorf("必须要有操作步骤")
|
||||||
|
}
|
||||||
|
|
||||||
db := dao.GetDB()
|
db := dao.GetDB()
|
||||||
dao.Begin(db)
|
dao.Begin(db)
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -36,58 +94,66 @@ func CreateFoodRecipe(ctx *jxcontext.Context, foodRecipe *model.FoodRecipe, item
|
|||||||
if err = dao.CreateEntity(db, foodRecipe); err != nil {
|
if err = dao.CreateEntity(db, foodRecipe); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for k, v := range itemList {
|
if err = updateFoodRecipeItemAndStep(ctx, db, foodRecipe.ID, itemList, stepList); err != nil {
|
||||||
if len(v.SkuIDs) == 0 {
|
return err
|
||||||
return fmt.Errorf("SkuIDs必须要有值")
|
|
||||||
}
|
|
||||||
skuIDs, err2 := dao.GetSkus(db, v.SkuIDs, nil, nil, nil)
|
|
||||||
if err = err2; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(v.SkuIDs) != len(skuIDs) {
|
|
||||||
return fmt.Errorf("某些SkuIDs不存在")
|
|
||||||
}
|
|
||||||
|
|
||||||
item := &model.FoodRecipeItem{
|
|
||||||
RecipeID: foodRecipe.ID,
|
|
||||||
Index: int8(k + 1),
|
|
||||||
Name: v.Name,
|
|
||||||
}
|
|
||||||
dao.WrapAddIDCULEntity(item, ctx.GetUserName())
|
|
||||||
if err = dao.CreateEntity(db, item); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for k2, v2 := range v.SkuIDs {
|
|
||||||
choice := &model.FoodRecipeItemChoice{
|
|
||||||
RecipeID: foodRecipe.ID,
|
|
||||||
Index: item.Index,
|
|
||||||
SkuID: v2,
|
|
||||||
|
|
||||||
ChoiceIndex: int8(k2 + 1),
|
|
||||||
}
|
|
||||||
dao.WrapAddIDCULEntity(choice, ctx.GetUserName())
|
|
||||||
if err = dao.CreateEntity(db, choice); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range stepList {
|
dao.Commit(db)
|
||||||
step := &model.FoodRecipeStep{
|
return err
|
||||||
RecipeID: foodRecipe.ID,
|
}
|
||||||
Index: int8(k + 1),
|
|
||||||
Name: v.Name,
|
func UpdateFoodRecipe(ctx *jxcontext.Context, recipeID int, mapData map[string]interface{}, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
|
||||||
|
db := dao.GetDB()
|
||||||
|
|
||||||
|
localRecipe := &model.FoodRecipe{}
|
||||||
|
localRecipe.ID = recipeID
|
||||||
|
if err = dao.GetEntity(db, localRecipe); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
valid := dao.StrictMakeMapByStructObject(mapData, localRecipe, ctx.GetUserName())
|
||||||
|
|
||||||
|
dao.Begin(db)
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil || err != nil {
|
||||||
|
dao.Rollback(db)
|
||||||
|
if r != nil {
|
||||||
|
panic(r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dao.WrapAddIDCULEntity(step, ctx.GetUserName())
|
}()
|
||||||
if err = dao.CreateEntity(db, step); err != nil {
|
|
||||||
|
if len(valid) > 0 {
|
||||||
|
if _, err = dao.UpdateEntityLogically(db, localRecipe, valid, ctx.GetUserName(), nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(itemList) > 0 {
|
||||||
|
choice := &model.FoodRecipeItemChoice{
|
||||||
|
RecipeID: recipeID,
|
||||||
|
}
|
||||||
|
if _, err = dao.DeleteEntity(db, choice, "RecipeID"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
recipeItems := &model.FoodRecipeItem{
|
||||||
|
RecipeID: recipeID,
|
||||||
|
}
|
||||||
|
if _, err = dao.DeleteEntity(db, recipeItems, "RecipeID"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(stepList) > 0 {
|
||||||
|
recipeSteps := &model.FoodRecipeStep{
|
||||||
|
RecipeID: recipeID,
|
||||||
|
}
|
||||||
|
if _, err = dao.DeleteEntity(db, recipeSteps, "RecipeID"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = updateFoodRecipeItemAndStep(ctx, db, recipeID, itemList, stepList); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
dao.Commit(db)
|
dao.Commit(db)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateFoodRecipe(ctx *jxcontext.Context, foodRecipe *model.FoodRecipe, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user