enterprise/common/dao/checkin.go

68 lines
1.3 KiB
Go

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("user_id = ?", 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
}