-
Notifications
You must be signed in to change notification settings - Fork 263
/
category_repo.go
executable file
·166 lines (148 loc) · 4.1 KB
/
category_repo.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright 2015 @ 56x.net.
* name : category_repo.go
* author : jarryliu
* date : 2016-06-04 13:01
* description :
* history :
*/
package repos
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"log"
"sort"
promodel "github.com/ixre/go2o/core/domain/interface/pro_model"
"github.com/ixre/go2o/core/domain/interface/product"
"github.com/ixre/go2o/core/domain/interface/registry"
productImpl "github.com/ixre/go2o/core/domain/product"
"github.com/ixre/go2o/core/infrastructure/format"
"github.com/ixre/gof/db"
"github.com/ixre/gof/db/orm"
"github.com/ixre/gof/storage"
)
var _ product.ICategoryRepo = new(categoryRepo)
var categoryPrefix = "go2o:repo:cat:list_"
type categoryRepo struct {
db.Connector
registryRepo registry.IRegistryRepo
_globService product.IGlobCatService
_modelRepo promodel.IProductModelRepo
o orm.Orm
storage storage.Interface
}
func NewCategoryRepo(o orm.Orm,
modelRepo promodel.IProductModelRepo,
registryRepo registry.IRegistryRepo,
storage storage.Interface) product.ICategoryRepo {
return &categoryRepo{
Connector: o.Connector(),
o: o,
_modelRepo: modelRepo,
registryRepo: registryRepo,
storage: storage,
}
}
func (c *categoryRepo) GlobCatService() product.IGlobCatService {
if c._globService == nil {
c._globService = productImpl.NewCategoryManager(0, c, c._modelRepo, c.registryRepo)
}
return c._globService
}
func (c *categoryRepo) getCategoryCacheKey(id int) string {
return fmt.Sprintf("go2o:repo:cat:c%d", id)
}
func (c *categoryRepo) SaveCategory(v *product.Category) (int, error) {
id, err := orm.Save(c.o, v, v.Id)
// 清理缓存
if err == nil {
c.storage.Delete(c.getCategoryCacheKey(id))
c.storage.DeleteWith(categoryPrefix)
}
return id, err
}
// 检查分类是否关联商品
func (c *categoryRepo) CheckContainGoods(vendorId int64, catId int) bool {
num := 0
if vendorId <= 0 {
c.Connector.ExecScalar(`SELECT COUNT(1) FROM product WHERE cat_id= $1`, &num, catId)
} else {
panic(errors.New("暂时不支持商户绑定分类"))
}
return num > 0
}
func (c *categoryRepo) DeleteCategory(mchId int64, id int) error {
//删除子类
_, err := c.Connector.ExecNonQuery("DELETE FROM product_category WHERE parent_id= $1",
id)
//删除分类
_, err = c.Connector.ExecNonQuery("DELETE FROM product_category WHERE id= $1",
id)
// 清理缓存
if err == nil {
c.storage.Delete(c.getCategoryCacheKey(id))
c.storage.DeleteWith(categoryPrefix)
}
return err
}
func (c *categoryRepo) GetCategory(mchId, id int) *product.Category {
e := product.Category{}
key := c.getCategoryCacheKey(id)
if c.storage.Get(key, &e) != nil {
err := c.o.Get(id, &e)
if err != nil {
return nil
}
c.storage.Set(key, &e)
}
return &e
}
func (c *categoryRepo) convertICategory(list product.CategoryList) []product.ICategory {
sort.Sort(list)
slice := make([]product.ICategory, len(list))
for i, v := range list {
slice[i] = c.GlobCatService().CreateCategory(v)
}
return slice
}
func (c *categoryRepo) redirectGetCats() []*product.Category {
var list []*product.Category
err := c.o.Select(&list, "")
if err != nil {
handleError(err)
}
return list
}
func (c *categoryRepo) GetCategories(mchId int) []*product.Category {
key := categoryPrefix + "data"
var list []*product.Category
jsonStr, err := c.storage.GetBytes(key)
if err == nil {
//println("---",string(jsonStr))
err = json.Unmarshal(jsonStr, &list)
}
if err != nil {
handleError(err)
err := c.o.Select(&list, "1=1 ORDER BY sort_num DESC,id ASC")
if err == nil {
b, _ := json.Marshal(list)
c.storage.Set(key, b)
}
}
return list
}
// 获取关联的品牌
func (c *categoryRepo) GetRelationBrands(idArr []int) []*promodel.ProductBrand {
var list []*promodel.ProductBrand
if len(idArr) > 0 {
err := c.o.Select(&list, `id IN (SELECT brand_id FROM product_model_brand
WHERE prod_model IN (SELECT distinct prod_model FROM product_category WHERE id IN(`+
format.IntArrStrJoin(idArr)+`)))`)
if err != nil && err != sql.ErrNoRows {
log.Println("[ Orm][ Error]:", err.Error(), "; Entity:ProModelBrand")
}
}
return list
}