workshop.go
7.4 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package domain
import (
"fmt"
"sort"
"time"
)
const (
// 所有
All = 0
// 未删除
NotDeleted = 1
// 已删除
Deleted = 2
)
// 车间
type Workshop struct {
// 企业id
CompanyId int `json:"companyId,omitempty"`
// 组织ID
OrgId int `json:"orgId,omitempty"`
// 车间ID
WorkshopId int `json:"workshopId,omitempty"`
// 车间名称
WorkshopName string `json:"workshopName,omitempty"`
// 负责人 (用户对象)
Principal *User `json:"principal,omitempty"`
// 生产线
ProductLines []*ProductLine `json:"productLines,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
// 删除时间
DeletedAt time.Time `json:"deletedAt,omitempty"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
}
type WorkshopRepository interface {
Save(workshop *Workshop) (*Workshop, error)
Remove(workshop *Workshop) (*Workshop, error)
FindOne(queryOptions map[string]interface{}) (*Workshop, error)
Find(queryOptions map[string]interface{}) (int64, []*Workshop, error)
}
func (workshop *Workshop) Identify() interface{} {
if workshop.WorkshopId == 0 {
return nil
}
return workshop.WorkshopId
}
func (workshop *Workshop) Update(data map[string]interface{}) error {
if workshopName, ok := data["workshopName"]; ok {
workshop.WorkshopName = workshopName.(string)
}
if userId, ok := data["userId"]; ok {
workshop.Principal.UserId = userId.(int)
}
if principal, ok := data["principal"]; ok {
workshop.Principal = principal.(*User)
}
workshop.UpdatedAt = time.Now()
return nil
}
// AddLine 添加生产线
func (workshop *Workshop) AddLine(line *ProductLine) error {
for i := range workshop.ProductLines {
item := workshop.ProductLines[i]
if item.Removed == Deleted {
continue
}
if item.LineName == line.LineName {
return fmt.Errorf("生产线:%v已存在", line.LineName)
}
}
workshop.ProductLines = append(workshop.ProductLines, line)
return nil
}
// RemoveLine 移除生产线
func (workshop *Workshop) RemoveLine(lineId int) (*ProductLine, error) {
line, err := workshop.FindLine(lineId)
if err != nil {
return nil, err
}
if line.Removed == Deleted {
return nil, fmt.Errorf("生产线:%v已删除", line.LineName)
}
line.Removed = Deleted
return line, nil
}
// RemoveLine 更新生产线
func (workshop *Workshop) UpdateLine(lineId int, lineName string) error {
line, err := workshop.FindLine(lineId)
if err != nil {
return err
}
if line.Removed == Deleted {
return fmt.Errorf("生产线:%v已删除", line.LineName)
}
for _, v := range workshop.ProductLines {
if v.Removed == Deleted {
continue
}
if v.LineName == lineName && v.LineId != lineId {
return fmt.Errorf("生产线:%v已存在", lineName)
}
}
line.LineName = lineName
return nil
}
// FindLine 查询生产线
func (workshop *Workshop) FindLine(lineId int) (*ProductLine, error) {
for i := range workshop.ProductLines {
item := workshop.ProductLines[i]
if item.LineId == lineId {
return workshop.ProductLines[i], nil
}
}
return nil, fmt.Errorf("生产线不存在")
}
// RemoveLine 车间是否可删除 (存在任意一个生产线时,可删除)
func (workshop *Workshop) CanRemove() bool {
for i := range workshop.ProductLines {
item := workshop.ProductLines[i]
if item.Removed == NotDeleted {
return false
}
}
return true
}
// AddLine 添加生产线
func (workshop *Workshop) AddSection(lineId int, section *ProductSection) error {
line, err := workshop.FindLine(lineId)
if err != nil {
return err
}
if line.Removed == Deleted {
return fmt.Errorf("生产线:%v已删除", line.LineName)
}
for i := range line.ProductSections {
item := line.ProductSections[i]
if item.Removed == Deleted {
continue
}
if item.SectionName == section.SectionName {
return fmt.Errorf("工段:%v已存在", section.SectionName)
}
}
line.ProductSections = append(line.ProductSections, section)
return nil
}
// RemoveLine 移除生产线
func (workshop *Workshop) RemoveSection(lineId, sectionId int) error {
section, err := workshop.FindSection(lineId, sectionId)
if err != nil {
return err
}
if section.Removed == Deleted {
return fmt.Errorf("工段:%v已删除", section.SectionName)
}
section.Removed = Deleted
return nil
}
// RemoveLine 更新生产线
func (workshop *Workshop) UpdateSection(lineId, sectionId int, sectionName string) error {
section, err := workshop.FindSection(lineId, sectionId)
if err != nil {
return err
}
if section.Removed == Deleted {
return fmt.Errorf("工段:%v已删除", section.SectionName)
}
// 当前线下面工段不能重复
line, err := workshop.FindLine(lineId)
if err != nil {
return err
}
for _, v := range line.ProductSections {
if v.Removed == Deleted {
continue
}
if v.SectionName == sectionName && v.SectionId != sectionId {
return fmt.Errorf("工段:%v已存在", sectionName)
}
}
section.SectionName = sectionName
return nil
}
// 查询生产线
func (workshop *Workshop) FindSection(lineId, sectionId int) (*ProductSection, error) {
line, err := workshop.FindLine(lineId)
if err != nil {
return nil, err
}
for i := range line.ProductSections {
item := line.ProductSections[i]
if item.SectionId == sectionId {
return item, nil
}
}
return nil, fmt.Errorf("工段不存在")
}
// 查询生产线
func (workshop *Workshop) FindWorkStation(workshopId, lineId, sectionId int) (*WorkStation, error) {
if workshop.WorkshopId != workshopId {
return nil, fmt.Errorf("不存在")
}
line, err := workshop.FindLine(lineId)
if err != nil {
return nil, err
}
section, err := workshop.FindSection(lineId, sectionId)
if err != nil {
return nil, err
}
return NewWorkStation(workshop, line, section), nil
}
func (workshop *Workshop) FindWorkStationOrNil(workshopId, lineId, sectionId int) (*WorkStation, error) {
if workshop.WorkshopId != workshopId {
return nil, fmt.Errorf("不存在")
}
line, _ := workshop.FindLine(lineId)
//if err != nil {
// return nil, err
//}
section, _ := workshop.FindSection(lineId, sectionId)
//if err != nil {
// return nil, err
//}
return NewWorkStation(workshop, line, section), nil
}
func (workshop *Workshop) GetProductLines(removed int) []*ProductLine {
var result = make([]*ProductLine, 0)
for i := range workshop.ProductLines {
if removed > 0 && workshop.ProductLines[i].Removed != removed {
continue
}
item := workshop.ProductLines[i]
item.Removed = 0 // 隐藏
item.ProductSections = item.GetProductSections(removed)
result = append(result, item)
}
return result
}
func (workshop *Workshop) AllProductSections(removed int) []*ProductSection {
var result ProductSections = make([]*ProductSection, 0)
lines := workshop.GetProductLines(removed)
for i := range lines {
for j := range lines[i].ProductSections {
section := lines[i].ProductSections[j]
if removed > 0 && section.Removed != removed {
continue
}
result = append(result, section.CloneSample())
}
}
sort.Stable(result)
return result
}
// 查询生产线
func (workshop *Workshop) FindSectionById(sectionId int) (*ProductLine, *ProductSection, error) {
lines := workshop.GetProductLines(0)
for k := range lines {
line := lines[k]
section, _ := line.FindSection(sectionId)
if section != nil {
return line, section, nil
}
}
return nil, nil, fmt.Errorf("工段不存在")
}
func (workshop *Workshop) CloneSample() *Workshop {
return &Workshop{
WorkshopId: workshop.WorkshopId,
WorkshopName: workshop.WorkshopName,
}
}