-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
141 lines (126 loc) · 3.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright 2014 @ z3q.net.
* name :
* author : jarryliu
* date : 2013-12-08 10:53
* description :
* history :
*/
package sale
import (
"bytes"
"fmt"
"go2o/src/core/domain/interface/sale"
"go2o/src/core/infrastructure/domain"
"strconv"
"strings"
)
var _ sale.ICategory = new(Category)
type Category struct {
_value *sale.ValueCategory
_rep sale.ISaleRep
_parentIdChanged bool
_childIdArr []int
_opt domain.IOptionStore
}
func newCategory(saleRep sale.ISaleRep, v *sale.ValueCategory) sale.ICategory {
return &Category{
_value: v,
_rep: saleRep,
}
}
func (this *Category) GetDomainId() int {
return this._value.Id
}
func (this *Category) GetValue() *sale.ValueCategory {
return this._value
}
func (this *Category) GetOption() domain.IOptionStore {
if this._opt == nil {
opt := newCategoryOption(this)
if err := opt.Stat(); err != nil {
opt.Set(sale.C_OptionViewName, &domain.Option{
Key: sale.C_OptionViewName,
Type: domain.OptionTypeString,
Must: false,
Title: "显示页面",
Value: "goods_list.html",
})
opt.Set(sale.C_OptionDescribe, &domain.Option{
Key: sale.C_OptionDescribe,
Type: domain.OptionTypeString,
Must: false,
Title: "描述",
Value: "",
})
opt.Flush()
}
this._opt = opt
}
return this._opt
}
func (this *Category) SetValue(v *sale.ValueCategory) error {
val := this._value
if val.Id == v.Id {
val.Description = v.Description
val.Enabled = v.Enabled
val.Name = v.Name
val.SortNumber = v.SortNumber
val.Icon = v.Icon
if val.ParentId != v.ParentId {
this._parentIdChanged = true
val.ParentId = v.ParentId
} else {
this._parentIdChanged = false
}
}
return nil
}
// 获取子栏目的编号
func (this *Category) GetChildId() []int {
if this._childIdArr == nil {
childCats := this._rep.GetChildCategories(this._value.PartnerId, this.GetDomainId())
this._childIdArr = make([]int, len(childCats))
for i, v := range childCats {
this._childIdArr[i] = v.Id
}
}
return this._childIdArr
}
func (this *Category) Save() (int, error) {
id, err := this._rep.SaveCategory(this._value)
if err == nil {
this._value.Id = id
if len(this._value.Url) == 0 || (this._parentIdChanged &&
strings.HasPrefix(this._value.Url, "/c-")) {
this._value.Url = this.getAutomaticUrl(this._value.PartnerId, id)
this._parentIdChanged = false
return this.Save()
}
}
return id, err
}
func (this *Category) getAutomaticUrl(partnerId, id int) string {
var relCategories []*sale.ValueCategory = this._rep.GetRelationCategories(partnerId, id)
var buf *bytes.Buffer = bytes.NewBufferString("/c")
var l int = len(relCategories)
for i := l; i > 0; i-- {
buf.WriteString("-" + strconv.Itoa(relCategories[i-1].Id))
}
buf.WriteString(".htm")
return buf.String()
}
var _ domain.IOptionStore = new(categoryOption)
type categoryOption struct {
domain.IOptionStore
_partnerId int
_c *Category
}
func newCategoryOption(c *Category) domain.IOptionStore {
i := fmt.Sprintf("conf/%d/option/c/%d", c.GetValue().PartnerId, c.GetDomainId())
return &categoryOption{
_partnerId: c.GetValue().ParentId,
_c: c,
IOptionStore: domain.NewOptionStoreWrapper(i),
}
}