品牌类别接口

This commit is contained in:
suyl
2021-09-03 09:07:55 +08:00
parent c75699bdeb
commit a29fef75e5
4 changed files with 342 additions and 0 deletions

View File

@@ -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
}

View File

@@ -1396,3 +1396,36 @@ func GetBrandBill(db *DaoDB, brandID int, vendorOrderID string, billType, feeTyp
err = GetRows(db, &result, sql, sqlParams)
return result, err
}
func GetBrandCategoryMap(db *DaoDB, parentID, level, brandID, categoryID int) (brandCatMaps []*model.BrandCategoryMap, err error) {
sql := `
SELECT a.*
FROM brand_category_map a
WHERE a.deleted_at = ?
`
sqlParams := []interface{}{
utils.DefaultTimeValue,
}
if parentID >= 0 {
sql += " AND a.parent_id = ?"
sqlParams = append(sqlParams, parentID)
}
if level > 0 {
sql += " AND a.level = ?"
sqlParams = append(sqlParams, level)
}
if brandID > 0 {
sql += " AND a.brand_id = ?"
sqlParams = append(sqlParams, brandID)
}
if categoryID > 0 {
sql += " AND a.category_id = ?"
sqlParams = append(sqlParams, categoryID)
}
sql += " ORDER BY a.level, a.brand_category_seq"
err = GetRows(db, &brandCatMaps, sql, sqlParams)
if err != nil {
return nil, err
}
return brandCatMaps, err
}

View File

@@ -1210,3 +1210,90 @@ func (c *StoreController) UpdateOrCreateCourierStoresByBrand() {
return retVal, "", err
})
}
// @Title 得到品牌类别
// @Description 得到品牌类别
// @Param token header string true "认证token"
// @Param parentID query int false "父ID"
// @Param level query int false "分类等级"
// @Param brandID query int true "品牌ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetBrandCategoryMap [get]
func (c *StoreController) GetBrandCategoryMap() {
c.callGetBrandCategoryMap(func(params *tStoreGetBrandCategoryMapParams) (retVal interface{}, errCode string, err error) {
retVal, err = cms.GetBrandCategoryMap(params.Ctx, params.ParentID, params.Level, params.BrandID)
return retVal, "", err
})
}
// @Title 新增品牌类别
// @Description 新增品牌类别
// @Param token header string true "认证token"
// @Param brandID formData int true "品牌ID"
// @Param categroyID formData int false "京西分类id"
// @Param level formData int true "分类级别"
// @Param parentID formData int true "分类父ID"
// @Param brandCategroyName formData string true "类别name"
// @Param brandCategroySeq formData int true "类别序号"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddBrandCategoryMap [post]
func (c *StoreController) AddBrandCategoryMap() {
c.callAddBrandCategoryMap(func(params *tStoreAddBrandCategoryMapParams) (retVal interface{}, errCode string, err error) {
brandCategoryMap := &model.BrandCategoryMap{
BrandID: params.BrandID,
CategoryID: params.CategroyID,
BrandCategoryName: params.BrandCategroyName,
BrandCategorySeq: params.BrandCategroySeq,
Level: params.Level,
ParentID: params.ParentID,
}
retVal, err = cms.AddBrandCategoryMap(params.Ctx, brandCategoryMap)
return retVal, "", err
})
}
// @Title 修改品牌类别
// @Description 修改品牌类别
// @Param token header string true "认证token"
// @Param ID formData int true "记录ID"
// @Param categoryID formData int false "京西分类id"
// @Param brandCategroyName formData string false "类别name"
// @Param level formData int true "分类级别"
// @Param parentID formData int true "分类父ID"
// @Param isDelete formData bool false "是否是删除操作默认false"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateBrandCategoryMap [put]
func (c *StoreController) UpdateBrandCategoryMap() {
c.callUpdateBrandCategoryMap(func(params *tStoreUpdateBrandCategoryMapParams) (retVal interface{}, errCode string, err error) {
brandCategoryMap := &model.BrandCategoryMap{
CategoryID: params.CategoryID,
BrandCategoryName: params.BrandCategroyName,
Level: params.Level,
ParentID: params.ParentID,
}
retVal, err = cms.UpdateBrandCategoryMap(params.Ctx, params.ID, brandCategoryMap, params.IsDelete)
return retVal, "", err
})
}
// @Title 品牌类别重排序
// @Description 品牌类别重排序
// @Param token header string true "认证token"
// @Param categoryID formData int true "父ID"
// @Param brandID formData int true "门店ID"
// @Param categoryIDs formData string true "同一父类别下的所有子类别ID列表([1,2,3,4])"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /ReorderBrandCategories [put]
func (c *SkuController) ReorderBrandCategories() {
c.callReorderBrandCategories(func(params *tSkuReorderBrandCategoriesParams) (retVal interface{}, errCode string, err error) {
var idList []int
if err = utils.UnmarshalUseNumber([]byte(params.CategoryIDs), &idList); err == nil {
err = cms.ReorderBrandCategories(params.Ctx, params.CategoryID, params.BrandID, idList)
}
return retVal, "", err
})
}

View File

@@ -2439,6 +2439,42 @@ func init() {
Filters: nil,
Params: nil})
web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
web.ControllerComments{
Method: "GetBrandCategoryMap",
Router: `/GetBrandCategoryMap`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})
web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
web.ControllerComments{
Method: "AddBrandCategoryMap",
Router: `/AddBrandCategoryMap`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})
web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
web.ControllerComments{
Method: "UpdateBrandCategoryMap",
Router: `/UpdateBrandCategoryMap`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})
web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
web.ControllerComments{
Method: "ReorderBrandCategories",
Router: `/ReorderBrandCategories`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})
web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(web.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"],
web.ControllerComments{
Method: "GetStoreCourierMaps",