liujiandao
2024-01-11 40e540f8ca398fee68f4520dbebd6db6fe2e164c
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
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
    "sync"
)
 
type (
    ContactInformation struct {
        Id   int    `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"`
        Name string `json:"name" gorm:"column:name;type:varchar(255);comment:名称"`
    }
 
    ContactInformationSearch struct {
        ContactInformation
        Orm *gorm.DB
    }
)
 
func (ContactInformation) TableName() string {
    return "contact_information"
}
 
func NewContactInformationSearch() *ContactInformationSearch {
    return &ContactInformationSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *ContactInformationSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&ContactInformation{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
    }
 
    return db
}
 
// Create 创建
func (slf *ContactInformationSearch) Create(record []*ContactInformation) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *ContactInformationSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&ContactInformation{}).Error
}
 
func (slf *ContactInformationSearch) Update(record *ContactInformation) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *ContactInformationSearch) Find() ([]*ContactInformation, error) {
    var db = slf.build()
    var result []*ContactInformation
    err := db.Find(&result).Error
    return result, err
}
 
func (slf *ContactInformationSearch) FindOne() (*ContactInformation, error) {
    var db = slf.build()
    var result ContactInformation
    err := db.First(&result).Error
    return &result, err
}
 
func (slf *ContactInformationSearch) SetId(id int) *ContactInformationSearch {
    slf.Id = id
    return slf
}
 
func (slf *ContactInformationSearch) SetName(name string) *ContactInformationSearch {
    slf.Name = name
    return slf
}
 
func (slf *ContactInformationSearch) CreateBatch(records []*ContactInformation) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
// InitDefaultData 初始化数据
func (slf *ContactInformationSearch) InitDefaultData(errCh chan<- error, wg *sync.WaitGroup) {
    var (
        db          = slf.Orm.Table(slf.TableName())
        total int64 = 0
    )
    defer wg.Done()
 
    if err := db.Count(&total).Error; err != nil {
        errCh <- err
        return
    }
    if total != 0 {
        return
    }
    records := []*ContactInformation{
        {1, "线下拜访"},
        {2, "电话联系"},
        {3, "会议"},
    }
    err := slf.CreateBatch(records)
    if err != nil {
        errCh <- err
        return
    }
}