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
package model
 
import (
    "aps_crm/constvar"
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
    "time"
)
 
type (
    ServiceFeeManage struct {
        Id         int       `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"`
        ClientId   int       `json:"client_id" gorm:"column:client_id;type:int(11);comment:客户ID"`
        Client     *Client   `json:"client" gorm:"foreignKey:ClientId"`
        MemberId   int       `json:"member_id" gorm:"column:member_id;type:int(11);comment:员工ID"`
        LatestDate time.Time `json:"latest_date" gorm:"column:latest_date;type:datetime;comment:最晚服务时间"`
        Remark     string    `json:"remark" gorm:"column:remark;type:varchar(255);comment:备注"`
        File       string    `json:"file" gorm:"column:file;type:varchar(255);comment:文件"`
        gorm.Model `json:"-"`
    }
 
    ServiceFeeManageSearch struct {
        ServiceFeeManage
        Orm         *gorm.DB
        QueryClass  constvar.ServiceFeeQueryClass
        KeywordType constvar.ServiceFeeKeywordType
        Keyword     string
        OrderBy     string
        PageNum     int
        PageSize    int
    }
)
 
func (ServiceFeeManage) TableName() string {
    return "service_fee_manage"
}
 
func NewServiceFeeManageSearch(db *gorm.DB) *ServiceFeeManageSearch {
    if db == nil {
        db = mysqlx.GetDB()
    }
 
    return &ServiceFeeManageSearch{
        Orm: db,
    }
}
 
func (slf *ServiceFeeManageSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&ServiceFeeManage{})
    if slf.Id != 0 {
        db.Where("id = ?", slf.Id)
    }
    if slf.ClientId != 0 {
        db.Where("client_id = ?", slf.ClientId)
    }
    switch slf.QueryClass {
    case constvar.ServiceFeeQueryClassExpireLessThen60Days:
        db = db.Where("latest_date > ? and latest_date < ?", time.Now(), time.Now().AddDate(0, 0, 60))
    case constvar.ServiceFeeQueryClassExpireLessThen30Days:
        db = db.Where("latest_date > ? and latest_date < ?", time.Now(), time.Now().AddDate(0, 0, 30))
    case constvar.ServiceFeeQueryClassExpireAboutTo60Day:
        db = db.Where("latest_date = ?", time.Now().AddDate(0, 0, -60))
    case constvar.ServiceFeeQueryClassExpireAboutTo30Day:
        db = db.Where("latest_date = ?", time.Now().AddDate(0, 0, -30))
    case constvar.ServiceFeeQueryClassExpired:
        db = db.Where("latest_date < ?", time.Now())
    case constvar.ServiceFeeQueryClassNoService:
        db = db.Where("latest_date < ?", time.Now().AddDate(0, 0, -60))
 
    }
 
    switch slf.KeywordType {
    case constvar.ServiceFeeKeywordCustomerName:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.name = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordCustomerType:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.client_type_id = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordSalesPrincipal:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.member_id = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordCustomerScale:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.enterprise_scale_id = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordClientLevel:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.client_level_id = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordCustomerNo:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.number = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordCustomerStatus:
        db.Joins("left join clients on clients.id = service_fee_manage.client_id")
        db = db.Where("clients.client_status_id = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordServiceEndDate:
        db = db.Where("latest_date = ?", slf.Keyword)
    case constvar.ServiceFeeKeywordProductName:
        //todo
    }
 
    return db
}
 
func (slf *ServiceFeeManageSearch) Create(record *ServiceFeeManage) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *ServiceFeeManageSearch) Update(record *ServiceFeeManage) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *ServiceFeeManageSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&ServiceFeeManage{}).Error
}
 
func (slf *ServiceFeeManageSearch) SetId(id int) *ServiceFeeManageSearch {
    slf.Id = id
    return slf
}
 
func (slf *ServiceFeeManageSearch) SetKeywordType(keyword constvar.ServiceFeeKeywordType) *ServiceFeeManageSearch {
    slf.KeywordType = keyword
    return slf
}
 
func (slf *ServiceFeeManageSearch) SetQueryClass(queryClass constvar.ServiceFeeQueryClass) *ServiceFeeManageSearch {
    slf.QueryClass = queryClass
    return slf
}
 
func (slf *ServiceFeeManageSearch) SetKeyword(keyword string) *ServiceFeeManageSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *ServiceFeeManageSearch) Find() (*ServiceFeeManage, error) {
    var db = slf.build()
    var record = new(ServiceFeeManage)
    err := db.First(record).Error
    return record, err
}
 
func (slf *ServiceFeeManageSearch) FindAll() ([]*ServiceFeeManage, int64, error) {
    var db = slf.build()
    var records = make([]*ServiceFeeManage, 0)
    var total int64
    if err := db.Count(&total).Error; err != nil {
        return records, total, err
    }
    if slf.PageNum > 0 && slf.PageSize > 0 {
        db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
    }
 
    if slf.PageNum > 0 && slf.PageSize > 0 {
        db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
    }
 
    err := db.Preload("Client").Find(&records).Error
    return records, total, err
}
 
func (slf *ServiceFeeManageSearch) SetPage(page, size int) *ServiceFeeManageSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *ServiceFeeManageSearch) SetOrder(order string) *ServiceFeeManageSearch {
    slf.OrderBy = order
    return slf
}
func (slf *ServiceFeeManageSearch) SetIds(ids []int) *ServiceFeeManageSearch {
    slf.Orm = slf.Orm.Where("id in (?)", ids)
    return slf
}