enterprise/common/dao/checkin.go

110 lines
2.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
}
2024-01-31 20:49:43 +08:00
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
}
2024-01-31 22:54:06 +08:00
func (d *CheckinDao) Query(username, month string, filterException bool) ([]*model.Checkin, error) {
2024-01-31 20:49:43 +08:00
tx := GetDB().Table(d.TableName())
tx = tx.Where("month = ?", month)
tx = tx.Where("username = ?", username)
if filterException {
tx = tx.Where("exception = ''")
}
2024-01-31 22:54:06 +08:00
var u []*model.Checkin
tx = tx.Find(&u)
2024-01-31 20:49:43 +08:00
if tx.Error != nil {
2024-01-31 22:54:06 +08:00
return nil, tx.Error
2024-01-31 20:49:43 +08:00
}
2024-01-31 22:54:06 +08:00
return u, nil
2024-01-31 20:49:43 +08:00
}
2024-04-03 13:53:48 +08:00
func (d *CheckinDao) QueryDay(day string) ([]*model.Checkin, error) {
tx := GetDB().Table(d.TableName())
tx = tx.Where("day = ?", day)
var u []*model.Checkin
tx = tx.Find(&u)
if tx.Error != nil {
return nil, tx.Error
}
return u, nil
}