zhangqian
2024-12-12 973c3b1684d0d019aa74113b1983fe29f20d2a49
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
package db
 
import (
    "fmt"
    "gorm.io/gorm"
)
 
// KeyPersonBase 重点人员底库
type KeyPersonBase struct {
    BaseModel
    Name    string `json:"name" gorm:"type:varchar(255);"`   //底库名称
    Color   string `json:"color" gorm:"type:varchar(255);"`  //底库颜色
    Level   string `json:"level" gorm:"type:varchar(255);"`  //底库等级
    TagName string `json:"tagName" gorm:"type:varchar(255)"` //标签名称
    TagType string `json:"tagType" gorm:"type:varchar(255)"` //标签类型
}
 
func (m *KeyPersonBase) TableName() string {
    return "key_person_base"
}
 
type KeyPersonBaseSearch struct {
    KeyPersonBase
    Orm      *gorm.DB
    PageNum  int
    PageSize int
    Order    string
    TagTypes []string
}
 
func NewKeyPersonBaseSearch() *KeyPersonBaseSearch {
    return &KeyPersonBaseSearch{
        Orm:      GetDB(),
        PageNum:  1,
        PageSize: 10,
    }
}
func (slf *KeyPersonBaseSearch) SetPage(page, size int) *KeyPersonBaseSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *KeyPersonBaseSearch) SetOrder(order string) *KeyPersonBaseSearch {
    slf.Order = order
    return slf
}
 
func (slf *KeyPersonBaseSearch) SetID(id string) *KeyPersonBaseSearch {
    slf.ID = id
    return slf
}
 
func (slf *KeyPersonBaseSearch) Set(id string) *KeyPersonBaseSearch {
    slf.ID = id
    return slf
}
 
func (slf *KeyPersonBaseSearch) SetTagTypes(ids []string) *KeyPersonBaseSearch {
    slf.TagTypes = ids
    return slf
}
 
func (slf *KeyPersonBaseSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    if slf.ID != "" {
        db = db.Where("id = ?", slf.ID)
    }
 
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
    }
 
    if len(slf.TagTypes) != 0 {
        db = db.Where("tag_type in ?", slf.TagTypes)
    }
 
    return db
}
 
func (slf *KeyPersonBaseSearch) First() (*KeyPersonBase, error) {
    var (
        record = new(KeyPersonBase)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *KeyPersonBaseSearch) Find() ([]*KeyPersonBase, int64, error) {
    var (
        records = make([]*KeyPersonBase, 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 *KeyPersonBaseSearch) FindAll() ([]*KeyPersonBase, error) {
    var (
        records = make([]*KeyPersonBase, 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 *KeyPersonBaseSearch) Count() int64 {
    var (
        count int64
        db    = slf.build()
    )
 
    if err := db.Count(&count).Error; err != nil {
        return count
    }
 
    return count
}
 
func (slf *KeyPersonBaseSearch) Create(record *KeyPersonBase) 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
}
 
func (slf *KeyPersonBaseSearch) Update(record *KeyPersonBase) error {
    var db = slf.build()
 
    if err := db.Updates(record).Error; err != nil {
        return fmt.Errorf("update err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *KeyPersonBaseSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&KeyPersonBase{}).Error
}