品牌类别接口
This commit is contained in:
@@ -5233,3 +5233,189 @@ func RefreshStoreIsOnline(ctx *jxcontext.Context) (err error) {
|
||||
task.GetID()
|
||||
return err
|
||||
}
|
||||
|
||||
func GetBrandCategoryMap(ctx *jxcontext.Context, parentID, level int, brandID int) (brandCatMaps []*model.BrandCategoryMap, err error) {
|
||||
db := dao.GetDB()
|
||||
brandCatMaps, err = dao.GetBrandCategoryMap(db, parentID, level, brandID, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ctx.GetLoginType() != weixin.AuthTypeMP && ctx.GetLoginType() != weixin.AuthTypeMini && ctx.GetLoginType() != weixin.AuthTypeWxApp && ctx.GetLoginType() != auth2.AuthTypeMobile {
|
||||
return brandCatMaps, err
|
||||
}
|
||||
return brandCatMaps, err
|
||||
}
|
||||
|
||||
func AddBrandCategoryMap(ctx *jxcontext.Context, brandCategoryMap *model.BrandCategoryMap) (result *model.BrandCategoryMap, err error) {
|
||||
var (
|
||||
db = dao.GetDB()
|
||||
)
|
||||
if brandCategoryMap.Level != 1 {
|
||||
brandCatMaps, _ := dao.GetBrandCategoryMap(db, -1, 0, brandCategoryMap.BrandID, brandCategoryMap.CategoryID)
|
||||
if len(brandCatMaps) > 0 {
|
||||
return nil, fmt.Errorf("已存在绑定的京西分类,分类名:[%v]", brandCatMaps[0].BrandCategoryName)
|
||||
}
|
||||
}
|
||||
brandCategoryMap.BrandCategoryName = strings.Trim(brandCategoryMap.BrandCategoryName, " ")
|
||||
dao.WrapAddIDCULDEntity(brandCategoryMap, ctx.GetUserName())
|
||||
txDB, _ := dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dao.Rollback(db, txDB)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
if err = dao.CreateEntity(db, brandCategoryMap); err != nil {
|
||||
dao.Rollback(db, txDB)
|
||||
return nil, err
|
||||
}
|
||||
dao.Commit(db, txDB)
|
||||
result = brandCategoryMap
|
||||
if brandCategoryMap.CategoryID != 0 {
|
||||
var storeIDList []int
|
||||
if stores, _ := dao.GetStoreList(db, nil, nil, nil, []int{brandCategoryMap.BrandID}, nil, ""); len(stores) > 0 {
|
||||
for _, v := range stores {
|
||||
storeIDList = append(storeIDList, v.ID)
|
||||
}
|
||||
if len(storeIDList) > 0 {
|
||||
SetStoreCategorySyncStatus2(db, storeIDList, []int{brandCategoryMap.CategoryID}, model.SyncFlagModifiedMask)
|
||||
}
|
||||
}
|
||||
}
|
||||
// _, err = CurVendorSync.SyncCategory(ctx, nil, cat.ID, false, userName)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func UpdateBrandCategoryMap(ctx *jxcontext.Context, ID int, brandCategoryMap *model.BrandCategoryMap, isDelete bool) (num int64, err error) {
|
||||
var (
|
||||
db = dao.GetDB()
|
||||
valid = make(map[string]interface{})
|
||||
brandCategoryMap2 = &model.BrandCategoryMap{}
|
||||
storeIDList []int
|
||||
)
|
||||
brandCategoryMap2.ID = ID
|
||||
if err = dao.GetEntity(db, brandCategoryMap2); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if brandCategoryMap.BrandCategoryName != "" {
|
||||
valid["brandCategoryName"] = brandCategoryMap.BrandCategoryName
|
||||
}
|
||||
if brandCategoryMap.CategoryID != 0 {
|
||||
valid["categoryID"] = brandCategoryMap.CategoryID
|
||||
if !isDelete {
|
||||
brandCatMaps, _ := dao.GetBrandCategoryMap(db, -1, 0, brandCategoryMap2.BrandID, brandCategoryMap.CategoryID)
|
||||
if len(brandCatMaps) > 0 {
|
||||
for _, v := range brandCatMaps {
|
||||
if v.ID != ID {
|
||||
return 0, fmt.Errorf("已存在绑定的京西分类,分类名:[%v]", brandCatMaps[0].BrandCategoryName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if brandCategoryMap.Level != 0 {
|
||||
valid["level"] = brandCategoryMap.Level
|
||||
if brandCategoryMap.Level == 2 {
|
||||
cat2, _ := dao.GetCategories(db, -1, 0, []int{brandCategoryMap.CategoryID}, false)
|
||||
if len(cat2) > 0 {
|
||||
if cat2[0].ParentID != brandCategoryMap.ParentID {
|
||||
return 0, fmt.Errorf("此二级分类只能绑定到对应一级分类下!")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if stores, _ := dao.GetStoreList(db, nil, nil, nil, []int{brandCategoryMap.BrandID}, nil, ""); len(stores) > 0 {
|
||||
for _, v := range stores {
|
||||
storeIDList = append(storeIDList, v.ID)
|
||||
}
|
||||
}
|
||||
if isDelete {
|
||||
valid["deletedAt"] = time.Now()
|
||||
valid["updatedAt"] = time.Now()
|
||||
valid["lastOperator"] = ctx.GetUserName()
|
||||
//如果是1级分类则删除下面的子分类
|
||||
var catIDs []int
|
||||
if brandCategoryMap2.Level == 1 {
|
||||
brandCatMaps, _ := dao.GetBrandCategoryMap(db, brandCategoryMap2.CategoryID, 2, brandCategoryMap2.BrandID, 0)
|
||||
if len(brandCatMaps) > 0 {
|
||||
for _, v := range brandCatMaps {
|
||||
catIDs = append(catIDs, v.CategoryID)
|
||||
v.DeletedAt = time.Now()
|
||||
v.LastOperator = ctx.GetUserName()
|
||||
dao.UpdateEntity(db, v, "DeletedAt", "LastOperator")
|
||||
}
|
||||
}
|
||||
}
|
||||
catIDs = append(catIDs, brandCategoryMap.CategoryID)
|
||||
if len(storeIDList) > 0 {
|
||||
SetStoreCategorySyncStatus2(db, storeIDList, catIDs, model.SyncFlagModifiedMask)
|
||||
}
|
||||
} else {
|
||||
if len(storeIDList) > 0 {
|
||||
SetStoreCategorySyncStatus2(db, storeIDList, []int{brandCategoryMap.CategoryID, brandCategoryMap2.CategoryID}, model.SyncFlagModifiedMask)
|
||||
}
|
||||
}
|
||||
txDB, _ := dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dao.Rollback(db, txDB)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
if num, err = dao.UpdateEntityLogically(db, brandCategoryMap2, valid, ctx.GetUserName(), nil); err != nil {
|
||||
dao.Rollback(db, txDB)
|
||||
return 0, err
|
||||
}
|
||||
dao.Commit(db, txDB)
|
||||
return num, err
|
||||
}
|
||||
|
||||
func ReorderBrandCategories(ctx *jxcontext.Context, parentID, brandID int, categoryIDs []int) (err error) {
|
||||
var (
|
||||
brandCatsMap []*model.BrandCategoryMap
|
||||
storeIDList []int
|
||||
)
|
||||
db := dao.GetDB()
|
||||
brandCatsMap, err = dao.GetBrandCategoryMap(db, parentID, 0, brandID, 0)
|
||||
catsLen := len(brandCatsMap)
|
||||
if catsLen != len(categoryIDs) {
|
||||
return ErrInputCatsDoesntMatch
|
||||
}
|
||||
catsMap := make(map[int]*model.BrandCategoryMap, catsLen)
|
||||
for _, cat := range brandCatsMap {
|
||||
catsMap[cat.CategoryID] = cat
|
||||
}
|
||||
txDB, _ := dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dao.Rollback(db, txDB)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
for k, v := range categoryIDs {
|
||||
if catsMap[v] == nil {
|
||||
dao.Rollback(db, txDB)
|
||||
return fmt.Errorf("分类:%d不在%d分类下", v, parentID)
|
||||
}
|
||||
catsMap[v].BrandCategorySeq = k
|
||||
catsMap[v].LastOperator = ctx.GetUserName()
|
||||
if _, err = dao.UpdateEntity(db, catsMap[v]); err != nil {
|
||||
dao.Rollback(db, txDB)
|
||||
return err
|
||||
}
|
||||
}
|
||||
dao.Commit(db, txDB)
|
||||
if stores, _ := dao.GetStoreList(db, nil, nil, nil, []int{brandID}, nil, ""); len(stores) > 0 {
|
||||
for _, v := range stores {
|
||||
storeIDList = append(storeIDList, v.ID)
|
||||
}
|
||||
}
|
||||
if len(storeIDList) > 0 {
|
||||
SetStoreCategorySyncStatus2(db, storeIDList, categoryIDs, model.SyncFlagModifiedMask)
|
||||
}
|
||||
if err == nil {
|
||||
CurVendorSync.SyncStoresCategory(ctx, db, nil, storeIDList, false, true, true)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user