product_calendar.go
4.0 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
package domain
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"strconv"
"strings"
"time"
)
// 工厂日历
type ProductCalendar struct {
// 工厂日历ID
ProductCalendarId int `json:"productCalendarId,omitempty"`
// 企业id
CompanyId int `json:"companyId,omitempty"`
// 组织ID
OrgId int `json:"orgId,omitempty"`
// 工作位置
WorkStation *WorkStation `json:"workStation,omitempty"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `json:"workOn,omitempty"`
// 日历选择
CalendarSelected []string `json:"calendarSelected,omitempty"`
// 上岗时间
InWorkAt string `json:"inWorkAt,omitempty"`
// 下岗时间
OutWorkAt string `json:"outWorkAt,omitempty"`
// 休息时间 (单位 h)
BreakTime float64 `json:"breakTime,omitempty"`
// 工时 (单位 h)
WorkTime float64 `json:"workTime,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
// 删除时间
DeletedAt time.Time `json:"deletedAt,omitempty"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
// 休息时间周期列表
BreakTimePeriods []*ProductCalendarBreakTimePeriod `json:"breakTimePeriods"`
}
type ProductCalendarRepository interface {
Save(productCalendar *ProductCalendar) (*ProductCalendar, error)
Remove(productCalendar *ProductCalendar) (*ProductCalendar, error)
FindOne(queryOptions map[string]interface{}) (*ProductCalendar, error)
Find(queryOptions map[string]interface{}) (int64, []*ProductCalendar, error)
}
func (productCalendar *ProductCalendar) Identify() interface{} {
if productCalendar.ProductCalendarId == 0 {
return nil
}
return productCalendar.ProductCalendarId
}
func (productCalendar *ProductCalendar) Update(data map[string]interface{}) error {
productCalendar.UpdatedAt = time.Now()
return nil
}
func (productCalendar *ProductCalendar) ResetWorkTime(v float64) error {
if v != 0 {
productCalendar.WorkTime = v
return nil
}
td, err := utils.ComputeTimeDuration(productCalendar.InWorkAt, productCalendar.OutWorkAt)
if err != nil {
return err
}
if td.Hours() <= productCalendar.BreakTime {
productCalendar.WorkTime = 0
return nil
}
// 工作时长 = (上班时间-下班时间)- 休息时间
productCalendar.WorkTime = td.Hours() - productCalendar.BreakTime
return nil
}
func (productCalendar *ProductCalendar) MatchCalendarSelected(t time.Time) bool {
weekdays := productCalendar.CalendarSelectedToWeekDays()
for _, v := range weekdays {
if v == t.Weekday() {
return true
}
}
return false
}
func (productCalendar *ProductCalendar) CalendarSelectedToWeekDays() []time.Weekday {
weekdays := make([]time.Weekday, 0)
for _, v := range productCalendar.CalendarSelected {
switch v {
case "7": //周日
weekdays = append(weekdays, time.Sunday)
break
case "1": //"周一":
weekdays = append(weekdays, time.Monday)
break
case "2": //"周二":
weekdays = append(weekdays, time.Tuesday)
break
case "3": //"周三":
weekdays = append(weekdays, time.Wednesday)
break
case "4": //"周四":
weekdays = append(weekdays, time.Thursday)
break
case "5": //"周五":
weekdays = append(weekdays, time.Friday)
break
case "6": //"周六":
weekdays = append(weekdays, time.Saturday)
break
}
}
return weekdays
}
// 检查时间是否跨天
func (productCalendar *ProductCalendar) CheckOverDay(t time.Time) (bool, error) {
th := t.Hour()
inHour, parseInHourErr := strconv.Atoi(strings.Split(productCalendar.InWorkAt, ":")[0])
if parseInHourErr != nil {
return false, parseInHourErr
}
outHour, parseOutHourErr := strconv.Atoi(strings.Split(productCalendar.OutWorkAt, ":")[0])
if parseOutHourErr != nil {
return false, parseOutHourErr
}
if inHour < outHour {
return false, nil // eg: 7:00 17:00 t 9:00
}
if th >= inHour && th < 24 {
return false, nil // eg: 7:00 24:00 t 20:00
}
if th >= 0 && th < outHour {
return true, nil
}
return false, nil
}