You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.0 KiB
121 lines
3.0 KiB
package gys
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/gorm"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/goods"
|
|
"recook/internal/model/vend"
|
|
"recook/tools"
|
|
)
|
|
|
|
type Category struct {
|
|
base
|
|
}
|
|
|
|
type replyCategory struct {
|
|
goods.Category
|
|
Children []goods.Category `json:"children"`
|
|
}
|
|
|
|
// @Style 获取商品类目
|
|
func (cate *Category) List(c *gin.Context) {
|
|
myGysId, err := cate.GetGysId(c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
var categories []replyCategory
|
|
|
|
// 获取供应商允许分类id
|
|
gysEnterpriseBrands := []vend.GysEnterpriseBrand{}
|
|
dbc.DB.Model(&vend.GysEnterpriseBrand{}).Select("category_id").Find(&gysEnterpriseBrands, "user_id = ? and state = ?", myGysId, 2)
|
|
|
|
if len(gysEnterpriseBrands) == 0 {
|
|
back.Fail(c, "暂无数据")
|
|
return
|
|
}
|
|
|
|
// 获取一级分类数据
|
|
categoryIds := []string{}
|
|
for _, gysEnterpriseBrand := range gysEnterpriseBrands {
|
|
categoryIds = append(categoryIds, gysEnterpriseBrand.CategoryId)
|
|
}
|
|
err = dbc.DB.Table((&goods.Category{}).TableName()).Order("location asc").Find(&categories, "parent_id=0 AND depth=1 and id in (?)", categoryIds).Error
|
|
if err != nil {
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
back.Fail(c, "暂无数据")
|
|
} else {
|
|
back.Err(c, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
subCategoryIds := []uint{}
|
|
for _, category := range categories {
|
|
subCategoryIds = append(subCategoryIds, category.ID)
|
|
}
|
|
|
|
// 查询二级分类
|
|
subLists := []goods.Category{}
|
|
err = dbc.DB.Table((&goods.Category{}).TableName()).Order("location asc").Find(&subLists, "parent_id in (?) AND depth=2", subCategoryIds).Error
|
|
subListMap := map[uint][]goods.Category{}
|
|
for _, subList := range subLists {
|
|
subListMap[subList.ParentId] = append(subListMap[subList.ParentId], subList)
|
|
}
|
|
// 拼装数据
|
|
for i, category := range categories {
|
|
categories[i].Children = subListMap[category.ID]
|
|
}
|
|
|
|
back.Suc(c, "获取成功", categories)
|
|
}
|
|
|
|
type argsListAll struct {
|
|
HasChildren int `json:"hasChildren" form:"hasChildren"`
|
|
}
|
|
|
|
// @Style 获取商品类目
|
|
func (cate *Category) ListAll(c *gin.Context) {
|
|
result := []replyCategory{}
|
|
args := argsListAll{}
|
|
if err := tools.Params(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
depth := []int{1}
|
|
if args.HasChildren == 1 {
|
|
depth = append(depth, 2)
|
|
}
|
|
categorys := []goods.Category{}
|
|
err := dbc.DB.Table((&goods.Category{}).TableName()).Order("location asc").Find(&categorys, "depth in (?)", depth).Error
|
|
if err != nil {
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
back.Fail(c, "暂无数据")
|
|
} else {
|
|
back.Err(c, err.Error())
|
|
}
|
|
return
|
|
}
|
|
categorysMap := map[uint][]goods.Category{}
|
|
for _, category := range categorys {
|
|
categorysMap[category.ParentId] = append(categorysMap[category.ParentId], category)
|
|
}
|
|
|
|
for _, category := range categorys {
|
|
if category.ParentId == 0 {
|
|
children := categorysMap[category.ID]
|
|
if children == nil {
|
|
children = []goods.Category{}
|
|
}
|
|
result = append(result, replyCategory{
|
|
Category: category,
|
|
Children: children,
|
|
})
|
|
}
|
|
}
|
|
|
|
back.Suc(c, "获取成功", result)
|
|
}
|