-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
executable file
·100 lines (90 loc) · 2.73 KB
/
category.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package service
import (
"github.com/892294101/jxlife/server/common"
"github.com/892294101/jxlife/server/global"
"github.com/892294101/jxlife/server/models/app"
"github.com/892294101/jxlife/server/models/web"
)
type WebCategoryService struct {
}
type AppCategoryService struct {
}
// 创建商品类目
func (c *WebCategoryService) Create(param web.CategoryCreateParam) int64 {
category := web.Category{
Name: param.Name,
ParentId: param.ParentId,
Level: param.Level,
Sort: param.Sort,
Created: common.NowTime(),
Sid: param.Sid,
}
return global.Db.Create(&category).RowsAffected
}
// 删除商品类目
func (c *WebCategoryService) Delete(param web.CategoryDeleteParam) int64 {
rows := global.Db.Where("parent_id = ?", param.Id).Delete(&web.Category{}).RowsAffected
if rows < 0 {
return 0
}
return global.Db.Delete(&web.Category{}, param.Id).RowsAffected
}
// 更新商品类目
func (c *WebCategoryService) Update(param web.CategoryUpdateParam) int64 {
category := web.Category{
Id: param.Id,
Name: param.Name,
Sort: param.Sort,
Updated: common.NowTime(),
}
return global.Db.Model(&category).Updates(&category).RowsAffected
}
// 获取商品类目列表
func (g *WebCategoryService) GetList(param web.CategoryQueryParam) []web.CategoryList {
query := &web.Category{
Name: param.Name,
ParentId: param.ParentId,
Sid: param.Sid,
}
categoryList := make([]web.CategoryList, 0)
global.Db.Table("category").Where(query).Find(&categoryList)
return categoryList
}
// 获取商品类目选项
func (c *WebCategoryService) GetOption() (option []web.CategoryOption) {
selectList := make([]web.CategoryList, 0)
global.Db.Table("category").Find(&selectList)
return getTreeOptions(1, selectList)
}
// 获取商品类目选项
func (c *AppCategoryService) GetOption(param app.CategoryQueryParam) (option []app.CategoryOption) {
categorys := make([]app.Category, 0)
categoryOptions := make([]app.CategoryOption, 0)
global.Db.Table("category").Where("level = 1 and sid =?", param.Sid).Find(&categorys)
for _, item := range categorys {
option := app.CategoryOption{
Id: item.Id,
Text: item.Name,
}
categoryOptions = append(categoryOptions, option)
}
return categoryOptions
}
// 获取树形结构的选项
func getTreeOptions(id uint64, cateList []web.CategoryList) (option []web.CategoryOption) {
optionList := make([]web.CategoryOption, 0)
for _, opt := range cateList {
if opt.ParentId == id && (opt.Level == 1 || opt.Level == 2) {
option := web.CategoryOption{
Value: opt.Id,
Label: opt.Name,
Children: getTreeOptions(opt.Id, cateList),
}
if opt.Level == 2 {
option.Children = nil
}
optionList = append(optionList, option)
}
}
return optionList
}