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 BrcListService struct {
}

// CreateBrcList 创建BrcList记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcListService *BrcListService) CreateBrcList(brcList brc.BrcList) (err error) {
	err = global.GVA_DB.Create(&brcList).Error
	return err
}

// DeleteBrcList 删除BrcList记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcListService *BrcListService) DeleteBrcList(brcList brc.BrcList) (err error) {
	err = global.GVA_DB.Delete(&brcList).Error
	return err
}

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

// UpdateBrcList 更新BrcList记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcListService *BrcListService) UpdateBrcList(brcList brc.BrcList) (err error) {
	err = global.GVA_DB.Save(&brcList).Error
	return err
}

// GetBrcList 根据id获取BrcList记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcListService *BrcListService) GetBrcList(id uint) (brcList brc.BrcList, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&brcList).Error
	return
}

func (brcListService *BrcListService) GetBrcListByToken(token string) (brcList brc.BrcList, err error) {
	err = global.GVA_DB.Where("token = ?", token).First(&brcList).Error
	return
}

// GetBrcListInfoList 分页获取BrcList记录
// Author [piexlmax](https://github.com/piexlmax)
func (brcListService *BrcListService) GetBrcListInfoList(info brcReq.BrcListSearch) (list []brc.BrcList, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&brc.BrcList{})
	var brcLists []brc.BrcList
	// 如果有条件搜索 下方会自动创建搜索语句
	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(&brcLists).Error
	return brcLists, total, err
}
