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(corpId int64, month string) ([]*model.UsernameCount, error) {
	var userCount []*model.UsernameCount
	tx := GetDB().Table(d.TableName())
	tx = tx.Where("corp_id = ?", corpId)
	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) Query(corpId int64, username, month string, filterException bool) ([]*model.Checkin, error) {
	tx := GetDB().Table(d.TableName())
	tx = tx.Where("corp_id = ?", corpId)
	tx = tx.Where("month = ?", month)
	tx = tx.Where("username = ?", username)
	if filterException {
		tx = tx.Where("exception = ''")
	}

	var u []*model.Checkin
	tx = tx.Find(&u)
	if tx.Error != nil {
		return nil, tx.Error
	}
	return u, nil
}

func (d *CheckinDao) QueryDay(corpId int64, day string) ([]*model.Checkin, error) {
	tx := GetDB().Table(d.TableName())
	tx = tx.Where("day = ?", day)
	tx.Where("corp_id = ?", corpId)

	var u []*model.Checkin
	tx = tx.Find(&u)
	if tx.Error != nil {
		return nil, tx.Error
	}
	return u, nil
}

func (d *CheckinDao) QueryAdmin(page, size int, corpId int64, username, startDay, endDay string) ([]*model.Checkin, int64, error) {
	tx := GetDB().Table(d.TableName())
	tx = tx.Where("corp_id = ?", corpId)
	if startDay != "" {
		tx = tx.Where("day >= ?", startDay)
	}
	if endDay != "" {
		tx = tx.Where("day <= ?", endDay)
	}
	if username != "" {
		tx = tx.Where("username = ?", username)
	}
	var count int64
	tx.Count(&count)

	tx.Offset((page - 1) * size).Limit(size)
	var u []*model.Checkin
	tx = tx.Find(&u)
	if tx.Error != nil {
		return nil, 0, tx.Error
	}
	return u, count, nil
}