/
read_record.gen.go
151 lines (126 loc) · 4.5 KB
/
read_record.gen.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
package dao
import (
"github.com/gin-gonic/gin"
"github.com/i2eco/ecology/appgo/model/mysql"
"github.com/i2eco/ecology/appgo/model/trans"
"github.com/i2eco/muses/pkg/logger"
"github.com/jinzhu/gorm"
"go.uber.org/zap"
)
type readRecord struct {
logger *logger.Client
db *gorm.DB
}
func InitReadRecord(logger *logger.Client, db *gorm.DB) *readRecord {
return &readRecord{
logger: logger,
db: db,
}
}
// Create 新增一条记
func (g *readRecord) Create(c *gin.Context, db *gorm.DB, data *mysql.ReadRecord) (err error) {
if err = db.Create(data).Error; err != nil {
g.logger.Error("create readRecord create error", zap.Error(err))
return
}
return nil
}
// Update 根据主键更新一条记录
func (g *readRecord) Update(c *gin.Context, db *gorm.DB, paramId int, ups mysql.Ups) (err error) {
var sql = "`id`=?"
var binds = []interface{}{paramId}
if err = db.Table("read_record").Where(sql, binds...).Updates(ups).Error; err != nil {
g.logger.Error("read_record update error", zap.Error(err))
return
}
return
}
// UpdateX Update的扩展方法,根据Cond更新一条或多条记录
func (g *readRecord) UpdateX(c *gin.Context, db *gorm.DB, conds mysql.Conds, ups mysql.Ups) (err error) {
sql, binds := mysql.BuildQuery(conds)
if err = db.Table("read_record").Where(sql, binds...).Updates(ups).Error; err != nil {
g.logger.Error("read_record update error", zap.Error(err))
return
}
return
}
// Delete 根据主键删除一条记录。如果有delete_time则软删除,否则硬删除。
func (g *readRecord) Delete(c *gin.Context, db *gorm.DB, paramId int) (err error) {
var sql = "`id`=?"
var binds = []interface{}{paramId}
if err = db.Table("read_record").Where(sql, binds...).Delete(&mysql.ReadRecord{}).Error; err != nil {
g.logger.Error("read_record delete error", zap.Error(err))
return
}
return
}
// DeleteX Delete的扩展方法,根据Cond删除一条或多条记录。如果有delete_time则软删除,否则硬删除。
func (g *readRecord) DeleteX(c *gin.Context, db *gorm.DB, conds mysql.Conds) (err error) {
sql, binds := mysql.BuildQuery(conds)
if err = db.Table("read_record").Where(sql, binds...).Delete(&mysql.ReadRecord{}).Error; err != nil {
g.logger.Error("read_record delete error", zap.Error(err))
return
}
return
}
// Info 根据PRI查询单条记录
func (g *readRecord) Info(c *gin.Context, paramId int) (resp mysql.ReadRecord, err error) {
var sql = "`id`=?"
var binds = []interface{}{paramId}
if err = g.db.Table("read_record").Where(sql, binds...).First(&resp).Error; err != nil {
g.logger.Error("read_record info error", zap.Error(err))
return
}
return
}
// InfoX Info的扩展方法,根据Cond查询单条记录
func (g *readRecord) InfoX(c *gin.Context, conds mysql.Conds) (resp mysql.ReadRecord, err error) {
sql, binds := mysql.BuildQuery(conds)
if err = g.db.Table("read_record").Where(sql, binds...).First(&resp).Error; err != nil {
g.logger.Error("read_record info error", zap.Error(err))
return
}
return
}
// List 查询list,extra[0]为sorts
func (g *readRecord) List(c *gin.Context, conds mysql.Conds, extra ...string) (resp []mysql.ReadRecord, err error) {
sql, binds := mysql.BuildQuery(conds)
sorts := ""
if len(extra) >= 1 {
sorts = extra[0]
}
if err = g.db.Table("read_record").Where(sql, binds...).Order(sorts).Find(&resp).Error; err != nil {
g.logger.Error("read_record info error", zap.Error(err))
return
}
return
}
// ListMap 查询map,map遍历的时候是无序的,所以指定sorts参数没有意义
func (g *readRecord) ListMap(c *gin.Context, conds mysql.Conds) (resp map[int]mysql.ReadRecord, err error) {
sql, binds := mysql.BuildQuery(conds)
mysqlSlice := make([]mysql.ReadRecord, 0)
resp = make(map[int]mysql.ReadRecord, 0)
if err = g.db.Table("read_record").Where(sql, binds...).Find(&mysqlSlice).Error; err != nil {
g.logger.Error("read_record info error", zap.Error(err))
return
}
for _, value := range mysqlSlice {
resp[value.Id] = value
}
return
}
// ListPage 根据分页条件查询list
func (g *readRecord) ListPage(c *gin.Context, conds mysql.Conds, reqList *trans.ReqPage) (total int, respList []mysql.ReadRecord) {
if reqList.PageSize == 0 {
reqList.PageSize = 10
}
if reqList.Current == 0 {
reqList.Current = 1
}
sql, binds := mysql.BuildQuery(conds)
db := g.db.Table("read_record").Where(sql, binds...)
respList = make([]mysql.ReadRecord, 0)
db.Count(&total)
db.Order(reqList.Sort).Offset((reqList.Current - 1) * reqList.PageSize).Limit(reqList.PageSize).Find(&respList)
return
}