You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
|
|
"github.com/jinzhu/now"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
var _ UserCheckInModel = (*customUserCheckInModel)(nil)
|
|
|
|
type (
|
|
// UserCheckInModel is an interface to be customized, add more methods here,
|
|
// and implement the added methods in customUserCheckInModel.
|
|
UserCheckInModel interface {
|
|
userCheckInModel
|
|
// FindThisWeek 查询用户本周签到记录
|
|
FindThisWeek(ctx context.Context, tx *gorm.DB, userId int64) ([]UserCheckIn, error)
|
|
// CheckInToday 查询用户本日是否已签到
|
|
CheckInToday(ctx context.Context, tx *gorm.DB, userId int64) (bool, error)
|
|
}
|
|
|
|
customUserCheckInModel struct {
|
|
*defaultUserCheckInModel
|
|
}
|
|
)
|
|
|
|
// NewUserCheckInModel returns a model for the database table.
|
|
func NewUserCheckInModel(conn *gorm.DB) UserCheckInModel {
|
|
return &customUserCheckInModel{
|
|
defaultUserCheckInModel: newUserCheckInModel(conn),
|
|
}
|
|
}
|
|
|
|
func (m *customUserCheckInModel) FindThisWeek(ctx context.Context, tx *gorm.DB, userId int64) ([]UserCheckIn, error) {
|
|
db := gormx.WithTx(ctx, m.DB, tx)
|
|
// 取签到时间大于本周一的所有该用户签到记录,按签到时间升序排列
|
|
var resp []UserCheckIn
|
|
err := db.Model(&UserCheckIn{}).
|
|
Where("user_id = ? AND check_in_time >= ", userId, now.Monday()).
|
|
Order("check_in_time ASC").Find(resp).Error
|
|
err = gormx.WrapSelectErr(err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (m *customUserCheckInModel) CheckInToday(ctx context.Context, tx *gorm.DB, userId int64) (bool, error) {
|
|
db := gormx.WithTx(ctx, m.DB, tx)
|
|
n := time.Now()
|
|
|
|
var count int64
|
|
err := db.Table(m.table).
|
|
Where("user_id = ? AND check_in_time >= ? AND check_in_time <= ?", userId, now.With(n).BeginningOfDay(), now.With(n).EndOfDay()).
|
|
Count(&count).
|
|
Error
|
|
switch err {
|
|
case nil:
|
|
return count > 0, nil
|
|
case gormx.ErrNotFound:
|
|
return false, nil
|
|
default:
|
|
return false, err
|
|
}
|
|
}
|