product_calendar_break_time_period.go 1.9 KB
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,
	}
}