package cms import ( "fmt" "time" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "git.rosy.net.cn/jx-callback/business/jxutils/tasksch" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/business/partner" "git.rosy.net.cn/jx-callback/globals" ) type tMultiStoreVendorInfo struct { VendorID int OrgCode string } func CombineVendorIDAndOrgCode(vendorID int, orgCode string) string { return fmt.Sprintf("%d-%s", vendorID, orgCode) } func getMultiStoreVendorInfoList() (list []*tMultiStoreVendorInfo) { vendorIDs := partner.GetMultiStoreVendorIDs() for _, vendorID := range vendorIDs { orgCodeList := partner.CurAPIManager.GetAppOrgCodeList(vendorID) for _, v := range orgCodeList { list = append(list, &tMultiStoreVendorInfo{ VendorID: vendorID, OrgCode: v, }) } } return list } func SyncCategories(ctx *jxcontext.Context, parentTask tasksch.ITask, vendorIDs []int, appOrgCodes []string, catIDs []int, isAsync bool) (hint string, err error) { globals.SugarLogger.Debugf("SyncCategories catIDs:%v", catIDs) db := dao.GetDB() catList, err := dao.GetSkuCategoryWithVendor(db, nil, nil, -1, catIDs, true) if err == nil { // todo 按vendorID orgCode合并操作 task := tasksch.NewParallelTask(fmt.Sprintf("同步分类:%v", catIDs), nil, ctx, func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { catVendorInfo := batchItemList[0].(*dao.SkuCategoryWithVendor) if multiStoresHandler, ok := partner.GetPurchasePlatformFromVendorID(catVendorInfo.VendorID).(partner.IMultipleStoresHandler); ok { if model.IsSyncStatusDelete(catVendorInfo.SyncStatus) { //删除 if !dao.IsVendorThingIDEmpty(catVendorInfo.VendorCatID) && model.IsSyncStatusNeedDelete(catVendorInfo.SyncStatus) { err = multiStoresHandler.DeleteCategory2(ctx, catVendorInfo) } } else if model.IsSyncStatusNew(catVendorInfo.SyncStatus) { // 新增 err = multiStoresHandler.CreateCategory2(ctx, catVendorInfo) } else if model.IsSyncStatusUpdate(catVendorInfo.SyncStatus) { // 修改 err = multiStoresHandler.UpdateCategory2(ctx, catVendorInfo) } } else { err = fmt.Errorf("平台:%d不合法", catVendorInfo.VendorID) } if err = OnThingSync(ctx, dao.GetDB(), SkuCategoryVendor2ThingMap(catVendorInfo), err); err == nil { retVal = []int{catVendorInfo.ID} } return retVal, err }, catList) tasksch.HandleTask(task, parentTask, len(catIDs) > 0).Run() if isAsync { hint = task.GetID() } else { resultList, err2 := task.GetResult(0) if err = err2; err == nil { hint = utils.Int2Str(len(resultList)) } } } return hint, err } func SyncReorderCategories(ctx *jxcontext.Context, parentCatID int, isAsync bool) (hint string, err error) { globals.SugarLogger.Debugf("SyncReorderCategories parentCatID:%d", parentCatID) db := dao.GetDB() hint, err = CurVendorSync.LoopMultiStoresVendors(ctx, db, fmt.Sprintf("分类重排序:%d", parentCatID), isAsync, false, func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { vendorInfo := batchItemList[0].(*tMultiStoreVendorInfo) multiStoresHandler := CurVendorSync.GetMultiStoreHandler(vendorInfo.VendorID) if multiStoresHandler != nil { catList, err2 := dao.GetSkuCategoryWithVendor(db, []int{vendorInfo.VendorID}, []string{vendorInfo.OrgCode}, parentCatID, nil, false) if err = err2; err == nil { var vendorCatIDList []string for _, v := range catList { if v.VendorCatID != "" { vendorCatIDList = append(vendorCatIDList, v.VendorCatID) } } if len(vendorCatIDList) > 0 { if err = multiStoresHandler.ReorderCategories2(ctx, vendorInfo.OrgCode, catList[0].VendorParentCatID, vendorCatIDList); err == nil { retVal = []int{len(vendorCatIDList)} } } } } else { err = fmt.Errorf("非法平台:%d", vendorInfo.VendorID) } return retVal, err }) return hint, err } func OnCreateThing(ctx *jxcontext.Context, db *dao.DaoDB, thingID int64, thingType int8) (err error) { for _, v := range getMultiStoreVendorInfoList() { thingMap := &model.ThingMap{ ThingID: thingID, ThingType: thingType, VendorID: v.VendorID, VendorOrgCode: v.OrgCode, SyncStatus: model.SyncFlagNewMask, } dao.WrapAddIDCULDEntity(thingMap, ctx.GetUserName()) err = dao.CreateEntity(db, thingMap) } return nil } func OnUpdateThing(ctx *jxcontext.Context, db *dao.DaoDB, thingID int64, thingType int8) (err error) { for _, v := range getMultiStoreVendorInfoList() { thingMap := &model.ThingMap{ ThingID: thingID, ThingType: thingType, VendorID: v.VendorID, VendorOrgCode: v.OrgCode, } thingMap.DeletedAt = utils.DefaultTimeValue if err = dao.GetEntity(db, thingMap, "ThingID", "ThingType", "VendorID", "VendorOrgCode", model.FieldDeletedAt); err == nil { thingMap.SyncStatus |= model.SyncFlagModifiedMask thingMap.LastOperator = ctx.GetUserName() _, err = dao.UpdateEntity(db, thingMap) } } return nil } func OnDeleteThing(ctx *jxcontext.Context, db *dao.DaoDB, thingID int64, thingType int8) (err error) { for _, v := range getMultiStoreVendorInfoList() { thingMap := &model.ThingMap{ ThingID: thingID, ThingType: thingType, VendorID: v.VendorID, VendorOrgCode: v.OrgCode, } thingMap.DeletedAt = utils.DefaultTimeValue if err = dao.GetEntity(db, thingMap, "ThingID", "ThingType", "VendorID", "VendorOrgCode", model.FieldDeletedAt); err == nil { if model.IsSyncStatusNew(thingMap.SyncStatus) { thingMap.SyncStatus = 0 thingMap.DeletedAt = time.Now() } else { thingMap.SyncStatus |= model.SyncFlagDeletedMask } thingMap.LastOperator = ctx.GetUserName() _, err = dao.UpdateEntity(db, thingMap) } } return nil } func SkuCategoryVendor2ThingMap(cat *dao.SkuCategoryWithVendor) (thingMap *model.ThingMap) { thingMap = &model.ThingMap{ ThingID: int64(cat.ID), ThingType: model.ThingTypeCategory, VendorID: cat.VendorID, VendorOrgCode: cat.VendorOrgCode, SyncStatus: cat.SyncStatus, VendorThingID: cat.VendorCatID, } thingMap.ID = cat.MapID // 一定要赋值 return thingMap } func OnThingSync(ctx *jxcontext.Context, db *dao.DaoDB, thingMap *model.ThingMap, syncErr error) (err error) { if syncErr != nil { err = syncErr thingMap.Remark = utils.LimitUTF8StringLen(err.Error(), 255) dao.UpdateEntity(db, thingMap, "Remark") } else { updateFields := []string{ model.FieldSyncStatus, model.FieldUpdatedAt, model.FieldLastOperator, "Remark", } if model.IsSyncStatusDelete(thingMap.SyncStatus) { //删除 thingMap.DeletedAt = time.Now() thingMap.VendorThingID = "" updateFields = append(updateFields, "VendorThingID", model.FieldDeletedAt) } else if model.IsSyncStatusNew(thingMap.SyncStatus) { // 新增 updateFields = append(updateFields, "VendorThingID") } thingMap.SyncStatus = 0 thingMap.LastOperator = ctx.GetUserName() thingMap.UpdatedAt = time.Now() thingMap.Remark = "" _, err = dao.UpdateEntity(db, thingMap, updateFields...) } return err }