-
Notifications
You must be signed in to change notification settings - Fork 0
/
staffService.go
244 lines (216 loc) · 7.25 KB
/
staffService.go
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package service
import (
. "github.com/galahade/bus_staff_managment/domain"
"time"
"fmt"
"errors"
)
var defaultStaff = Staff{}
type StaffModel struct {
ID string `json:"id"`
Name string `json:"name"`
JobType DictionaryModel `json:"jobType"`
OnboardTime string `json:"onboardTime"`
PersonalID string `json:"personalID"`
DriverType DictionaryModel `json:"driverType"`
IsInternship bool `json:"isInternship"`
IsMultitimeHired bool `json:"isMultitimeHired"`
IsResign bool `json:"isResign"`
FirstOnboardTime string `json:"firstOnboardTime"`
Phone string `json:"phone,omitempty"`
Department DictionaryModel `json:"department"`
EmergencyContact string `json:"emergencyContact"`
EmergencyContactPhone string `json:"emergencyContactPhone,omitempty"`
EmergencyContactRelation string `json:"emergencyContactRelation"`
}
type DictionaryModel struct {
ID string `json:"id"`
Name string `json:"name"`
Type int `json:"type"`
IsActive bool `json:"isActive"`
}
func CreateStaff(staffModel *StaffModel) error {
staff, err := staffModel.toDomain()
if (err == nil) {
err = staff.Create()
staffModel.ID = staff.ID
}
return err
}
func FetchDictionariesByType(dicType string) ([]DictionaryModel, error) {
switch dicType {
case "1":
jobType := JobType{}
return fillFromJobTypeDomains(jobType.QueryAll()), nil
case "2":
department := Department{}
return fillFromDepartmentDomains(department.QueryAll()), nil
case "3":
driverType := DriverType{}
return fillFromDriverTypeDomains(driverType.QueryAll()), nil
default:
return nil, errors.New(fmt.Sprintf("There is not result for the dictType: %s", dicType))
}
}
func FetchStaffByID(id string) (StaffModel, bool) {
staff := new(Staff)
staff.ID = id
err := staff.QueryUnique()
if err == nil {
staffModel := new(StaffModel)
staffModel.fillFromDomain(*staff)
return *staffModel, true
} else {
return *new(StaffModel), false
}
}
func GetAllDrivers() []StaffModel {
return fillFromStaffDomains(queryDriver(nil, nil))
}
func GetAllStaffs() []StaffModel {
return fillFromStaffDomains(defaultStaff.Query(nil, false))
}
func GetSupportStaffs() []StaffModel {
dic1 := Dictionary{
Name: "后勤",
Type: 1,
}
dic2 := Dictionary{
Name: "管理",
Type: 1,
}
(&dic1).QueryUnique()
(&dic2).QueryUnique()
joins := make([]string,1)
joins[0] = "LEFT JOIN dictionary ON dictionary.id = job_type_id"
conditions := make([]string,1)
conditions[0] = fmt.Sprintf("dictionary.id = '%s' OR dictionary.id = '%s'", dic1.ID, dic2.ID)
return fillFromStaffDomains(queryDriver(joins, conditions))
}
func GetAllQualifiedDrivers() []StaffModel {
driverType1 := Dictionary{
Name: "A1",
Type: 3,
}
driverType2 := Dictionary{
Name: "A3",
Type: 3,
}
(&driverType1).QueryUnique()
(&driverType2).QueryUnique()
joins := make([]string, 1)
joins[0] = "LEFT JOIN dictionary d2 ON d2.id = staff.driver_type_id"
conditions := make([]string, 2)
conditions[0] = fmt.Sprintf("d2.id = '%s' OR d2.id = '%s'", driverType1.ID, driverType2.ID)
conditions[1] = "is_internship = false"
return fillFromStaffDomains(queryDriver(joins, conditions))
}
func GetAllInternshipDrivers() [] StaffModel {
conditions := make([]string, 1)
conditions[0] = "is_internship = true"
return fillFromStaffDomains(queryDriver(nil, conditions))
}
func fillFromStaffDomains(staffs []Staff) (staffModels []StaffModel) {
for _, staff := range staffs {
staffModel := new(StaffModel)
staffModel.fillFromDomain(staff)
staffModels = append(staffModels, *staffModel)
}
return
}
func fillFromJobTypeDomains(dics []JobType) (dicModels []DictionaryModel) {
for _, dic := range dics {
dicModel := new(DictionaryModel)
dicModel.fillFromDomain(Dictionary(dic))
dicModels = append(dicModels, *dicModel)
}
return
}
func fillFromDepartmentDomains(dics []Department) (dicModels []DictionaryModel) {
for _, dic := range dics {
dicModel := new(DictionaryModel)
dicModel.fillFromDomain(Dictionary(dic))
dicModels = append(dicModels, *dicModel)
}
return
}
func fillFromDriverTypeDomains(dics []DriverType) (dicModels []DictionaryModel) {
for _, dic := range dics {
dicModel := new(DictionaryModel)
dicModel.fillFromDomain(Dictionary(dic))
dicModels = append(dicModels, *dicModel)
}
return
}
func (dicModel *DictionaryModel) fillFromDomain(dic Dictionary) {
dicModel.ID = dic.ID
dicModel.Name = dic.Name
dicModel.Type = dic.Type
dicModel.IsActive = dic.IsActive
}
func (dicModel *DictionaryModel) toDomain() (*Dictionary) {
dic := new(Dictionary)
dic.ID = dicModel.ID
dic.Type = dicModel.Type
dic.IsActive = dicModel.IsActive
dic.Name = dicModel.Name
return dic
}
func (staffModel *StaffModel) fillFromDomain(staff Staff) {
staffModel.ID = staff.ID
staffModel.Name = staff.Name
staffModel.JobType.fillFromDomain(Dictionary(staff.JobType))
staffModel.OnboardTime = staff.OnboardTime.Format(DateString)
staffModel.PersonalID = staff.PersonalID
staffModel.DriverType.fillFromDomain(Dictionary(staff.DriverType))
staffModel.IsInternship = staff.IsInternship
staffModel.IsMultitimeHired = staff.IsMultitimeHired
staffModel.IsResign = staff.IsResign
staffModel.FirstOnboardTime = staff.FirstOnboardTime.Format(DateString)
staffModel.Phone = staff.Phone
staffModel.Department.fillFromDomain(Dictionary(staff.Department))
staffModel.EmergencyContact = staff.EmergencyContact
staffModel.EmergencyContactRelation = staff.EmergencyContactRelation
staffModel.EmergencyContactPhone = staff.EmergencyContactPhone
}
func (staffModel StaffModel) toDomain() (staff *Staff, err error) {
staff = new(Staff)
staff.ID = staffModel.ID
staff.Name = staffModel.Name
staff.JobTypeID = staffModel.JobType.ID
staff.OnboardTime, err = time.Parse(DateString, staffModel.OnboardTime)
staff.PersonalID = staffModel.PersonalID
staff.DriverTypeID = staffModel.DriverType.ID
staff.IsInternship = staffModel.IsInternship
staff.IsResign = staffModel.IsResign
staff.Phone = staffModel.Phone
staff.DepartmentID = staffModel.Department.ID
staff.EmergencyContact = staffModel.EmergencyContact
staff.EmergencyContactRelation = staffModel.EmergencyContactRelation
staff.EmergencyContactPhone = staffModel.EmergencyContactPhone
if (staffModel.FirstOnboardTime == "") {
staff.FirstOnboardTime = staff.OnboardTime
}
if serviceErr, ok := hasError(err); ok {
return nil, serviceErr
}
return staff, nil
}
func queryDriver(subJoins, query []string) []Staff {
dic := Dictionary{
Name: "司机",
Type: 1,
}
(&dic).QueryUnique()
joins := make([]string, len(subJoins) + 1)
joins[0] = "LEFT JOIN dictionary ON dictionary.id = job_type_id"
for _, join := range subJoins {
joins = append(joins, join)
}
conditions := make([]string, len(query) + 1)
conditions[0] = fmt.Sprintf("dictionary.id = '%s'", dic.ID)
for _, condition := range query {
conditions = append(conditions, condition)
}
return defaultStaff.QueryByJoin(joins, conditions)
}