package entrustPool

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

type PoolActionTxsService struct {
}

// CreatePoolActionTxs 创建PoolActionTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolActionTxsService *PoolActionTxsService) CreatePoolActionTxs(poolActionTxs entrustPool.PoolActionTxs) (err error) {
	err = global.GVA_DB.Create(&poolActionTxs).Error
	return err
}

// DeletePoolActionTxs 删除PoolActionTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolActionTxsService *PoolActionTxsService)DeletePoolActionTxs(poolActionTxs entrustPool.PoolActionTxs) (err error) {
	err = global.GVA_DB.Delete(&poolActionTxs).Error
	return err
}

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

// UpdatePoolActionTxs 更新PoolActionTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolActionTxsService *PoolActionTxsService)UpdatePoolActionTxs(poolActionTxs entrustPool.PoolActionTxs) (err error) {
	err = global.GVA_DB.Save(&poolActionTxs).Error
	return err
}

// GetPoolActionTxs 根据id获取PoolActionTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolActionTxsService *PoolActionTxsService)GetPoolActionTxs(id uint) (poolActionTxs entrustPool.PoolActionTxs, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolActionTxs).Error
	return
}

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