diff --git a/business/jxstore/act/act.go b/business/jxstore/act/act.go index 4987a17e3..84e4053ef 100644 --- a/business/jxstore/act/act.go +++ b/business/jxstore/act/act.go @@ -145,7 +145,8 @@ func getVendorPriceFromStoreSkuBind(bind *model.StoreSkuBind, vendorID int) (ven return vendorPrice } -func ActStoreSkuParam2Model(ctx *jxcontext.Context, db *dao.DaoDB, act *model.Act, vendorIDs []int, actStoreSku []*ActStoreSkuParam) (validVendorIDs []int, actStoreSkuList []*model.ActStoreSku, actStoreSkuMapList []*model.ActStoreSkuMap, err error) { +func ActStoreSkuParam2Model(ctx *jxcontext.Context, db *dao.DaoDB, act *model.Act, vendorIDs []int, actStoreSku []*ActStoreSkuParam) ( + validVendorIDs []int, actStoreSkuList []*model.ActStoreSku, actStoreSkuMapList []*model.ActStoreSkuMap, conflictActStoreSku []*model.ActStoreSku2, err error) { wholeValidVendorMap := make(map[int]int) if len(actStoreSku) > 0 { storeIDMap := make(map[int]int) @@ -166,33 +167,29 @@ func ActStoreSkuParam2Model(ctx *jxcontext.Context, db *dao.DaoDB, act *model.Ac } } if len(wrongSkuList) > 0 { - return nil, nil, nil, jsonerr.New(wrongSkuList, model.ErrCodeJsonActEarningPriceIsZero) + return nil, nil, nil, nil, jsonerr.New(wrongSkuList, model.ErrCodeJsonActEarningPriceIsZero) } storeIDs := jxutils.IntMap2List(storeIDMap) skuIDs := jxutils.IntMap2List(skuIDMap) // 判断活动是否重叠的检查,当前忽略京东平台及所有结算信息 - if act.OverlapRule == model.OverlapRuleNormal && act.Type != model.ActSkuFake { + if act.Type != model.ActSkuFake { effectActStoreSkuList, err := dao.GetEffectiveActStoreSkuInfo(db, 0, vendorIDs, act.Type, storeIDs, skuIDs, act.BeginAt, act.EndAt) if err != nil { globals.SugarLogger.Errorf("GetEffectiveActStoreSkuInfo can not get sku promotion info for error:%v", err) - return nil, nil, nil, err + return nil, nil, nil, nil, err } if len(effectActStoreSkuList) > 0 { - var realEffectActStoreSkuList []*model.ActStoreSku2 for _, v := range effectActStoreSkuList { if actStoreSkuMap[jxutils.Combine2Int(v.StoreID, v.SkuID)] { - realEffectActStoreSkuList = append(realEffectActStoreSkuList, v) + conflictActStoreSku = append(conflictActStoreSku, v) } } - if len(realEffectActStoreSkuList) > 0 { - return nil, nil, nil, jsonerr.New(realEffectActStoreSkuList, model.ErrCodeJsonActSkuConflict) - } } } storeSkuList, err2 := dao.GetStoresSkusInfo(db, storeIDs, skuIDs) if err = err2; err != nil { - return nil, nil, nil, err + return nil, nil, nil, nil, err } storeSkuMap := make(map[int64]*model.StoreSkuBind) for _, v := range storeSkuList { @@ -266,7 +263,7 @@ func ActStoreSkuParam2Model(ctx *jxcontext.Context, db *dao.DaoDB, act *model.Ac } } } else if !dao.IsNoRowsError(err) { - return nil, nil, nil, err + return nil, nil, nil, nil, err } else { err = nil } @@ -282,10 +279,10 @@ func ActStoreSkuParam2Model(ctx *jxcontext.Context, db *dao.DaoDB, act *model.Ac } } if len(wrongSkuList) > 0 { - return nil, nil, nil, jsonerr.New(wrongSkuList, model.ErrCodeJsonActPriceTooLarger) + return nil, nil, nil, nil, jsonerr.New(wrongSkuList, model.ErrCodeJsonActPriceTooLarger) } } - return jxutils.IntMap2List(wholeValidVendorMap), actStoreSkuList, actStoreSkuMapList, err + return jxutils.IntMap2List(wholeValidVendorMap), actStoreSkuList, actStoreSkuMapList, conflictActStoreSku, err } func addActStoreSkuBind(ctx *jxcontext.Context, db *dao.DaoDB, actStoreSkuList []*model.ActStoreSku, actStoreSkuMapList []*model.ActStoreSkuMap) (err error) { @@ -362,7 +359,8 @@ func AddActStoreSkuBind(ctx *jxcontext.Context, db *dao.DaoDB, actID int, actSto return fmt.Errorf("当前活动状态:%s不能进行此操作,或已过期", model.ActStatusName[act.Status]) } - _, actStoreSkuList, actStoreSkuMapList, err := ActStoreSkuParam2Model(ctx, db, act, vendorIDs, actStoreSku) + // TODO conflictActStoreSku的处理 + _, actStoreSkuList, actStoreSkuMapList, _, err := ActStoreSkuParam2Model(ctx, db, act, vendorIDs, actStoreSku) if err != nil { return err } @@ -467,14 +465,16 @@ func PreCreateAct(ctx *jxcontext.Context, act *model.Act, vendorIDs []int, actRu setActDefault(act) db := dao.GetDB() - validVendorIDs, actStoreSkuList, actStoreSkuMapList, err := ActStoreSkuParam2Model(ctx, db, act, vendorIDs, actStoreSku) + validVendorIDs, actStoreSkuList, actStoreSkuMapList, conflictActStoreSku, err := ActStoreSkuParam2Model(ctx, db, act, vendorIDs, actStoreSku) if err != nil { return nil, err } if len(validVendorIDs) == 0 { - dao.Rollback(db) return nil, fmt.Errorf("没有一个合法平台可以创建活动") } + if act.OverlapRule == model.OverlapRuleNormal && len(conflictActStoreSku) > 0 { + return nil, jsonerr.New(conflictActStoreSku, model.ErrCodeJsonActSkuConflict) + } preCreateActInfo = &tPreCreateActInfo{ ValidVendorIDs: validVendorIDs, @@ -511,6 +511,9 @@ func CreateAct(ctx *jxcontext.Context, act *model.Act, vendorIDs []int, vendorOr setActDefault(act) db := dao.GetDB() + // TODO + // 事务应该从ActStoreSkuParam2Model之后开始 + // 创建Act也应该往后放 dao.Begin(db) defer func() { if r := recover(); r != nil { @@ -525,7 +528,7 @@ func CreateAct(ctx *jxcontext.Context, act *model.Act, vendorIDs []int, vendorOr return "", err } - validVendorIDs, actStoreSkuList, actStoreSkuMapList, err := ActStoreSkuParam2Model(ctx, db, act, vendorIDs, actStoreSku) + validVendorIDs, actStoreSkuList, actStoreSkuMapList, conflictActStoreSku, err := ActStoreSkuParam2Model(ctx, db, act, vendorIDs, actStoreSku) if err != nil { dao.Rollback(db) return "", err @@ -534,6 +537,17 @@ func CreateAct(ctx *jxcontext.Context, act *model.Act, vendorIDs []int, vendorOr dao.Rollback(db) return "", fmt.Errorf("没有一个合法平台可以创建活动") } + var neeSyncActIDs []int + if len(conflictActStoreSku) > 0 { + if act.OverlapRule == model.OverlapRuleNormal { + dao.Rollback(db) + return "", jsonerr.New(conflictActStoreSku, model.ErrCodeJsonActSkuConflict) + } + if neeSyncActIDs, err = DeleteActStoreSkuList(ctx, db, conflictActStoreSku); err != nil { + dao.Rollback(db) + return "", err + } + } var actMapList []*model.ActMap for _, vendorID := range validVendorIDs { @@ -548,12 +562,10 @@ func CreateAct(ctx *jxcontext.Context, act *model.Act, vendorIDs []int, vendorOr dao.WrapAddIDCULDEntity(actMap, ctx.GetUserName()) actMapList = append(actMapList, actMap) } - if len(actMapList) > 0 { - err = dao.CreateMultiEntities(db, actMapList) - if err != nil { - dao.Rollback(db) - return "", err - } + err = dao.CreateMultiEntities(db, actMapList) + if err != nil { + dao.Rollback(db) + return "", err } if err = addActStoreSkuBind(ctx, db, actStoreSkuList, actStoreSkuMapList); err != nil { @@ -562,9 +574,20 @@ func CreateAct(ctx *jxcontext.Context, act *model.Act, vendorIDs []int, vendorOr } dao.Commit(db) - hint, err = SyncAct(ctx, nil, act.ID, nil, isAsync) + neeSyncActIDs = append(neeSyncActIDs, act.ID) + task := tasksch.NewParallelTask(fmt.Sprintf("处理活动创建%d", act.ID), tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(true), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + actID := batchItemList[0].(int) + _, err = SyncAct(ctx, task, actID, nil, false) + return retVal, err + }, neeSyncActIDs) + + tasksch.HandleTask(task, nil, true).Run() if !isAsync { hint = utils.Int2Str(act.ID) + } else { + _, err = task.GetResult(0) + hint = utils.Int2Str(len(neeSyncActIDs)) } return hint, err } @@ -847,6 +870,32 @@ func DeleteActStoreSkuBind(ctx *jxcontext.Context, db *dao.DaoDB, actID int, act return originSyncStatus, err } +func DeleteActStoreSkuList(ctx *jxcontext.Context, db *dao.DaoDB, actStoreSkuList []*model.ActStoreSku2) (actIDs []int, err error) { + if globals.IsStoreSkuAct { + actStoreSkuParamMap := make(map[int][]*ActStoreSkuParam) + for _, v := range actStoreSkuList { + param := &ActStoreSkuParam{ + ActStoreSku: model.ActStoreSku{ + ActID: v.ActID, + StoreID: v.StoreID, + SkuID: v.SkuID, + }, + } + actStoreSkuParamMap[v.ActID] = append(actStoreSkuParamMap[v.ActID], param) + } + for actID := range actStoreSkuParamMap { + if _, err = DeleteActStoreSkuBind(ctx, db, actID, actStoreSkuParamMap[actID]); err != nil { + return nil, err + } + } + + for actID := range actStoreSkuParamMap { + actIDs = append(actIDs, actID) + } + } + return actIDs, nil +} + // todo 当前逻辑要求传入活动的全部SKU信息(以便低层做一些判断,比如全部删除时要取消,所以暂时删除storeIDs与skuIDs这两个参数 func SyncAct(ctx *jxcontext.Context, parentTask tasksch.ITask, actID int, vendorIDs /*, storeIDs, skuIDs */ []int, isAsync bool) (hint string, err error) { db := dao.GetDB() diff --git a/business/model/dao/act.go b/business/model/dao/act.go index 3daf2375b..e1710e829 100644 --- a/business/model/dao/act.go +++ b/business/model/dao/act.go @@ -152,7 +152,7 @@ func GetActStoreSkuVendorList(db *DaoDB, actID int, vendorIDs, storeIDs, skuIDs // globals.SugarLogger.Debug(utils.Format4Output(sqlParams, false)) // globals.SugarLogger.Debug(utils.Format4Output(actStoreSkuList, false)) Begin(db) - defer Rollback(db) + defer Commit(db) if err = GetRows(db, &actStoreSkuList, sql, sqlParams...); err == nil { totalCount = GetLastTotalRowCount(db) } diff --git a/business/model/dao/food_recipe.go b/business/model/dao/food_recipe.go index f2b1854d9..638403b5a 100644 --- a/business/model/dao/food_recipe.go +++ b/business/model/dao/food_recipe.go @@ -81,7 +81,7 @@ func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID sqlParams = append(sqlParams, pageSize, offset) Begin(db) - defer Rollback(db) + defer Commit(db) if err = GetRows(db, &recipeList, sql, sqlParams...); err == nil { totalCount = GetLastTotalRowCount(db) }