package entrustPool

import (
	"courseSign/server/global"
	"courseSign/server/model/entrustPool"
	"courseSign/server/model/common/request"
    entrustPoolReq "courseSign/server/model/entrustPool/request"
)

type PoolConfigService struct {
}

// CreatePoolConfig 创建PoolConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolConfigService *PoolConfigService) CreatePoolConfig(poolConfig entrustPool.PoolConfig) (err error) {
	err = global.GVA_DB.Create(&poolConfig).Error
	return err
}

// DeletePoolConfig 删除PoolConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolConfigService *PoolConfigService)DeletePoolConfig(poolConfig entrustPool.PoolConfig) (err error) {
	err = global.GVA_DB.Delete(&poolConfig).Error
	return err
}

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

// UpdatePoolConfig 更新PoolConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolConfigService *PoolConfigService)UpdatePoolConfig(poolConfig entrustPool.PoolConfig) (err error) {
	err = global.GVA_DB.Save(&poolConfig).Error
	return err
}

// GetPoolConfig 根据id获取PoolConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolConfigService *PoolConfigService)GetPoolConfig(id uint) (poolConfig entrustPool.PoolConfig, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolConfig).Error
	return
}

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