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.
138 lines
3.0 KiB
138 lines
3.0 KiB
package category
|
|
|
|
import (
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/goods"
|
|
"recook/internal/model/manage"
|
|
"recook/tools"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
type queryChildrenCategoryParam struct {
|
|
ParentId uint `json:"parentID" validate:"required"`
|
|
}
|
|
|
|
type queryAllCategoriesParam struct {
|
|
goods.Category
|
|
Children []goods.Category `json:"children"`
|
|
}
|
|
type queryLocParam struct {
|
|
CategoryID uint
|
|
Location uint
|
|
}
|
|
|
|
func QueryFirstCategories(c *gin.Context) {
|
|
var categories []goods.Category
|
|
|
|
// 采购根据权限获取商品类目
|
|
var userId = c.MustGet("Manager_ID").(uint)
|
|
var user manage.UserInfo
|
|
var where string
|
|
user.ID = userId
|
|
if err := dbc.DB.Find(&user).Error; err != nil {
|
|
back.Fail(c, "数据错误:"+err.Error())
|
|
return
|
|
}
|
|
if user.RoleID == 92 {
|
|
where = " id In(" + user.GoodsType + ")"
|
|
}
|
|
|
|
err := dbc.DB.Table((&goods.Category{}).TableName()).Where(where).Find(&categories, "parent_id=0 AND depth=1").Error
|
|
if err != nil {
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
back.Fail(c, "暂无数据")
|
|
} else {
|
|
back.Err(c, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
back.Suc(c, "获取成功", categories)
|
|
}
|
|
|
|
func QueryChildrenCategories(c *gin.Context) {
|
|
var p queryChildrenCategoryParam
|
|
|
|
err := tools.Params(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
var categories []goods.Category
|
|
|
|
err = dbc.DB.Table((&goods.Category{}).TableName()).Find(&categories, "parent_id=?", p.ParentId).Error
|
|
if err != nil {
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
back.Fail(c, "暂无数据")
|
|
} else {
|
|
back.Err(c, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
back.Suc(c, "获取成功", categories)
|
|
}
|
|
|
|
/*
|
|
获取所有类目
|
|
*/
|
|
func QueryAllCategories(c *gin.Context) {
|
|
var categories []queryAllCategoriesParam
|
|
|
|
// 采购根据权限获取商品类目
|
|
var userId = c.MustGet("Manager_ID").(uint)
|
|
var user manage.UserInfo
|
|
where := " 1=1 "
|
|
user.ID = userId
|
|
if err := dbc.DB.Find(&user).Error; err != nil {
|
|
back.Fail(c, "数据错误:"+err.Error())
|
|
return
|
|
}
|
|
if user.NewroleId == 9 {
|
|
where = " id In(" + user.GoodsType + ")"
|
|
}
|
|
|
|
tx := dbc.DB.Begin()
|
|
|
|
err := tx.Table((&goods.Category{}).TableName()).Where(where).Order("location asc").Find(&categories, "parent_id=0 AND depth=1").Error
|
|
if err != nil {
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
back.Fail(c, "暂无数据")
|
|
} else {
|
|
back.Err(c, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
// 填充子类目 sub
|
|
for i, _ := range categories {
|
|
err = tx.Table((&goods.Category{}).TableName()).Find(&categories[i].Children, "parent_id=? AND depth=2", categories[i].ID).Error
|
|
if err != nil && !gorm.IsRecordNotFoundError(err) {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
back.Suc(c, "获取成功", categories)
|
|
}
|
|
|
|
func SetLocation(c *gin.Context) {
|
|
var p queryLocParam
|
|
err := tools.Params(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
category := goods.Category{}
|
|
category.Location = p.Location
|
|
dbc.DB.Model(&category).Where("id = ?", p.CategoryID).Update(category)
|
|
back.Suc(c, "设置成功", nil)
|
|
}
|