package entrustPool

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

type PoolAddrWithdrawListService struct {
}

// CreatePoolAddrWithdrawList 创建PoolAddrWithdrawList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrWithdrawListService *PoolAddrWithdrawListService) CreatePoolAddrWithdrawList(poolAddrWithdrawList entrustPool.PoolAddrWithdrawList) (err error) {
	err = global.GVA_DB.Create(&poolAddrWithdrawList).Error
	return err
}

// DeletePoolAddrWithdrawList 删除PoolAddrWithdrawList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrWithdrawListService *PoolAddrWithdrawListService)DeletePoolAddrWithdrawList(poolAddrWithdrawList entrustPool.PoolAddrWithdrawList) (err error) {
	err = global.GVA_DB.Delete(&poolAddrWithdrawList).Error
	return err
}

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

// UpdatePoolAddrWithdrawList 更新PoolAddrWithdrawList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrWithdrawListService *PoolAddrWithdrawListService)UpdatePoolAddrWithdrawList(poolAddrWithdrawList entrustPool.PoolAddrWithdrawList) (err error) {
	err = global.GVA_DB.Save(&poolAddrWithdrawList).Error
	return err
}

// GetPoolAddrWithdrawList 根据id获取PoolAddrWithdrawList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrWithdrawListService *PoolAddrWithdrawListService)GetPoolAddrWithdrawList(id uint) (poolAddrWithdrawList entrustPool.PoolAddrWithdrawList, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolAddrWithdrawList).Error
	return
}

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

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