product_calendar.go 4.0 KB
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
}