package entrustPool

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

type PoolAddrFeeHistoryService struct {
}

// CreatePoolAddrFeeHistory 创建PoolAddrFeeHistory记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrFeeHistoryService *PoolAddrFeeHistoryService) CreatePoolAddrFeeHistory(poolAddrFeeHistory entrustPool.PoolAddrFeeHistory) (err error) {
	err = global.GVA_DB.Create(&poolAddrFeeHistory).Error
	return err
}

// DeletePoolAddrFeeHistory 删除PoolAddrFeeHistory记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrFeeHistoryService *PoolAddrFeeHistoryService)DeletePoolAddrFeeHistory(poolAddrFeeHistory entrustPool.PoolAddrFeeHistory) (err error) {
	err = global.GVA_DB.Delete(&poolAddrFeeHistory).Error
	return err
}

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

// UpdatePoolAddrFeeHistory 更新PoolAddrFeeHistory记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrFeeHistoryService *PoolAddrFeeHistoryService)UpdatePoolAddrFeeHistory(poolAddrFeeHistory entrustPool.PoolAddrFeeHistory) (err error) {
	err = global.GVA_DB.Save(&poolAddrFeeHistory).Error
	return err
}

// GetPoolAddrFeeHistory 根据id获取PoolAddrFeeHistory记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrFeeHistoryService *PoolAddrFeeHistoryService)GetPoolAddrFeeHistory(id uint) (poolAddrFeeHistory entrustPool.PoolAddrFeeHistory, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolAddrFeeHistory).Error
	return
}

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