改InitVendorCategory为支持异步

This commit is contained in:
gazebo
2019-10-25 14:00:52 +08:00
parent 15a6fa09d4
commit d4dd333117
2 changed files with 47 additions and 30 deletions

View File

@@ -243,42 +243,58 @@ func InitSkuName(ctx *jxcontext.Context, isForce, isAsync, isContinueWhenError b
return hint, err return hint, err
} }
func InitVendorCategory(ctx *jxcontext.Context, vendorID int) (num int64, err error) { func InitVendorCategory(ctx *jxcontext.Context, vendorID int, isAsync bool) (hint string, err error) {
if handler := partner.GetPurchasePlatformFromVendorID(vendorID); handler != nil { if handler := partner.GetPurchasePlatformFromVendorID(vendorID); handler != nil {
cats, err2 := handler.GetVendorCategories(ctx) var cats []*model.SkuVendorCategory
if err2 != nil { rootTask := tasksch.NewSeqTask(fmt.Sprintf("创建%s的平台分类", model.VendorChineseNames[vendorID]), ctx,
return num, err2 func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
} switch step {
db := dao.GetDB() case 0:
dao.Begin(db) cats, err = handler.GetVendorCategories(ctx)
defer func() { if err != nil {
if r := recover(); r != nil || err != nil { return nil, err
dao.Rollback(db) }
if r != nil { case 1:
panic(r) db := dao.GetDB()
dao.Begin(db)
defer func() {
if r := recover(); r != nil || err != nil {
dao.Rollback(db)
if r != nil {
panic(r)
}
}
}()
sql := `
DELETE
FROM sku_vendor_category
WHERE vendor_id = ?
`
if _, err = dao.ExecuteSQL(db, sql, vendorID); err != nil {
return nil, err
}
for _, cat := range cats {
dao.WrapAddIDCULEntity(cat, ctx.GetUserName())
if err = dao.CreateEntity(db, cat); err != nil {
return nil, err
}
}
dao.Commit(db)
} }
return nil, err
}, 2)
tasksch.HandleTask(rootTask, nil, true).Run()
if !isAsync {
if _, err = rootTask.GetResult(0); err == nil {
hint = utils.Int2Str(len(cats))
} }
}() } else {
sql := ` hint = rootTask.ID
DELETE
FROM sku_vendor_category
WHERE vendor_id = ?
`
if _, err = dao.ExecuteSQL(db, sql, vendorID); err != nil {
return num, err
} }
for _, cat := range cats {
dao.WrapAddIDCULEntity(cat, ctx.GetUserName())
if err = dao.CreateEntity(db, cat); err != nil {
return num, err
}
}
dao.Commit(db)
num = int64(len(cats))
} else { } else {
err = fmt.Errorf("找不到平台:%d", vendorID) err = fmt.Errorf("找不到平台:%d", vendorID)
} }
return num, err return hint, err
} }
func UploadImg4Vendors(ctx *jxcontext.Context, isAsync, isContinueWhenError bool) (hint string, err error) { func UploadImg4Vendors(ctx *jxcontext.Context, isAsync, isContinueWhenError bool) (hint string, err error) {

View File

@@ -42,12 +42,13 @@ func (c *InitDataController) InitSkuName() {
// @Description 初始化vendor category当前只有美团外卖的通过这个设置 // @Description 初始化vendor category当前只有美团外卖的通过这个设置
// @Param token header string true "认证token" // @Param token header string true "认证token"
// @Param vendorID formData int true "厂商ID" // @Param vendorID formData int true "厂商ID"
// @Param isAsync formData bool false "是否异步操作"
// @Success 200 {object} controllers.CallResult // @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult
// @router /InitVendorCategory [post] // @router /InitVendorCategory [post]
func (c *InitDataController) InitVendorCategory() { func (c *InitDataController) InitVendorCategory() {
c.callInitVendorCategory(func(params *tInitdataInitVendorCategoryParams) (retVal interface{}, errCode string, err error) { c.callInitVendorCategory(func(params *tInitdataInitVendorCategoryParams) (retVal interface{}, errCode string, err error) {
retVal, err = initdata.InitVendorCategory(params.Ctx, params.VendorID) retVal, err = initdata.InitVendorCategory(params.Ctx, params.VendorID, params.IsAsync)
return retVal, "", err return retVal, "", err
}) })
} }