zhangqian
2024-05-06 3abb522a610fa41a8d5570b643d88a23030e56db
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package models
 
import (
    "fmt"
    "gorm.io/gorm"
    "wms/constvar"
    "wms/pkg/mysqlx"
)
 
type (
    FileTemplateAttachment struct {
        WmsModel
        Id       int                           `json:"id"  gorm:"column:id;primary_key;AUTO_INCREMENT"`
        Category constvar.FileTemplateCategory `json:"category" gorm:"type:int(11);comment:模版种类"`
        Name     string                        `json:"name" gorm:"type:varchar(63);comment:模版名称"`
        //AttachmentId uint                          `json:"attachmentId" gorm:"comment:附件表外键"`
        //Attachment   Attachment                    `json:"attachment" gorm:"foreignKey:AttachmentId;references:Id"`
        TableInfo string `json:"tableInfo" gorm:"type:varchar(31);comment:表名"`
        FileUrl   string `json:"fileUrl" gorm:"type:varchar(255);comment:文件地址"`
    }
    FileTemplateAttachmentSearch struct {
        FileTemplateAttachment
        Order    string
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
        Preload  bool
    }
)
 
func (slf *FileTemplateAttachment) TableName() string {
    return "wms_file_template_attachment"
}
 
func NewFileTemplateAttachmentSearch() *FileTemplateAttachmentSearch {
    return &FileTemplateAttachmentSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *FileTemplateAttachmentSearch) SetOrm(tx *gorm.DB) *FileTemplateAttachmentSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *FileTemplateAttachmentSearch) SetPage(page, size int) *FileTemplateAttachmentSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *FileTemplateAttachmentSearch) SetOrder(order string) *FileTemplateAttachmentSearch {
    slf.Order = order
    return slf
}
 
func (slf *FileTemplateAttachmentSearch) SetID(id int) *FileTemplateAttachmentSearch {
    slf.Id = id
    return slf
}
 
func (slf *FileTemplateAttachmentSearch) SetKeyword(keyword string) *FileTemplateAttachmentSearch {
    slf.Keyword = keyword
    return slf
}
 
//func (slf *FileTemplateAttachmentSearch) SetPreload(preload bool) *FileTemplateAttachmentSearch {
//    slf.Preload = preload
//    return slf
//}
 
func (slf *FileTemplateAttachmentSearch) SetCategory(category constvar.FileTemplateCategory) *FileTemplateAttachmentSearch {
    slf.Category = category
    return slf
}
 
func (slf *FileTemplateAttachmentSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&FileTemplateAttachment{})
 
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    //if slf.Preload {
    //    db = db.Model(&FileTemplateAttachment{}).Preload("Attachment")
    //}
 
    if int(slf.Category) != 0 {
        db = db.Where("category=?", slf.Category)
    }
 
    return db
}
 
// Create 单条插入
func (slf *FileTemplateAttachmentSearch) Create(record *FileTemplateAttachment) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return err
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *FileTemplateAttachmentSearch) CreateBatch(records []*FileTemplateAttachment) error {
    var db = slf.build()
 
    if err := db.Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
 
    return nil
}
 
func (slf *FileTemplateAttachmentSearch) Update(record *FileTemplateAttachment) error {
    var db = slf.build()
 
    if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *FileTemplateAttachmentSearch) Delete() error {
    var db = slf.build()
    return db.Unscoped().Delete(&FileTemplateAttachment{}).Error
}
 
func (slf *FileTemplateAttachmentSearch) First() (*FileTemplateAttachment, error) {
    var (
        record = new(FileTemplateAttachment)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *FileTemplateAttachmentSearch) FindByPage() ([]*FileTemplateAttachment, int64, error) {
    var (
        records = make([]*FileTemplateAttachment, 0)
        total   int64
        db      = slf.build()
    )
 
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
 
    return records, total, nil
}
 
func (slf *FileTemplateAttachmentSearch) Find() ([]*FileTemplateAttachment, error) {
    var (
        records = make([]*FileTemplateAttachment, 0)
        db      = slf.build()
    )
 
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}
 
func (slf *FileTemplateAttachmentSearch) FirstRes() (*FileTemplateAttachment, *gorm.DB) {
    var (
        record = new(FileTemplateAttachment)
        db     = slf.build()
    )
 
    return record, db.First(&record)
}
 
func (slf *FileTemplateAttachmentSearch) Count() (int64, error) {
    var (
        total int64
        db    = slf.build()
    )
    err := db.Count(&total).Error
    return total, err
}
 
func (slf *FileTemplateAttachmentSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
 
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
 
    return nil
}