package brc

import (
	"ffie-admin/server/global"
	"ffie-admin/server/model/brc"
	brcReq "ffie-admin/server/model/brc/request"
	"ffie-admin/server/model/common/request"
)

type BrcLockService struct {
}

// CreateBrcLock 创建BrcLock记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcLockService *BrcLockService) CreateBrcLock(brcLock brc.BrcLock) (err error) {
	err = global.GVA_DB.Create(&brcLock).Error
	return err
}

// DeleteBrcLock 删除BrcLock记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcLockService *BrcLockService) DeleteBrcLock(brcLock brc.BrcLock) (err error) {
	err = global.GVA_DB.Delete(&brcLock).Error
	return err
}

// DeleteBrcLockByIds 批量删除BrcLock记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcLockService *BrcLockService) DeleteBrcLockByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]brc.BrcLock{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateBrcLock 更新BrcLock记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcLockService *BrcLockService) UpdateBrcLock(brcLock brc.BrcLock) (err error) {
	err = global.GVA_DB.Save(&brcLock).Error
	return err
}

// GetBrcLock 根据id获取BrcLock记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcLockService *BrcLockService) GetBrcLock(id uint) (brcLock brc.BrcLock, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&brcLock).Error
	return
}

// GetBrcLockInfoList 分页获取BrcLock记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcLockService *BrcLockService) GetBrcLockInfoList(info brcReq.BrcLockSearch) (list []brc.BrcLock, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&brc.BrcLock{})
	var brcLocks []brc.BrcLock
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&brcLocks).Order("id desc").Error
	return brcLocks, total, err
}
