package dao import ( "enterprise/common/model" "gorm.io/gorm" "time" ) type CheckinDao struct { } func NewCheckinDao() *CheckinDao { return &CheckinDao{} } func (d *CheckinDao) TableName() string { return "checkin" } func (d *CheckinDao) Create(o *model.Checkin) (int64, error) { o.CreateTime = time.Now().Unix() res := GetDB().Table(d.TableName()).Create(o) return o.Id, res.Error } func (d *CheckinDao) Update(o *model.Checkin) error { o.UpdateTime = time.Now().Unix() tx := GetDB().Table(d.TableName()) res := tx.Save(o) return res.Error } func (d *CheckinDao) Delete(id int64) error { res := GetDB().Table(d.TableName()).Delete(&model.Checkin{}, id) return res.Error } func (d *CheckinDao) Get(id int64) (*model.Checkin, error) { var u model.Checkin tx := GetDB().Table(d.TableName()) tx = tx.Where("id = ?", id) res := tx.First(&u) if res.Error == gorm.ErrRecordNotFound { return nil, nil } if res.Error != nil { return nil, res.Error } return &u, nil } func (d *CheckinDao) GetByDay(userId, day string) (*model.Checkin, error) { var u model.Checkin tx := GetDB().Table(d.TableName()) tx = tx.Where("username = ?", userId) tx = tx.Where("day = ?", day) res := tx.First(&u) if res.Error == gorm.ErrRecordNotFound { return nil, nil } if res.Error != nil { return nil, res.Error } return &u, nil } func (d *CheckinDao) CountUsername(month string) ([]*model.UsernameCount, error) { var userCount []*model.UsernameCount tx := GetDB().Table(d.TableName()) tx = tx.Where("month = ?", month) tx.Select("username,COUNT(1) AS count") tx.Group("username") tx = tx.Find(&userCount) if tx.Error != nil { return nil, tx.Error } return userCount, nil } func (d *CheckinDao) GetUsernameCount(username, month string, filterException bool) (int64, error) { tx := GetDB().Table(d.TableName()) tx = tx.Where("month = ?", month) tx = tx.Where("username = ?", username) if filterException { tx = tx.Where("exception = ''") } var count int64 tx.Select("COUNT(1) AS count").Pluck("count", &count) if tx.Error != nil { return 0, tx.Error } return count, nil }