/
actions.go
156 lines (127 loc) · 4 KB
/
actions.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
// 模型的行为动作,可见不支持泛型写这些东西是多么的啰嗦
package domain
import (
"github.com/fenixsoft/monolithic_arch_golang/infrasturcture/db"
"github.com/google/uuid"
"gorm.io/gorm/clause"
"time"
)
func (s *BaseModel) IsNew() bool {
return s.ID == 0
}
// 广告行为
func NewAdvertisement(db *db.Database) *Advertisement {
return &Advertisement{BaseModel: BaseModel{Database: db}}
}
func (s *Advertisement) FindAll() (r []Advertisement, err error) {
return r, s.Session.Find(&r).Error
}
// 产品行为
func NewProduct(db *db.Database) *Product {
return &Product{BaseModel: BaseModel{Database: db}}
}
func (s *Product) FindAll() (r []Product, err error) {
return r, s.Session.Find(&r).Error
}
func (s *Product) FindByIDs(ids []uint) (r []Product, err error) {
return r, s.Session.Find(&r, ids).Error
}
func (s *Product) Get(id uint) (*Product, error) {
return s, s.Session.Preload(clause.Associations).Take(s, id).Error
}
func (s *Product) Update() (int64, error) {
ret := s.Session.Save(s)
return ret.RowsAffected, ret.Error
}
func (s *Product) Create() (int64, error) {
ret := s.Session.Create(s)
return ret.RowsAffected, ret.Error
}
func (s *Product) Delete(id ...uint) (int64, error) {
if len(id) > 0 {
ret := s.Session.Delete(&Product{}, id)
return ret.RowsAffected, ret.Error
} else {
ret := s.Session.Delete(&Product{}, s.ID)
return ret.RowsAffected, ret.Error
}
}
// 库存行为
func NewStockpile(db *db.Database) *Stockpile {
return &Stockpile{BaseModel: BaseModel{Database: db}}
}
func (s *Stockpile) Get(id uint) (*Stockpile, error) {
return s, s.Session.Take(s, id).Error
}
func (s *Stockpile) GetByProductId(id uint) (*Stockpile, error) {
return s, s.Session.Model(&Stockpile{}).Where("product_id = ?", id).Take(s).Error
}
func (s *Stockpile) Update() (int64, error) {
ret := s.Session.Save(s)
return ret.RowsAffected, ret.Error
}
func (s *Stockpile) UpdateAmount(id uint, amount uint) (int64, error) {
ret := s.Session.Model(&Stockpile{}).Where("id = ?", id).Update("amount", amount)
return ret.RowsAffected, ret.Error
}
// 用户行为
func NewAccount(db *db.Database) *Account {
return &Account{BaseModel: BaseModel{Database: db}}
}
func (s *Account) GetByName(username string) (*Account, error) {
return s, s.Session.Where("username = ?", username).Take(s).Error
}
func (s *Account) Update() (int64, error) {
ret := s.Session.Save(s)
return ret.RowsAffected, ret.Error
}
func (s *Account) FindByNameOrEmailOrTelephone(name, email, telephone string) (r []Account, err error) {
return r, s.Session.Where("username = ? OR email =? OR telephone = ?", name, email, telephone).Find(&r).Error
}
func (s *Account) Create() (int64, error) {
ret := s.Session.Create(s)
return ret.RowsAffected, ret.Error
}
// 支付单行为
func NewPayment(db *db.Database) *Payment {
return &Payment{
BaseModel: BaseModel{Database: db},
}
}
func NewPaymentWithInfo(db *db.Database, user string, total float64, expires int64) *Payment {
id := uuid.New().String()
return &Payment{
BaseModel: BaseModel{Database: db},
TotalPrice: total,
Expires: expires,
CreateTime: time.Now(),
PayState: Waiting,
PayId: id,
PaymentLink: "/pay/modify/" + id + "?state=PAYED&accountId=" + user,
}
}
func (s *Payment) Create() (int64, error) {
ret := s.Session.Create(s)
return ret.RowsAffected, ret.Error
}
func (s *Payment) Get(id uint) (*Payment, error) {
return s, s.Session.Take(s, id).Error
}
func (s *Payment) GetByPayID(payId string) (*Payment, error) {
return s, s.Session.Where("pay_id = ?", payId).Take(s).Error
}
func (s *Payment) Update() (int64, error) {
ret := s.Session.Save(s)
return ret.RowsAffected, ret.Error
}
// 用户钱包行为
func NewWallet(db *db.Database) *Wallet {
return &Wallet{BaseModel: BaseModel{Database: db}}
}
func (s *Wallet) GetByAccountId(id uint) (*Wallet, error) {
return s, s.Session.Where("account_id = ?", id).Take(s).Error
}
func (s *Wallet) Update() (int64, error) {
ret := s.Session.Save(s)
return ret.RowsAffected, ret.Error
}