zhangqian
2023-10-13 13194e787d51e4ce07dfc35341d536fb5db7aaa3
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
package model
 
import (
    "aps_crm/constvar"
    "aps_crm/pkg/mysqlx"
    "fmt"
    "gorm.io/gorm"
    "gorm.io/gorm/clause"
)
 
type (
    ServiceFollowup struct {
        Id             int          `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        ClientId       int          `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"`
        Client         Client       `json:"client" gorm:"foreignKey:ClientId"`
        Number         string       `json:"number" gorm:"column:number;type:varchar(255);comment:合同编号"`
        ContactId      int          `json:"contactId" gorm:"column:contact_id;type:int;comment:联系人id"`
        Contact        Contact      `json:"contact"  gorm:"foreignKey:ContactId"`
        ServiceOrderId int          `json:"serviceOrderId" gorm:"column:service_order_id;type:int;comment:客户服务单id"`
        ServiceOrder   ServiceOrder `gorm:"foreignKey:ServiceOrderId"`
        MemberId       int          `json:"memberId" gorm:"column:member_id;type:int;comment:服务人员id"`
        Member         User         `json:"Member" gorm:"foreignKey:MemberId"`
        PlanId         int          `json:"planId" gorm:"column:plan_id;type:int;comment:服务计划id"`
        SatisfactionId int          `json:"satisfactionId" gorm:"column:satisfaction_id;type:int;comment:满意度id"`
        Satisfaction   Satisfaction `json:"satisfaction" gorm:"foreignKey:SatisfactionId"`
        TimelyRateId   int          `json:"timelyRateId" gorm:"column:timely_rate_id;type:int;comment:及时率id"`
        TimelyRate     TimelyRate   `json:"timelyRate" gorm:"foreignKey:TimelyRateId"`
        SolveRateId    int          `json:"solveRateId" gorm:"column:solve_rate_id;type:int;comment:解决率id"`
        SolveRate      SolveRate    `json:"solveRate" gorm:"foreignKey:SolveRateId"`
        IsVisitId      int          `json:"isVisitId" gorm:"column:is_visit_id;type:int;comment:服务人员是否来过id"`
        IsVisit        IsVisit      `json:"isVisit"  gorm:"foreignKey:IsVisitId"`
        OldMemberId    int          `json:"oldMemberId" gorm:"column:old_member_id;type:int;comment:原服务人员"`
        OldMember      User         `json:"oldMember" gorm:"foreignKey:OldMemberId"`
        Remark         string       `json:"remark" gorm:"column:remark;type:text;comment:备注"`
        File           string       `json:"file" gorm:"column:file;type:varchar(255);comment:附件"`
        CrmModel
    }
 
    ServiceFollowupSearch struct {
        ServiceFollowup
        Orm         *gorm.DB
        KeywordType constvar.ServiceFollowupKeywordType
        Keyword     interface{}
        OrderBy     string
        PageNum     int
        PageSize    int
        Preload     bool
        MemberIds   []int
    }
)
 
func (*ServiceFollowup) TableName() string {
    return "service_followup"
}
 
func NewServiceFollowupSearch() *ServiceFollowupSearch {
    return &ServiceFollowupSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *ServiceFollowupSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&ServiceFollowup{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    switch slf.KeywordType {
    case constvar.ServiceFollowupKeywordFollowupNo:
        db = db.Where("number like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    case constvar.ServiceFollowupKeywordCustomerName:
        db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    case constvar.ServiceFollowupKeywordContactName:
        db = db.Joins("Contact").Where("Contact.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    case constvar.ServiceFollowupKeywordCustomerServiceNo:
        db = db.Joins("ServiceOrder").Where("ServiceOrder.service_number like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    case constvar.ServiceFollowupKeywordVisitor:
        db = db.Joins("left join user on user.id = service_followup.member_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    case constvar.ServiceFollowupKeywordSatisfactionDegree:
        db = db.Joins("Satisfaction").Where("Satisfaction.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
 
    }
 
    if slf.Preload {
        db = db.Preload("Client").
            Preload("Contact").
            Preload("ServiceOrder").
            Preload("Member").
            Preload("Satisfaction").
            Preload("TimelyRate").
            Preload("SolveRate").
            Preload("IsVisit").
            Preload("OldMember")
    }
 
    if len(slf.MemberIds) > 0 {
        db = db.Where("member_id in ?", slf.MemberIds)
    }
 
    return db
}
 
func (slf *ServiceFollowupSearch) Create(record *ServiceFollowup) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *ServiceFollowupSearch) Update(record *ServiceFollowup) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *ServiceFollowupSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&ServiceFollowup{}).Error
}
 
func (slf *ServiceFollowupSearch) Find() (*ServiceFollowup, error) {
    var db = slf.build()
    var record = &ServiceFollowup{}
    err := db.First(record).Error
    return record, err
}
 
func (slf *ServiceFollowupSearch) FindAll() ([]*ServiceFollowup, int64, error) {
    var db = slf.build()
    var records = make([]*ServiceFollowup, 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.Order("id desc").Find(&records).Error
    return records, total, err
}
 
func (slf *ServiceFollowupSearch) SetId(id int) *ServiceFollowupSearch {
    slf.Id = id
    return slf
}
 
func (slf *ServiceFollowupSearch) SetServiceOrderId(id int) *ServiceFollowupSearch {
    slf.ServiceOrderId = id
    return slf
}
 
func (slf *ServiceFollowupSearch) SetPreload(preload bool) *ServiceFollowupSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *ServiceFollowupSearch) SetKeywordType(keyword constvar.ServiceFollowupKeywordType) *ServiceFollowupSearch {
    slf.KeywordType = keyword
    return slf
}
 
func (slf *ServiceFollowupSearch) SetKeyword(keyword string) *ServiceFollowupSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *ServiceFollowupSearch) SetPage(page, size int) *ServiceFollowupSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *ServiceFollowupSearch) SetOrder(order string) *ServiceFollowupSearch {
    slf.OrderBy = order
    return slf
}
func (slf *ServiceFollowupSearch) SetIds(ids []int) *ServiceFollowupSearch {
    slf.Orm = slf.Orm.Where("id in (?)", ids)
    return slf
}
 
func (slf *ServiceFollowupSearch) SetMemberIds(memberIds []int) *ServiceFollowupSearch {
    slf.MemberIds = memberIds
    return slf
}