- 菜谱相关

This commit is contained in:
gazebo
2019-09-16 18:40:43 +08:00
parent ea58ab6f79
commit 312ce745cc
7 changed files with 282 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
package userstore
import (
"fmt"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
type FoodRecipeItemParam struct {
Name string `json:"name"`
SkuIDs []int `json:"skuIDs"`
}
type FoodRecipeStepParam struct {
Name string `orm:"size(48)" json:"name"`
Description string `orm:"size(4096)" json:"description"`
Img string `orm:"size(48)" json:"img"`
}
func CreateFoodRecipe(ctx *jxcontext.Context, foodRecipe *model.FoodRecipe, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
db := dao.GetDB()
dao.Begin(db)
defer func() {
if r := recover(); r != nil || err != nil {
dao.Rollback(db)
if r != nil {
panic(r)
}
}
}()
_, foodRecipe.AuthorID = ctx.GetMobileAndUserID()
dao.WrapAddIDCULEntity(foodRecipe, ctx.GetUserName())
if err = dao.CreateEntity(db, foodRecipe); err != nil {
return err
}
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: 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 {
step := &model.FoodRecipeStep{
RecipeID: foodRecipe.ID,
Index: int8(k + 1),
Name: v.Name,
}
dao.WrapAddIDCULEntity(step, ctx.GetUserName())
if err = dao.CreateEntity(db, step); err != nil {
return err
}
}
dao.Commit(db)
return err
}
func UpdateFoodRecipe(ctx *jxcontext.Context, foodRecipe *model.FoodRecipe, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
return err
}