enterprise/common/dao/checkin.go

68 lines
1.3 KiB
Go
Raw Normal View History

2023-08-04 18:27:37 +08:00
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())
2023-08-10 19:26:09 +08:00
tx = tx.Where("username = ?", userId)
2023-08-04 18:27:37 +08:00
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
}