liujiandao
2023-12-22 dd97f2626579ca8be69b9b6d68ce9d592b62eb6a
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
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
    "sync"
)
 
type (
    // Industry 行业
    Industry struct {
        Id   int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        Name string `json:"name" gorm:"column:name;type:varchar(255);comment:行业名称"`
    }
 
    // IndustrySearch 行业搜索条件
    IndustrySearch struct {
        Industry
        Orm *gorm.DB
    }
)
 
func (Industry) TableName() string {
    return "industry"
}
 
func NewIndustrySearch() *IndustrySearch {
    return &IndustrySearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *IndustrySearch) build() *gorm.DB {
    var db = slf.Orm.Model(&Industry{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
    }
 
    return db
}
 
func (slf *IndustrySearch) Create(record *Industry) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *IndustrySearch) Delete() error {
    var db = slf.build()
    return db.Delete(&Industry{}).Error
}
 
func (slf *IndustrySearch) Update(record *Industry) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *IndustrySearch) Find() (*Industry, error) {
    var db = slf.build()
    var record = &Industry{}
    err := db.First(record).Error
    return record, err
}
 
func (slf *IndustrySearch) FindAll() ([]*Industry, error) {
    var db = slf.build()
    var records = make([]*Industry, 0)
    err := db.Find(&records).Error
    return records, err
}
 
func (slf *IndustrySearch) SetId(id int) *IndustrySearch {
    slf.Id = id
    return slf
}
 
func (slf *IndustrySearch) SetName(name string) *IndustrySearch {
    slf.Name = name
    return slf
}
 
func (slf *IndustrySearch) Updates(data map[string]interface{}) error {
    var db = slf.build()
    return db.Updates(data).Error
}
 
func (slf *IndustrySearch) CreateBatch(records []*Industry) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
// InitDefaultData 初始化数据
func (slf *IndustrySearch) 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 := []*Industry{
        {1, "制造业"},
        {2, "交通运输"},
        {3, "建筑业"},
        {4, "环境和公共设施管理业"},
        {5, "批发和零售业"},
        {6, "其他"},
    }
    err := slf.CreateBatch(records)
    if err != nil {
        errCh <- err
        return
    }
}