product_calendar_break_time_period.go
1.9 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
package domain
import (
"strconv"
"strings"
"time"
)
type ProductCalendarBreakTimePeriod struct {
// 开始时间 eg: 11:00
BeginAt string `json:"beginAt,omitempty"`
// 结束时间 eg: 11:30
EndAt string `json:"endAt,omitempty"`
// 休息时间 (单位 h) eg:0.5
BreakTime float64 `json:"breakTime,omitempty"`
// 备注 eg:午饭
Remark string `json:"remark,omitempty"`
}
func (period *ProductCalendarBreakTimePeriod) CheckOverDay() (bool, error) {
inHour, parseInHourErr := strconv.Atoi(strings.Split(period.BeginAt, ":")[0])
if parseInHourErr != nil {
return false, parseInHourErr
}
outHour, parseOutHourErr := strconv.Atoi(strings.Split(period.EndAt, ":")[0])
if parseOutHourErr != nil {
return false, parseOutHourErr
}
if inHour <= outHour {
return false, nil // eg: 7:00 17:00 t 9:00
}
return true, nil
}
func (period *ProductCalendarBreakTimePeriod) GetCheckBeginTime(t time.Time) time.Time {
y, m, d := t.Date()
inHour, _ := strconv.ParseInt(strings.Split(period.BeginAt, ":")[0], 10, 64)
inMinuter, _ := strconv.ParseInt(strings.Split(period.BeginAt, ":")[1], 10, 64)
return time.Date(y, m, d, int(inHour), int(inMinuter), 0, 0, time.Local)
}
func (period *ProductCalendarBreakTimePeriod) GetCheckEndTime(t time.Time) time.Time {
y, m, d := t.Date()
inHour, _ := strconv.ParseInt(strings.Split(period.EndAt, ":")[0], 10, 64)
inMinuter, _ := strconv.ParseInt(strings.Split(period.EndAt, ":")[1], 10, 64)
checkTime := time.Date(y, m, d, int(inHour), int(inMinuter), 0, 0, time.Local)
if overDay, err := period.CheckOverDay(); overDay && err == nil {
return checkTime.AddDate(0, 0, 1)
}
return checkTime
}
func NewProductCalendarBreakTimePeriod(begin, end string, breakTime float64) *ProductCalendarBreakTimePeriod {
return &ProductCalendarBreakTimePeriod{
BeginAt: begin,
EndAt: end,
BreakTime: breakTime,
}
}