package dao

import (
	"gorm.io/gorm"
	"youngee_b_api/app/entity"
)

type BookInfoDao struct{}

// 获取指定任务id的最新预约记录
func (d BookInfoDao) GetLastByTaskId(taskId string) (*entity.BookInfo, error) {
	var bookInfo entity.BookInfo
	err := Db.Model(&entity.BookInfo{}).Where("task_id = ?", taskId).Order("create_at DESC").First(&bookInfo).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return &bookInfo, nil
		} else {
			return nil, err
		}
	}
	return &bookInfo, nil
}

// 更新预约状态
func (d BookInfoDao) UpdateBookStatus(bookIds []int64, bookInfo entity.BookInfo) error {
	err := Db.Debug().Model(&entity.BookInfo{}).Where("book_id IN ?", bookIds).Updates(bookInfo).Error
	if err != nil {
		return err
	}
	return nil
}

// 更新字段
func (d BookInfoDao) UpdateField(bookId int64, updateData map[string]interface{}) error {
	err := Db.Model(&entity.BookInfo{}).Where("book_id = ?", bookId).Updates(updateData).Error
	if err != nil {
		return err
	}
	return nil
}

// 批量更新字段
func (d BookInfoDao) UpdateFieldBatch(bookIds []int64, updateData map[string]interface{}) error {
	err := Db.Model(&entity.BookInfo{}).Where("book_id IN ?", bookIds).Updates(updateData).Error
	if err != nil {
		return err
	}
	return nil
}
