zhangqian
2024-04-17 e63c9d7f5f5c0819ee949697aec753895cc88ab5
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package models
 
import (
    "fmt"
    "gorm.io/gorm"
    "srm/constvar"
    "srm/global"
)
 
type (
    // OutsourcingEnterprise 委外企业
    OutsourcingEnterprise struct {
        gorm.Model
        Number           string                `json:"number" gorm:"unique;type:varchar(255);not null;comment:委外企业编号"`            //委外企业编号
        Name             string                `json:"name" gorm:"type:varchar(255);not null;comment:委外企业名称"`                     //委外企业编号名称
        EnterpriseType   string                `json:"enterpriseType" gorm:"type:varchar(255);not null;comment:企业类型"`             //企业类型
        Contact          string                `json:"contact" gorm:"type:varchar(255);not null;comment:联系人"`                     //联系人
        Tel              string                `json:"tel" gorm:"type:varchar(255);not null;comment:联系方式"`                        //联系方式
        Address          string                `json:"address" gorm:"type:varchar(255);not null;comment:地址"`                      //地址
        CreditGrade      string                `json:"creditGrade" gorm:"type:varchar(255);not null;comment:信用等级"`                //信用等级
        SupplyCapacity   string                `json:"supplyCapacity" gorm:"type:varchar(255);not null;comment:供货能力"`             //供货能力
        OrganizationCode string                `json:"organizationCode" gorm:"type:varchar(255);not null;comment:组织机构代码"`         //组织机构代码
        SupplyRange      string                `json:"supplyRange" gorm:"type:varchar(255);not null;comment:供货范围"`                //供货范围
        Status           constvar.RecordStatus `json:"status" gorm:"type:tinyint(1);not null;default:0;comment:状态 0 新建 1 启用 2停用"` //状态 0 新建 1 启用 2停用
    }
 
    OutsourcingEnterpriseSearch struct {
        OutsourcingEnterprise
        Preload  bool
        Order    string
        PageNum  int
        PageSize int
        Orm      *gorm.DB
        Keyword  string
    }
)
 
func (slf OutsourcingEnterprise) TableName() string {
    return "outsourcing_enterprise"
}
 
func NewOutsourcingEnterpriseSearch() *OutsourcingEnterpriseSearch {
    return &OutsourcingEnterpriseSearch{Orm: global.GVA_DB}
}
 
func (slf *OutsourcingEnterpriseSearch) SetOrm(tx *gorm.DB) *OutsourcingEnterpriseSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetPage(page, size int) *OutsourcingEnterpriseSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetOrder(order string) *OutsourcingEnterpriseSearch {
    slf.Order = order
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetID(id uint) *OutsourcingEnterpriseSearch {
    slf.ID = id
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetStatus(status constvar.RecordStatus) *OutsourcingEnterpriseSearch {
    slf.Status = status
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetNumber(number string) *OutsourcingEnterpriseSearch {
    slf.Number = number
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetKeyword(keyword string) *OutsourcingEnterpriseSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) SetPreload(preload bool) *OutsourcingEnterpriseSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *OutsourcingEnterpriseSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
 
    if slf.Number != "" {
        db = db.Where("number = ?", slf.Number)
    }
 
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    if slf.Keyword != "" {
        keywordFmt := fmt.Sprintf("%%%s%%", slf.Keyword)
        db = db.Where("name like ? or number like ? or enterprise_type like ?", keywordFmt, keywordFmt, keywordFmt)
    }
 
    if slf.Status != 0 {
        db = db.Where("status = ?", slf.Status)
    }
 
    return db
}
 
// Create 单条插入
func (slf *OutsourcingEnterpriseSearch) Create(record *OutsourcingEnterprise) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *OutsourcingEnterpriseSearch) CreateBatch(records []*OutsourcingEnterprise) 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 *OutsourcingEnterpriseSearch) Save(record *OutsourcingEnterprise) error {
    var db = slf.build()
 
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *OutsourcingEnterpriseSearch) 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
}
 
func (slf *OutsourcingEnterpriseSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
 
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
 
    return nil
}
 
func (slf *OutsourcingEnterpriseSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Unscoped().Delete(&OutsourcingEnterprise{}).Error; err != nil {
        return err
    }
 
    return nil
}
 
func (slf *OutsourcingEnterpriseSearch) First() (*OutsourcingEnterprise, error) {
    var (
        record = new(OutsourcingEnterprise)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *OutsourcingEnterpriseSearch) Find() ([]*OutsourcingEnterprise, int64, error) {
    var (
        records = make([]*OutsourcingEnterprise, 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 *OutsourcingEnterpriseSearch) FindNotTotal() ([]*OutsourcingEnterprise, error) {
    var (
        records = make([]*OutsourcingEnterprise, 0)
        db      = slf.build()
    )
 
    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, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}
 
// FindByQuery 指定条件查询.
func (slf *OutsourcingEnterpriseSearch) FindByQuery(query string, args []interface{}) ([]*OutsourcingEnterprise, int64, error) {
    var (
        records = make([]*OutsourcingEnterprise, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
 
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query 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 by query records err: %v, query: %s, args: %+v", err, query, args)
    }
 
    return records, total, nil
}
 
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *OutsourcingEnterpriseSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*OutsourcingEnterprise, error) {
    var (
        records = make([]*OutsourcingEnterprise, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
 
    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, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
 
    return records, nil
}
 
type CountGroupByStatus struct {
    Status int
    Total  int64
}
 
func (slf *OutsourcingEnterpriseSearch) CountGroupByStatus() ([]CountGroupByStatus, error) {
    var (
        records = make([]CountGroupByStatus, 0)
        db      = slf.build()
    )
    if err := db.Select("count(status) as total, status").Group("status").Find(&records).Error; err != nil {
        return nil, fmt.Errorf("CountGroupByStatus err: %v", err)
    }
    return records, nil
}